threads.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. typedef pthread_t netdata_thread_t;
  16. struct netdata_static_thread {
  17. // the name of the thread as it should appear in the logs
  18. char *name;
  19. // the section of netdata.conf to check if this is enabled or not
  20. char *config_section;
  21. // the name of the config option to check if it is true or false
  22. char *config_name;
  23. // the current status of the thread
  24. volatile sig_atomic_t enabled;
  25. // internal use, to maintain a pointer to the created thread
  26. netdata_thread_t *thread;
  27. // an initialization function to run before spawning the thread
  28. void (*init_routine) (void);
  29. // the threaded worker
  30. void *(*start_routine) (void *);
  31. // the environment variable to create
  32. char *env_name;
  33. // global variable
  34. bool *global_variable;
  35. };
  36. #define NETDATA_MAIN_THREAD_RUNNING CONFIG_BOOLEAN_YES
  37. #define NETDATA_MAIN_THREAD_EXITING (CONFIG_BOOLEAN_YES + 1)
  38. #define NETDATA_MAIN_THREAD_EXITED CONFIG_BOOLEAN_NO
  39. #define NETDATA_THREAD_TAG_MAX 100
  40. const char *netdata_thread_tag(void);
  41. int netdata_thread_tag_exists(void);
  42. size_t netdata_threads_init(void);
  43. void netdata_threads_init_after_fork(size_t stacksize);
  44. void netdata_threads_init_for_external_plugins(size_t stacksize);
  45. int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg);
  46. #ifdef NETDATA_INTERNAL_CHECKS
  47. #define netdata_thread_cancel(thread) netdata_thread_cancel_with_trace(thread, __LINE__, __FILE__, __FUNCTION__)
  48. int netdata_thread_cancel_with_trace(netdata_thread_t thread, int line, const char *file, const char *function);
  49. #else
  50. int netdata_thread_cancel(netdata_thread_t thread);
  51. #endif
  52. int netdata_thread_join(netdata_thread_t thread, void **retval);
  53. int netdata_thread_detach(pthread_t thread);
  54. #define NETDATA_THREAD_NAME_MAX 15
  55. void uv_thread_set_name_np(uv_thread_t ut, const char* name);
  56. void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]);
  57. void webrtc_set_thread_name(void);
  58. #define netdata_thread_self pthread_self
  59. #define netdata_thread_testcancel pthread_testcancel
  60. #endif //NETDATA_THREADS_H