1 | /* $Id: timer-posix.cpp 39093 2011-10-24 14:01:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Timer, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Defined Constants And Macros *
|
---|
29 | *******************************************************************************/
|
---|
30 | /** Enables the use of POSIX RT timers. */
|
---|
31 | #ifndef RT_OS_SOLARIS /* Solaris 10 doesn't have SIGEV_THREAD */
|
---|
32 | # define IPRT_WITH_POSIX_TIMERS
|
---|
33 | #endif /* !RT_OS_SOLARIS */
|
---|
34 |
|
---|
35 | /** @def RT_TIMER_SIGNAL
|
---|
36 | * The signal number that the timers use.
|
---|
37 | * We currently use SIGALRM for both setitimer and posix real time timers
|
---|
38 | * out of simplicity, but we might want change this later for the posix ones. */
|
---|
39 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
40 | # define RT_TIMER_SIGNAL SIGALRM
|
---|
41 | #else
|
---|
42 | # define RT_TIMER_SIGNAL SIGALRM
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Header Files *
|
---|
48 | *******************************************************************************/
|
---|
49 | #define LOG_GROUP RTLOGGROUP_TIMER
|
---|
50 | #include <iprt/timer.h>
|
---|
51 | #include <iprt/alloc.h>
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/thread.h>
|
---|
54 | #include <iprt/log.h>
|
---|
55 | #include <iprt/asm.h>
|
---|
56 | #include <iprt/semaphore.h>
|
---|
57 | #include <iprt/string.h>
|
---|
58 | #include <iprt/once.h>
|
---|
59 | #include <iprt/err.h>
|
---|
60 | #include <iprt/critsect.h>
|
---|
61 | #include "internal/magics.h"
|
---|
62 |
|
---|
63 | #include <unistd.h>
|
---|
64 | #include <sys/fcntl.h>
|
---|
65 | #include <sys/ioctl.h>
|
---|
66 | #ifdef RT_OS_LINUX
|
---|
67 | # include <linux/rtc.h>
|
---|
68 | #endif
|
---|
69 | #include <sys/time.h>
|
---|
70 | #include <signal.h>
|
---|
71 | #include <errno.h>
|
---|
72 | #include <pthread.h>
|
---|
73 |
|
---|
74 |
|
---|
75 | /*******************************************************************************
|
---|
76 | * Global Variables *
|
---|
77 | *******************************************************************************/
|
---|
78 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
79 | /** Init the critsect on first call. */
|
---|
80 | static RTONCE g_TimerOnce = RTONCE_INITIALIZER;
|
---|
81 | /** Global critsect that serializes timer creation and destruction.
|
---|
82 | * This is lazily created on the first RTTimerCreateEx call and will not be
|
---|
83 | * freed up (I'm afraid). */
|
---|
84 | static RTCRITSECT g_TimerCritSect;
|
---|
85 | /**
|
---|
86 | * Global counter of RTTimer instances. The signal thread is
|
---|
87 | * started when it changes from 0 to 1. The signal thread
|
---|
88 | * terminates when it becomes 0 again.
|
---|
89 | */
|
---|
90 | static uint32_t volatile g_cTimerInstances;
|
---|
91 | /** The signal handling thread. */
|
---|
92 | static RTTHREAD g_TimerThread;
|
---|
93 | #endif /* IPRT_WITH_POSIX_TIMERS */
|
---|
94 |
|
---|
95 |
|
---|
96 | /*******************************************************************************
|
---|
97 | * Structures and Typedefs *
|
---|
98 | *******************************************************************************/
|
---|
99 | /**
|
---|
100 | * The internal representation of a timer handle.
|
---|
101 | */
|
---|
102 | typedef struct RTTIMER
|
---|
103 | {
|
---|
104 | /** Magic.
|
---|
105 | * This is RTTIMER_MAGIC, but changes to something else before the timer
|
---|
106 | * is destroyed to indicate clearly that thread should exit. */
|
---|
107 | uint32_t volatile u32Magic;
|
---|
108 | /** Flag indicating the timer is suspended. */
|
---|
109 | uint8_t volatile fSuspended;
|
---|
110 | /** Flag indicating that the timer has been destroyed. */
|
---|
111 | uint8_t volatile fDestroyed;
|
---|
112 | #ifndef IPRT_WITH_POSIX_TIMERS /** @todo We have to take the signals on a dedicated timer thread as
|
---|
113 | * we (might) have code assuming that signals doesn't screw around
|
---|
114 | * on existing threads. (It would be sufficient to have one thread
|
---|
115 | * per signal of course since the signal will be masked while it's
|
---|
116 | * running, however, it may just cause more complications than its
|
---|
117 | * worth - sigwait/sigwaitinfo work atomically anyway...)
|
---|
118 | * Also, must block the signal in the thread main procedure too. */
|
---|
119 | /** The timer thread. */
|
---|
120 | RTTHREAD Thread;
|
---|
121 | /** Event semaphore on which the thread is blocked. */
|
---|
122 | RTSEMEVENT Event;
|
---|
123 | #endif /* !IPRT_WITH_POSIX_TIMERS */
|
---|
124 | /** User argument. */
|
---|
125 | void *pvUser;
|
---|
126 | /** Callback. */
|
---|
127 | PFNRTTIMER pfnTimer;
|
---|
128 | /** The timer interval. 0 if one-shot. */
|
---|
129 | uint64_t u64NanoInterval;
|
---|
130 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
131 | /** The first shot interval. 0 if ASAP. */
|
---|
132 | uint64_t volatile u64NanoFirst;
|
---|
133 | #endif /* !IPRT_WITH_POSIX_TIMERS */
|
---|
134 | /** The current timer tick. */
|
---|
135 | uint64_t volatile iTick;
|
---|
136 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
137 | /** The error/status of the timer.
|
---|
138 | * Initially -1, set to 0 when the timer have been successfully started, and
|
---|
139 | * to errno on failure in starting the timer. */
|
---|
140 | int volatile iError;
|
---|
141 | #else /* IPRT_WITH_POSIX_TIMERS */
|
---|
142 | timer_t NativeTimer;
|
---|
143 | #endif /* IPRT_WITH_POSIX_TIMERS */
|
---|
144 |
|
---|
145 | } RTTIMER;
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * RTOnce callback that initializes the critical section.
|
---|
153 | *
|
---|
154 | * @returns RTCritSectInit return code.
|
---|
155 | * @param pvUser1 NULL, ignored.
|
---|
156 | * @param pvUser2 NULL, ignored.
|
---|
157 | *
|
---|
158 | */
|
---|
159 | static DECLCALLBACK(int) rtTimerOnce(void *pvUser1, void *pvUser2)
|
---|
160 | {
|
---|
161 | NOREF(pvUser1);
|
---|
162 | NOREF(pvUser2);
|
---|
163 | return RTCritSectInit(&g_TimerCritSect);
|
---|
164 | }
|
---|
165 | #endif
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Signal handler which ignore everything it gets.
|
---|
170 | *
|
---|
171 | * @param iSignal The signal number.
|
---|
172 | */
|
---|
173 | static void rttimerSignalIgnore(int iSignal)
|
---|
174 | {
|
---|
175 | //AssertBreakpoint();
|
---|
176 | NOREF(iSignal);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * RT_TIMER_SIGNAL wait thread.
|
---|
182 | */
|
---|
183 | static DECLCALLBACK(int) rttimerThread(RTTHREAD hThreadSelf, void *pvArg)
|
---|
184 | {
|
---|
185 | NOREF(hThreadSelf); NOREF(pvArg);
|
---|
186 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
187 | PRTTIMER pTimer = (PRTTIMER)pvArg;
|
---|
188 | RTTIMER Timer = *pTimer;
|
---|
189 | Assert(pTimer->u32Magic == RTTIMER_MAGIC);
|
---|
190 | #endif /* !IPRT_WITH_POSIX_TIMERS */
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Install signal handler.
|
---|
194 | */
|
---|
195 | struct sigaction SigAct;
|
---|
196 | memset(&SigAct, 0, sizeof(SigAct));
|
---|
197 | SigAct.sa_flags = SA_RESTART;
|
---|
198 | sigemptyset(&SigAct.sa_mask);
|
---|
199 | SigAct.sa_handler = rttimerSignalIgnore;
|
---|
200 | if (sigaction(RT_TIMER_SIGNAL, &SigAct, NULL))
|
---|
201 | {
|
---|
202 | SigAct.sa_flags &= ~SA_RESTART;
|
---|
203 | if (sigaction(RT_TIMER_SIGNAL, &SigAct, NULL))
|
---|
204 | AssertMsgFailed(("sigaction failed, errno=%d\n", errno));
|
---|
205 | }
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Mask most signals except those which might be used by the pthread implementation (linux).
|
---|
209 | */
|
---|
210 | sigset_t SigSet;
|
---|
211 | sigfillset(&SigSet);
|
---|
212 | sigdelset(&SigSet, SIGTERM);
|
---|
213 | sigdelset(&SigSet, SIGHUP);
|
---|
214 | sigdelset(&SigSet, SIGINT);
|
---|
215 | sigdelset(&SigSet, SIGABRT);
|
---|
216 | sigdelset(&SigSet, SIGKILL);
|
---|
217 | #ifdef SIGRTMIN
|
---|
218 | for (int iSig = SIGRTMIN; iSig < SIGRTMAX; iSig++)
|
---|
219 | sigdelset(&SigSet, iSig);
|
---|
220 | #endif
|
---|
221 | if (sigprocmask(SIG_SETMASK, &SigSet, NULL))
|
---|
222 | {
|
---|
223 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
224 | int rc = RTErrConvertFromErrno(errno);
|
---|
225 | #else
|
---|
226 | int rc = pTimer->iError = RTErrConvertFromErrno(errno);
|
---|
227 | #endif
|
---|
228 | AssertMsgFailed(("sigprocmask -> errno=%d\n", errno));
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * The work loop.
|
---|
234 | */
|
---|
235 | RTThreadUserSignal(hThreadSelf);
|
---|
236 |
|
---|
237 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
238 | while ( !pTimer->fDestroyed
|
---|
239 | && pTimer->u32Magic == RTTIMER_MAGIC)
|
---|
240 | {
|
---|
241 | /*
|
---|
242 | * Wait for a start or destroy event.
|
---|
243 | */
|
---|
244 | if (pTimer->fSuspended)
|
---|
245 | {
|
---|
246 | int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
|
---|
247 | if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
|
---|
248 | {
|
---|
249 | AssertRC(rc);
|
---|
250 | if (pTimer->fDestroyed)
|
---|
251 | continue;
|
---|
252 | RTThreadSleep(1000); /* Don't cause trouble! */
|
---|
253 | }
|
---|
254 | if ( pTimer->fSuspended
|
---|
255 | || pTimer->fDestroyed)
|
---|
256 | continue;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Start the timer.
|
---|
261 | *
|
---|
262 | * For some SunOS (/SysV?) threading compatibility Linux will only
|
---|
263 | * deliver the RT_TIMER_SIGNAL to the thread calling setitimer(). Therefore
|
---|
264 | * we have to call it here.
|
---|
265 | *
|
---|
266 | * It turns out this might not always be the case, see RT_TIMER_SIGNAL killing
|
---|
267 | * processes on RH 2.4.21.
|
---|
268 | */
|
---|
269 | struct itimerval TimerVal;
|
---|
270 | if (pTimer->u64NanoFirst)
|
---|
271 | {
|
---|
272 | uint64_t u64 = RT_MAX(1000, pTimer->u64NanoFirst);
|
---|
273 | TimerVal.it_value.tv_sec = u64 / 1000000000;
|
---|
274 | TimerVal.it_value.tv_usec = (u64 % 1000000000) / 1000;
|
---|
275 | }
|
---|
276 | else
|
---|
277 | {
|
---|
278 | TimerVal.it_value.tv_sec = 0;
|
---|
279 | TimerVal.it_value.tv_usec = 10;
|
---|
280 | }
|
---|
281 | if (pTimer->u64NanoInterval)
|
---|
282 | {
|
---|
283 | uint64_t u64 = RT_MAX(1000, pTimer->u64NanoInterval);
|
---|
284 | TimerVal.it_interval.tv_sec = u64 / 1000000000;
|
---|
285 | TimerVal.it_interval.tv_usec = (u64 % 1000000000) / 1000;
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | TimerVal.it_interval.tv_sec = 0;
|
---|
290 | TimerVal.it_interval.tv_usec = 0;
|
---|
291 | }
|
---|
292 |
|
---|
293 | if (setitimer(ITIMER_REAL, &TimerVal, NULL))
|
---|
294 | {
|
---|
295 | ASMAtomicXchgU8(&pTimer->fSuspended, true);
|
---|
296 | pTimer->iError = RTErrConvertFromErrno(errno);
|
---|
297 | RTThreadUserSignal(hThreadSelf);
|
---|
298 | continue; /* back to suspended mode. */
|
---|
299 | }
|
---|
300 | pTimer->iError = 0;
|
---|
301 | RTThreadUserSignal(hThreadSelf);
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Timer Service Loop.
|
---|
305 | */
|
---|
306 | sigemptyset(&SigSet);
|
---|
307 | sigaddset(&SigSet, RT_TIMER_SIGNAL);
|
---|
308 | do
|
---|
309 | {
|
---|
310 | siginfo_t SigInfo;
|
---|
311 | RT_ZERO(SigInfo);
|
---|
312 | #ifdef RT_OS_DARWIN
|
---|
313 | if (RT_LIKELY(sigwait(&SigSet, &SigInfo.si_signo) >= 0))
|
---|
314 | {
|
---|
315 | #else
|
---|
316 | if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
|
---|
317 | {
|
---|
318 | if (RT_LIKELY(SigInfo.si_signo == RT_TIMER_SIGNAL))
|
---|
319 | #endif
|
---|
320 | {
|
---|
321 | if (RT_UNLIKELY( pTimer->fSuspended
|
---|
322 | || pTimer->fDestroyed
|
---|
323 | || pTimer->u32Magic != RTTIMER_MAGIC))
|
---|
324 | break;
|
---|
325 |
|
---|
326 | pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
|
---|
327 |
|
---|
328 | /* auto suspend one-shot timers. */
|
---|
329 | if (RT_UNLIKELY(!pTimer->u64NanoInterval))
|
---|
330 | {
|
---|
331 | ASMAtomicWriteU8(&pTimer->fSuspended, true);
|
---|
332 | break;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 | else if (errno != EINTR)
|
---|
337 | AssertMsgFailed(("sigwaitinfo -> errno=%d\n", errno));
|
---|
338 | } while (RT_LIKELY( !pTimer->fSuspended
|
---|
339 | && !pTimer->fDestroyed
|
---|
340 | && pTimer->u32Magic == RTTIMER_MAGIC));
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * Disable the timer.
|
---|
344 | */
|
---|
345 | struct itimerval TimerVal2 = {{0,0}, {0,0}};
|
---|
346 | if (setitimer(ITIMER_REAL, &TimerVal2, NULL))
|
---|
347 | AssertMsgFailed(("setitimer(ITIMER_REAL,&{0}, NULL) failed, errno=%d\n", errno));
|
---|
348 |
|
---|
349 | /*
|
---|
350 | * ACK any pending suspend request.
|
---|
351 | */
|
---|
352 | if (!pTimer->fDestroyed)
|
---|
353 | {
|
---|
354 | pTimer->iError = 0;
|
---|
355 | RTThreadUserSignal(hThreadSelf);
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Exit.
|
---|
361 | */
|
---|
362 | pTimer->iError = 0;
|
---|
363 | RTThreadUserSignal(hThreadSelf);
|
---|
364 |
|
---|
365 | #else /* IPRT_WITH_POSIX_TIMERS */
|
---|
366 |
|
---|
367 | sigemptyset(&SigSet);
|
---|
368 | sigaddset(&SigSet, RT_TIMER_SIGNAL);
|
---|
369 | while (g_cTimerInstances)
|
---|
370 | {
|
---|
371 | siginfo_t SigInfo;
|
---|
372 | RT_ZERO(SigInfo);
|
---|
373 | if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
|
---|
374 | {
|
---|
375 | LogFlow(("rttimerThread: signo=%d pTimer=%p\n", SigInfo.si_signo, SigInfo.si_value.sival_ptr));
|
---|
376 | if (RT_LIKELY( SigInfo.si_signo == RT_TIMER_SIGNAL
|
---|
377 | && SigInfo.si_code == SI_TIMER)) /* The SI_TIMER check is *essential* because of the pthread_kill. */
|
---|
378 | {
|
---|
379 | PRTTIMER pTimer = (PRTTIMER)SigInfo.si_value.sival_ptr;
|
---|
380 | AssertPtr(pTimer);
|
---|
381 | if (RT_UNLIKELY( !VALID_PTR(pTimer)
|
---|
382 | || ASMAtomicUoReadU8(&pTimer->fSuspended)
|
---|
383 | || ASMAtomicUoReadU8(&pTimer->fDestroyed)
|
---|
384 | || pTimer->u32Magic != RTTIMER_MAGIC))
|
---|
385 | continue;
|
---|
386 |
|
---|
387 | pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
|
---|
388 |
|
---|
389 | /* auto suspend one-shot timers. */
|
---|
390 | if (RT_UNLIKELY(!pTimer->u64NanoInterval))
|
---|
391 | ASMAtomicWriteU8(&pTimer->fSuspended, true);
|
---|
392 | }
|
---|
393 | }
|
---|
394 | }
|
---|
395 | #endif /* IPRT_WITH_POSIX_TIMERS */
|
---|
396 |
|
---|
397 | return VINF_SUCCESS;
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 | RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
|
---|
402 | {
|
---|
403 | /*
|
---|
404 | * We don't support the fancy MP features.
|
---|
405 | */
|
---|
406 | if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
407 | return VERR_NOT_SUPPORTED;
|
---|
408 |
|
---|
409 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
410 | /*
|
---|
411 | * Check if timer is busy.
|
---|
412 | */
|
---|
413 | struct itimerval TimerVal;
|
---|
414 | if (getitimer(ITIMER_REAL, &TimerVal))
|
---|
415 | {
|
---|
416 | AssertMsgFailed(("getitimer() -> errno=%d\n", errno));
|
---|
417 | return VERR_NOT_IMPLEMENTED;
|
---|
418 | }
|
---|
419 | if ( TimerVal.it_value.tv_usec
|
---|
420 | || TimerVal.it_value.tv_sec
|
---|
421 | || TimerVal.it_interval.tv_usec
|
---|
422 | || TimerVal.it_interval.tv_sec)
|
---|
423 | {
|
---|
424 | AssertMsgFailed(("A timer is running. System limit is one timer per process!\n"));
|
---|
425 | return VERR_TIMER_BUSY;
|
---|
426 | }
|
---|
427 | #endif /* !IPRT_WITH_POSIX_TIMERS */
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Block RT_TIMER_SIGNAL from calling thread.
|
---|
431 | */
|
---|
432 | sigset_t SigSet;
|
---|
433 | sigemptyset(&SigSet);
|
---|
434 | sigaddset(&SigSet, RT_TIMER_SIGNAL);
|
---|
435 | sigprocmask(SIG_BLOCK, &SigSet, NULL);
|
---|
436 |
|
---|
437 | #ifndef IPRT_WITH_POSIX_TIMERS /** @todo combine more of the setitimer/timer_create code. setitimer could also use the global thread. */
|
---|
438 | /** @todo Move this RTC hack else where... */
|
---|
439 | static bool fDoneRTC;
|
---|
440 | if (!fDoneRTC)
|
---|
441 | {
|
---|
442 | fDoneRTC = true;
|
---|
443 | /* check resolution. */
|
---|
444 | TimerVal.it_interval.tv_sec = 0;
|
---|
445 | TimerVal.it_interval.tv_usec = 1000;
|
---|
446 | TimerVal.it_value = TimerVal.it_interval;
|
---|
447 | if ( setitimer(ITIMER_REAL, &TimerVal, NULL)
|
---|
448 | || getitimer(ITIMER_REAL, &TimerVal)
|
---|
449 | || TimerVal.it_interval.tv_usec > 1000)
|
---|
450 | {
|
---|
451 | /*
|
---|
452 | * Try open /dev/rtc to set the irq rate to 1024 and
|
---|
453 | * turn periodic
|
---|
454 | */
|
---|
455 | Log(("RTTimerCreate: interval={%ld,%ld} trying to adjust /dev/rtc!\n", TimerVal.it_interval.tv_sec, TimerVal.it_interval.tv_usec));
|
---|
456 | # ifdef RT_OS_LINUX
|
---|
457 | int fh = open("/dev/rtc", O_RDONLY);
|
---|
458 | if (fh >= 0)
|
---|
459 | {
|
---|
460 | if ( ioctl(fh, RTC_IRQP_SET, 1024) < 0
|
---|
461 | || ioctl(fh, RTC_PIE_ON, 0) < 0)
|
---|
462 | Log(("RTTimerCreate: couldn't configure rtc! errno=%d\n", errno));
|
---|
463 | ioctl(fh, F_SETFL, O_ASYNC);
|
---|
464 | ioctl(fh, F_SETOWN, getpid());
|
---|
465 | /* not so sure if closing it is a good idea... */
|
---|
466 | //close(fh);
|
---|
467 | }
|
---|
468 | else
|
---|
469 | Log(("RTTimerCreate: couldn't configure rtc! open failed with errno=%d\n", errno));
|
---|
470 | # endif
|
---|
471 | }
|
---|
472 | /* disable it */
|
---|
473 | TimerVal.it_interval.tv_sec = 0;
|
---|
474 | TimerVal.it_interval.tv_usec = 0;
|
---|
475 | TimerVal.it_value = TimerVal.it_interval;
|
---|
476 | setitimer(ITIMER_REAL, &TimerVal, NULL);
|
---|
477 | }
|
---|
478 |
|
---|
479 | /*
|
---|
480 | * Create a new timer.
|
---|
481 | */
|
---|
482 | int rc;
|
---|
483 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
484 | if (pTimer)
|
---|
485 | {
|
---|
486 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
487 | pTimer->fSuspended = true;
|
---|
488 | pTimer->fDestroyed = false;
|
---|
489 | pTimer->Thread = NIL_RTTHREAD;
|
---|
490 | pTimer->Event = NIL_RTSEMEVENT;
|
---|
491 | pTimer->pfnTimer = pfnTimer;
|
---|
492 | pTimer->pvUser = pvUser;
|
---|
493 | pTimer->u64NanoInterval = u64NanoInterval;
|
---|
494 | pTimer->u64NanoFirst = 0;
|
---|
495 | pTimer->iTick = 0;
|
---|
496 | pTimer->iError = 0;
|
---|
497 | rc = RTSemEventCreate(&pTimer->Event);
|
---|
498 | AssertRC(rc);
|
---|
499 | if (RT_SUCCESS(rc))
|
---|
500 | {
|
---|
501 | rc = RTThreadCreate(&pTimer->Thread, rttimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
|
---|
502 | AssertRC(rc);
|
---|
503 | if (RT_SUCCESS(rc))
|
---|
504 | {
|
---|
505 | /*
|
---|
506 | * Wait for the timer thread to initialize it self.
|
---|
507 | * This might take a little while...
|
---|
508 | */
|
---|
509 | rc = RTThreadUserWait(pTimer->Thread, 45*1000);
|
---|
510 | AssertRC(rc);
|
---|
511 | if (RT_SUCCESS(rc))
|
---|
512 | {
|
---|
513 | rc = RTThreadUserReset(pTimer->Thread); AssertRC(rc);
|
---|
514 | rc = pTimer->iError;
|
---|
515 | AssertRC(rc);
|
---|
516 | if (RT_SUCCESS(rc))
|
---|
517 | {
|
---|
518 | RTThreadYield(); /* <-- Horrible hack to make tstTimer work. (linux 2.6.12) */
|
---|
519 | *ppTimer = pTimer;
|
---|
520 | return VINF_SUCCESS;
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | /* bail out */
|
---|
525 | ASMAtomicXchgU8(&pTimer->fDestroyed, true);
|
---|
526 | ASMAtomicXchgU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
|
---|
527 | RTThreadWait(pTimer->Thread, 45*1000, NULL);
|
---|
528 | }
|
---|
529 | RTSemEventDestroy(pTimer->Event);
|
---|
530 | pTimer->Event = NIL_RTSEMEVENT;
|
---|
531 | }
|
---|
532 | RTMemFree(pTimer);
|
---|
533 | }
|
---|
534 | else
|
---|
535 | rc = VERR_NO_MEMORY;
|
---|
536 |
|
---|
537 | #else /* IPRT_WITH_POSIX_TIMERS */
|
---|
538 |
|
---|
539 | /*
|
---|
540 | * Do the global init first.
|
---|
541 | */
|
---|
542 | int rc = RTOnce(&g_TimerOnce, rtTimerOnce, NULL, NULL);
|
---|
543 | if (RT_FAILURE(rc))
|
---|
544 | return rc;
|
---|
545 |
|
---|
546 | /*
|
---|
547 | * Create a new timer structure.
|
---|
548 | */
|
---|
549 | LogFlow(("RTTimerCreateEx: u64NanoInterval=%llu fFlags=%lu\n", u64NanoInterval, fFlags));
|
---|
550 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
551 | if (pTimer)
|
---|
552 | {
|
---|
553 | /* Initialize timer structure. */
|
---|
554 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
555 | pTimer->fSuspended = true;
|
---|
556 | pTimer->fDestroyed = false;
|
---|
557 | pTimer->pfnTimer = pfnTimer;
|
---|
558 | pTimer->pvUser = pvUser;
|
---|
559 | pTimer->u64NanoInterval = u64NanoInterval;
|
---|
560 | pTimer->iTick = 0;
|
---|
561 |
|
---|
562 | /*
|
---|
563 | * Create a timer that deliver RT_TIMER_SIGNAL upon timer expiration.
|
---|
564 | */
|
---|
565 | struct sigevent SigEvt;
|
---|
566 | SigEvt.sigev_notify = SIGEV_SIGNAL;
|
---|
567 | SigEvt.sigev_signo = RT_TIMER_SIGNAL;
|
---|
568 | SigEvt.sigev_value.sival_ptr = pTimer; /* sigev_value gets copied to siginfo. */
|
---|
569 | int err = timer_create(CLOCK_REALTIME, &SigEvt, &pTimer->NativeTimer);
|
---|
570 | if (!err)
|
---|
571 | {
|
---|
572 | /*
|
---|
573 | * Increment the timer count, do this behind the critsect to avoid races.
|
---|
574 | */
|
---|
575 | RTCritSectEnter(&g_TimerCritSect);
|
---|
576 |
|
---|
577 | if (ASMAtomicIncU32(&g_cTimerInstances) != 1)
|
---|
578 | {
|
---|
579 | Assert(g_cTimerInstances > 1);
|
---|
580 | RTCritSectLeave(&g_TimerCritSect);
|
---|
581 |
|
---|
582 | LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
|
---|
583 | *ppTimer = pTimer;
|
---|
584 | return VINF_SUCCESS;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /*
|
---|
588 | * Create the signal handling thread. It will wait for the signal
|
---|
589 | * and execute the timer functions.
|
---|
590 | */
|
---|
591 | rc = RTThreadCreate(&g_TimerThread, rttimerThread, NULL, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
|
---|
592 | if (RT_SUCCESS(rc))
|
---|
593 | {
|
---|
594 | rc = RTThreadUserWait(g_TimerThread, 45*1000); /* this better not fail... */
|
---|
595 | if (RT_SUCCESS(rc))
|
---|
596 | {
|
---|
597 | RTCritSectLeave(&g_TimerCritSect);
|
---|
598 |
|
---|
599 | LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
|
---|
600 | *ppTimer = pTimer;
|
---|
601 | return VINF_SUCCESS;
|
---|
602 | }
|
---|
603 | /* darn, what do we do here? */
|
---|
604 | }
|
---|
605 |
|
---|
606 | /* bail out */
|
---|
607 | ASMAtomicDecU32(&g_cTimerInstances);
|
---|
608 | Assert(!g_cTimerInstances);
|
---|
609 |
|
---|
610 | RTCritSectLeave(&g_TimerCritSect);
|
---|
611 |
|
---|
612 | timer_delete(pTimer->NativeTimer);
|
---|
613 | }
|
---|
614 | else
|
---|
615 | {
|
---|
616 | rc = RTErrConvertFromErrno(err);
|
---|
617 | Log(("RTTimerCreateEx: err=%d (%Rrc)\n", err, rc));
|
---|
618 | }
|
---|
619 |
|
---|
620 | RTMemFree(pTimer);
|
---|
621 | }
|
---|
622 | else
|
---|
623 | rc = VERR_NO_MEMORY;
|
---|
624 |
|
---|
625 | #endif /* IPRT_WITH_POSIX_TIMERS */
|
---|
626 | return rc;
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | RTR3DECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
631 | {
|
---|
632 | LogFlow(("RTTimerDestroy: pTimer=%p\n", pTimer));
|
---|
633 |
|
---|
634 | /*
|
---|
635 | * Validate input.
|
---|
636 | */
|
---|
637 | /* NULL is ok. */
|
---|
638 | if (!pTimer)
|
---|
639 | return VINF_SUCCESS;
|
---|
640 | int rc = VINF_SUCCESS;
|
---|
641 | AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
|
---|
642 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
|
---|
643 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
644 | AssertReturn(g_TimerThread != RTThreadSelf(), VERR_INTERNAL_ERROR);
|
---|
645 | #else
|
---|
646 | AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
|
---|
647 | #endif
|
---|
648 |
|
---|
649 | /*
|
---|
650 | * Mark the semaphore as destroyed.
|
---|
651 | */
|
---|
652 | ASMAtomicWriteU8(&pTimer->fDestroyed, true);
|
---|
653 | ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
|
---|
654 |
|
---|
655 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
656 | /*
|
---|
657 | * Suspend the timer if it's running.
|
---|
658 | */
|
---|
659 | if (pTimer->fSuspended)
|
---|
660 | {
|
---|
661 | struct itimerspec TimerSpec;
|
---|
662 | TimerSpec.it_value.tv_sec = 0;
|
---|
663 | TimerSpec.it_value.tv_nsec = 0;
|
---|
664 | int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL); NOREF(err);
|
---|
665 | AssertMsg(!err, ("%d\n", err));
|
---|
666 | }
|
---|
667 | #endif
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * Poke the thread and wait for it to finish.
|
---|
671 | * This is only done for the last timer when using posix timers.
|
---|
672 | */
|
---|
673 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
674 | RTTHREAD Thread = NIL_RTTHREAD;
|
---|
675 | RTCritSectEnter(&g_TimerCritSect);
|
---|
676 | if (ASMAtomicDecU32(&g_cTimerInstances) == 0)
|
---|
677 | {
|
---|
678 | Thread = g_TimerThread;
|
---|
679 | g_TimerThread = NIL_RTTHREAD;
|
---|
680 | }
|
---|
681 | RTCritSectLeave(&g_TimerCritSect);
|
---|
682 | #else /* IPRT_WITH_POSIX_TIMERS */
|
---|
683 | RTTHREAD Thread = pTimer->Thread;
|
---|
684 | rc = RTSemEventSignal(pTimer->Event);
|
---|
685 | AssertRC(rc);
|
---|
686 | #endif /* IPRT_WITH_POSIX_TIMERS */
|
---|
687 | if (Thread != NIL_RTTHREAD)
|
---|
688 | {
|
---|
689 | /* Signal it so it gets out of the sigwait if it's stuck there... */
|
---|
690 | pthread_kill((pthread_t)RTThreadGetNative(Thread), RT_TIMER_SIGNAL);
|
---|
691 |
|
---|
692 | /*
|
---|
693 | * Wait for the thread to complete.
|
---|
694 | */
|
---|
695 | rc = RTThreadWait(Thread, 30 * 1000, NULL);
|
---|
696 | AssertRC(rc);
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * Free up the resources associated with the timer.
|
---|
702 | */
|
---|
703 | #ifdef IPRT_WITH_POSIX_TIMERS
|
---|
704 | timer_delete(pTimer->NativeTimer);
|
---|
705 | #else
|
---|
706 | RTSemEventDestroy(pTimer->Event);
|
---|
707 | pTimer->Event = NIL_RTSEMEVENT;
|
---|
708 | #endif /* !IPRT_WITH_POSIX_TIMERS */
|
---|
709 | if (RT_SUCCESS(rc))
|
---|
710 | RTMemFree(pTimer);
|
---|
711 | return rc;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
716 | {
|
---|
717 | /*
|
---|
718 | * Validate input.
|
---|
719 | */
|
---|
720 | AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
|
---|
721 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
|
---|
722 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
723 | AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
|
---|
724 | #endif
|
---|
725 |
|
---|
726 | /*
|
---|
727 | * Already running?
|
---|
728 | */
|
---|
729 | if (!ASMAtomicXchgU8(&pTimer->fSuspended, false))
|
---|
730 | return VERR_TIMER_ACTIVE;
|
---|
731 | LogFlow(("RTTimerStart: pTimer=%p u64First=%llu u64NanoInterval=%llu\n", pTimer, u64First, pTimer->u64NanoInterval));
|
---|
732 |
|
---|
733 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
734 | /*
|
---|
735 | * Tell the thread to start servicing the timer.
|
---|
736 | * Wait for it to ACK the request to avoid reset races.
|
---|
737 | */
|
---|
738 | RTThreadUserReset(pTimer->Thread);
|
---|
739 | ASMAtomicUoWriteU64(&pTimer->u64NanoFirst, u64First);
|
---|
740 | ASMAtomicUoWriteU64(&pTimer->iTick, 0);
|
---|
741 | ASMAtomicWriteU8(&pTimer->fSuspended, false);
|
---|
742 | int rc = RTSemEventSignal(pTimer->Event);
|
---|
743 | if (RT_SUCCESS(rc))
|
---|
744 | {
|
---|
745 | rc = RTThreadUserWait(pTimer->Thread, 45*1000);
|
---|
746 | AssertRC(rc);
|
---|
747 | RTThreadUserReset(pTimer->Thread);
|
---|
748 | }
|
---|
749 | else
|
---|
750 | AssertRC(rc);
|
---|
751 |
|
---|
752 | #else /* IPRT_WITH_POSIX_TIMERS */
|
---|
753 | /*
|
---|
754 | * Start the timer.
|
---|
755 | */
|
---|
756 | struct itimerspec TimerSpec;
|
---|
757 | TimerSpec.it_value.tv_sec = u64First / 1000000000; /* nanosec => sec */
|
---|
758 | TimerSpec.it_value.tv_nsec = u64First ? u64First % 1000000000 : 10; /* 0 means disable, replace it with 10. */
|
---|
759 | TimerSpec.it_interval.tv_sec = pTimer->u64NanoInterval / 1000000000;
|
---|
760 | TimerSpec.it_interval.tv_nsec = pTimer->u64NanoInterval % 1000000000;
|
---|
761 | int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
|
---|
762 | int rc = RTErrConvertFromErrno(err);
|
---|
763 | #endif /* IPRT_WITH_POSIX_TIMERS */
|
---|
764 |
|
---|
765 | if (RT_FAILURE(rc))
|
---|
766 | ASMAtomicXchgU8(&pTimer->fSuspended, false);
|
---|
767 | return rc;
|
---|
768 | }
|
---|
769 |
|
---|
770 |
|
---|
771 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
772 | {
|
---|
773 | /*
|
---|
774 | * Validate input.
|
---|
775 | */
|
---|
776 | AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
|
---|
777 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * Already running?
|
---|
781 | */
|
---|
782 | if (ASMAtomicXchgU8(&pTimer->fSuspended, true))
|
---|
783 | return VERR_TIMER_SUSPENDED;
|
---|
784 | LogFlow(("RTTimerStop: pTimer=%p\n", pTimer));
|
---|
785 |
|
---|
786 | #ifndef IPRT_WITH_POSIX_TIMERS
|
---|
787 | /*
|
---|
788 | * Tell the thread to stop servicing the timer.
|
---|
789 | */
|
---|
790 | RTThreadUserReset(pTimer->Thread);
|
---|
791 | ASMAtomicXchgU8(&pTimer->fSuspended, true);
|
---|
792 | int rc = VINF_SUCCESS;
|
---|
793 | if (RTThreadSelf() != pTimer->Thread)
|
---|
794 | {
|
---|
795 | pthread_kill((pthread_t)RTThreadGetNative(pTimer->Thread), RT_TIMER_SIGNAL);
|
---|
796 | rc = RTThreadUserWait(pTimer->Thread, 45*1000);
|
---|
797 | AssertRC(rc);
|
---|
798 | RTThreadUserReset(pTimer->Thread);
|
---|
799 | }
|
---|
800 |
|
---|
801 | #else /* IPRT_WITH_POSIX_TIMERS */
|
---|
802 | /*
|
---|
803 | * Stop the timer.
|
---|
804 | */
|
---|
805 | struct itimerspec TimerSpec;
|
---|
806 | TimerSpec.it_value.tv_sec = 0;
|
---|
807 | TimerSpec.it_value.tv_nsec = 0;
|
---|
808 | int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
|
---|
809 | int rc = RTErrConvertFromErrno(err);
|
---|
810 | #endif /* IPRT_WITH_POSIX_TIMERS */
|
---|
811 |
|
---|
812 | return rc;
|
---|
813 | }
|
---|
814 |
|
---|
815 |
|
---|
816 | RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
|
---|
817 | {
|
---|
818 | AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
|
---|
819 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
|
---|
820 | NOREF(u64NanoInterval);
|
---|
821 | return VERR_NOT_SUPPORTED;
|
---|
822 | }
|
---|
823 |
|
---|