diff --git a/src/quo-vadis-pthread.cc b/src/quo-vadis-pthread.cc index dad6baf..44ef5ad 100644 --- a/src/quo-vadis-pthread.cc +++ b/src/quo-vadis-pthread.cc @@ -60,8 +60,8 @@ qv_pthread_scope_split( int nthreads, qv_scope_t ***subscope ) { - const bool invld_args = !scope || npieces < 0 || - !color_array || nthreads < 0 || !subscope; + const bool invld_args = !scope || npieces < 0 || !color_array || + nthreads < 0 || !subscope; if (qvi_unlikely(invld_args)) { return QV_ERR_INVLD_ARG; } @@ -107,19 +107,14 @@ qv_pthread_create( // Since this is meant to behave similarly to // pthread_create(), return a reasonable errno. if (qvi_unlikely(rc != QV_SUCCESS)) return ENOMEM; - // TODO(skg) Cleanup. + + auto group = dynamic_cast(qvi_scope_group_get(scope)); qvi_pthread_group_pthread_create_args_s *cargs = nullptr; - rc = qvi_new(&cargs); + rc = qvi_new(&cargs, group->thgroup, qvi_pthread_routine, arg_ptr); if (qvi_unlikely(rc != QV_SUCCESS)) { qvi_delete(&arg_ptr); return ENOMEM; } - // TODO(skg) Cleanup. - auto gt = dynamic_cast(qvi_scope_group_get(scope)); - cargs->group = gt->thgroup; - cargs->th_routine = qvi_pthread_routine; - cargs->th_routine_argp = arg_ptr; - return pthread_create( thread, attr, qvi_pthread_group_s::call_first_from_pthread_create, cargs ); diff --git a/src/qvi-pthread.cc b/src/qvi-pthread.cc index 9b1cbc9..aeb24c6 100644 --- a/src/qvi-pthread.cc +++ b/src/qvi-pthread.cc @@ -20,15 +20,15 @@ void * qvi_pthread_group_s::call_first_from_pthread_create( void *arg ) { - // TODO(skg) Cleanup. + const pid_t mytid = qvi_gettid(); auto args = (qvi_pthread_group_pthread_create_args_s *)arg; - auto group = args->group; - auto thread_routine = args->th_routine; - auto th_routine_argp = args->th_routine_argp; + qvi_pthread_group_t *const group = args->group; + const qvi_pthread_routine_fun_ptr_t thread_routine = args->throutine; + void *const th_routine_argp = args->throutine_argp; // Let the threads add their TIDs to the vector. { std::lock_guard guard(group->m_mutex); - group->m_tids.push_back(qvi_gettid()); + group->m_tids.push_back(mytid); } // Make sure they all contribute before continuing. pthread_barrier_wait(&group->m_barrier); @@ -36,7 +36,7 @@ qvi_pthread_group_s::call_first_from_pthread_create( bool worker = false; { std::lock_guard guard(group->m_mutex); - worker = group->m_tids.at(0) == qvi_gettid(); + worker = (group->m_tids.at(0) == mytid); } // The worker populates the TID to rank mapping, while the others wait. if (worker) { @@ -57,7 +57,7 @@ qvi_pthread_group_s::call_first_from_pthread_create( qvi_task_t *task = nullptr; const int rc = qvi_new(&task); if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error(); - group->m_tid2task.insert({qvi_gettid(), task}); + group->m_tid2task.insert({mytid, task}); } // Make sure they all finish before continuing. pthread_barrier_wait(&group->m_barrier); @@ -76,6 +76,16 @@ qvi_pthread_group_s::~qvi_pthread_group_s(void) pthread_barrier_destroy(&m_barrier); } +int +qvi_pthread_group_s::barrier(void) +{ + const int rc = pthread_barrier_wait(&(m_barrier)); + if (qvi_unlikely((rc != 0) && (rc != PTHREAD_BARRIER_SERIAL_THREAD))) { + return QV_ERR_INTERNAL; + } + return QV_SUCCESS; +} + /* * vim: ft=cpp ts=4 sts=4 sw=4 expandtab */ diff --git a/src/qvi-pthread.h b/src/qvi-pthread.h index c22ca0c..88e6ffb 100644 --- a/src/qvi-pthread.h +++ b/src/qvi-pthread.h @@ -17,16 +17,28 @@ #include "qvi-common.h" #include "qvi-utils.h" -// TODO(skg) Rename typedef void *(*qvi_pthread_routine_fun_ptr_t)(void *); struct qvi_pthread_group_s; typedef struct qvi_pthread_group_s qvi_pthread_group_t; struct qvi_pthread_group_pthread_create_args_s { + /** Thread group. */ qvi_pthread_group_t *group = nullptr; - qvi_pthread_routine_fun_ptr_t th_routine = nullptr; - void *th_routine_argp = nullptr; + /** The routine to call after group construction. */ + qvi_pthread_routine_fun_ptr_t throutine = nullptr; + /** Thread routine arguments. */ + void *throutine_argp = nullptr; + /** Constructor. */ + qvi_pthread_group_pthread_create_args_s(void) = delete; + /** Constructor. */ + qvi_pthread_group_pthread_create_args_s( + qvi_pthread_group_t *group_a, + qvi_pthread_routine_fun_ptr_t throutine_a, + void *throutine_argp_a + ) : group(group_a) + , throutine(throutine_a) + , throutine_argp(throutine_argp_a) { } }; struct qvi_pthread_group_s { @@ -93,11 +105,7 @@ struct qvi_pthread_group_s { } int - barrier(void) - { - pthread_barrier_wait(&m_barrier); - return QV_SUCCESS; - } + barrier(void); int create_from_split(