qt: medialib: convert description into Q_GADGET
[vlc.git] / src / test / timer.c
bloba071bd93eedb0c79d1178acb5a39efc4df0af5f0
1 /*****************************************************************************
2 * timer.c: Test for timer API
3 *****************************************************************************
4 * Copyright (C) 2009 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #undef NDEBUG
28 #include <assert.h>
30 #include <vlc_common.h>
31 #undef vlc_tick_sleep
33 const char vlc_module_name[] = "test_timer";
35 struct timer_data
37 vlc_timer_t timer;
38 vlc_mutex_t lock;
39 vlc_cond_t wait;
40 unsigned count;
43 static void callback (void *ptr)
45 struct timer_data *data = ptr;
47 vlc_mutex_lock (&data->lock);
48 data->count += 1 + vlc_timer_getoverrun (data->timer);
49 vlc_cond_signal (&data->wait);
50 vlc_mutex_unlock (&data->lock);
54 int main (void)
56 struct timer_data data;
57 vlc_tick_t ts;
58 int val;
60 vlc_mutex_init (&data.lock);
61 vlc_cond_init (&data.wait);
62 data.count = 0;
64 val = vlc_timer_create (&data.timer, callback, &data);
65 assert (val == 0);
66 vlc_timer_destroy (data.timer);
67 assert (data.count == 0);
69 val = vlc_timer_create (&data.timer, callback, &data);
70 assert (val == 0);
71 vlc_timer_schedule (data.timer, false, CLOCK_FREQ << 20, CLOCK_FREQ);
72 vlc_timer_destroy (data.timer);
73 assert (data.count == 0);
75 val = vlc_timer_create (&data.timer, callback, &data);
76 assert (val == 0);
78 /* Relative timer */
79 ts = vlc_tick_now ();
80 vlc_timer_schedule (data.timer, false, 1, VLC_TICK_FROM_MS(10));
82 vlc_mutex_lock (&data.lock);
83 while (data.count <= 10)
84 vlc_cond_wait(&data.wait, &data.lock);
86 ts = vlc_tick_now () - ts;
87 printf ("%u iterations in %"PRId64" us\n", data.count, ts);
88 data.count = 0;
89 vlc_mutex_unlock (&data.lock);
90 assert(ts >= VLC_TICK_FROM_MS(100));
92 vlc_timer_disarm (data.timer);
94 /* Absolute timer */
95 ts = vlc_tick_now ();
97 vlc_timer_schedule (data.timer, true, ts + VLC_TICK_FROM_MS(100),
98 VLC_TICK_FROM_MS(10));
100 vlc_mutex_lock (&data.lock);
101 while (data.count <= 10)
102 vlc_cond_wait(&data.wait, &data.lock);
104 ts = vlc_tick_now () - ts;
105 printf ("%u iterations in %"PRId64" us\n", data.count, ts);
106 vlc_mutex_unlock (&data.lock);
107 assert(ts >= VLC_TICK_FROM_MS(200));
109 vlc_timer_destroy (data.timer);
110 return 0;