Set thread name for debugging

Especially with other components (libavcodec, OSX stuff), the thread
list can get quite populated. Setting the thread name helps when
debugging.

Since this is not portable, we check the OS variants in waf configure.
old-configure just gets a special-case for glibc, since doing a full
check here would probably be a waste of effort.
This commit is contained in:
wm4 2014-10-19 23:32:34 +02:00
parent c6dca55665
commit 9ba6641879
17 changed files with 73 additions and 0 deletions

View File

@ -303,6 +303,7 @@ static void *playthread(void *arg)
{
struct ao *ao = arg;
struct ao_push_state *p = ao->api_priv;
mpthread_set_name("ao");
pthread_mutex_lock(&p->lock);
while (!p->terminate) {
if (!p->paused)

View File

@ -33,6 +33,7 @@
#include "talloc.h"
#include "common/msg.h"
#include "common/global.h"
#include "osdep/threads.h"
#include "stream/stream.h"
#include "demux.h"
@ -455,6 +456,7 @@ static void execute_seek(struct demux_internal *in)
static void *demux_thread(void *pctx)
{
struct demux_internal *in = pctx;
mpthread_set_name("demux");
pthread_mutex_lock(&in->lock);
while (!in->thread_terminate) {
in->thread_paused = in->thread_request_pause > 0;

View File

@ -1451,6 +1451,8 @@ static void *input_src_thread(void *ptr)
void (*loop_fn)(struct mp_input_src *src, void *ctx) = args[1];
void *ctx = args[2];
mpthread_set_name("input source");
src->in->thread_running = true;
loop_fn(src, ctx);

View File

@ -29,6 +29,7 @@
#include "config.h"
#include "osdep/io.h"
#include "osdep/threads.h"
#include "common/common.h"
#include "common/global.h"
@ -471,6 +472,8 @@ static void *client_thread(void *p)
struct client_arg *arg = p;
bstr client_msg = { talloc_strdup(NULL, ""), 0 };
mpthread_set_name(arg->client_name);
int pipe_fd = mpv_get_wakeup_pipe(arg->client);
if (pipe_fd < 0) {
MP_ERR(arg, "Could not get wakeup pipe\n");
@ -649,6 +652,8 @@ static void *ipc_thread(void *p)
struct mp_ipc_ctx *arg = p;
mpthread_set_name("ipc socket listener");
MP_INFO(arg, "Starting IPC master\n");
ipc_fd = socket(AF_UNIX, SOCK_STREAM, 0);

View File

@ -990,6 +990,9 @@ cat > $TMPC << EOF
#define HAVE_SDL1 0
#define HAVE_WAIO 0
#define HAVE_POSIX_SPAWN 1
#define HAVE_GLIBC_THREAD_NAME (!!__GLIBC__)
#define HAVE_OSX_THREAD_NAME 0
#define HAVE_BSD_THREAD_NAME 0
#define DEFAULT_CDROM_DEVICE "/dev/cdrom"
#define DEFAULT_DVD_DEVICE "/dev/dvd"

View File

@ -26,6 +26,7 @@
#import "osdep/macosx_application_objc.h"
#include "osdep/macosx_compat.h"
#import "osdep/macosx_events_objc.h"
#include "osdep/threads.h"
#define MPV_PROTOCOL @"mpv://"
@ -283,6 +284,7 @@ struct playback_thread_ctx {
static void *playback_thread(void *ctx_obj)
{
mpthread_set_name("playback core (OSX)");
@autoreleasepool {
struct playback_thread_ctx *ctx = (struct playback_thread_ctx*) ctx_obj;
ctx->mpv_main(*ctx->argc, *ctx->argv);

View File

@ -45,6 +45,7 @@
#include <poll.h>
#include "osdep/io.h"
#include "osdep/threads.h"
#include "common/common.h"
#include "misc/bstr.h"
@ -757,6 +758,7 @@ static void quit_request_sighandler(int signum)
static void *terminal_thread(void *ptr)
{
mpthread_set_name("terminal");
bool stdin_ok = isatty(STDIN_FILENO); // if false, we still wait for SIGTERM
while (1) {
struct pollfd fds[2] = {

View File

@ -37,6 +37,7 @@
#include "input/input.h"
#include "terminal.h"
#include "osdep/io.h"
#include "osdep/threads.h"
#include "osdep/w32_keyboard.h"
#define hSTDOUT GetStdHandle(STD_OUTPUT_HANDLE)
@ -122,6 +123,7 @@ static void read_input(void)
static void *input_thread_fn(void *ptr)
{
mpthread_set_name("terminal");
HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
HANDLE stuff[2] = {in, death};
while (1) {

View File

@ -14,6 +14,9 @@
* You should have received a copy of the GNU General Public License along
* with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "config.h"
#include "threads.h"
#include "timer.h"
@ -40,3 +43,16 @@ int mpthread_mutex_init_recursive(pthread_mutex_t *mutex)
pthread_mutexattr_destroy(&attr);
return r;
}
void mpthread_set_name(const char *name)
{
char tname[90];
snprintf(tname, sizeof(tname), "mpv %s", name);
#if HAVE_GLIBC_THREAD_NAME
pthread_setname_np(pthread_self(), tname);
#elif HAVE_BSD_THREAD_NAME
pthread_set_name_np(pthread_self(), tname);
#elif HAVE_OSX_THREAD_NAME
pthread_setname_np(tname);
#endif
}

View File

@ -16,4 +16,7 @@ int mpthread_cond_timedwait_rel(pthread_cond_t *cond, pthread_mutex_t *mutex,
// Helper to reduce boiler plate.
int mpthread_mutex_init_recursive(pthread_mutex_t *mutex);
// Set thread name (for debuggers).
void mpthread_set_name(const char *name);
#endif

View File

@ -419,6 +419,8 @@ static void *playback_thread(void *p)
struct MPContext *mpctx = p;
mpctx->autodetach = true;
mpthread_set_name("playback core");
mp_play_files(mpctx);
// This actually waits until all clients are gone before actually

View File

@ -26,6 +26,7 @@
#include "osdep/io.h"
#include "osdep/timer.h"
#include "osdep/threads.h"
#include "common/msg.h"
#include "options/options.h"
@ -249,6 +250,7 @@ struct wrapper_args {
static void *thread_wrapper(void *pctx)
{
struct wrapper_args *args = pctx;
mpthread_set_name("opener");
args->thread_fn(args->thread_arg);
pthread_mutex_lock(&args->mutex);
args->done = true;

View File

@ -26,6 +26,7 @@
#include "config.h"
#include "osdep/io.h"
#include "osdep/threads.h"
#include "common/common.h"
#include "common/msg.h"
@ -78,6 +79,10 @@ static void *script_thread(void *p)
struct thread_arg *arg = p;
char name[90];
snprintf(name, sizeof(name), "script (%s)", arg->fname);
mpthread_set_name(name);
if (arg->backend->load(arg->client, arg->fname) < 0)
MP_ERR(arg, "Could not load script %s\n", arg->fname);

View File

@ -457,6 +457,7 @@ static void cache_execute_control(struct priv *s)
static void *cache_thread(void *arg)
{
struct priv *s = arg;
mpthread_set_name("cache");
pthread_mutex_lock(&s->mutex);
update_cached_controls(s);
double last = mp_time_sec();

View File

@ -47,6 +47,7 @@
#include "video/vfcap.h"
#include "sub/osd.h"
#include "osdep/io.h"
#include "osdep/threads.h"
extern const struct vo_driver video_out_x11;
extern const struct vo_driver video_out_vdpau;
@ -666,6 +667,8 @@ static void *vo_thread(void *ptr)
struct vo *vo = ptr;
struct vo_internal *in = vo->in;
mpthread_set_name("vo");
int r = vo->driver->preinit(vo) ? -1 : 0;
mp_rendezvous(vo, r); // init barrier
if (r < 0)

View File

@ -34,6 +34,7 @@
#include "win_state.h"
#include "w32_common.h"
#include "osdep/io.h"
#include "osdep/threads.h"
#include "osdep/w32_keyboard.h"
#include "misc/dispatch.h"
#include "misc/rendezvous.h"
@ -990,6 +991,8 @@ static void *gui_thread(void *ptr)
bool ole_ok = false;
int res = 0;
mpthread_set_name("win32 window");
HINSTANCE hInstance = GetModuleHandleW(NULL);
WNDCLASSEXW wcex = {

19
wscript
View File

@ -211,6 +211,25 @@ iconv support use --disable-iconv.",
'deps_neg': [ 'glob' ],
'deps_any': [ 'os-win32', 'os-cygwin' ],
'func': check_true
}, {
'name': 'glibc-thread-name',
'desc': 'GLIBC API for setting thread name',
'func': check_statement('pthread.h',
'pthread_setname_np(pthread_self(), "ducks")',
use=['pthreads']),
}, {
'name': 'osx-thread-name',
'desc': 'OSX API for setting thread name',
'deps_neg': [ 'glibc-thread-name' ],
'func': check_statement('pthread.h',
'pthread_setname_np("ducks")', use=['pthreads']),
}, {
'name': 'bsd-thread-name',
'desc': 'BSD API for setting thread name',
'deps_neg': [ 'glibc-thread-name', 'osx-thread-name' ],
'func': check_statement('pthread.h',
'pthread_set_name_np(pthread_self(), "ducks")',
use=['pthreads']),
}, {
'name': 'bsd-fstatfs',
'desc': "BSD's fstatfs()",