X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=mutex.c;h=414e5532d1385f3b9ceda3b6e316907b5a978376;hp=f3c8326663f1d8b350a4b6cc0afd484111682711;hb=5921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373;hpb=cdd18ad87ed9a3639b76c41cfc9682ad7cce652e diff --git a/mutex.c b/mutex.c index f3c83266..414e5532 100644 --- a/mutex.c +++ b/mutex.c @@ -7,11 +7,12 @@ #include #include "mutex.h" +#include "arch/arch.h" void fio_mutex_remove(struct fio_mutex *mutex) { close(mutex->mutex_fd); - munmap(mutex, sizeof(*mutex)); + munmap((void *) mutex, sizeof(*mutex)); } struct fio_mutex *fio_mutex_init(int value) @@ -33,8 +34,8 @@ struct fio_mutex *fio_mutex_init(int value) goto err; } - mutex = mmap(NULL, sizeof(struct fio_mutex), PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0); + mutex = (void *) mmap(NULL, sizeof(struct fio_mutex), + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mutex == MAP_FAILED) { perror("mmap mutex"); close(fd); @@ -76,8 +77,13 @@ err: void fio_mutex_down(struct fio_mutex *mutex) { pthread_mutex_lock(&mutex->lock); - while (mutex->value == 0) + + while (!mutex->value) { + mutex->waiters++; pthread_cond_wait(&mutex->cond, &mutex->lock); + mutex->waiters--; + } + mutex->value--; pthread_mutex_unlock(&mutex->lock); } @@ -85,8 +91,57 @@ void fio_mutex_down(struct fio_mutex *mutex) void fio_mutex_up(struct fio_mutex *mutex) { pthread_mutex_lock(&mutex->lock); - if (!mutex->value) + 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) +{ + pthread_mutex_lock(&mutex->lock); + mutex->value--; + read_barrier(); + if (mutex->value >= 0 && mutex->waiters) + pthread_cond_signal(&mutex->cond); + pthread_mutex_unlock(&mutex->lock); +} + +void fio_mutex_up_write(struct fio_mutex *mutex) +{ + pthread_mutex_lock(&mutex->lock); + mutex->value++; + read_barrier(); + if (mutex->value >= 0 && mutex->waiters) + pthread_cond_signal(&mutex->cond); + pthread_mutex_unlock(&mutex->lock); +}