threads: add wrapper for initializing recursive mutexes

Damn this overly verbose pthread API.
This commit is contained in:
wm4 2014-01-31 19:50:25 +01:00
parent 2305ffcaba
commit a17be5576f
4 changed files with 17 additions and 10 deletions

View File

@ -41,6 +41,7 @@
#include "keycodes.h"
#include "cmd_list.h"
#include "cmd_parse.h"
#include "osdep/threads.h"
#include "osdep/timer.h"
#include "common/msg.h"
#include "common/global.h"
@ -1474,11 +1475,7 @@ struct input_ctx *mp_input_init(struct mpv_global *global)
.wakeup_pipe = {-1, -1},
};
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&ictx->mutex, &attr);
pthread_mutexattr_destroy(&attr);
mpthread_mutex_init_recursive(&ictx->mutex);
// Setup default section, so that it does nothing.
mp_input_enable_section(ictx, NULL, MP_INPUT_ALLOW_VO_DRAGGING |

View File

@ -55,3 +55,14 @@ int mpthread_cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
timespec_add_seconds(&ts, timeout);
return pthread_cond_timedwait(cond, mutex, &ts);
}
// Helper to reduce boiler plate.
int mpthread_mutex_init_recursive(pthread_mutex_t *mutex)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
int r = pthread_mutex_init(mutex, &attr);
pthread_mutexattr_destroy(&attr);
return r;
}

View File

@ -6,4 +6,6 @@
int mpthread_cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
double timeout);
int mpthread_mutex_init_recursive(pthread_mutex_t *mutex);
#endif

View File

@ -31,6 +31,7 @@
#include "common/global.h"
#include "common/msg.h"
#include "misc/charset_conv.h"
#include "osdep/threads.h"
extern const struct sd_functions sd_ass;
extern const struct sd_functions sd_lavc;
@ -96,11 +97,7 @@ struct dec_sub *sub_create(struct mpv_global *global)
sub->log = mp_log_new(sub, global->log, "sub");
sub->opts = global->opts;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&sub->lock, &attr);
pthread_mutexattr_destroy(&attr);
mpthread_mutex_init_recursive(&sub->lock);
return sub;
}