headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / sys / itimer.cpp
blob42a7b72e2442459e45534fb6bf9812ada435ac54
1 /*
2 * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2004-2006, Axel Dörfler, axeld@pinc-software.de.
4 * All rights reserved.
5 * Distributed under the terms of the MIT License.
6 */
9 #include <sys/time.h>
11 #include <errno.h>
12 #include <string.h>
14 #include <OS.h>
16 #include <errno_private.h>
17 #include <syscall_utils.h>
19 #include <user_timer_defs.h>
21 #include <time_private.h>
24 static bool
25 itimerval_to_itimerspec(const itimerval& val, itimerspec& spec)
27 return timeval_to_timespec(val.it_value, spec.it_value)
28 && timeval_to_timespec(val.it_interval, spec.it_interval);
32 static void
33 itimerspec_to_itimerval(const itimerspec& spec, itimerval& val)
35 timespec_to_timeval(spec.it_value, val.it_value);
36 timespec_to_timeval(spec.it_interval, val.it_interval);
40 static bool
41 prepare_timer(__timer_t& timer, int which)
43 switch (which) {
44 case ITIMER_REAL:
45 timer.SetTo(USER_TIMER_REAL_TIME_ID, -1);
46 return true;
47 case ITIMER_VIRTUAL:
48 timer.SetTo(USER_TIMER_TEAM_USER_TIME_ID, -1);
49 return true;
50 case ITIMER_PROF:
51 timer.SetTo(USER_TIMER_TEAM_TOTAL_TIME_ID, -1);
52 return true;
55 return false;
59 // #pragma mark -
62 int
63 getitimer(int which, struct itimerval* value)
65 // prepare the respective timer
66 __timer_t timer;
67 if (!prepare_timer(timer, which))
68 RETURN_AND_SET_ERRNO(EINVAL);
70 // let timer_gettime() do the job
71 itimerspec valueSpec;
72 if (timer_gettime(&timer, &valueSpec) != 0)
73 return -1;
75 // convert back to itimerval value
76 itimerspec_to_itimerval(valueSpec, *value);
78 return 0;
82 int
83 setitimer(int which, const struct itimerval* value, struct itimerval* oldValue)
85 // prepare the respective timer
86 __timer_t timer;
87 if (!prepare_timer(timer, which))
88 RETURN_AND_SET_ERRNO(EINVAL);
90 // convert value to itimerspec
91 itimerspec valueSpec;
92 if (!itimerval_to_itimerspec(*value, valueSpec))
93 RETURN_AND_SET_ERRNO(EINVAL);
95 // let timer_settime() do the job
96 itimerspec oldValueSpec;
97 if (timer_settime(&timer, 0, &valueSpec,
98 oldValue != NULL ? &oldValueSpec : NULL) != 0) {
99 return -1;
102 // convert back to itimerval oldValue
103 if (oldValue != NULL)
104 itimerspec_to_itimerval(oldValueSpec, *oldValue);
106 return 0;