11 void fio_sem_remove(struct fio_sem *sem)
13 unlink(sem->sem_name);
14 munmap(sem, sizeof(*sem));
17 struct fio_sem *fio_sem_init(int value)
19 pthread_mutexattr_t attr;
24 sprintf(sem_name, "/tmp/.fio_lock.XXXXXX");
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) {
47 strcpy(sem->sem_name, sem_name);
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");
57 if (pthread_mutex_init(&sem->lock, &attr)) {
58 perror("pthread_mutex_init");
64 munmap(sem, sizeof(*sem));
69 void fio_sem_down(struct fio_sem *sem)
71 pthread_mutex_lock(&sem->lock);
72 while (sem->value == 0)
73 pthread_cond_wait(&sem->cond, &sem->lock);
75 pthread_mutex_unlock(&sem->lock);
78 void fio_sem_up(struct fio_sem *sem)
80 pthread_mutex_lock(&sem->lock);
82 pthread_cond_signal(&sem->cond);
84 pthread_mutex_unlock(&sem->lock);