]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
move mutex into thread instance of the file reader
authorAlan T. DeKok <aland@freeradius.org>
Tue, 30 Oct 2018 15:23:31 +0000 (11:23 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 30 Oct 2018 15:46:22 +0000 (11:46 -0400)
src/modules/proto_detail/proto_detail.c
src/modules/proto_detail/proto_detail.h
src/modules/proto_detail/proto_detail_file.c
src/modules/proto_detail/proto_detail_work.c

index 6900598d7f0eaf107b622d3c8e1a6454fd036534..8c1ec29b039c2bddab3e51c9b4803bc56c1c377a 100644 (file)
@@ -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,
index e32c52173f8c2048c752ed8b02ad96bef8b0b49c..e42084583e11d589057128c8731185fef89bf163 100644 (file)
@@ -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 {
index 4ca2ff96fa55836dc780aa70cd97aa4083f106d6..5fde3462ebd7afe5a0a76279b5ce4fc87aa83c23 100644 (file)
@@ -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;
index f4dd78c8170b26e01b809aced04b4f1786cb449f..36bd3f9531d42916e50fd9d5e28f9f43a1564828 100644 (file)
@@ -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);