aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHolger Freyther <zecke@selfish.org>2008-12-27 09:42:59 +0000
committerHolger Freyther <zecke@selfish.org>2008-12-27 09:42:59 +0000
commit5f75598c2804380ca07a5d52a411b5b64ba57fc3 (patch)
tree668e806888d49e1f32b026b5f7b83bb8c6e733d4 /tests
parent5677ae35ba3e4b617c590b46f0f224a15f924e5b (diff)
Introduce a simple timer API....
One can use add_timer or schedule_timer to add a timer. After the timeout time has been reached the callback will be called. One can call add_time/schedule_timer and del_timer from within the callback.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/timer/Makefile.am5
-rw-r--r--tests/timer/timer_test.c62
3 files changed, 68 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 000000000..f7cfd549f
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = timer
diff --git a/tests/timer/Makefile.am b/tests/timer/Makefile.am
new file mode 100644
index 000000000..726fb7ff2
--- /dev/null
+++ b/tests/timer/Makefile.am
@@ -0,0 +1,5 @@
+INCLUDES = $(all_includes) -I$(top_srcdir)/include
+bin_PROGRAMS = timer_test
+
+timer_test_SOURCES = timer_test.c $(top_srcdir)/src/timer.c $(top_srcdir)/src/select.c
+
diff --git a/tests/timer/timer_test.c b/tests/timer/timer_test.c
new file mode 100644
index 000000000..4d98aef42
--- /dev/null
+++ b/tests/timer/timer_test.c
@@ -0,0 +1,62 @@
+/*
+ * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdio.h>
+
+#include <openbsc/timer.h>
+#include <openbsc/select.h>
+
+static void timer_fired(unsigned long data);
+
+static struct timer_list timer_one = {
+ .cb = timer_fired,
+ .data = (void*)1,
+};
+
+static struct timer_list timer_two = {
+ .cb = timer_fired,
+ .data = (void*)2,
+};
+
+static void timer_fired(unsigned long data)
+{
+ printf("Fired timer: %lu\n", data);
+
+ if (data == 1) {
+ schedule_timer(&timer_one, 3, 0);
+ del_timer(&timer_two);
+ } else if (data == 2) {
+ printf("Should not be fired... bug in del_timer\n");
+ } else {
+ printf("wtf... wrong data\n");
+ }
+}
+
+int main(int argc, char** argv)
+{
+ printf("Starting... timer\n");
+
+ schedule_timer(&timer_one, 3, 0);
+ schedule_timer(&timer_two, 5, 0);
+
+ while (1) {
+ bsc_select_main();
+ }
+}