Commit | Line | Data |
---|---|---|
07739b57 | 1 | #include <string.h> |
07739b57 | 2 | #include <sys/mman.h> |
8b4e954c | 3 | #include <assert.h> |
07739b57 | 4 | |
4fa6d0f8 | 5 | #include "log.h" |
07739b57 | 6 | #include "mutex.h" |
ae626d4e | 7 | #include "pshared.h" |
3c2d93ed | 8 | #include "os/os.h" |
ef3c94b6 | 9 | #include "fio_time.h" |
ef635057 | 10 | #include "gettime.h" |
07739b57 | 11 | |
f5a42524 | 12 | void __fio_mutex_remove(struct fio_mutex *mutex) |
07739b57 | 13 | { |
8f801ad5 | 14 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
58a157d4 | 15 | pthread_cond_destroy(&mutex->cond); |
4e1cc8e6 JA |
16 | |
17 | /* | |
18 | * Ensure any subsequent attempt to grab this mutex will fail | |
19 | * with an assert, instead of just silently hanging. | |
20 | */ | |
21 | memset(mutex, 0, sizeof(*mutex)); | |
f5a42524 JA |
22 | } |
23 | ||
24 | void fio_mutex_remove(struct fio_mutex *mutex) | |
25 | { | |
26 | __fio_mutex_remove(mutex); | |
5921e80c | 27 | munmap((void *) mutex, sizeof(*mutex)); |
07739b57 JA |
28 | } |
29 | ||
34febb23 JA |
30 | int __fio_mutex_init(struct fio_mutex *mutex, int value) |
31 | { | |
32 | int ret; | |
33 | ||
34 | mutex->value = value; | |
35 | mutex->magic = FIO_MUTEX_MAGIC; | |
36 | ||
37 | ret = mutex_cond_init_pshared(&mutex->lock, &mutex->cond); | |
38 | if (ret) | |
39 | return ret; | |
40 | ||
72242057 JA |
41 | return 0; |
42 | } | |
43 | ||
44 | struct fio_mutex *fio_mutex_init(int value) | |
45 | { | |
46 | struct fio_mutex *mutex = NULL; | |
47 | ||
48 | mutex = (void *) mmap(NULL, sizeof(struct fio_mutex), | |
49 | PROT_READ | PROT_WRITE, | |
50 | OS_MAP_ANON | MAP_SHARED, -1, 0); | |
51 | if (mutex == MAP_FAILED) { | |
52 | perror("mmap mutex"); | |
53 | return NULL; | |
54 | } | |
03e20d68 | 55 | |
72242057 JA |
56 | if (!__fio_mutex_init(mutex, value)) |
57 | return mutex; | |
f7c9e00e | 58 | |
72242057 | 59 | fio_mutex_remove(mutex); |
07739b57 JA |
60 | return NULL; |
61 | } | |
62 | ||
8b6a404c | 63 | static bool mutex_timed_out(struct timespec *t, unsigned int msecs) |
ef635057 | 64 | { |
8b6a404c VF |
65 | struct timeval tv; |
66 | struct timespec now; | |
67 | ||
68 | gettimeofday(&tv, NULL); | |
69 | now.tv_sec = tv.tv_sec; | |
70 | now.tv_nsec = tv.tv_usec * 1000; | |
09400a60 | 71 | |
09400a60 | 72 | return mtime_since(t, &now) >= msecs; |
ef635057 JA |
73 | } |
74 | ||
09400a60 | 75 | int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int msecs) |
656b1393 | 76 | { |
ef635057 | 77 | struct timeval tv_s; |
7e92a66a | 78 | struct timespec base; |
656b1393 JA |
79 | struct timespec t; |
80 | int ret = 0; | |
81 | ||
8f801ad5 | 82 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
8b4e954c | 83 | |
b11e4ccf | 84 | gettimeofday(&tv_s, NULL); |
7e92a66a YS |
85 | base.tv_sec = t.tv_sec = tv_s.tv_sec; |
86 | base.tv_nsec = t.tv_nsec = tv_s.tv_usec * 1000; | |
656b1393 | 87 | |
09400a60 | 88 | t.tv_sec += msecs / 1000; |
90eff1c9 | 89 | t.tv_nsec += ((msecs * 1000000ULL) % 1000000000); |
09400a60 JA |
90 | if (t.tv_nsec >= 1000000000) { |
91 | t.tv_nsec -= 1000000000; | |
92 | t.tv_sec++; | |
93 | } | |
94 | ||
656b1393 JA |
95 | pthread_mutex_lock(&mutex->lock); |
96 | ||
09400a60 | 97 | mutex->waiters++; |
656b1393 | 98 | while (!mutex->value && !ret) { |
ef635057 JA |
99 | /* |
100 | * Some platforms (FreeBSD 9?) seems to return timed out | |
101 | * way too early, double check. | |
102 | */ | |
656b1393 | 103 | ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t); |
7e92a66a | 104 | if (ret == ETIMEDOUT && !mutex_timed_out(&base, msecs)) |
ef635057 | 105 | ret = 0; |
656b1393 | 106 | } |
09400a60 | 107 | mutex->waiters--; |
656b1393 JA |
108 | |
109 | if (!ret) { | |
110 | mutex->value--; | |
111 | pthread_mutex_unlock(&mutex->lock); | |
42e833fa | 112 | return 0; |
656b1393 JA |
113 | } |
114 | ||
09400a60 | 115 | pthread_mutex_unlock(&mutex->lock); |
656b1393 JA |
116 | return ret; |
117 | } | |
118 | ||
66608372 | 119 | bool fio_mutex_down_trylock(struct fio_mutex *mutex) |
72242057 | 120 | { |
66608372 | 121 | bool ret = true; |
72242057 JA |
122 | |
123 | assert(mutex->magic == FIO_MUTEX_MAGIC); | |
124 | ||
125 | pthread_mutex_lock(&mutex->lock); | |
126 | if (mutex->value) { | |
127 | mutex->value--; | |
66608372 | 128 | ret = false; |
72242057 JA |
129 | } |
130 | pthread_mutex_unlock(&mutex->lock); | |
131 | ||
132 | return ret; | |
133 | } | |
134 | ||
cdd18ad8 | 135 | void fio_mutex_down(struct fio_mutex *mutex) |
07739b57 | 136 | { |
8f801ad5 | 137 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
8b4e954c | 138 | |
cdd18ad8 | 139 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
140 | |
141 | while (!mutex->value) { | |
142 | mutex->waiters++; | |
cdd18ad8 | 143 | pthread_cond_wait(&mutex->cond, &mutex->lock); |
4d4e80f2 JA |
144 | mutex->waiters--; |
145 | } | |
146 | ||
cdd18ad8 JA |
147 | mutex->value--; |
148 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 JA |
149 | } |
150 | ||
cdd18ad8 | 151 | void fio_mutex_up(struct fio_mutex *mutex) |
07739b57 | 152 | { |
6899b6cb JA |
153 | int do_wake = 0; |
154 | ||
8f801ad5 | 155 | assert(mutex->magic == FIO_MUTEX_MAGIC); |
8b4e954c | 156 | |
cdd18ad8 | 157 | pthread_mutex_lock(&mutex->lock); |
4d4e80f2 JA |
158 | read_barrier(); |
159 | if (!mutex->value && mutex->waiters) | |
6899b6cb | 160 | do_wake = 1; |
cdd18ad8 | 161 | mutex->value++; |
6899b6cb JA |
162 | |
163 | if (do_wake) | |
164 | pthread_cond_signal(&mutex->cond); | |
e4ccf13b JA |
165 | |
166 | pthread_mutex_unlock(&mutex->lock); | |
07739b57 | 167 | } |