diff options
author | wm4 <wm4@nowhere> | 2020-03-18 22:34:48 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2020-03-18 22:42:13 +0100 |
commit | cb82cbbbae99d5b8beebae76320a9097a2bc34f7 (patch) | |
tree | 144715054c3ac0ee203dcc23004e583923334961 /osdep/threads.c | |
parent | 373dad9962e4d7b23b95fc95bbbe811c8adafc92 (diff) | |
download | mpv-cb82cbbbae99d5b8beebae76320a9097a2bc34f7.tar.bz2 mpv-cb82cbbbae99d5b8beebae76320a9097a2bc34f7.tar.xz |
osdep: add a pthread debugging wrapper
Because pthread failures are virtually undebuggable (which sure is
pretty strange, given all these heavy instrumentation tools these days).
Of course it affects only files which include osdep/threads.h.
I'm departing from the usual way to add symbols with config.h and using
"#if", and defining it on the compiler command line + "#ifdef" because I
don't want to include config.h from a header (which would be necessary
in this case) to keep things slightly cleaner. Maybe this is misguided,
but still.
This would have been easier if mpv defined its own wrappers for all
thread functions. But we don't (which to be honest is probably better
than e.g. going crazy like VLC and essentially reimplementing
everything). This seems to be a good compromise. Since it's off by
default and basically a developer tool, the minor undefined behavior
(redefining reserved symbols) isn't much of an issue.
Diffstat (limited to 'osdep/threads.c')
-rw-r--r-- | osdep/threads.c | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/osdep/threads.c b/osdep/threads.c index 8cd4545c1e..5b164c5179 100644 --- a/osdep/threads.c +++ b/osdep/threads.c @@ -19,8 +19,8 @@ #include <errno.h> #include <pthread.h> +#include "common/common.h" #include "config.h" - #include "threads.h" #include "timer.h" @@ -49,3 +49,29 @@ void mpthread_set_name(const char *name) pthread_setname_np(tname); #endif } + +int mp_ptwrap_check(const char *file, int line, int res) +{ + if (res && res != ETIMEDOUT) { + fprintf(stderr, "%s:%d: internal error: pthread result %d (%s)\n", + file, line, res, mp_strerror(res)); + abort(); + } + return res; +} + +int mp_ptwrap_mutex_init(const char *file, int line, pthread_mutex_t *m, + const pthread_mutexattr_t *attr) +{ + pthread_mutexattr_t m_attr; + if (!attr) { + attr = &m_attr; + pthread_mutexattr_init(&m_attr); + // Force normal mutexes to error checking. + pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_ERRORCHECK); + } + int res = mp_ptwrap_check(file, line, (pthread_mutex_init)(m, attr)); + if (attr == &m_attr) + pthread_mutexattr_destroy(&m_attr); + return res; +} |