summaryrefslogtreecommitdiff
path: root/third_party/ptpd-1.1.0/src/dep/timer.c
diff options
context:
space:
mode:
authorYuval Adam <yuv.adm@gmail.com>2014-08-08 14:42:07 +0300
committerYuval Adam <yuv.adm@gmail.com>2014-08-08 14:42:07 +0300
commita6163888f3c56123b1db313743c6147ba498732c (patch)
treeff7d15d991d1d09ba6cbc0cec80924f57445bfdd /third_party/ptpd-1.1.0/src/dep/timer.c
parentc3e4c9a25c2910d2d66d52215b3406b13d5b23d5 (diff)
Add third_party libs
Diffstat (limited to 'third_party/ptpd-1.1.0/src/dep/timer.c')
-rw-r--r--third_party/ptpd-1.1.0/src/dep/timer.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/third_party/ptpd-1.1.0/src/dep/timer.c b/third_party/ptpd-1.1.0/src/dep/timer.c
new file mode 100644
index 0000000..4bd7f80
--- /dev/null
+++ b/third_party/ptpd-1.1.0/src/dep/timer.c
@@ -0,0 +1,96 @@
+/**
+ * @file timer.c
+ * @date Wed Jun 23 09:41:26 2010
+ *
+ * @brief The timers which run the state machine.
+ *
+ * Timers in the PTP daemon are run off of the signal system.
+ */
+
+#include "../ptpd.h"
+
+#define TIMER_INTERVAL 1
+int elapsed;
+
+void
+catch_alarm(int sig)
+{
+ elapsed += TIMER_INTERVAL;
+
+ DBGV("catch_alarm: elapsed %d\n", elapsed);
+}
+
+void
+initTimer(void)
+{
+ struct itimerval itimer;
+
+ DBG("initTimer\n");
+
+ signal(SIGALRM, SIG_IGN);
+
+ elapsed = 0;
+ itimer.it_value.tv_sec = itimer.it_interval.tv_sec = TIMER_INTERVAL;
+ itimer.it_value.tv_usec = itimer.it_interval.tv_usec = 0;
+
+ signal(SIGALRM, catch_alarm);
+ setitimer(ITIMER_REAL, &itimer, 0);
+}
+
+void
+timerUpdate(IntervalTimer * itimer)
+{
+ int i, delta;
+
+ delta = elapsed;
+ elapsed = 0;
+
+ if (delta <= 0)
+ return;
+
+ for (i = 0; i < TIMER_ARRAY_SIZE; ++i) {
+ if (itimer[i].interval > 0 && (itimer[i].left -= delta) <= 0) {
+ itimer[i].left = itimer[i].interval;
+ itimer[i].expire = TRUE;
+ DBGV("timerUpdate: timer %u expired\n", i);
+ }
+ }
+}
+
+void
+timerStop(UInteger16 index, IntervalTimer * itimer)
+{
+ if (index >= TIMER_ARRAY_SIZE)
+ return;
+
+ itimer[index].interval = 0;
+}
+
+void
+timerStart(UInteger16 index, UInteger16 interval, IntervalTimer * itimer)
+{
+ if (index >= TIMER_ARRAY_SIZE)
+ return;
+
+ itimer[index].expire = FALSE;
+ itimer[index].left = interval;
+ itimer[index].interval = itimer[index].left;
+
+ DBGV("timerStart: set timer %d to %d\n", index, interval);
+}
+
+Boolean
+timerExpired(UInteger16 index, IntervalTimer * itimer)
+{
+ timerUpdate(itimer);
+
+ if (index >= TIMER_ARRAY_SIZE)
+ return FALSE;
+
+ if (!itimer[index].expire)
+ return FALSE;
+
+ itimer[index].expire = FALSE;
+
+ return TRUE;
+}