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