summaryrefslogtreecommitdiffstats
path: root/osdep
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-02-06 21:04:24 +0100
committerwm4 <wm4@nowhere>2014-02-10 01:12:34 +0100
commitc6166ff448432dc74c300933e5c93838d06c420a (patch)
tree4697e0fb88c2380cc5e5214c14a756c9ad88c0f3 /osdep
parent8437356b6c615ac93f49fe122a591a229722dbe8 (diff)
downloadmpv-c6166ff448432dc74c300933e5c93838d06c420a.tar.bz2
mpv-c6166ff448432dc74c300933e5c93838d06c420a.tar.xz
timer: init only once
This avoids trouble if another mpv instance is initialized in the same process. Since timeBeginPeriod/timeEndPeriod are hereby not easily matched anymore, use an atexit() handler to call timeEndPeriod, so that we can be sure these calls are matched, even if we allow multiple initializations later when introducing the client API.
Diffstat (limited to 'osdep')
-rw-r--r--osdep/timer-win2.c12
-rw-r--r--osdep/timer.c9
2 files changed, 18 insertions, 3 deletions
diff --git a/osdep/timer-win2.c b/osdep/timer-win2.c
index 136bb0becc..ed09ed18db 100644
--- a/osdep/timer-win2.c
+++ b/osdep/timer-win2.c
@@ -21,6 +21,7 @@
#include <windows.h>
#include <sys/time.h>
#include <mmsystem.h>
+#include <stdlib.h>
#include "timer.h"
void mp_sleep_us(int64_t us)
@@ -42,8 +43,15 @@ uint64_t mp_raw_time_us(void)
return tv.tv_sec * 1000000LL + tv.tv_usec;
}
+static void restore_timer(void)
+{
+ // The MSDN documents that begin/end "must" be matched. This satisfies
+ // this requirement.
+ timeEndPeriod(1);
+}
+
void mp_raw_time_init(void)
{
- // request 1ms timer resolution
- timeBeginPeriod(1);
+ timeBeginPeriod(1); // request 1ms timer resolution
+ atexit(restore_timer);
}
diff --git a/osdep/timer.c b/osdep/timer.c
index d8dd49b9f7..f2c64959ab 100644
--- a/osdep/timer.c
+++ b/osdep/timer.c
@@ -16,12 +16,14 @@
*/
#include <stdlib.h>
+#include <pthread.h>
#include "timer.h"
static uint64_t raw_time_offset;
+pthread_once_t timer_init_once = PTHREAD_ONCE_INIT;
-void mp_time_init(void)
+static void do_timer_init(void)
{
mp_raw_time_init();
srand(mp_raw_time_us());
@@ -32,6 +34,11 @@ void mp_time_init(void)
raw_time_offset -= 10000000;
}
+void mp_time_init(void)
+{
+ pthread_once(&timer_init_once, do_timer_init);
+}
+
int64_t mp_time_us(void)
{
return mp_raw_time_us() - raw_time_offset;