threads.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_THREADS_H
  3. #define NETDATA_THREADS_H 1
  4. #include "../libnetdata.h"
  5. pid_t gettid(void);
  6. typedef enum {
  7. NETDATA_THREAD_OPTION_DEFAULT = 0 << 0,
  8. NETDATA_THREAD_OPTION_JOINABLE = 1 << 0,
  9. NETDATA_THREAD_OPTION_DONT_LOG_STARTUP = 1 << 1,
  10. NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP = 1 << 2,
  11. NETDATA_THREAD_OPTION_DONT_LOG = NETDATA_THREAD_OPTION_DONT_LOG_STARTUP|NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP,
  12. } NETDATA_THREAD_OPTIONS;
  13. #define netdata_thread_cleanup_push(func, arg) pthread_cleanup_push(func, arg)
  14. #define netdata_thread_cleanup_pop(execute) pthread_cleanup_pop(execute)
  15. void netdata_thread_set_tag(const char *tag);
  16. typedef pthread_t netdata_thread_t;
  17. struct netdata_static_thread {
  18. // the name of the thread as it should appear in the logs
  19. char *name;
  20. // the section of netdata.conf to check if this is enabled or not
  21. char *config_section;
  22. // the name of the config option to check if it is true or false
  23. char *config_name;
  24. // the current status of the thread
  25. volatile sig_atomic_t enabled;
  26. // internal use, to maintain a pointer to the created thread
  27. netdata_thread_t *thread;
  28. // an initialization function to run before spawning the thread
  29. void (*init_routine) (void);
  30. // the threaded worker
  31. void *(*start_routine) (void *);
  32. // the environment variable to create
  33. char *env_name;
  34. // global variable
  35. bool *global_variable;
  36. };
  37. #define NETDATA_MAIN_THREAD_RUNNING CONFIG_BOOLEAN_YES
  38. #define NETDATA_MAIN_THREAD_EXITING (CONFIG_BOOLEAN_YES + 1)
  39. #define NETDATA_MAIN_THREAD_EXITED CONFIG_BOOLEAN_NO
  40. #define NETDATA_THREAD_TAG_MAX 100
  41. const char *netdata_thread_tag(void);
  42. int netdata_thread_tag_exists(void);
  43. #define THREAD_TAG_STREAM_RECEIVER "RCVR"
  44. #define THREAD_TAG_STREAM_SENDER "SNDR"
  45. size_t netdata_threads_init(void);
  46. void netdata_threads_init_after_fork(size_t stacksize);
  47. void netdata_threads_init_for_external_plugins(size_t stacksize);
  48. int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg);
  49. #ifdef NETDATA_INTERNAL_CHECKS
  50. #define netdata_thread_cancel(thread) netdata_thread_cancel_with_trace(thread, __LINE__, __FILE__, __FUNCTION__)
  51. int netdata_thread_cancel_with_trace(netdata_thread_t thread, int line, const char *file, const char *function);
  52. #else
  53. int netdata_thread_cancel(netdata_thread_t thread);
  54. #endif
  55. int netdata_thread_join(netdata_thread_t thread, void **retval);
  56. int netdata_thread_detach(pthread_t thread);
  57. #define NETDATA_THREAD_NAME_MAX 15
  58. void uv_thread_set_name_np(uv_thread_t ut, const char* name);
  59. void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]);
  60. void webrtc_set_thread_name(void);
  61. #define netdata_thread_self pthread_self
  62. #define netdata_thread_testcancel pthread_testcancel
  63. #endif //NETDATA_THREADS_H