]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
tvh thread: compile the debug thread code only when traces are enabled
authorJaroslav Kysela <perex@perex.cz>
Tue, 27 Nov 2018 18:19:31 +0000 (19:19 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 27 Nov 2018 18:19:31 +0000 (19:19 +0100)
src/main.c
src/tvh_thread.c
src/tvh_thread.h

index 12bcb75b41bb0e90013247386474de636e42d30a..7434047287c10c419b50c6c22204def4cfed9ea7 100644 (file)
@@ -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
 
   };
 
index 437e30ef75257b7eabee3eb5bdfd0cb64d471d7a..139b61090b5dee495b68ff076dd6ce9f6515be32 100644 (file)
 #include <pthread_np.h>
 #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
 }
index e09f9104752dbb5854b7e8d648f1a0876c560960..1e1ad89496a6c74c3ad8b6996fa462e20dbbda31 100644 (file)
@@ -23,6 +23,7 @@
 #include <pthread.h>
 
 #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);