From: Jens Axboe Date: Wed, 25 May 2016 19:55:48 +0000 (-0600) Subject: mutex: abstract out cond/lock pshared init X-Git-Tag: fio-2.12~19 X-Git-Url: https://git.kernel.dk/?p=fio.git;a=commitdiff_plain;h=34febb23fa9c7b9b0d54c324effff1a808a8fe6e;ds=sidebyside mutex: abstract out cond/lock pshared init Signed-off-by: Jens Axboe --- diff --git a/backend.c b/backend.c index 45ee1569..d8f4f4cf 100644 --- a/backend.c +++ b/backend.c @@ -1428,8 +1428,6 @@ static void *thread_main(void *data) struct thread_data *td = fd->td; struct thread_options *o = &td->o; struct sk_out *sk_out = fd->sk_out; - pthread_condattr_t attr; - pthread_mutexattr_t mattr; int clear_state; int ret; @@ -1456,34 +1454,16 @@ static void *thread_main(void *data) INIT_FLIST_HEAD(&td->next_rand_list); td->io_hist_tree = RB_ROOT; - ret = pthread_mutexattr_init(&mattr); + ret = mutex_cond_init_pshared(&td->io_u_lock, &td->free_cond); if (ret) { - td_verror(td, ret, "pthread_mutexattr_init"); + td_verror(td, ret, "mutex_cond_init_pshared"); goto err; } -#ifdef FIO_HAVE_PSHARED_MUTEX - ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + ret = cond_init_pshared(&td->verify_cond); if (ret) { - td_verror(td, ret, "pthread_mutexattr_setpshared"); + td_verror(td, ret, "mutex_cond_pshared"); goto err; } -#endif - pthread_mutex_init(&td->io_u_lock, &mattr); - - ret = pthread_condattr_init(&attr); - if (ret) { - td_verror(td, ret, "pthread_condattr_init"); - goto err; - } -#ifdef FIO_HAVE_PSHARED_MUTEX - ret = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); - if (ret) { - td_verror(td, ret, "pthread_condattr_setpshared"); - goto err; - } -#endif - pthread_cond_init(&td->verify_cond, &attr); - pthread_cond_init(&td->free_cond, &attr); td_set_runstate(td, TD_INITIALIZED); dprint(FD_MUTEX, "up startup_mutex\n"); diff --git a/helper_thread.c b/helper_thread.c index c14296fb..e788af5b 100644 --- a/helper_thread.c +++ b/helper_thread.c @@ -142,38 +142,17 @@ int helper_thread_create(struct fio_mutex *startup_mutex, struct sk_out *sk_out) { struct helper_data *hd; int ret; - pthread_condattr_t cattr; - pthread_mutexattr_t mattr; hd = smalloc(sizeof(*hd)); setup_disk_util(); hd->sk_out = sk_out; - ret = pthread_mutexattr_init(&mattr); - if (ret) { - log_err("pthread_mutexattr_init: %s\n", strerror(ret)); - return 1; - } - ret = pthread_condattr_init(&cattr); - if (ret) { - log_err("pthread_condattr_init: %s\n", strerror(ret)); - return 1; - } -#ifdef FIO_HAVE_PSHARED_MUTEX - ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); - if (ret) { - log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); - return 1; - } - ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); - if (ret) { - log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); + + ret = mutex_cond_init_pshared(&hd->lock, &hd->cond); + if (ret) return 1; - } -#endif - pthread_cond_init(&hd->cond, &cattr); - pthread_mutex_init(&hd->lock, &mattr); + hd->startup_mutex = startup_mutex; ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd); diff --git a/iolog.c b/iolog.c index e2f9776e..93915079 100644 --- a/iolog.c +++ b/iolog.c @@ -576,7 +576,6 @@ void setup_log(struct io_log **log, struct log_params *p, const char *filename) { struct io_log *l; - pthread_mutexattr_t mattr; l = scalloc(1, sizeof(*l)); INIT_FLIST_HEAD(&l->io_logs); @@ -605,11 +604,7 @@ void setup_log(struct io_log **log, struct log_params *p, if (l->log_gz && !p->td) l->log_gz = 0; else if (l->log_gz || l->log_gz_store) { - pthread_mutexattr_init(&mattr); -#ifdef FIO_HAVE_PSHARED_MUTEX - pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); -#endif - pthread_mutex_init(&l->chunk_lock, &mattr); + mutex_init_pshared(&l->chunk_lock); p->td->flags |= TD_F_COMPRESS_LOG; } diff --git a/mutex.c b/mutex.c index 16107dd4..75809221 100644 --- a/mutex.c +++ b/mutex.c @@ -30,16 +30,39 @@ void fio_mutex_remove(struct fio_mutex *mutex) munmap((void *) mutex, sizeof(*mutex)); } -int __fio_mutex_init(struct fio_mutex *mutex, int value) +int cond_init_pshared(pthread_cond_t *cond) { - pthread_mutexattr_t attr; - pthread_condattr_t cond; + pthread_condattr_t cattr; int ret; - mutex->value = value; - mutex->magic = FIO_MUTEX_MAGIC; + ret = pthread_condattr_init(&cattr); + if (ret) { + log_err("pthread_condattr_init: %s\n", strerror(ret)); + return ret; + } + +#ifdef FIO_HAVE_PSHARED_MUTEX + ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); + if (ret) { + log_err("pthread_condattr_setpshared: %s\n", strerror(ret)); + return ret; + } +#endif + ret = pthread_cond_init(cond, &cattr); + if (ret) { + log_err("pthread_cond_init: %s\n", strerror(ret)); + return ret; + } + + return 0; +} - ret = pthread_mutexattr_init(&attr); +int mutex_init_pshared(pthread_mutex_t *mutex) +{ + pthread_mutexattr_t mattr; + int ret; + + ret = pthread_mutexattr_init(&mattr); if (ret) { log_err("pthread_mutexattr_init: %s\n", strerror(ret)); return ret; @@ -49,27 +72,47 @@ int __fio_mutex_init(struct fio_mutex *mutex, int value) * Not all platforms support process shared mutexes (FreeBSD) */ #ifdef FIO_HAVE_PSHARED_MUTEX - ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); if (ret) { log_err("pthread_mutexattr_setpshared: %s\n", strerror(ret)); return ret; } #endif - - pthread_condattr_init(&cond); -#ifdef FIO_HAVE_PSHARED_MUTEX - pthread_condattr_setpshared(&cond, PTHREAD_PROCESS_SHARED); -#endif - pthread_cond_init(&mutex->cond, &cond); - - ret = pthread_mutex_init(&mutex->lock, &attr); + ret = pthread_mutex_init(mutex, &mattr); if (ret) { log_err("pthread_mutex_init: %s\n", strerror(ret)); return ret; } - pthread_condattr_destroy(&cond); - pthread_mutexattr_destroy(&attr); + return 0; +} + +int mutex_cond_init_pshared(pthread_mutex_t *mutex, pthread_cond_t *cond) +{ + int ret; + + ret = mutex_init_pshared(mutex); + if (ret) + return ret; + + ret = cond_init_pshared(cond); + if (ret) + return ret; + + return 0; +} + +int __fio_mutex_init(struct fio_mutex *mutex, int value) +{ + int ret; + + mutex->value = value; + mutex->magic = FIO_MUTEX_MAGIC; + + ret = mutex_cond_init_pshared(&mutex->lock, &mutex->cond); + if (ret) + return ret; + return 0; } diff --git a/mutex.h b/mutex.h index 8c1a7111..54009bae 100644 --- a/mutex.h +++ b/mutex.h @@ -40,4 +40,8 @@ extern void fio_rwlock_unlock(struct fio_rwlock *); extern struct fio_rwlock *fio_rwlock_init(void); extern void fio_rwlock_remove(struct fio_rwlock *); +extern int mutex_init_pshared(pthread_mutex_t *); +extern int cond_init_pshared(pthread_cond_t *); +extern int mutex_cond_init_pshared(pthread_mutex_t *, pthread_cond_t *); + #endif diff --git a/workqueue.c b/workqueue.c index 13edafae..2e01b584 100644 --- a/workqueue.c +++ b/workqueue.c @@ -276,26 +276,13 @@ static int start_worker(struct workqueue *wq, unsigned int index, { struct submit_worker *sw = &wq->workers[index]; int ret; - pthread_condattr_t cattr; - pthread_mutexattr_t mattr; INIT_FLIST_HEAD(&sw->work_list); - ret = pthread_condattr_init(&cattr); - if (ret) - return ret; - ret = pthread_mutexattr_init(&mattr); - if (ret) - return ret; -#ifdef FIO_HAVE_PSHARED_MUTEX - ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); - if (ret) - return ret; - ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + + ret = mutex_cond_init_pshared(&sw->lock, &sw->cond); if (ret) return ret; -#endif - pthread_cond_init(&sw->cond, &cattr); - pthread_mutex_init(&sw->lock, &mattr); + sw->wq = wq; sw->index = index; sw->sk_out = sk_out; @@ -325,8 +312,6 @@ int workqueue_init(struct thread_data *td, struct workqueue *wq, unsigned int running; int i, error; int ret; - pthread_condattr_t cattr; - pthread_mutexattr_t mattr; wq->max_workers = max_workers; wq->td = td; @@ -334,31 +319,12 @@ int workqueue_init(struct thread_data *td, struct workqueue *wq, wq->work_seq = 0; wq->next_free_worker = 0; - ret = pthread_condattr_init(&cattr); - if (ret) { - td_verror(td, ret, "pthread_condattr_init"); - goto err; - } - ret = pthread_mutexattr_init(&mattr); - if (ret) { - td_verror(td, ret, "pthread_mutexattr_init"); - goto err; - } -#ifdef FIO_HAVE_PSHARED_MUTEX - ret = pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); - if (ret) { - td_verror(td, ret, "pthread_condattr_setpshared"); + ret = mutex_cond_init_pshared(&wq->flush_lock, &wq->flush_cond); + if (ret) goto err; - } - ret = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); - if (ret) { - td_verror(td, ret, "pthread_mutexattr_setpshared"); + ret = mutex_init_pshared(&wq->stat_lock); + if (ret) goto err; - } -#endif - pthread_cond_init(&wq->flush_cond, &cattr); - pthread_mutex_init(&wq->flush_lock, &mattr); - pthread_mutex_init(&wq->stat_lock, &mattr); wq->workers = smalloc(wq->max_workers * sizeof(struct submit_worker));