From c615aa8cb2c33817e37604e9af776a2495c2d0df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20K=2E=20Guti=C3=A9rrez?= Date: Fri, 12 Jul 2024 14:48:00 -0600 Subject: [PATCH] Hide internal details in quo-vadis-pthread.h. (#220) Signed-off-by: Samuel K. Gutierrez --- include/quo-vadis-pthread.h | 11 -------- src/quo-vadis-pthread.cc | 51 ++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/include/quo-vadis-pthread.h b/include/quo-vadis-pthread.h index b59acca..c75a094 100644 --- a/include/quo-vadis-pthread.h +++ b/include/quo-vadis-pthread.h @@ -27,17 +27,6 @@ extern "C" { #endif -typedef struct { - qv_scope_t *scope; - void *(*thread_routine)(void *); - void *arg; -} qv_pthread_args_t; - -void * -qv_pthread_routine( - void *arg -); - int qv_pthread_create( pthread_t *thread, diff --git a/src/quo-vadis-pthread.cc b/src/quo-vadis-pthread.cc index 96234e3..bfd8714 100644 --- a/src/quo-vadis-pthread.cc +++ b/src/quo-vadis-pthread.cc @@ -22,6 +22,36 @@ #include "qvi-scope.h" #include "qvi-utils.h" +typedef void *(*qvi_pthread_routine_fun_ptr_t)(void *); + +struct qvi_pthread_args_s { + qv_scope_t *scope = nullptr; + qvi_pthread_routine_fun_ptr_t th_routine = nullptr; + void *th_routine_argp = nullptr; + /** Constructor. */ + qvi_pthread_args_s(void) = delete; + /** Constructor. */ + qvi_pthread_args_s( + qv_scope_t *scope_a, + qvi_pthread_routine_fun_ptr_t th_routine_a, + void *th_routine_argp_a + ) : scope(scope_a) + , th_routine(th_routine_a) + , th_routine_argp(th_routine_argp_a) { } +}; + +static void * +qvi_pthread_routine( + void *arg +) { + qvi_pthread_args_s *arg_ptr = (qvi_pthread_args_s *)arg; + qvi_scope_bind_push(arg_ptr->scope); + + void *const ret = arg_ptr->th_routine(arg_ptr->th_routine_argp); + qvi_delete(&arg_ptr); + pthread_exit(ret); +} + int qv_pthread_scope_split( qv_scope_t *scope, @@ -61,18 +91,6 @@ qv_pthread_scope_split_at( qvi_catch_and_return(); } -void * -qv_pthread_routine( - void *arg -) { - qv_pthread_args_t *arg_ptr = (qv_pthread_args_t *)arg; - qvi_scope_bind_push(arg_ptr->scope); - - void *ret = arg_ptr->thread_routine(arg_ptr->arg); - qvi_delete(&arg_ptr); - pthread_exit(ret); -} - /** * Similar to pthread_create(3). */ @@ -85,16 +103,13 @@ qv_pthread_create( qv_scope_t *scope ) { // Memory will be freed in qv_pthread_routine to avoid memory leaks. - qv_pthread_args_t *arg_ptr = nullptr; - const int rc = qvi_new(&arg_ptr); + qvi_pthread_args_s *arg_ptr = nullptr; + const int rc = qvi_new(&arg_ptr, scope, thread_routine, arg); // Since this is meant to behave similarly to // pthread_create(), return a reasonable errno. if (rc != QV_SUCCESS) return ENOMEM; - arg_ptr->scope = scope; - arg_ptr->thread_routine = thread_routine; - arg_ptr->arg = arg; - return pthread_create(thread, attr, qv_pthread_routine, arg_ptr); + return pthread_create(thread, attr, qvi_pthread_routine, arg_ptr); } int