summaryrefslogtreecommitdiffstats
path: root/osdep/timer-darwin.c
blob: 1ce9cd584b29fe84d9228e5110235a7b2305cb6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Precise timer routines using Mach timing
 *
 * Copyright (c) 2003-2004, Dan Villiom Podlaski Christiansen
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 */

#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <sys/time.h>
#include <mach/mach_time.h>

#include "config.h"
#include "core/mp_msg.h"
#include "timer.h"

/* global variables */
static double timebase_ratio;


/* the core sleep function, uses floats and is used in MPlayer G2 */
static float sleep_accurate(float time_frame)
{
	uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time();

	mach_wait_until(deadline);

	return (mach_absolute_time() - deadline) * timebase_ratio;
}

/* wrapper for MPlayer G1 */
int usec_sleep(int usec_delay)
{
  return sleep_accurate(usec_delay / 1e6) * 1e6;
}


/* current time in microseconds */
unsigned int GetTimer(void)
{
  return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e6);
}

/* current time in milliseconds */
unsigned int GetTimerMS(void)
{
  return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e3);
}

/* initialize timer, must be called at least once at start */
void InitTimer(void)
{
  struct mach_timebase_info timebase;

  mach_timebase_info(&timebase);
  timebase_ratio = (double)timebase.numer / (double)timebase.denom
    * (double)1e-9;
}

#if 0
#include <stdio.h>

int main(void) {
  int i,j, r, c = 200;
  long long t = 0;

  InitTimer();

  for (i = 0; i < c; i++) {
    const int delay = rand() / (RAND_MAX / 1e5);
    j = GetTimer();
#if 1
    r = usec_sleep(delay);
#else
    r = sleep_accurate(delay / 1e6) * 1e6;
#endif
    j = (GetTimer() - j) - delay;
    printf("sleep time:%8i %5i (%i)\n", delay, j, j - r);
    t += j - r;
  }
  fprintf(stderr, "average error:\t%lli\n", t / c);

  return 0;
}
#endif