From 2b909fe58059a29a2989901ee9aa87ab54598f9f Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Thu, 5 Sep 2013 22:19:25 +0100 Subject: [PATCH] debug: add wrapper for pthread_create to add a meaningful name This is very useful for debugging. Though tbh I need to do this code properly. --- src/avahi.c | 2 +- src/descrambler/capmt.c | 2 +- src/descrambler/cwc.c | 4 ++-- src/dvr/dvr_inotify.c | 2 +- src/dvr/dvr_rec.c | 2 +- src/epggrab.c | 2 +- src/epggrab/module.c | 2 +- src/htsp_server.c | 2 +- src/idnode.c | 2 +- src/imagecache.c | 2 +- src/input/mpegps/v4l/v4l.c | 2 +- src/input/mpegts/iptv/iptv.c | 4 ++-- src/input/mpegts/linuxdvb/linuxdvb_frontend.c | 4 ++-- src/input/mpegts/tsfile/tsfile_input.c | 4 ++-- src/service.c | 2 +- src/service_mapper.c | 2 +- src/tcp.c | 4 ++-- src/timeshift.c | 4 ++-- src/timeshift/timeshift_filemgr.c | 2 +- src/tvheadend.h | 6 ++++++ src/tvhlog.c | 2 +- src/wrappers.c | 17 ++++++++++++++++- 22 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/avahi.c b/src/avahi.c index 76ec45c8..65fef8a0 100644 --- a/src/avahi.c +++ b/src/avahi.c @@ -269,5 +269,5 @@ avahi_init(void) { pthread_t tid; - pthread_create(&tid, NULL, avahi_thread, NULL); + tvhthread_create(&tid, NULL, avahi_thread, NULL); } diff --git a/src/descrambler/capmt.c b/src/descrambler/capmt.c index adb797e8..0b4b5656 100644 --- a/src/descrambler/capmt.c +++ b/src/descrambler/capmt.c @@ -997,7 +997,7 @@ capmt_entry_find(const char *id, int create) pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_create(&ptid, &attr, capmt_thread, capmt); + tvhthread_create(&ptid, &attr, capmt_thread, capmt); pthread_attr_destroy(&attr); return capmt; diff --git a/src/descrambler/cwc.c b/src/descrambler/cwc.c index 3d7023c5..b81e0626 100644 --- a/src/descrambler/cwc.c +++ b/src/descrambler/cwc.c @@ -1112,7 +1112,7 @@ cwc_session(cwc_t *cwc) pthread_cond_init(&cwc->cwc_writer_cond, NULL); pthread_mutex_init(&cwc->cwc_writer_mutex, NULL); TAILQ_INIT(&cwc->cwc_writeq); - pthread_create(&writer_thread_id, NULL, cwc_writer_thread, cwc); + tvhthread_create(&writer_thread_id, NULL, cwc_writer_thread, cwc); /** * Mainloop @@ -2091,7 +2091,7 @@ cwc_entry_find(const char *id, int create) pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_create(&ptid, &attr, cwc_thread, cwc); + tvhthread_create(&ptid, &attr, cwc_thread, cwc); pthread_attr_destroy(&attr); return cwc; diff --git a/src/dvr/dvr_inotify.c b/src/dvr/dvr_inotify.c index 7d6f177c..e19a562c 100644 --- a/src/dvr/dvr_inotify.c +++ b/src/dvr/dvr_inotify.c @@ -66,7 +66,7 @@ void dvr_inotify_init ( void ) return; } - pthread_create(&tid, NULL, _dvr_inotify_thread, NULL); + tvhthread_create(&tid, NULL, _dvr_inotify_thread, NULL); } /** diff --git a/src/dvr/dvr_rec.c b/src/dvr/dvr_rec.c index c5726b01..34256b18 100644 --- a/src/dvr/dvr_rec.c +++ b/src/dvr/dvr_rec.c @@ -90,7 +90,7 @@ dvr_rec_subscribe(dvr_entry_t *de) buf, st, flags, NULL, NULL, NULL); - pthread_create(&de->de_thread, NULL, dvr_thread, de); + tvhthread_create(&de->de_thread, NULL, dvr_thread, de); } /** diff --git a/src/epggrab.c b/src/epggrab.c index b421b260..7541b7cf 100644 --- a/src/epggrab.c +++ b/src/epggrab.c @@ -400,7 +400,7 @@ void epggrab_init ( void ) pthread_attr_t tattr; pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); - pthread_create(&tid, &tattr, _epggrab_internal_thread, NULL); + tvhthread_create(&tid, &tattr, _epggrab_internal_thread, NULL); pthread_attr_destroy(&tattr); } diff --git a/src/epggrab/module.c b/src/epggrab/module.c index 9af9f285..462b9477 100644 --- a/src/epggrab/module.c +++ b/src/epggrab/module.c @@ -414,7 +414,7 @@ int epggrab_module_enable_socket ( void *m, uint8_t e ) tvhlog(LOG_DEBUG, mod->id, "starting socket thread"); pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); - pthread_create(&tid, &tattr, _epggrab_socket_thread, mod); + tvhthread_create(&tid, &tattr, _epggrab_socket_thread, mod); } mod->enabled = e; return 1; diff --git a/src/htsp_server.c b/src/htsp_server.c index 0adac558..7c3ad332 100644 --- a/src/htsp_server.c +++ b/src/htsp_server.c @@ -2082,7 +2082,7 @@ htsp_serve(int fd, void *opaque, struct sockaddr_storage *source, LIST_INSERT_HEAD(&htsp_connections, &htsp, htsp_link); pthread_mutex_unlock(&global_lock); - pthread_create(&htsp.htsp_writer_thread, NULL, htsp_write_scheduler, &htsp); + tvhthread_create(&htsp.htsp_writer_thread, NULL, htsp_write_scheduler, &htsp); /** * Reader loop diff --git a/src/idnode.c b/src/idnode.c index b9bc9cc6..2b711a93 100644 --- a/src/idnode.c +++ b/src/idnode.c @@ -134,7 +134,7 @@ idnode_init(void) idnode_queue = NULL; pthread_mutex_init(&idnode_mutex, NULL); pthread_cond_init(&idnode_cond, NULL); - pthread_create(&tid, NULL, idnode_thread, NULL); + tvhthread_create(&tid, NULL, idnode_thread, NULL); } /** diff --git a/src/imagecache.c b/src/imagecache.c index d586129f..b302d74f 100644 --- a/src/imagecache.c +++ b/src/imagecache.c @@ -165,7 +165,7 @@ void imagecache_init ( void ) #if ENABLE_IMAGECACHE { pthread_t tid; - pthread_create(&tid, NULL, _imagecache_thread, NULL); + tvhthread_create(&tid, NULL, _imagecache_thread, NULL); } #endif } diff --git a/src/input/mpegps/v4l/v4l.c b/src/input/mpegps/v4l/v4l.c index 63d9ea4a..2804a6c4 100644 --- a/src/input/mpegps/v4l/v4l.c +++ b/src/input/mpegps/v4l/v4l.c @@ -230,7 +230,7 @@ v4l_service_start(service_t *t, int instance) va->va_fd = fd; va->va_current_service = t; - pthread_create(&va->va_thread, NULL, v4l_thread, va); + tvhthread_create(&va->va_thread, NULL, v4l_thread, va); v4l_adapter_notify(va); return 0; } diff --git a/src/input/mpegts/iptv/iptv.c b/src/input/mpegts/iptv/iptv.c index 7c6fcffa..7f2112b6 100644 --- a/src/input/mpegts/iptv/iptv.c +++ b/src/input/mpegts/iptv/iptv.c @@ -534,13 +534,13 @@ void iptv_init ( void ) mpegts_input_set_network((mpegts_input_t*)&iptv_input, (mpegts_network_t*)&iptv_network); /* Set table thread */ - pthread_create(&tid, NULL, mpegts_input_table_thread, &iptv_input); + tvhthread_create(&tid, NULL, mpegts_input_table_thread, &iptv_input); /* Setup TS thread */ // TODO: could set this up only when needed iptv_poll = tvhpoll_create(10); pthread_mutex_init(&iptv_lock, NULL); - pthread_create(&iptv_thread, NULL, iptv_input_thread, NULL); + tvhthread_create(&iptv_thread, NULL, iptv_input_thread, NULL); /* Load config */ iptv_mux_load_all(); diff --git a/src/input/mpegts/linuxdvb/linuxdvb_frontend.c b/src/input/mpegts/linuxdvb/linuxdvb_frontend.c index ca524775..e4de3eff 100644 --- a/src/input/mpegts/linuxdvb/linuxdvb_frontend.c +++ b/src/input/mpegts/linuxdvb/linuxdvb_frontend.c @@ -550,7 +550,7 @@ linuxdvb_frontend_monitor ( void *aux ) /* Start input */ tvh_pipe(O_NONBLOCK, &lfe->lfe_dvr_pipe); pthread_mutex_lock(&lfe->lfe_dvr_lock); - pthread_create(&lfe->lfe_dvr_thread, NULL, + tvhthread_create(&lfe->lfe_dvr_thread, NULL, linuxdvb_frontend_input_thread, lfe); pthread_cond_wait(&lfe->lfe_dvr_cond, &lfe->lfe_dvr_lock); pthread_mutex_unlock(&lfe->lfe_dvr_lock); @@ -883,7 +883,7 @@ linuxdvb_frontend_create0 pthread_cond_init(&lfe->lfe_dvr_cond, NULL); /* Start table thread */ - pthread_create(&tid, NULL, mpegts_input_table_thread, lfe); + tvhthread_create(&tid, NULL, mpegts_input_table_thread, lfe); /* No conf */ if (!conf) diff --git a/src/input/mpegts/tsfile/tsfile_input.c b/src/input/mpegts/tsfile/tsfile_input.c index c7ae08d1..9667e9f4 100644 --- a/src/input/mpegts/tsfile/tsfile_input.c +++ b/src/input/mpegts/tsfile/tsfile_input.c @@ -206,7 +206,7 @@ tsfile_input_start_mux ( mpegts_input_t *mi, mpegts_mux_instance_t *t ) return SM_CODE_TUNING_FAILED; } tvhtrace("tsfile", "adapter %d starting thread", mi->mi_instance); - pthread_create(&mi->mi_thread_id, NULL, tsfile_input_thread, mi); + tvhthread_create(&mi->mi_thread_id, NULL, tsfile_input_thread, mi); } /* Current */ @@ -243,7 +243,7 @@ tsfile_input_create ( int idx ) LIST_INSERT_HEAD(&tsfile_inputs, mi, mi_global_link); /* Start table thread */ - pthread_create(&tid, NULL, mpegts_input_table_thread, mi); + tvhthread_create(&tid, NULL, mpegts_input_table_thread, mi); return mi; } diff --git a/src/service.c b/src/service.c index b59721f6..44a0252d 100644 --- a/src/service.c +++ b/src/service.c @@ -984,7 +984,7 @@ service_init(void) TAILQ_INIT(&service_all); pthread_mutex_init(&pending_save_mutex, NULL); pthread_cond_init(&pending_save_cond, NULL); - pthread_create(&tid, NULL, service_saver, NULL); + tvhthread_create(&tid, NULL, service_saver, NULL); } diff --git a/src/service_mapper.c b/src/service_mapper.c index 00974bc3..84ad6f23 100644 --- a/src/service_mapper.c +++ b/src/service_mapper.c @@ -48,7 +48,7 @@ service_mapper_init ( void ) pthread_t tid; TAILQ_INIT(&service_mapper_queue); pthread_cond_init(&service_mapper_cond, NULL); - pthread_create(&tid, NULL, service_mapper_thread, NULL); + tvhthread_create(&tid, NULL, service_mapper_thread, NULL); } /* diff --git a/src/tcp.c b/src/tcp.c index 9bce82d4..980a0dba 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -490,7 +490,7 @@ tcp_server_loop(void *aux) continue; } - pthread_create(&tid, &attr, tcp_server_start, tsl); + tvhthread_create(&tid, &attr, tcp_server_start, tsl); } } return NULL; @@ -590,7 +590,7 @@ tcp_server_init(int opt_ipv6) tcp_preferred_address_family = AF_INET6; tcp_server_poll = tvhpoll_create(10); - pthread_create(&tid, NULL, tcp_server_loop, NULL); + tvhthread_create(&tid, NULL, tcp_server_loop, NULL); } diff --git a/src/timeshift.c b/src/timeshift.c index 3a4063d7..0f5e49b4 100644 --- a/src/timeshift.c +++ b/src/timeshift.c @@ -253,8 +253,8 @@ streaming_target_t *timeshift_create /* Initialise input */ streaming_queue_init(&ts->wr_queue, 0); streaming_target_init(&ts->input, timeshift_input, ts, 0); - pthread_create(&ts->wr_thread, NULL, timeshift_writer, ts); - pthread_create(&ts->rd_thread, NULL, timeshift_reader, ts); + tvhthread_create(&ts->wr_thread, NULL, timeshift_writer, ts); + tvhthread_create(&ts->rd_thread, NULL, timeshift_reader, ts); /* Update index */ timeshift_index++; diff --git a/src/timeshift/timeshift_filemgr.c b/src/timeshift/timeshift_filemgr.c index cd58d33f..a98a4280 100644 --- a/src/timeshift/timeshift_filemgr.c +++ b/src/timeshift/timeshift_filemgr.c @@ -344,7 +344,7 @@ void timeshift_filemgr_init ( void ) pthread_mutex_init(×hift_reaper_lock, NULL); pthread_cond_init(×hift_reaper_cond, NULL); TAILQ_INIT(×hift_reaper_list); - pthread_create(×hift_reaper_thread, NULL, + tvhthread_create(×hift_reaper_thread, NULL, timeshift_reaper_callback, NULL); } diff --git a/src/tvheadend.h b/src/tvheadend.h index a60ac404..6c062c9c 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -20,6 +20,7 @@ #include "config.h" +#define _GNU_SOURCE #include #include #include @@ -522,6 +523,11 @@ static inline void mystrset(char **p, const char *s) *p = s ? strdup(s) : NULL; } +int tvhthread_create0(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine) (void *), void *arg, const char *name); + +#define tvhthread_create(a, b, c, d) tvhthread_create0(a, b, c, d, #c) + int tvh_open(const char *pathname, int flags, mode_t mode); int tvh_socket(int domain, int type, int protocol); diff --git a/src/tvhlog.c b/src/tvhlog.c index f56a70dd..86f5ee79 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -393,7 +393,7 @@ tvhlog_init ( int level, int options, const char *path ) pthread_mutex_init(&tvhlog_mutex, NULL); pthread_cond_init(&tvhlog_cond, NULL); TAILQ_INIT(&tvhlog_queue); - pthread_create(&tvhlog_tid, NULL, tvhlog_thread, NULL); + tvhthread_create(&tvhlog_tid, NULL, tvhlog_thread, NULL); } void diff --git a/src/wrappers.c b/src/wrappers.c index c9b79c1a..c3114c07 100644 --- a/src/wrappers.c +++ b/src/wrappers.c @@ -1,8 +1,10 @@ +#define __USE_GNU +#include "tvheadend.h" #include #include /* See NOTES */ #include #include -#include "tvheadend.h" +#include int tvh_open(const char *pathname, int flags, mode_t mode) @@ -78,3 +80,16 @@ tvh_write(int fd, const void *buf, size_t len) return len ? 1 : 0; } + +int tvhthread_create0(pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine) (void *), void *arg, const char *name) +{ + int r; + char buf[16] = { 0 }; + strncpy(buf, name, sizeof(buf)-1); + r = pthread_create(thread, attr, start_routine, arg); + tvhinfo("thread", "created thread %ld [%s / %p]", *thread, name, start_routine); + if (r) return r; + pthread_setname_np(*thread, buf); + return r; +}