summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/kernel-unix.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/kernel-unix.c')
-rw-r--r--firmware/target/hosted/kernel-unix.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/firmware/target/hosted/kernel-unix.c b/firmware/target/hosted/kernel-unix.c
new file mode 100644
index 0000000000..a0167d46c5
--- /dev/null
+++ b/firmware/target/hosted/kernel-unix.c
@@ -0,0 +1,168 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (c) 2010 Thomas Martitz
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22
23#include <stdlib.h>
24#include <time.h>
25#include <signal.h>
26#include <errno.h>
27#include <unistd.h>
28#include <pthread.h>
29#include "config.h"
30#include "system.h"
31#include "button.h"
32#include "audio.h"
33#include "panic.h"
34#include "timer.h"
35
36
37static pthread_cond_t wfi_cond = PTHREAD_COND_INITIALIZER;
38static pthread_mutex_t wfi_mtx = PTHREAD_MUTEX_INITIALIZER;
39/*
40 * call tick tasks and wake the scheduler up */
41void timer_signal(union sigval arg)
42{
43 (void)arg;
44 call_tick_tasks();
45 interrupt();
46}
47
48/*
49 * wait on the sem which the signal handler posts to save cpu time (aka sleep)
50 *
51 * other mechanisms could use them as well */
52void wait_for_interrupt(void)
53{
54 pthread_cond_wait(&wfi_cond, &wfi_mtx);
55}
56
57/*
58 * Wakeup the kernel, if sleeping (shall not be called from a signal handler) */
59void interrupt(void)
60{
61 pthread_cond_signal(&wfi_cond);
62}
63
64
65/*
66 * setup a hrtimer to send a signal to our process every tick
67 *
68 * WARNING for Android: JNI calls are not permitted from tick tasks, as the
69 * underlying thread is not attached to the Java VM
70 *
71 * Can be possibly be attached if it really needs to be. but let's
72 * keep this leightweight
73 */
74
75void tick_start(unsigned int interval_in_ms)
76{
77 int ret = 0;
78 timer_t timerid;
79 struct itimerspec ts;
80 sigevent_t sigev;
81
82 /* initializing in the declaration causes some weird warnings */
83 memset(&sigev, 0, sizeof(sigevent_t));
84 sigev.sigev_notify = SIGEV_THREAD,
85 sigev.sigev_notify_function = timer_signal;
86
87 ts.it_value.tv_sec = ts.it_interval.tv_sec = 0;
88 ts.it_value.tv_nsec = ts.it_interval.tv_nsec = interval_in_ms*1000*1000;
89
90 /* add the timer */
91 ret |= timer_create(CLOCK_REALTIME, &sigev, &timerid);
92 ret |= timer_settime(timerid, 0, &ts, NULL);
93
94 /* Grab the mutex already now and leave it to this thread. We don't
95 * care about race conditions when signaling the condition (because
96 * they are not critical), but a mutex is necessary due to the API */
97 pthread_mutex_lock(&wfi_mtx);
98
99 if (ret != 0)
100 panicf("%s(): %s\n", __func__, strerror(errno));
101}
102
103#define cycles_to_microseconds(cycles) \
104 ((int)((1000000*cycles)/TIMER_FREQ))
105
106
107static timer_t timer_tid;
108static int timer_prio = -1;
109void (*global_unreg_callback)(void);
110void (*global_timer_callback)(void);
111
112static void timer_cb(union sigval arg)
113{
114 (void)arg;
115 if (global_timer_callback)
116 global_timer_callback();
117}
118
119bool timer_register(int reg_prio, void (*unregister_callback)(void),
120 long cycles, void (*timer_callback)(void))
121{
122 int ret = 0;
123 struct itimerspec ts;
124 sigevent_t sigev;
125 long in_us = cycles_to_microseconds(cycles);
126
127 if (reg_prio <= timer_prio || in_us <= 0)
128 return false;
129
130 if (timer_prio >= 0 && global_unreg_callback)
131 global_unreg_callback();
132
133 memset(&sigev, 0, sizeof(sigevent_t));
134 sigev.sigev_notify = SIGEV_THREAD,
135 sigev.sigev_notify_function = timer_cb;
136
137 div_t q = div(in_us, 1000000);
138 ts.it_value.tv_sec = ts.it_interval.tv_sec = q.quot;
139 ts.it_value.tv_nsec = ts.it_interval.tv_nsec = q.rem*1000;
140
141 /* add the timer */
142 ret |= timer_create(CLOCK_REALTIME, &sigev, &timer_tid);
143 ret |= timer_settime(timer_tid, 0, &ts, NULL);
144
145 global_timer_callback = timer_callback;
146 global_unreg_callback = unregister_callback;
147 timer_prio = reg_prio;
148
149 return ret == 0;
150}
151
152bool timer_set_period(long cycles)
153{
154 struct itimerspec ts;
155 long in_us = cycles_to_microseconds(cycles);
156 div_t q = div(in_us, 1000000);
157 ts.it_value.tv_sec = ts.it_interval.tv_sec = q.quot;
158 ts.it_value.tv_nsec = ts.it_interval.tv_nsec = q.rem*1000;
159
160 return timer_settime(timer_tid, 0, &ts, NULL) == 0;
161}
162
163void timer_unregister(void)
164{
165 timer_delete(timer_tid);
166 timer_prio = -1;
167}
168