]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: return thread pointer from thread_add*
authorQuentin Young <qlyoung@cumulusnetworks.com>
Tue, 16 May 2017 15:46:41 +0000 (15:46 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Tue, 16 May 2017 15:59:51 +0000 (15:59 +0000)
Allow some more flexibility in case callers wish to manage their own
thread pointers and don't require or don't want the thread to keep a
back reference to its holding pointer.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/thread.c
lib/thread.h

index aef9ac5cf985f290837225652b90977444f5cf5e..efc0dfb2a476ff1772dd9068412edf856973cdb4 100644 (file)
@@ -777,7 +777,7 @@ fd_clear_read_write (struct thread *thread)
 }
 
 /* Add new read thread. */
-void
+struct thread *
 funcname_thread_add_read_write (int dir, struct thread_master *m,
         int (*func) (struct thread *), void *arg, int fd, struct thread **t_ptr,
         debugargdef)
@@ -789,7 +789,7 @@ funcname_thread_add_read_write (int dir, struct thread_master *m,
     if (t_ptr && *t_ptr) // thread is already scheduled; don't reschedule
       {
         pthread_mutex_unlock (&m->mtx);
-        return;
+        return NULL;
       }
 
 #if defined (HAVE_POLL_CALL)
@@ -839,9 +839,11 @@ funcname_thread_add_read_write (int dir, struct thread_master *m,
       }
   }
   pthread_mutex_unlock (&m->mtx);
+
+  return thread;
 }
 
-static void
+static struct thread *
 funcname_thread_add_timer_timeval (struct thread_master *m,
          int (*func) (struct thread *), int type, void *arg,
          struct timeval *time_relative, struct thread **t_ptr, debugargdef)
@@ -859,7 +861,7 @@ funcname_thread_add_timer_timeval (struct thread_master *m,
     if (t_ptr && *t_ptr) // thread is already scheduled; don't reschedule
       {
         pthread_mutex_unlock (&m->mtx);
-        return;
+        return NULL;
       }
 
     queue = ((type == THREAD_TIMER) ? m->timer : m->background);
@@ -879,11 +881,13 @@ funcname_thread_add_timer_timeval (struct thread_master *m,
     pthread_mutex_unlock (&thread->mtx);
   }
   pthread_mutex_unlock (&m->mtx);
+
+  return thread;
 }
 
 
 /* Add timer event thread. */
-void
+struct thread *
 funcname_thread_add_timer (struct thread_master *m,
         int (*func) (struct thread *), void *arg, long timer,
         struct thread **t_ptr, debugargdef)
@@ -900,7 +904,7 @@ funcname_thread_add_timer (struct thread_master *m,
 }
 
 /* Add timer event thread with "millisecond" resolution */
-void
+struct thread *
 funcname_thread_add_timer_msec (struct thread_master *m,
         int (*func) (struct thread *), void *arg, long timer,
         struct thread **t_ptr, debugargdef)
@@ -912,22 +916,22 @@ funcname_thread_add_timer_msec (struct thread_master *m,
   trel.tv_sec = timer / 1000;
   trel.tv_usec = 1000*(timer % 1000);
 
-  funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg, &trel,
-                                     t_ptr, debugargpass);
+  return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg, &trel,
+                                            t_ptr, debugargpass);
 }
 
 /* Add timer event thread with "millisecond" resolution */
-void
+struct thread *
 funcname_thread_add_timer_tv (struct thread_master *m,
         int (*func) (struct thread *), void *arg, struct timeval *tv,
         struct thread **t_ptr, debugargdef)
 {
-  funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg, tv, t_ptr,
-                                     debugargpass);
+  return funcname_thread_add_timer_timeval (m, func, THREAD_TIMER, arg, tv,
+                                            t_ptr, debugargpass);
 }
 
 /* Add a background thread, with an optional millisec delay */
-void
+struct thread *
 funcname_thread_add_background (struct thread_master *m,
         int (*func) (struct thread *), void *arg, long delay,
         struct thread **t_ptr, debugargdef)
@@ -947,12 +951,12 @@ funcname_thread_add_background (struct thread_master *m,
       trel.tv_usec = 0;
     }
 
-  funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND, arg, &trel,
-                                     t_ptr, debugargpass);
+  return funcname_thread_add_timer_timeval (m, func, THREAD_BACKGROUND, arg, &trel,
+                                            t_ptr, debugargpass);
 }
 
 /* Add simple event thread. */
-void
+struct thread *
 funcname_thread_add_event (struct thread_master *m,
         int (*func) (struct thread *), void *arg, int val,
         struct thread **t_ptr, debugargdef)
@@ -966,7 +970,7 @@ funcname_thread_add_event (struct thread_master *m,
     if (t_ptr && *t_ptr) // thread is already scheduled; don't reschedule
       {
         pthread_mutex_unlock (&m->mtx);
-        return;
+        return NULL;
       }
 
     thread = thread_get (m, THREAD_EVENT, func, arg, debugargpass);
@@ -984,6 +988,8 @@ funcname_thread_add_event (struct thread_master *m,
       }
   }
   pthread_mutex_unlock (&m->mtx);
+
+  return thread;
 }
 
 static void
index 3eaae8883b9d0ea57a202e99aa31e7cc721a77c1..fa2d2e03878e3576f8240469eca961c15f207b95 100644 (file)
@@ -185,22 +185,22 @@ extern struct thread_master *thread_master_create (void);
 extern void thread_master_free (struct thread_master *);
 extern void thread_master_free_unused(struct thread_master *);
 
-extern void funcname_thread_add_read_write (int dir, struct thread_master *,
+extern struct thread * funcname_thread_add_read_write (int dir, struct thread_master *,
     int (*)(struct thread *), void *, int, struct thread **, debugargdef);
 
-extern void funcname_thread_add_timer (struct thread_master *,
+extern struct thread * funcname_thread_add_timer (struct thread_master *,
     int (*)(struct thread *), void *, long, struct thread **, debugargdef);
 
-extern void funcname_thread_add_timer_msec (struct thread_master *,
+extern struct thread * funcname_thread_add_timer_msec (struct thread_master *,
     int (*)(struct thread *), void *, long, struct thread **, debugargdef);
 
-extern void funcname_thread_add_timer_tv (struct thread_master *,
+extern struct thread * funcname_thread_add_timer_tv (struct thread_master *,
     int (*)(struct thread *), void *, struct timeval *, struct thread **, debugargdef);
 
-extern void funcname_thread_add_event (struct thread_master *,
+extern struct thread * funcname_thread_add_event (struct thread_master *,
     int (*)(struct thread *), void *, int, struct thread **, debugargdef);
 
-extern void funcname_thread_add_background (struct thread_master *,
+extern struct thread * funcname_thread_add_background (struct thread_master *,
     int (*)(struct thread *), void *, long, struct thread **, debugargdef);
 
 extern void funcname_thread_execute (struct thread_master *,