5 #ifdef CONFIG_VALGRIND_DEV
6 #include <valgrind/valgrind.h>
8 #define RUNNING_ON_VALGRIND 0
17 void __fio_sem_remove(struct fio_sem *sem)
19 assert(sem->magic == FIO_SEM_MAGIC);
20 pthread_mutex_destroy(&sem->lock);
21 pthread_cond_destroy(&sem->cond);
24 * When not running on Valgrind, ensure any subsequent attempt to grab
25 * this semaphore will fail with an assert, instead of just silently
26 * hanging. When running on Valgrind, let Valgrind detect
29 if (!RUNNING_ON_VALGRIND)
30 memset(sem, 0, sizeof(*sem));
33 void fio_sem_remove(struct fio_sem *sem)
35 __fio_sem_remove(sem);
36 munmap((void *) sem, sizeof(*sem));
39 int __fio_sem_init(struct fio_sem *sem, int value)
44 /* Initialize .waiters explicitly for Valgrind. */
46 sem->magic = FIO_SEM_MAGIC;
48 ret = mutex_cond_init_pshared(&sem->lock, &sem->cond);
55 struct fio_sem *fio_sem_init(int value)
57 struct fio_sem *sem = NULL;
59 sem = (void *) mmap(NULL, sizeof(struct fio_sem),
60 PROT_READ | PROT_WRITE,
61 OS_MAP_ANON | MAP_SHARED, -1, 0);
62 if (sem == MAP_FAILED) {
63 perror("mmap semaphore");
67 if (!__fio_sem_init(sem, value))
74 static bool sem_timed_out(struct timespec *t, unsigned int msecs)
79 gettimeofday(&tv, NULL);
80 now.tv_sec = tv.tv_sec;
81 now.tv_nsec = tv.tv_usec * 1000;
83 return mtime_since(t, &now) >= msecs;
86 int fio_sem_down_timeout(struct fio_sem *sem, unsigned int msecs)
92 assert(sem->magic == FIO_SEM_MAGIC);
94 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
95 clock_gettime(CLOCK_MONOTONIC, &t);
97 clock_gettime(CLOCK_REALTIME, &t);
102 t.tv_sec += msecs / 1000;
103 t.tv_nsec += ((msecs * 1000000ULL) % 1000000000);
104 if (t.tv_nsec >= 1000000000) {
105 t.tv_nsec -= 1000000000;
109 pthread_mutex_lock(&sem->lock);
112 while (!sem->value && !ret) {
114 * Some platforms (FreeBSD 9?) seems to return timed out
115 * way too early, double check.
117 ret = pthread_cond_timedwait(&sem->cond, &sem->lock, &t);
118 if (ret == ETIMEDOUT && !sem_timed_out(&base, msecs))
125 pthread_mutex_unlock(&sem->lock);
129 pthread_mutex_unlock(&sem->lock);
133 bool fio_sem_down_trylock(struct fio_sem *sem)
137 assert(sem->magic == FIO_SEM_MAGIC);
139 pthread_mutex_lock(&sem->lock);
144 pthread_mutex_unlock(&sem->lock);
149 void fio_sem_down(struct fio_sem *sem)
151 assert(sem->magic == FIO_SEM_MAGIC);
153 pthread_mutex_lock(&sem->lock);
155 while (!sem->value) {
157 pthread_cond_wait(&sem->cond, &sem->lock);
162 pthread_mutex_unlock(&sem->lock);
165 void fio_sem_up(struct fio_sem *sem)
169 assert(sem->magic == FIO_SEM_MAGIC);
171 pthread_mutex_lock(&sem->lock);
173 if (!sem->value && sem->waiters)
178 pthread_cond_signal(&sem->cond);
180 pthread_mutex_unlock(&sem->lock);