From: Alan T. DeKok Date: Tue, 30 Oct 2018 15:23:31 +0000 (-0400) Subject: move mutex into thread instance of the file reader X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=318f09ca3cef92f28d31872904bb2e83f97dd3ef;p=thirdparty%2Ffreeradius-server.git move mutex into thread instance of the file reader --- diff --git a/src/modules/proto_detail/proto_detail.c b/src/modules/proto_detail/proto_detail.c index 6900598d7f0..8c1ec29b039 100644 --- a/src/modules/proto_detail/proto_detail.c +++ b/src/modules/proto_detail/proto_detail.c @@ -682,27 +682,9 @@ static int mod_bootstrap(void *instance, CONF_SECTION *conf) } } - (void) pthread_mutex_init(&inst->worker_mutex, NULL); - return 0; } -/** Detach the application - * - * - * @param[in] instance Ctx data for this application. - * @return - * - 0 on success. - * - -1 on failure. - */ -static int mod_detach(void *instance) -{ - proto_detail_t *inst = talloc_get_type_abort(instance, proto_detail_t); - - pthread_mutex_destroy(&inst->worker_mutex); - - return 0; -} fr_app_t proto_detail = { .magic = RLM_MODULE_INIT, @@ -712,7 +694,6 @@ fr_app_t proto_detail = { .bootstrap = mod_bootstrap, .instantiate = mod_instantiate, - .detach = mod_detach, .open = mod_open, .decode = mod_decode, .encode = mod_encode, diff --git a/src/modules/proto_detail/proto_detail.h b/src/modules/proto_detail/proto_detail.h index e32c52173f8..e42084583e1 100644 --- a/src/modules/proto_detail/proto_detail.h +++ b/src/modules/proto_detail/proto_detail.h @@ -64,9 +64,6 @@ typedef struct proto_detail_t { fr_listen_t *listen; //!< The listener structure which describes //!< the I/O path. - pthread_mutex_t worker_mutex; //!< for the workers - int num_workers; //!< number of workers - } proto_detail_t; @@ -107,6 +104,7 @@ typedef struct proto_detail_work_thread_t { fr_event_list_t *el; //!< for various timers fr_network_t *nr; //!< for Linux-specific callbacks fr_listen_t *listen; //!< talloc_parent() is slow + struct proto_detail_work_thread_t *file_parent; //!< thread instance of the directory reader that spawned us char const *filename_work; //!< work file name fr_dlist_head_t list; //!< for retransmissions @@ -131,6 +129,9 @@ typedef struct proto_detail_work_thread_t { off_t read_offset; //!< where we're reading from in filename_work fr_event_timer_t const *ev; //!< for detail file timers. + + pthread_mutex_t worker_mutex; //!< for the workers + int num_workers; //!< number of workers } proto_detail_work_thread_t; typedef struct proto_detail_process_t { diff --git a/src/modules/proto_detail/proto_detail_file.c b/src/modules/proto_detail/proto_detail_file.c index 4ca2ff96fa5..5fde3462ebd 100644 --- a/src/modules/proto_detail/proto_detail_file.c +++ b/src/modules/proto_detail/proto_detail_file.c @@ -105,14 +105,13 @@ static ssize_t mod_write(fr_listen_t *li, void *packet_ctx, fr_time_t request_ti static void mod_vnode_extend(fr_listen_t *li, UNUSED uint32_t fflags) { - proto_detail_file_t const *inst = talloc_get_type_abort_const(li->app_io_instance, proto_detail_file_t); proto_detail_file_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_file_thread_t); bool has_worker = false; - pthread_mutex_lock(&inst->parent->worker_mutex); - has_worker = (inst->parent->num_workers != 0); - pthread_mutex_unlock(&inst->parent->worker_mutex); + pthread_mutex_lock(&thread->worker_mutex); + has_worker = (thread->num_workers != 0); + pthread_mutex_unlock(&thread->worker_mutex); if (has_worker) return; @@ -145,6 +144,7 @@ static int mod_open(fr_listen_t *li) thread->inst = inst; thread->name = talloc_typed_asprintf(inst, "proto_detail polling for files matching %s", inst->filename); thread->vnode_fd = -1; + pthread_mutex_init(&thread->worker_mutex, NULL); DEBUG("Listening on %s bound to virtual server %s FD %d", thread->name, cf_section_name2(inst->parent->server_cs), thread->fd); @@ -325,6 +325,7 @@ static int work_exists(proto_detail_file_thread_t *thread, int fd) li->app_io_instance = inst->parent->work_io_instance; work->inst = li->app_io_instance; + work->file_parent = li->thread_instance; work->ev = NULL; li->fd = work->fd = dup(fd); @@ -370,9 +371,9 @@ static int work_exists(proto_detail_file_thread_t *thread, int fd) li->default_message_size = inst->parent->max_packet_size; li->num_messages = inst->parent->num_messages; - pthread_mutex_lock(&inst->parent->worker_mutex); - inst->parent->num_workers++; - pthread_mutex_unlock(&inst->parent->worker_mutex); + pthread_mutex_lock(&thread->worker_mutex); + thread->num_workers++; + pthread_mutex_unlock(&thread->worker_mutex); /* * Open the detail.work file. @@ -453,9 +454,9 @@ static void work_init(proto_detail_file_thread_t *thread) int fd, rcode; bool has_worker; - pthread_mutex_lock(&inst->parent->worker_mutex); - has_worker = (inst->parent->num_workers != 0); - pthread_mutex_unlock(&inst->parent->worker_mutex); + pthread_mutex_lock(&thread->worker_mutex); + has_worker = (thread->num_workers != 0); + pthread_mutex_unlock(&thread->worker_mutex); /* * The worker is still processing the file, poll until @@ -702,6 +703,8 @@ static int mod_close(fr_listen_t *li) } close(thread->vnode_fd); thread->vnode_fd = -1; + + pthread_mutex_destroy(&thread->worker_mutex); } return 0; diff --git a/src/modules/proto_detail/proto_detail_work.c b/src/modules/proto_detail/proto_detail_work.c index f4dd78c8170..36bd3f9531d 100644 --- a/src/modules/proto_detail/proto_detail_work.c +++ b/src/modules/proto_detail/proto_detail_work.c @@ -760,10 +760,12 @@ static int mod_close_internal(proto_detail_work_thread_t *thread) * hacks in proto_detail which let us start up with * "transport = work" for debugging purposes. */ - pthread_mutex_lock(&inst->parent->worker_mutex); - inst->parent->work_io_instance = NULL; - if (inst->parent->num_workers > 0) inst->parent->num_workers--; - pthread_mutex_unlock(&inst->parent->worker_mutex); + if (thread->file_parent) { + pthread_mutex_lock(&thread->file_parent->worker_mutex); + inst->parent->work_io_instance = NULL; + if (thread->file_parent->num_workers > 0) thread->file_parent->num_workers--; + pthread_mutex_unlock(&thread->file_parent->worker_mutex); + } DEBUG("Closing and deleting detail worker file %s", thread->name);