]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: threads: implement ha_tkill() and ha_tkillall()
authorWilly Tarreau <w@1wt.eu>
Wed, 22 May 2019 06:43:34 +0000 (08:43 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 22 May 2019 09:50:48 +0000 (11:50 +0200)
These functions are used respectively to signal one thread or all threads.
When multithreading is disabled, it's always the current thread which is
signaled.

include/common/hathreads.h
src/hathreads.c

index 0bc6e4505e3fd599926b7b197f7f7d6139a8df30..77bd617aa836d0d75523795863df690826edbff0 100644 (file)
@@ -22,6 +22,7 @@
 #ifndef _COMMON_HATHREADS_H
 #define _COMMON_HATHREADS_H
 
+#include <signal.h>
 #include <unistd.h>
 #ifdef _POSIX_PRIORITY_SCHEDULING
 #include <sched.h>
@@ -157,6 +158,18 @@ static inline void ha_thread_relax(void)
 #endif
 }
 
+/* send signal <sig> to thread <thr> */
+static inline void ha_tkill(unsigned int thr, int sig)
+{
+       raise(sig);
+}
+
+/* send signal <sig> to all threads */
+static inline void ha_tkillall(int sig)
+{
+       raise(sig);
+}
+
 static inline void __ha_barrier_atomic_load(void)
 {
 }
@@ -383,6 +396,8 @@ static inline unsigned long thread_isolated()
 void thread_harmless_till_end();
 void thread_isolate();
 void thread_release();
+void ha_tkill(unsigned int thr, int sig);
+void ha_tkillall(int sig);
 
 extern struct thread_info {
        pthread_t pthread;
index b25417cc89b9072b42f17510e58b0ef059bfe09a..8359dd5ebb7c9f0fb05180ffe58cd4e1a34537ee 100644 (file)
@@ -97,6 +97,29 @@ void thread_release()
        thread_harmless_end();
 }
 
+/* send signal <sig> to thread <thr> */
+void ha_tkill(unsigned int thr, int sig)
+{
+       pthread_kill(thread_info[thr].pthread, sig);
+}
+
+/* send signal <sig> to all threads. The calling thread is signaled last in
+ * order to allow all threads to synchronize in the handler.
+ */
+void ha_tkillall(int sig)
+{
+       unsigned int thr;
+
+       for (thr = 0; thr < global.nbthread; thr++) {
+               if (!(all_threads_mask & (1UL << thr)))
+                       continue;
+               if (thr == tid)
+                       continue;
+               pthread_kill(thread_info[thr].pthread, sig);
+       }
+       raise(sig);
+}
+
 /* these calls are used as callbacks at init time */
 void ha_spin_init(HA_SPINLOCK_T *l)
 {