configure: Improve detection of ibtool
[vlc.git] / src / test / timer.c
blobfa0a4fda1528af1336a6d48024b54d51f9c8f7db
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 msleep
33 struct timer_data
35 vlc_timer_t timer;
36 vlc_mutex_t lock;
37 vlc_cond_t wait;
38 unsigned count;
41 static void callback (void *ptr)
43 struct timer_data *data = ptr;
45 vlc_mutex_lock (&data->lock);
46 data->count += 1 + vlc_timer_getoverrun (data->timer);
47 vlc_cond_signal (&data->wait);
48 vlc_mutex_unlock (&data->lock);
52 int main (void)
54 struct timer_data data;
55 mtime_t ts;
56 int val;
58 vlc_mutex_init (&data.lock);
59 vlc_cond_init (&data.wait);
60 data.count = 0;
62 val = vlc_timer_create (&data.timer, callback, &data);
63 assert (val == 0);
64 vlc_timer_destroy (data.timer);
65 assert (data.count == 0);
67 val = vlc_timer_create (&data.timer, callback, &data);
68 assert (val == 0);
69 vlc_timer_schedule (data.timer, false, CLOCK_FREQ << 20, CLOCK_FREQ);
70 vlc_timer_destroy (data.timer);
71 assert (data.count == 0);
73 val = vlc_timer_create (&data.timer, callback, &data);
74 assert (val == 0);
76 /* Relative timer */
77 ts = mdate ();
78 vlc_timer_schedule (data.timer, false, 1, CLOCK_FREQ / 100);
80 vlc_mutex_lock (&data.lock);
81 while (data.count <= 10)
82 vlc_cond_wait(&data.wait, &data.lock);
84 ts = mdate () - ts;
85 printf ("%u iterations in %"PRId64" us\n", data.count, ts);
86 data.count = 0;
87 vlc_mutex_unlock (&data.lock);
88 assert(ts >= (CLOCK_FREQ / 10));
90 vlc_timer_schedule (data.timer, false, 0, 0);
92 /* Absolute timer */
93 ts = mdate ();
95 vlc_timer_schedule (data.timer, true, ts + CLOCK_FREQ / 10,
96 CLOCK_FREQ / 100);
98 vlc_mutex_lock (&data.lock);
99 while (data.count <= 10)
100 vlc_cond_wait(&data.wait, &data.lock);
102 ts = mdate () - ts;
103 printf ("%u iterations in %"PRId64" us\n", data.count, ts);
104 vlc_mutex_unlock (&data.lock);
105 assert(ts >= (CLOCK_FREQ / 5));
107 vlc_timer_destroy (data.timer);
108 vlc_cond_destroy (&data.wait);
109 vlc_mutex_destroy (&data.lock);
111 return 0;