diff options
Diffstat (limited to 'libnetdata/threads')
-rw-r--r-- | libnetdata/threads/threads.c | 35 | ||||
-rw-r--r-- | libnetdata/threads/threads.h | 29 |
2 files changed, 48 insertions, 16 deletions
diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c index 12007afff..5c3d2675c 100644 --- a/libnetdata/threads/threads.c +++ b/libnetdata/threads/threads.c @@ -29,26 +29,35 @@ const char *netdata_thread_tag(void) { // ---------------------------------------------------------------------------- // compatibility library functions +static __thread pid_t gettid_cached_tid = 0; pid_t gettid(void) { + pid_t tid = 0; + + if(likely(gettid_cached_tid > 0)) + return gettid_cached_tid; + #ifdef __FreeBSD__ - return (pid_t)pthread_getthreadid_np(); + tid = (pid_t)pthread_getthreadid_np(); #elif defined(__APPLE__) #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) uint64_t curthreadid; pthread_threadid_np(NULL, &curthreadid); - return (pid_t)curthreadid; + tid = (pid_t)curthreadid; #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */ - return (pid_t)pthread_self; + tid = (pid_t)pthread_self; #endif /* __MAC_OS_X_VERSION_MIN_REQUIRED */ #else /* __APPLE__*/ - return (pid_t)syscall(SYS_gettid); + tid = (pid_t)syscall(SYS_gettid); #endif /* __FreeBSD__, __APPLE__*/ + + gettid_cached_tid = tid; + return tid; } // ---------------------------------------------------------------------------- @@ -97,6 +106,10 @@ void netdata_threads_init_after_fork(size_t stacksize) { // ---------------------------------------------------------------------------- // netdata_thread_create +extern void rrdset_thread_rda_free(void); +extern void sender_thread_buffer_free(void); +extern void query_target_free(void); + static void thread_cleanup(void *ptr) { if(netdata_thread != ptr) { NETDATA_THREAD *info = (NETDATA_THREAD *)ptr; @@ -106,6 +119,11 @@ static void thread_cleanup(void *ptr) { if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP)) info("thread with task id %d finished", gettid()); + sender_thread_buffer_free(); + rrdset_thread_rda_free(); + query_target_free(); + thread_cache_destroy(); + freez((void *)netdata_thread->tag); netdata_thread->tag = NULL; @@ -213,11 +231,18 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR // ---------------------------------------------------------------------------- // netdata_thread_cancel - +#ifdef NETDATA_INTERNAL_CHECKS +int netdata_thread_cancel_with_trace(netdata_thread_t thread, int line, const char *file, const char *function) { +#else int netdata_thread_cancel(netdata_thread_t thread) { +#endif int ret = pthread_cancel(thread); if(ret != 0) +#ifdef NETDATA_INTERNAL_CHECKS + error("cannot cancel thread. pthread_cancel() failed with code %d at %d@%s, function %s()", ret, line, file, function); +#else error("cannot cancel thread. pthread_cancel() failed with code %d.", ret); +#endif return ret; } diff --git a/libnetdata/threads/threads.h b/libnetdata/threads/threads.h index e7d79d328..ccc18aff0 100644 --- a/libnetdata/threads/threads.h +++ b/libnetdata/threads/threads.h @@ -5,7 +5,7 @@ #include "../libnetdata.h" -extern pid_t gettid(void); +pid_t gettid(void); typedef enum { NETDATA_THREAD_OPTION_DEFAULT = 0 << 0, @@ -21,20 +21,27 @@ typedef enum { typedef pthread_t netdata_thread_t; #define NETDATA_THREAD_TAG_MAX 100 -extern const char *netdata_thread_tag(void); -extern int netdata_thread_tag_exists(void); +const char *netdata_thread_tag(void); +int netdata_thread_tag_exists(void); -extern size_t netdata_threads_init(void); -extern void netdata_threads_init_after_fork(size_t stacksize); +size_t netdata_threads_init(void); +void netdata_threads_init_after_fork(size_t stacksize); -extern int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg); -extern int netdata_thread_cancel(netdata_thread_t thread); -extern int netdata_thread_join(netdata_thread_t thread, void **retval); -extern int netdata_thread_detach(pthread_t thread); +int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg); + +#ifdef NETDATA_INTERNAL_CHECKS +#define netdata_thread_cancel(thread) netdata_thread_cancel_with_trace(thread, __LINE__, __FILE__, __FUNCTION__) +int netdata_thread_cancel_with_trace(netdata_thread_t thread, int line, const char *file, const char *function); +#else +int netdata_thread_cancel(netdata_thread_t thread); +#endif + +int netdata_thread_join(netdata_thread_t thread, void **retval); +int netdata_thread_detach(pthread_t thread); #define NETDATA_THREAD_NAME_MAX 15 -extern void uv_thread_set_name_np(uv_thread_t ut, const char* name); -extern void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]); +void uv_thread_set_name_np(uv_thread_t ut, const char* name); +void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]); #define netdata_thread_self pthread_self #define netdata_thread_testcancel pthread_testcancel |