Skip to content

Commit

Permalink
mutex: abstract out cond/lock pshared init
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Axboe <axboe@fb.com>
  • Loading branch information
axboe committed May 25, 2016
1 parent f9e5b5e commit 34febb2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 113 deletions.
28 changes: 4 additions & 24 deletions backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand Down
29 changes: 4 additions & 25 deletions helper_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions iolog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
77 changes: 60 additions & 17 deletions mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 7 additions & 41 deletions workqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -325,40 +312,19 @@ 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;
wq->ops = *ops;
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));

Expand Down

0 comments on commit 34febb2

Please sign in to comment.