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;
24 fd = mkstemp(sem_name);
30 if (ftruncate(fd, sizeof(struct fio_sem)) < 0) {
31 perror("ftruncate sem");
35 sem = mmap(NULL, sizeof(struct fio_sem), PROT_READ | PROT_WRITE,
37 if (sem == MAP_FAILED) {
48 if (pthread_mutexattr_init(&attr)) {
49 perror("pthread_mutexattr_init");
52 if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) {
53 perror("pthread_mutexattr_setpshared");
56 if (pthread_mutex_init(&sem->lock, &attr)) {
57 perror("pthread_mutex_init");
70 void fio_sem_down(struct fio_sem *sem)
72 pthread_mutex_lock(&sem->lock);
73 while (sem->value == 0)
74 pthread_cond_wait(&sem->cond, &sem->lock);
76 pthread_mutex_unlock(&sem->lock);
79 void fio_sem_up(struct fio_sem *sem)
81 pthread_mutex_lock(&sem->lock);
83 pthread_cond_signal(&sem->cond);
85 pthread_mutex_unlock(&sem->lock);