]> git.sesse.net Git - vlc/blob - src/test/timer.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / test / timer.c
1 /*****************************************************************************
2  * timer.c: Test for timer API
3  *****************************************************************************
4  * Copyright (C) 2009 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #undef NDEBUG
30 #include <assert.h>
31
32 struct timer_data
33 {
34     vlc_timer_t timer;
35     vlc_mutex_t lock;
36     unsigned count;
37 };
38
39 static void callback (void *ptr)
40 {
41     struct timer_data *data = ptr;
42
43     vlc_mutex_lock (&data->lock);
44     data->count += 1 + vlc_timer_getoverrun (data->timer);
45     vlc_mutex_unlock (&data->lock);
46 }
47
48
49 int main (void)
50 {
51     struct timer_data data;
52     int val;
53
54     vlc_mutex_init (&data.lock);
55     data.count = 0;
56
57     val = vlc_timer_create (&data.timer, callback, &data);
58     assert (val == 0);
59
60     /* Relative timer */
61     vlc_timer_schedule (data.timer, false, 1, CLOCK_FREQ / 10);
62     msleep (CLOCK_FREQ);
63     vlc_mutex_lock (&data.lock);
64     data.count += vlc_timer_getoverrun (data.timer);
65     printf ("Count = %u\n", data.count);
66     assert (data.count >= 10);
67     data.count = 0;
68     vlc_mutex_unlock (&data.lock);
69     vlc_timer_schedule (data.timer, false, 0, 0);
70
71     /* Absolute timer */
72     mtime_t now = mdate ();
73
74     vlc_timer_schedule (data.timer, true, now, CLOCK_FREQ / 10);
75     msleep (CLOCK_FREQ);
76     vlc_mutex_lock (&data.lock);
77     data.count += vlc_timer_getoverrun (data.timer);
78     printf ("Count = %u\n", data.count);
79     assert (data.count >= 10);
80     vlc_mutex_unlock (&data.lock);
81
82     vlc_timer_destroy (data.timer);
83     vlc_mutex_destroy (&data.lock);
84
85     return 0;
86 }