From: Jaroslav Kysela Date: Tue, 27 Nov 2018 18:19:31 +0000 (+0100) Subject: tvh thread: compile the debug thread code only when traces are enabled X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=752fd7cb2b318a86aa0041d736718ed9d6faf67b;p=thirdparty%2Ftvheadend.git tvh thread: compile the debug thread code only when traces are enabled --- diff --git a/src/main.c b/src/main.c index 12bcb75b4..743404728 100644 --- a/src/main.c +++ b/src/main.c @@ -905,7 +905,9 @@ main(int argc, char **argv) #endif { 0, "tprofile", N_("Gather timing statistics for the code"), OPT_BOOL, &opt_tprofile }, +#if ENABLE_TRACE { 0, "thrdebug", N_("Thread debugging"), OPT_INT, &opt_thread_debug }, +#endif }; diff --git a/src/tvh_thread.c b/src/tvh_thread.c index 437e30ef7..139b61090 100644 --- a/src/tvh_thread.c +++ b/src/tvh_thread.c @@ -19,12 +19,14 @@ #include #endif +#if ENABLE_TRACE int tvh_thread_debug; static int tvhwatch_done; static pthread_t thrwatch_tid; static pthread_mutex_t thrwatch_mutex = PTHREAD_MUTEX_INITIALIZER; static TAILQ_HEAD(, tvh_mutex) thrwatch_mutexes = TAILQ_HEAD_INITIALIZER(thrwatch_mutexes); static int64_t tvh_thread_crash_time; +#endif /* * thread routines @@ -41,6 +43,7 @@ thread_state { char name[17]; }; +#if ENABLE_TRACE static void thread_get_name(pthread_t tid, char *buf, int len) { @@ -57,6 +60,7 @@ thread_get_name(pthread_t tid, char *buf, int len) // ??? #endif } +#endif static void * thread_wrapper(void *p) @@ -151,6 +155,7 @@ int tvh_mutex_destroy(tvh_mutex_t *mutex) return pthread_mutex_destroy(&mutex->mutex); } +#if ENABLE_TRACE static void tvh_mutex_add_to_list(tvh_mutex_t *mutex, const char *filename, int lineno) { pthread_mutex_lock(&thrwatch_mutex); @@ -164,7 +169,9 @@ static void tvh_mutex_add_to_list(tvh_mutex_t *mutex, const char *filename, int TAILQ_INSERT_HEAD(&thrwatch_mutexes, mutex, link); pthread_mutex_unlock(&thrwatch_mutex); } +#endif +#if ENABLE_TRACE static void tvh_mutex_check_interval(tvh_mutex_t *mutex) { if (tvh_thread_debug > 10000) { @@ -174,7 +181,9 @@ static void tvh_mutex_check_interval(tvh_mutex_t *mutex) printf("mutex %p at %s:%d took %lldms\n", mutex, mutex->filename, mutex->lineno, diff / (MONOCLOCK_RESOLUTION / 1000)); } } +#endif +#if ENABLE_TRACE static void tvh_mutex_remove_from_list(tvh_mutex_t *mutex) { pthread_mutex_lock(&thrwatch_mutex); @@ -184,7 +193,9 @@ static void tvh_mutex_remove_from_list(tvh_mutex_t *mutex) mutex->lineno = 0; pthread_mutex_unlock(&thrwatch_mutex); } +#endif +#if ENABLE_TRACE static void tvh_mutex_remove_from_list_keep_info(tvh_mutex_t *mutex) { pthread_mutex_lock(&thrwatch_mutex); @@ -192,7 +203,9 @@ static void tvh_mutex_remove_from_list_keep_info(tvh_mutex_t *mutex) tvh_mutex_check_interval(mutex); pthread_mutex_unlock(&thrwatch_mutex); } +#endif +#if ENABLE_TRACE int tvh__mutex_lock(tvh_mutex_t *mutex, const char *filename, int lineno) { int r = pthread_mutex_lock(&mutex->mutex); @@ -200,7 +213,9 @@ int tvh__mutex_lock(tvh_mutex_t *mutex, const char *filename, int lineno) tvh_mutex_add_to_list(mutex, filename, lineno); return r; } +#endif +#if ENABLE_TRACE int tvh__mutex_trylock(tvh_mutex_t *mutex, const char *filename, int lineno) { int r = pthread_mutex_trylock(&mutex->mutex); @@ -208,7 +223,9 @@ int tvh__mutex_trylock(tvh_mutex_t *mutex, const char *filename, int lineno) tvh_mutex_add_to_list(mutex, filename, lineno); return r; } +#endif +#if ENABLE_TRACE int tvh__mutex_unlock(tvh_mutex_t *mutex) { int r; @@ -217,6 +234,7 @@ int tvh__mutex_unlock(tvh_mutex_t *mutex) tvh_mutex_remove_from_list(mutex); return r; } +#endif int tvh_mutex_timedlock @@ -289,9 +307,13 @@ tvh_cond_wait { int r; +#if ENABLE_TRACE tvh_mutex_remove_from_list_keep_info(mutex); +#endif r = pthread_cond_wait(&cond->cond, &mutex->mutex); +#if ENABLE_TRACE tvh_mutex_add_to_list(mutex, NULL, -1); +#endif return r; } @@ -301,7 +323,9 @@ tvh_cond_timedwait { int r; +#if ENABLE_TRACE tvh_mutex_remove_from_list_keep_info(mutex); +#endif #if defined(PLATFORM_DARWIN) /* Use a relative timedwait implementation */ @@ -325,7 +349,9 @@ tvh_cond_timedwait r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &ts); #endif +#if ENABLE_TRACE tvh_mutex_add_to_list(mutex, NULL, -1); +#endif return r; } @@ -333,9 +359,13 @@ int tvh_cond_timedwait_ts(tvh_cond_t *cond, tvh_mutex_t *mutex, struct timespec { int r; +#if ENABLE_TRACE tvh_mutex_remove_from_list_keep_info(mutex); +#endif r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, ts); +#if ENABLE_TRACE tvh_mutex_add_to_list(mutex, NULL, -1); +#endif return r; } @@ -346,6 +376,7 @@ tvh_mutex_not_held(const char *file, int line) abort(); } +#if ENABLE_TRACE static void tvh_thread_mutex_deadlock(tvh_mutex_t *mutex) { int fd = hts_settings_open_file(HTS_SETTINGS_OPEN_WRITE | HTS_SETTINGS_OPEN_DIRECT, "mutex-deadlock.txt"); @@ -359,7 +390,9 @@ static void tvh_thread_mutex_deadlock(tvh_mutex_t *mutex) fclose(f); abort(); } +#endif +#if ENABLE_TRACE static void *tvh_thread_watch_thread(void *aux) { int64_t now; @@ -383,21 +416,26 @@ static void *tvh_thread_watch_thread(void *aux) } return NULL; } +#endif void tvh_thread_init(int debug_level) { +#if ENABLE_TRACE tvh_thread_debug = debug_level; tvh_thread_crash_time = getfastmonoclock() + sec2mono(15); if (debug_level > 0) { tvhwatch_done = 0; tvh_thread_create(&thrwatch_tid, NULL, tvh_thread_watch_thread, NULL, "thrwatch"); } +#endif } void tvh_thread_done(void) { +#if ENABLE_TRACE if (tvh_thread_debug > 0) { tvhwatch_done = 1; pthread_join(thrwatch_tid, NULL); } +#endif } diff --git a/src/tvh_thread.h b/src/tvh_thread.h index e09f91047..1e1ad8949 100644 --- a/src/tvh_thread.h +++ b/src/tvh_thread.h @@ -23,6 +23,7 @@ #include #include "queue.h" +#include "build.h" typedef struct { pthread_cond_t cond; @@ -32,11 +33,13 @@ TAILQ_HEAD(tvh_mutex_queue, tvh_mutex); typedef struct tvh_mutex { pthread_mutex_t mutex; +#if ENABLE_TRACE pthread_t thread; const char *filename; int lineno; int64_t tstamp; TAILQ_ENTRY(tvh_mutex) link; +#endif } tvh_mutex_t; /* @@ -63,7 +66,9 @@ lock_assert0(tvh_mutex_t *l, const char *file, int line) * */ +#if ENABLE_TRACE extern int tvh_thread_debug; +#endif void tvh_thread_init(int debug_level); void tvh_thread_done(void); @@ -78,6 +83,7 @@ int tvh_thread_renice(int value); int tvh_mutex_init(tvh_mutex_t *mutex, const pthread_mutexattr_t *attr); int tvh_mutex_destroy(tvh_mutex_t *mutex); +#if ENABLE_TRACE int tvh__mutex_lock(tvh_mutex_t *mutex, const char *filename, int lineno); #define tvh_mutex_lock(_mutex) \ ({ \ @@ -99,6 +105,20 @@ static inline int tvh_mutex_unlock(tvh_mutex_t *mutex) return pthread_mutex_unlock(&mutex->mutex); return tvh__mutex_unlock(mutex); } +#else +static inline int tvh_mutex_lock(tvh_mutex_t *mutex) +{ + return pthread_mutex_lock(&mutex->mutex); +} +static inline int tvh_mutex_trylock(tvh_mutex_t *mutex) +{ + return pthread_mutex_trylock(&mutex->mutex); +} +static inline int tvh_mutex_unlock(tvh_mutex_t *mutex) +{ + return pthread_mutex_unlock(&mutex->mutex); +} +#endif int tvh_mutex_timedlock(tvh_mutex_t *mutex, int64_t usec); int tvh_cond_init(tvh_cond_t *cond, int monotonic);