X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=mutex.c;h=414e5532d1385f3b9ceda3b6e316907b5a978376;hp=bcc37ae648c0bf440949603d762463a1ea74a7f8;hb=6164890e4db96c073274990edca12706e9c41659;hpb=64d4d313ef4f537408d28fac863cf2a2b0f00175 diff --git a/mutex.c b/mutex.c index bcc37ae6..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,7 +91,8 @@ 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); @@ -94,8 +101,13 @@ void fio_mutex_up(struct fio_mutex *mutex) void fio_mutex_down_write(struct fio_mutex *mutex) { pthread_mutex_lock(&mutex->lock); - while (mutex->value != 0) + + while (mutex->value != 0) { + mutex->waiters++; pthread_cond_wait(&mutex->cond, &mutex->lock); + mutex->waiters--; + } + mutex->value--; pthread_mutex_unlock(&mutex->lock); } @@ -103,8 +115,13 @@ void fio_mutex_down_write(struct fio_mutex *mutex) void fio_mutex_down_read(struct fio_mutex *mutex) { pthread_mutex_lock(&mutex->lock); - while (mutex->value < 0) + + while (mutex->value < 0) { + mutex->waiters++; pthread_cond_wait(&mutex->cond, &mutex->lock); + mutex->waiters--; + } + mutex->value++; pthread_mutex_unlock(&mutex->lock); } @@ -113,7 +130,8 @@ void fio_mutex_up_read(struct fio_mutex *mutex) { pthread_mutex_lock(&mutex->lock); mutex->value--; - if (mutex->value >= 0) + read_barrier(); + if (mutex->value >= 0 && mutex->waiters) pthread_cond_signal(&mutex->cond); pthread_mutex_unlock(&mutex->lock); } @@ -122,7 +140,8 @@ void fio_mutex_up_write(struct fio_mutex *mutex) { pthread_mutex_lock(&mutex->lock); mutex->value++; - if (mutex->value >= 0) + read_barrier(); + if (mutex->value >= 0 && mutex->waiters) pthread_cond_signal(&mutex->cond); pthread_mutex_unlock(&mutex->lock); }