Commit | Line | Data |
---|---|---|
07739b57 JA |
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 | { | |
13 | unlink(sem->sem_name); | |
14 | munmap(sem, sizeof(*sem)); | |
15 | } | |
16 | ||
17 | struct fio_sem *fio_sem_init(int value) | |
18 | { | |
e53bd0b3 | 19 | struct fio_sem *sem = NULL; |
07739b57 | 20 | pthread_mutexattr_t attr; |
07739b57 JA |
21 | char sem_name[32]; |
22 | int fd; | |
23 | ||
24 | sprintf(sem_name, "/tmp/.fio_lock.XXXXXX"); | |
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"); | |
e53bd0b3 | 33 | goto err; |
07739b57 JA |
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); | |
e53bd0b3 JA |
41 | sem = NULL; |
42 | goto err; | |
07739b57 JA |
43 | } |
44 | ||
45 | close(fd); | |
46 | sem->value = value; | |
47 | strcpy(sem->sem_name, sem_name); | |
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 | } | |
57 | if (pthread_mutex_init(&sem->lock, &attr)) { | |
58 | perror("pthread_mutex_init"); | |
59 | goto err; | |
60 | } | |
61 | ||
62 | return sem; | |
63 | err: | |
e53bd0b3 JA |
64 | if (sem) |
65 | munmap(sem, sizeof(*sem)); | |
07739b57 JA |
66 | unlink(sem_name); |
67 | return NULL; | |
68 | } | |
69 | ||
70 | void fio_sem_down(struct fio_sem *sem) | |
71 | { | |
72 | pthread_mutex_lock(&sem->lock); | |
73 | while (sem->value == 0) | |
74 | pthread_cond_wait(&sem->cond, &sem->lock); | |
75 | sem->value--; | |
76 | pthread_mutex_unlock(&sem->lock); | |
77 | } | |
78 | ||
79 | void fio_sem_up(struct fio_sem *sem) | |
80 | { | |
81 | pthread_mutex_lock(&sem->lock); | |
82 | if (!sem->value) | |
83 | pthread_cond_signal(&sem->cond); | |
84 | sem->value++; | |
85 | pthread_mutex_unlock(&sem->lock); | |
86 | } |