Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <pthread.h> |
| 7 | #include <sys/mman.h> |
| 8 | |
| 9 | #include "mutex.h" |
| 10 | |
| 11 | void fio_sem_remove(struct fio_sem *sem) |
| 12 | { |
Jens Axboe | f7c9e00 | 2007-03-09 12:40:02 +0100 | [diff] [blame] | 13 | close(sem->sem_fd); |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 14 | munmap(sem, sizeof(*sem)); |
| 15 | } |
| 16 | |
| 17 | struct fio_sem *fio_sem_init(int value) |
| 18 | { |
Jens Axboe | f7c9e00 | 2007-03-09 12:40:02 +0100 | [diff] [blame] | 19 | char sem_name[] = "/tmp/.fio_sem.XXXXXX"; |
Jens Axboe | e53bd0b | 2007-03-08 20:29:11 +0100 | [diff] [blame] | 20 | struct fio_sem *sem = NULL; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 21 | pthread_mutexattr_t attr; |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame^] | 22 | pthread_condattr_t cond; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 23 | int fd; |
| 24 | |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 25 | fd = mkstemp(sem_name); |
| 26 | if (fd < 0) { |
| 27 | perror("open sem"); |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | if (ftruncate(fd, sizeof(struct fio_sem)) < 0) { |
| 32 | perror("ftruncate sem"); |
Jens Axboe | e53bd0b | 2007-03-08 20:29:11 +0100 | [diff] [blame] | 33 | goto err; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | sem = mmap(NULL, sizeof(struct fio_sem), PROT_READ | PROT_WRITE, |
| 37 | MAP_SHARED, fd, 0); |
| 38 | if (sem == MAP_FAILED) { |
| 39 | perror("mmap sem"); |
| 40 | close(fd); |
Jens Axboe | e53bd0b | 2007-03-08 20:29:11 +0100 | [diff] [blame] | 41 | sem = NULL; |
| 42 | goto err; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Jens Axboe | f7c9e00 | 2007-03-09 12:40:02 +0100 | [diff] [blame] | 45 | unlink(sem_name); |
| 46 | sem->sem_fd = fd; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 47 | sem->value = value; |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 48 | |
| 49 | if (pthread_mutexattr_init(&attr)) { |
| 50 | perror("pthread_mutexattr_init"); |
| 51 | goto err; |
| 52 | } |
| 53 | if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED)) { |
| 54 | perror("pthread_mutexattr_setpshared"); |
| 55 | goto err; |
| 56 | } |
Zhang, Yanmin | 108fcc1 | 2008-02-04 09:17:52 +0100 | [diff] [blame^] | 57 | |
| 58 | pthread_condattr_init(&cond); |
| 59 | pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED); |
| 60 | pthread_cond_init(&sem->cond, &cond); |
| 61 | |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 62 | if (pthread_mutex_init(&sem->lock, &attr)) { |
| 63 | perror("pthread_mutex_init"); |
| 64 | goto err; |
| 65 | } |
| 66 | |
| 67 | return sem; |
| 68 | err: |
Jens Axboe | e53bd0b | 2007-03-08 20:29:11 +0100 | [diff] [blame] | 69 | if (sem) |
Jens Axboe | f7c9e00 | 2007-03-09 12:40:02 +0100 | [diff] [blame] | 70 | fio_sem_remove(sem); |
| 71 | |
Jens Axboe | 07739b5 | 2007-03-08 20:25:46 +0100 | [diff] [blame] | 72 | unlink(sem_name); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | void fio_sem_down(struct fio_sem *sem) |
| 77 | { |
| 78 | pthread_mutex_lock(&sem->lock); |
| 79 | while (sem->value == 0) |
| 80 | pthread_cond_wait(&sem->cond, &sem->lock); |
| 81 | sem->value--; |
| 82 | pthread_mutex_unlock(&sem->lock); |
| 83 | } |
| 84 | |
| 85 | void fio_sem_up(struct fio_sem *sem) |
| 86 | { |
| 87 | pthread_mutex_lock(&sem->lock); |
| 88 | if (!sem->value) |
| 89 | pthread_cond_signal(&sem->cond); |
| 90 | sem->value++; |
| 91 | pthread_mutex_unlock(&sem->lock); |
| 92 | } |