From: Arran Cudbard-Bell Date: Fri, 2 Apr 2021 12:41:16 +0000 (+0100) Subject: Allow worker to set a default interpreter for a thread X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2e10508a37e31e5cb746a2cd46322a5c68830c36;p=thirdparty%2Ffreeradius-server.git Allow worker to set a default interpreter for a thread --- diff --git a/src/lib/io/schedule.c b/src/lib/io/schedule.c index b36879ead12..b497b17c4cd 100644 --- a/src/lib/io/schedule.c +++ b/src/lib/io/schedule.c @@ -420,11 +420,6 @@ fr_schedule_t *fr_schedule_create(TALLOC_CTX *ctx, fr_event_list_t *el, return NULL; } - /* - * Glue workers into the trigger code. - */ - trigger_worker_request_add = fr_worker_request_add; - sc->config = config; sc->el = el; sc->log = logger; diff --git a/src/lib/io/worker.c b/src/lib/io/worker.c index 6420e99984e..86b46a54c8d 100644 --- a/src/lib/io/worker.c +++ b/src/lib/io/worker.c @@ -70,8 +70,6 @@ static void worker_verify(fr_worker_t *worker); #define WORKER_VERIFY #endif -static _Thread_local fr_worker_t *thread_local_worker; - #define CACHE_LINE_SIZE 64 static alignas(CACHE_LINE_SIZE) atomic_uint64_t request_number = 0; @@ -915,7 +913,6 @@ void fr_worker_destroy(fr_worker_t *worker) fr_channel_responder_ack_close(worker->channel[i]); } - thread_local_worker = NULL; talloc_free(worker); } @@ -1200,8 +1197,7 @@ nomem: fr_strerror_const("Failed initialising interpreter"); goto fail; } - - thread_local_worker = worker; + unlang_interpret_set_thread_default(worker->intp); return worker; } @@ -1291,42 +1287,6 @@ void fr_worker_post_event(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void worker_run_request(worker, fr_time()); /* Event loop time can be too old, and trigger asserts */ } -/** Asynchronously add a request_t to a worker thread - * - * When we don't know what the worker is... - * - * @param[in] request to add. - * @param[in] process module method to call when the request is executed. - * @param[in] uctx to pass to the process method. - * @return - * - 0 on success. - * - -1 on failure. - */ -int fr_worker_request_add(request_t *request, module_method_t process, void *uctx) -{ - fr_worker_t *worker; - fr_time_t now; - - worker = thread_local_worker; - if (unlikely(!worker)) { - fr_strerror_const("No worker has been defined"); - return -1; - } - - now = fr_time(); - - worker_request_init(worker, request, now); - worker_request_name_number(request); - - fr_assert(request_is_internal(request)); - request->async->process = process; - request->async->process_inst = uctx; - - worker_request_time_tracking_start(worker, request, now); - - return 0; -} - /** Print debug information about the worker structure * * @param[in] worker the worker diff --git a/src/lib/io/worker.h b/src/lib/io/worker.h index d0f9a268787..ef3fc740157 100644 --- a/src/lib/io/worker.h +++ b/src/lib/io/worker.h @@ -87,8 +87,6 @@ int fr_worker_stats(fr_worker_t const *worker, int num, uint64_t *stats) CC_HIN #include -int fr_worker_request_add(request_t *request, module_method_t process, void *uctx) CC_HINT(nonnull(1,2)); - int fr_worker_subrequest_add(request_t *request) CC_HINT(nonnull); #ifdef __cplusplus diff --git a/src/lib/server/trigger.c b/src/lib/server/trigger.c index 4f729d853ea..1f586018986 100644 --- a/src/lib/server/trigger.c +++ b/src/lib/server/trigger.c @@ -32,12 +32,6 @@ RCSID("$Id$") #include #include -/* - * Public "thunk" API so that the various binaries can link to - * libfreeradius-server.a, and don't need to be linked to libfreeradius-io.a - */ -fr_trigger_worker_t trigger_worker_request_add = NULL; - /** Whether triggers are enabled globally * */ @@ -137,7 +131,7 @@ static int _trigger_last_fired_cmp(void const *a, void const *b) * We don't register the trigger xlat here, as we may inadvertently initialise * the xlat code, which is annoying when this is called from a utility. * - * @param cs to use as global trigger section. + * @param[in] cs to use as global trigger section. * @return * - 0 on success. * - -1 on failure. @@ -164,7 +158,6 @@ int trigger_exec_init(CONF_SECTION const *cs) trigger_mutex = talloc(talloc_null_ctx(), pthread_mutex_t); pthread_mutex_init(trigger_mutex, 0); talloc_set_destructor(trigger_mutex, _mutex_free); - triggers_init = true; return 0; @@ -405,6 +398,11 @@ int trigger_exec(request_t *request, CONF_SECTION const *cs, char const *name, b return -1; } + /* + * Always run triggers in the default interpreter + */ + unlang_interpret_set(child, unlang_interpret_get_thread_default()); + if (unlang_subrequest_child_push_and_detach(child) < 0) goto error; if (unlang_interpret_push_function(child, trigger_run, trigger_resume, diff --git a/src/lib/server/trigger.h b/src/lib/server/trigger.h index 7a18c916b79..12ce8e08b02 100644 --- a/src/lib/server/trigger.h +++ b/src/lib/server/trigger.h @@ -52,7 +52,6 @@ bool trigger_enabled(void); void trigger_args_afrom_server(TALLOC_CTX *ctx, fr_pair_list_t *list, char const *server, uint16_t port); typedef int (*fr_trigger_worker_t)(request_t *request, module_method_t process, void *ctx); -extern fr_trigger_worker_t trigger_worker_request_add; #ifdef __cplusplus } diff --git a/src/lib/unlang/interpret.c b/src/lib/unlang/interpret.c index 74786c511f2..d7ed314a199 100644 --- a/src/lib/unlang/interpret.c +++ b/src/lib/unlang/interpret.c @@ -34,6 +34,10 @@ RCSID("$Id$") #include "parallel_priv.h" #include "unlang_priv.h" +/** The default interpreter instance for this thread + */ +static _Thread_local unlang_interpret_t *intp_thread_default; + static fr_table_num_ordered_t const unlang_action_table[] = { { L("unwind"), UNLANG_ACTION_UNWIND }, { L("calculate-result"), UNLANG_ACTION_CALCULATE_RESULT }, @@ -1190,6 +1194,24 @@ unlang_interpret_t *unlang_interpret_get(request_t *request) return stack->intp; } +/** Set the default interpreter for this thread + * + + */ +void unlang_interpret_set_thread_default(unlang_interpret_t *intp) +{ + intp_thread_default = talloc_get_type_abort(intp, unlang_interpret_t); +} + +/** Get the default interpreter for this thread + * + * This allows detached requests to be executed asynchronously + */ +unlang_interpret_t *unlang_interpret_get_thread_default(void) +{ + return talloc_get_type_abort(intp_thread_default, unlang_interpret_t); +} + void unlang_interpret_init_global(void) { xlat_t *xlat; diff --git a/src/lib/unlang/interpret.h b/src/lib/unlang/interpret.h index be18e826644..9082c40e66b 100644 --- a/src/lib/unlang/interpret.h +++ b/src/lib/unlang/interpret.h @@ -119,6 +119,10 @@ void unlang_interpret_set(request_t *request, unlang_interpret_t *intp); unlang_interpret_t *unlang_interpret_get(request_t *request); +void unlang_interpret_set_thread_default(unlang_interpret_t *intp); + +unlang_interpret_t *unlang_interpret_get_thread_default(void); + rlm_rcode_t unlang_interpret(request_t *request) CC_HINT(hot); rlm_rcode_t unlang_interpret_synchronous(request_t *request); diff --git a/src/lib/unlang/subrequest_child.c b/src/lib/unlang/subrequest_child.c index 0db661d9207..cdcd312c7b1 100644 --- a/src/lib/unlang/subrequest_child.c +++ b/src/lib/unlang/subrequest_child.c @@ -309,11 +309,6 @@ int unlang_subrequest_child_push(rlm_rcode_t *out, request_t *child, unlang_frame_state_subrequest_t *state; unlang_stack_frame_t *frame; - /* - * Run in the same interpreter as the parent - */ - unlang_interpret_set(child, unlang_interpret_get(child->parent)); - /* * Push a new subrequest frame onto the stack * @@ -348,11 +343,6 @@ int unlang_subrequest_child_push(rlm_rcode_t *out, request_t *child, int unlang_subrequest_child_push_and_detach(request_t *child) { - /* - * Run in the same interpreter as the parent - */ - unlang_interpret_set(child, unlang_interpret_get(child->parent)); - /* * Ensures the child is setup correctly and adds * it into the runnable queue of whatever owns