1 |
/// @file systimer.cc |
2 |
/// @author Francois Revol |
3 |
/// |
4 |
|
5 |
// |
6 |
// Copyright (c) 2004 Francois Revol |
7 |
// |
8 |
// This program is free software; you can redistribute it and/or modify |
9 |
// it under the terms of the GNU General Public License version 2 as |
10 |
// published by the Free Software Foundation. |
11 |
// |
12 |
// This program is distributed in the hope that it will be useful, |
13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
// GNU General Public License for more details. |
16 |
// |
17 |
// You should have received a copy of the GNU General Public License |
18 |
// along with this program; if not, write to the Free Software |
19 |
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
// |
21 |
|
22 |
#include <signal.h> |
23 |
#include <time.h> |
24 |
#include <stdio.h> |
25 |
#include <string.h> |
26 |
#include <OS.h> |
27 |
#include "system/systimer.h" |
28 |
|
29 |
static void signal_handler(int signo, void *sa_userdata/*, regs*/); |
30 |
static const int kTimerSignal = SIGUSR2;//SIGRTMIN; |
31 |
|
32 |
struct sys_timer_struct |
33 |
{ |
34 |
sys_timer_callback callback; |
35 |
thread_id thread; |
36 |
thread_id target; |
37 |
bigtime_t period; |
38 |
bool periodic; |
39 |
|
40 |
sys_timer_struct(sys_timer_callback cb) |
41 |
: callback(cb), thread(-1), target(-1), period(0LL), periodic(false) |
42 |
{ |
43 |
} |
44 |
}; |
45 |
|
46 |
static void signal_handler(int signo, void *sa_userdata/*, siginfo_t *extra, void *junk*/) |
47 |
{ |
48 |
sys_timer_struct *timer = reinterpret_cast<sys_timer_struct *>(sa_userdata); |
49 |
timer->callback(reinterpret_cast<sys_timer>(timer)); |
50 |
} |
51 |
|
52 |
static int32 timer_thread(void *_arg) |
53 |
{ |
54 |
sys_timer_struct *timer = (sys_timer_struct *)_arg; |
55 |
do { |
56 |
snooze(timer->period); |
57 |
kill(timer->target, kTimerSignal); |
58 |
} while (timer->periodic); |
59 |
return B_OK; |
60 |
} |
61 |
|
62 |
bool sys_create_timer(sys_timer *t, sys_timer_callback cb_func) |
63 |
{ |
64 |
struct sigaction act; |
65 |
|
66 |
sigemptyset(&act.sa_mask); |
67 |
act.sa_handler = (__signal_func_ptr)signal_handler; // POSIX SUX |
68 |
//act.sa_flags = SA_SIGINFO; |
69 |
|
70 |
*t = 0; |
71 |
if (sigemptyset(&act.sa_mask) == -1) { |
72 |
perror("Error calling sigemptyset"); |
73 |
return false; |
74 |
} |
75 |
|
76 |
sys_timer_struct *newTimer = new sys_timer_struct( cb_func ); |
77 |
act.sa_userdata = newTimer; |
78 |
|
79 |
if (sigaction(kTimerSignal, &act, 0) == -1) { |
80 |
perror("Error calling sigaction"); |
81 |
delete newTimer; |
82 |
return false; |
83 |
} |
84 |
|
85 |
*t = reinterpret_cast<sys_timer>(newTimer); |
86 |
|
87 |
return true; |
88 |
} |
89 |
|
90 |
void sys_delete_timer(sys_timer t) |
91 |
{ |
92 |
sys_timer_struct *timer = reinterpret_cast<sys_timer_struct *>(t); |
93 |
|
94 |
if (timer->thread > B_OK) { |
95 |
status_t err; |
96 |
kill_thread(timer->thread); |
97 |
wait_for_thread(timer->thread, &err); |
98 |
timer->thread = -1; |
99 |
} |
100 |
delete timer; |
101 |
} |
102 |
|
103 |
static inline long long int toUSecs(time_t secs, long int nanosecs) |
104 |
{ |
105 |
return secs * 1000 * 1000 + nanosecs / 1000; |
106 |
} |
107 |
|
108 |
void sys_set_timer(sys_timer t, time_t secs, long int nanosecs, bool periodic) |
109 |
{ |
110 |
sys_timer_struct *timer = reinterpret_cast<sys_timer_struct *>(t); |
111 |
//fprintf(stderr, "%s(%p, %lds %ldns, %s)\n", __FUNCTION__, timer, secs, nanosecs, periodic?"t":"f"); |
112 |
timer->periodic = periodic; |
113 |
timer->period = toUSecs(secs, nanosecs); |
114 |
timer->target = find_thread(NULL); |
115 |
if (timer->thread > B_OK) { |
116 |
status_t err; |
117 |
kill_thread(timer->thread); |
118 |
wait_for_thread(timer->thread, &err); |
119 |
} |
120 |
timer->thread = spawn_thread(timer_thread, "timer_thread", B_DISPLAY_PRIORITY, (void *)timer); |
121 |
if (timer->thread >= B_OK) { |
122 |
resume_thread(timer->thread); |
123 |
} // else handle error ??? |
124 |
} |
125 |
|
126 |
uint64 sys_get_timer_resolution(sys_timer t) |
127 |
{ |
128 |
return 3*1000; // [ns] something like that. |
129 |
} |
130 |
|
131 |
uint64 sys_get_hiresclk_ticks() |
132 |
{ |
133 |
// so simple :p |
134 |
// on x86 it does use rdtsc, and the cpu counter on ppc. |
135 |
return system_time(); |
136 |
} |
137 |
|
138 |
uint64 sys_get_hiresclk_ticks_per_second() |
139 |
{ |
140 |
return 1000000ULL; |
141 |
} |