block signals in threads

This commit is contained in:
Maxim Devaev
2024-03-04 03:38:45 +02:00
parent c8dc5119fe
commit 98b5e52a68
8 changed files with 31 additions and 14 deletions

View File

@@ -24,6 +24,7 @@
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h>
#include <sys/syscall.h>
@@ -56,9 +57,14 @@
us_thread_set_name(m_new_tname_buf); \
}
#else
# define US_THREAD_RENAME(_fmt, ...)
# define US_THREAD_RENAME(x_fmt, ...)
#endif
#define US_THREAD_SETTLE(x_fmt, ...) { \
US_THREAD_RENAME((x_fmt), ##__VA_ARGS__); \
us_thread_block_signals(); \
}
#define US_MUTEX_INIT(x_mutex) assert(!pthread_mutex_init(&(x_mutex), NULL))
#define US_MUTEX_DESTROY(x_mutex) assert(!pthread_mutex_destroy(&(x_mutex)))
#define US_MUTEX_LOCK(x_mutex) assert(!pthread_mutex_lock(&(x_mutex)))
@@ -124,3 +130,11 @@ INLINE void us_thread_get_name(char *name) { // Always required for logging
}
#endif
}
INLINE void us_thread_block_signals(void) {
sigset_t mask;
assert(!sigemptyset(&mask));
assert(!sigaddset(&mask, SIGINT));
assert(!sigaddset(&mask, SIGTERM));
assert(!pthread_sigmask(SIG_BLOCK, &mask, NULL));
}

View File

@@ -54,7 +54,7 @@ static void _block_thread_signals(void) {
static void *_stream_loop_thread(void *arg) {
(void)arg;
US_THREAD_RENAME("stream");
US_THREAD_SETTLE("stream");
_block_thread_signals();
us_stream_loop(_g_stream);
return NULL;
@@ -62,7 +62,7 @@ static void *_stream_loop_thread(void *arg) {
static void *_server_loop_thread(void *arg) {
(void)arg;
US_THREAD_RENAME("http");
US_THREAD_SETTLE("http");
_block_thread_signals();
us_server_loop(_g_server);
return NULL;

View File

@@ -279,6 +279,7 @@ void _stream_set_capture_state(us_stream_s *stream, uint width, uint height, boo
}
static void *_releaser_thread(void *v_ctx) {
US_THREAD_SETTLE("str_rel")
_releaser_context_s *ctx = v_ctx;
while (!atomic_load(ctx->stop)) {
@@ -308,6 +309,7 @@ done:
}
static void *_jpeg_thread(void *v_ctx) {
US_THREAD_SETTLE("str_jpeg")
_jpeg_context_s *ctx = v_ctx;
us_stream_s *stream = ctx->stream;
@@ -372,6 +374,7 @@ static void *_jpeg_thread(void *v_ctx) {
}
static void *_h264_thread(void *v_ctx) {
US_THREAD_SETTLE("str_h264");
_h264_context_s *ctx = v_ctx;
ldf last_encode_ts = us_get_now_monotonic();

View File

@@ -178,7 +178,7 @@ long double us_workers_pool_get_fluency_delay(us_workers_pool_s *pool, const us_
static void *_worker_thread(void *v_worker) {
us_worker_s *wr = (us_worker_s *)v_worker;
US_THREAD_RENAME("%s", wr->name);
US_THREAD_SETTLE("%s", wr->name);
US_LOG_DEBUG("Hello! I am a worker %s ^_^", wr->name);
while (!atomic_load(&wr->pool->stop)) {

View File

@@ -227,11 +227,10 @@ static void _main_loop(void) {
}
static void *_follower_thread(void *v_unix_follow) {
US_THREAD_SETTLE("follower");
const char *path = v_unix_follow;
assert(path != NULL);
US_THREAD_RENAME("follower");
while (!atomic_load(&_g_stop)) {
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
assert(fd >= 0);