Shrink the semaphores a little
authorJens Axboe <jens.axboe@oracle.com>
Fri, 9 Mar 2007 11:40:02 +0000 (12:40 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Fri, 9 Mar 2007 11:40:02 +0000 (12:40 +0100)
The downside is that they hold the fd open, so it steals one
possible file open per-file. Will fix that in the next commit.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
mutex.c
mutex.h

diff --git a/mutex.c b/mutex.c
index 6ec5d573e8a30145f9c53801f560c7edc329acc7..be3258df8347937a47bf68a99402283d2381b51a 100644 (file)
--- a/mutex.c
+++ b/mutex.c
 
 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;
-       char sem_name[32];
        int fd;
 
-       sprintf(sem_name, "/tmp/.fio_lock.XXXXXX");
        fd = mkstemp(sem_name);
        if (fd < 0) {
                perror("open sem");
@@ -42,9 +41,9 @@ struct fio_sem *fio_sem_init(int value)
                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");
@@ -62,7 +61,8 @@ struct fio_sem *fio_sem_init(int value)
        return sem;
 err:
        if (sem)
-               munmap(sem, sizeof(*sem));
+               fio_sem_remove(sem);
+
        unlink(sem_name);
        return NULL;
 }
diff --git a/mutex.h b/mutex.h
index 483e5f45369e9979b4ee43697ae00416fe87cc26..c4b0d8edcc3e30e635b42085b095c4bb786d2343 100644 (file)
--- a/mutex.h
+++ b/mutex.h
@@ -8,7 +8,7 @@ struct fio_sem {
        pthread_cond_t cond;
        unsigned int value;
 
-       char sem_name[32];
+       int sem_fd;
 };
 
 extern struct fio_sem *fio_sem_init(int);