+int fio_mutex_down_timeout(struct fio_mutex *mutex, unsigned int seconds)
+{
+ struct timespec t;
+ int ret = 0;
+
+ clock_gettime(CLOCK_REALTIME, &t);
+ t.tv_sec += seconds;
+
+ pthread_mutex_lock(&mutex->lock);
+
+ while (!mutex->value && !ret) {
+ mutex->waiters++;
+ ret = pthread_cond_timedwait(&mutex->cond, &mutex->lock, &t);
+ mutex->waiters--;
+ }
+
+ if (!ret) {
+ mutex->value--;
+ pthread_mutex_unlock(&mutex->lock);
+ }
+
+ return ret;
+}
+
+void fio_mutex_down(struct fio_mutex *mutex)
+{
+ pthread_mutex_lock(&mutex->lock);
+
+ while (!mutex->value) {
+ mutex->waiters++;
+ pthread_cond_wait(&mutex->cond, &mutex->lock);
+ mutex->waiters--;
+ }
+
+ mutex->value--;
+ pthread_mutex_unlock(&mutex->lock);
+}
+
+void fio_mutex_up(struct fio_mutex *mutex)
+{
+ pthread_mutex_lock(&mutex->lock);
+ read_barrier();
+ if (!mutex->value && mutex->waiters)
+ pthread_cond_signal(&mutex->cond);
+ mutex->value++;
+ pthread_mutex_unlock(&mutex->lock);
+}
+
+void fio_mutex_down_write(struct fio_mutex *mutex)
+{
+ pthread_mutex_lock(&mutex->lock);
+
+ while (mutex->value != 0) {
+ mutex->waiters++;
+ pthread_cond_wait(&mutex->cond, &mutex->lock);
+ mutex->waiters--;
+ }
+
+ mutex->value--;
+ pthread_mutex_unlock(&mutex->lock);
+}
+
+void fio_mutex_down_read(struct fio_mutex *mutex)
+{
+ pthread_mutex_lock(&mutex->lock);
+
+ while (mutex->value < 0) {
+ mutex->waiters++;
+ pthread_cond_wait(&mutex->cond, &mutex->lock);
+ mutex->waiters--;
+ }
+
+ mutex->value++;
+ pthread_mutex_unlock(&mutex->lock);
+}
+
+void fio_mutex_up_read(struct fio_mutex *mutex)