| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_THREADS_H |
| 4 | #define NETDATA_THREADS_H 1 |
| 5 | |
| 6 | #include "../libnetdata-base.h" |
| 7 | #include <pthread.h> |
| 8 | #include <signal.h> |
| 9 | |
| 10 | typedef enum __attribute__((packed)) { |
| 11 | NETDATA_THREAD_OPTION_DEFAULT = 0 << 0, |
| 12 | NETDATA_THREAD_OPTION_DONT_LOG_STARTUP = 1 << 0, |
| 13 | NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP = 1 << 1, |
| 14 | NETDATA_THREAD_STATUS_STARTED = 1 << 2, |
| 15 | NETDATA_THREAD_STATUS_FINISHED = 1 << 3, |
| 16 | NETDATA_THREAD_STATUS_JOINED = 1 << 4, |
| 17 | } NETDATA_THREAD_OPTIONS; |
| 18 | |
| 19 | #define NETDATA_THREAD_OPTIONS_ALL (NETDATA_THREAD_OPTION_DONT_LOG_STARTUP | NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP) |
| 20 | #define NETDATA_THREAD_OPTION_DONT_LOG (NETDATA_THREAD_OPTION_DONT_LOG_STARTUP | NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP) |
| 21 | |
| 22 | #define netdata_thread_cleanup_push(func, arg) pthread_cleanup_push(func, arg) |
| 23 | #define netdata_thread_cleanup_pop(execute) pthread_cleanup_pop(execute) |
| 24 | |
| 25 | void nd_thread_tag_set(const char *tag); |
| 26 | |
| 27 | typedef struct nd_thread ND_THREAD; |
| 28 | |
| 29 | struct netdata_static_thread { |
| 30 | // the name of the thread as it should appear in the logs |
| 31 | char *name; |
| 32 | |
| 33 | // the section of netdata.conf to check if this is enabled or not |
| 34 | char *config_section; |
| 35 | |
| 36 | // the name of the config option to check if it is true or false |
| 37 | char *config_name; |
| 38 | |
| 39 | // the current status of the thread |
| 40 | volatile sig_atomic_t enabled; |
| 41 | |
| 42 | // internal use, to maintain a pointer to the created thread |
| 43 | ND_THREAD *thread; |
| 44 | |
| 45 | // a function to call to check it should be enabled or not |
| 46 | bool (*enable_routine) (void); |
| 47 | |
| 48 | // an initialization function to run before spawning the thread |
| 49 | void (*init_routine) (void); |
| 50 | |
| 51 | // the threaded worker |
| 52 | void (*start_routine) (void *); |
| 53 | |
| 54 | // the environment variable to create |
| 55 | char *env_name; |
| 56 | |
| 57 | // global variable |
| 58 | bool *global_variable; |
| 59 | }; |
| 60 | |
| 61 | #define NETDATA_MAIN_THREAD_RUNNING 1 |
| 62 | #define NETDATA_MAIN_THREAD_EXITING 2 |
| 63 | #define NETDATA_MAIN_THREAD_EXITED 0 |
| 64 | |
| 65 | #define NETDATA_THREAD_TAG_MAX 100 |
| 66 | const char *nd_thread_tag(void); |
| 67 | const char *nd_thread_tag_async_safe(void); |
| 68 | int nd_thread_has_tag(void); |
| 69 | |
| 70 | #define THREAD_TAG_STREAM_RECEIVER "RCVR" |
| 71 | #define THREAD_TAG_STREAM_SENDER "SNDR" |
| 72 | |
| 73 | size_t netdata_threads_init(void); |
| 74 | void netdata_threads_set_stack_size(size_t stacksize); |
| 75 | void netdata_threads_init_for_external_plugins(size_t stacksize); |
| 76 | |
| 77 | ND_THREAD *nd_thread_create(const char *tag, NETDATA_THREAD_OPTIONS options, void (*start_routine) (void *), void *arg); |
| 78 | int nd_thread_join(ND_THREAD * nti); |
| 79 | ND_THREAD *nd_thread_self(void); |
| 80 | bool nd_thread_is_me(ND_THREAD *nti); |
| 81 | |
| 82 | typedef void (*nd_thread_canceller)(void *data); |
| 83 | void nd_thread_register_canceller(nd_thread_canceller cb, void *data); |
| 84 | void nd_thread_signal_cancel(ND_THREAD *nti); |
| 85 | bool nd_thread_signaled_to_cancel(void); |
| 86 | |
| 87 | // Register a process-wide cleanup callback that runs once per thread |
| 88 | // at thread-exit time. Callbacks run in registration order, after the |
| 89 | // libnetdata-internal lock-leak assertions and before |
| 90 | // thread_cache_destroy / worker_unregister. Registration is intended |
| 91 | // to be done once at process startup; the registry has a small fixed |
| 92 | // capacity. |
| 93 | typedef void (*nd_thread_cleanup_fn)(void); |
| 94 | void nd_thread_register_cleanup(nd_thread_cleanup_fn fn); |
| 95 | |
| 96 | #define ND_THREAD_TAG_MAX 15 |
| 97 | void uv_thread_set_name_np(const char* name); |
| 98 | void webrtc_set_thread_name(void); |
| 99 | |
| 100 | #ifdef NETDATA_INTERNAL_CHECKS |
| 101 | void nd_thread_rwlock_read_locked(void); |
| 102 | void nd_thread_rwlock_read_unlocked(void); |
| 103 | void nd_thread_rwlock_write_locked(void); |
| 104 | void nd_thread_rwlock_write_unlocked(void); |
| 105 | void nd_thread_mutex_locked(void); |
| 106 | void nd_thread_mutex_unlocked(void); |
| 107 | void nd_thread_spinlock_locked(void); |
| 108 | void nd_thread_spinlock_unlocked(void); |
| 109 | void nd_thread_rwspinlock_read_locked(void); |
| 110 | void nd_thread_rwspinlock_read_unlocked(void); |
| 111 | void nd_thread_rwspinlock_write_locked(void); |
| 112 | void nd_thread_rwspinlock_write_unlocked(void); |
| 113 | #else |
| 114 | #define nd_thread_rwlock_read_locked() ((void)0) |
| 115 | #define nd_thread_rwlock_read_unlocked() ((void)0) |
| 116 | #define nd_thread_rwlock_write_locked() ((void)0) |
| 117 | #define nd_thread_rwlock_write_unlocked() ((void)0) |
| 118 | #define nd_thread_mutex_locked() ((void)0) |
| 119 | #define nd_thread_mutex_unlocked() ((void)0) |
| 120 | #define nd_thread_spinlock_locked() ((void)0) |
| 121 | #define nd_thread_spinlock_unlocked() ((void)0) |
| 122 | #define nd_thread_rwspinlock_read_locked() ((void)0) |
| 123 | #define nd_thread_rwspinlock_read_unlocked() ((void)0) |
| 124 | #define nd_thread_rwspinlock_write_locked() ((void)0) |
| 125 | #define nd_thread_rwspinlock_write_unlocked() ((void)0) |
| 126 | #endif |
| 127 | |
| 128 | void nd_thread_can_run_sql(bool exclude); |
| 129 | void nd_thread_join_threads(); |
| 130 | bool nd_thread_runs_sql(void); |
| 131 | |
| 132 | #endif //NETDATA_THREADS_H |