From 5f647647b2683875bdbed970d955a9e5123284bd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 4 Sep 2023 10:57:49 +0200 Subject: Merging upstream version 1.42.2. Signed-off-by: Daniel Baumann --- libnetdata/threads/threads.c | 21 +++++++++++++++++---- libnetdata/threads/threads.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) (limited to 'libnetdata/threads') diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c index adce0463c..ae3c7106d 100644 --- a/libnetdata/threads/threads.c +++ b/libnetdata/threads/threads.c @@ -123,10 +123,12 @@ size_t netdata_threads_init(void) { // -------------------------------------------------------------------- // get the required stack size of the threads of netdata - netdata_threads_attr = callocz(1, sizeof(pthread_attr_t)); - i = pthread_attr_init(netdata_threads_attr); - if(i != 0) - fatal("pthread_attr_init() failed with code %d.", i); + if(!netdata_threads_attr) { + netdata_threads_attr = callocz(1, sizeof(pthread_attr_t)); + i = pthread_attr_init(netdata_threads_attr); + if (i != 0) + fatal("pthread_attr_init() failed with code %d.", i); + } size_t stacksize = 0; i = pthread_attr_getstacksize(netdata_threads_attr, &stacksize); @@ -158,6 +160,17 @@ void netdata_threads_init_after_fork(size_t stacksize) { netdata_log_error("Invalid pthread stacksize %zu", stacksize); } +// ---------------------------------------------------------------------------- +// threads init for external plugins + +void netdata_threads_init_for_external_plugins(size_t stacksize) { + size_t default_stacksize = netdata_threads_init(); + if(default_stacksize < 1 * 1024 * 1024) + default_stacksize = 1 * 1024 * 1024; + + netdata_threads_init_after_fork(stacksize ? stacksize : default_stacksize); +} + // ---------------------------------------------------------------------------- // netdata_thread_create diff --git a/libnetdata/threads/threads.h b/libnetdata/threads/threads.h index ad31b8816..acb4e6ba2 100644 --- a/libnetdata/threads/threads.h +++ b/libnetdata/threads/threads.h @@ -20,12 +20,46 @@ typedef enum { typedef pthread_t netdata_thread_t; +struct netdata_static_thread { + // the name of the thread as it should appear in the logs + char *name; + + // the section of netdata.conf to check if this is enabled or not + char *config_section; + + // the name of the config option to check if it is true or false + char *config_name; + + // the current status of the thread + volatile sig_atomic_t enabled; + + // internal use, to maintain a pointer to the created thread + netdata_thread_t *thread; + + // an initialization function to run before spawning the thread + void (*init_routine) (void); + + // the threaded worker + void *(*start_routine) (void *); + + // the environment variable to create + char *env_name; + + // global variable + bool *global_variable; +}; + +#define NETDATA_MAIN_THREAD_RUNNING CONFIG_BOOLEAN_YES +#define NETDATA_MAIN_THREAD_EXITING (CONFIG_BOOLEAN_YES + 1) +#define NETDATA_MAIN_THREAD_EXITED CONFIG_BOOLEAN_NO + #define NETDATA_THREAD_TAG_MAX 100 const char *netdata_thread_tag(void); int netdata_thread_tag_exists(void); size_t netdata_threads_init(void); void netdata_threads_init_after_fork(size_t stacksize); +void netdata_threads_init_for_external_plugins(size_t stacksize); int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg); -- cgit v1.2.3