X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=mutex.c;h=88044f3a818753feb374bc0370e545fb2095d248;hb=d01c404b8971df6b86a7274177ddfb8d887061c8;hp=1538f62c4b1a286ebe015c299031ffede126e2c4;hpb=3c2d93ede7d03b3a6923edb55c7737fe014794cb;p=fio.git diff --git a/mutex.c b/mutex.c index 1538f62c..88044f3a 100644 --- a/mutex.c +++ b/mutex.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -10,6 +11,7 @@ #include "mutex.h" #include "arch/arch.h" #include "os/os.h" +#include "helpers.h" void fio_mutex_remove(struct fio_mutex *mutex) { @@ -31,6 +33,14 @@ struct fio_mutex *fio_mutex_init(int value) return NULL; } +#ifdef FIO_HAVE_FALLOCATE + ret = posix_fallocate(fd, 0, sizeof(struct fio_mutex)); + if (ret > 0) { + fprintf(stderr, "posix_fallocate mutex failed: %s\n", strerror(ret)); + goto err; + } +#endif + if (ftruncate(fd, sizeof(struct fio_mutex)) < 0) { perror("ftruncate mutex"); goto err; @@ -63,14 +73,18 @@ struct fio_mutex *fio_mutex_init(int value) log_err("pthread_mutexattr_init: %s\n", strerror(ret)); goto err; } +#ifdef FIO_HAVE_PSHARED_MUTEX ret = pthread_mutexattr_setpshared(&attr, mflag); if (ret) { log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); goto err; } +#endif pthread_condattr_init(&cond); +#ifdef FIO_HAVE_PSHARED_MUTEX pthread_condattr_setpshared(&cond, mflag); +#endif pthread_cond_init(&mutex->cond, &cond); ret = pthread_mutex_init(&mutex->lock, &attr); @@ -88,6 +102,30 @@ err: return NULL; } +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);