11 void fio_sem_remove(struct fio_sem *sem)
14 munmap(sem, sizeof(*sem));
17 struct fio_sem *fio_sem_init(int value)
19 char sem_name[] = "/tmp/.fio_sem.XXXXXX";
20 struct fio_sem *sem = NULL;
21 pthread_mutexattr_t attr;
22 pthread_condattr_t cond;
25 fd = mkstemp(sem_name);
31 if (ftruncate(fd, sizeof(struct fio_sem)) < 0) {
32 perror("ftruncate sem");
36 sem = mmap(NULL, sizeof(struct fio_sem), PROT_READ | PROT_WRITE,
38 if (sem == MAP_FAILED) {
49 if (pthread_mutexattr_init(&attr)) {
50 perror("pthread_mutexattr_init");
53 if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
54 perror("pthread_mutexattr_setpshared");
58 pthread_condattr_init(&cond);
59 pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED);
60 pthread_cond_init(&sem->cond, &cond);
62 if (pthread_mutex_init(&sem->lock, &attr)) {
63 perror("pthread_mutex_init");
76 void fio_sem_down(struct fio_sem *sem)
78 pthread_mutex_lock(&sem->lock);
79 while (sem->value == 0)
80 pthread_cond_wait(&sem->cond, &sem->lock);
82 pthread_mutex_unlock(&sem->lock);
85 void fio_sem_up(struct fio_sem *sem)
87 pthread_mutex_lock(&sem->lock);
89 pthread_cond_signal(&sem->cond);
91 pthread_mutex_unlock(&sem->lock);