X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=mutex.c;h=be3258df8347937a47bf68a99402283d2381b51a;hp=bb417c2741d3c22cd41c707d3a62bc34e0238de1;hb=7172cfe8e0e918cc279eb501986f6ec78cc7aad9;hpb=07739b57f09886b41323c605b0dbda7d2c12522b diff --git a/mutex.c b/mutex.c index bb417c27..be3258df 100644 --- a/mutex.c +++ b/mutex.c @@ -10,18 +10,17 @@ void fio_sem_remove(struct fio_sem *sem) { - unlink(sem->sem_name); + close(sem->sem_fd); munmap(sem, sizeof(*sem)); } struct fio_sem *fio_sem_init(int value) { + char sem_name[] = "/tmp/.fio_sem.XXXXXX"; + struct fio_sem *sem = NULL; pthread_mutexattr_t attr; - struct fio_sem *sem; - char sem_name[32]; int fd; - sprintf(sem_name, "/tmp/.fio_lock.XXXXXX"); fd = mkstemp(sem_name); if (fd < 0) { perror("open sem"); @@ -30,7 +29,7 @@ struct fio_sem *fio_sem_init(int value) if (ftruncate(fd, sizeof(struct fio_sem)) < 0) { perror("ftruncate sem"); - return NULL; + goto err; } sem = mmap(NULL, sizeof(struct fio_sem), PROT_READ | PROT_WRITE, @@ -38,13 +37,13 @@ struct fio_sem *fio_sem_init(int value) if (sem == MAP_FAILED) { perror("mmap sem"); close(fd); - unlink(sem_name); - return NULL; + sem = NULL; + goto err; } - close(fd); + unlink(sem_name); + sem->sem_fd = fd; sem->value = value; - strcpy(sem->sem_name, sem_name); if (pthread_mutexattr_init(&attr)) { perror("pthread_mutexattr_init"); @@ -61,7 +60,9 @@ struct fio_sem *fio_sem_init(int value) return sem; err: - munmap(sem, sizeof(*sem)); + if (sem) + fio_sem_remove(sem); + unlink(sem_name); return NULL; }