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 */ 7 8 9 #include <sys/time.h> 10 11 #include <errno.h> 12 #include <string.h> 13 14 #include <OS.h> 15 16 #include <syscall_utils.h> 17 18 #include <user_timer_defs.h> 19 20 #include <time_private.h> 21 22 23 static bool 24 itimerval_to_itimerspec(const itimerval& val, itimerspec& spec) 25 { 26 return timeval_to_timespec(val.it_value, spec.it_value) 27 && timeval_to_timespec(val.it_interval, spec.it_interval); 28 } 29 30 31 static void 32 itimerspec_to_itimerval(const itimerspec& spec, itimerval& val) 33 { 34 timespec_to_timeval(spec.it_value, val.it_value); 35 timespec_to_timeval(spec.it_interval, val.it_interval); 36 } 37 38 39 static bool 40 prepare_timer(__timer_t& timer, int which) 41 { 42 switch (which) { 43 case ITIMER_REAL: 44 timer.SetTo(USER_TIMER_REAL_TIME_ID, -1); 45 return true; 46 case ITIMER_VIRTUAL: 47 timer.SetTo(USER_TIMER_TEAM_USER_TIME_ID, -1); 48 return true; 49 case ITIMER_PROF: 50 timer.SetTo(USER_TIMER_TEAM_TOTAL_TIME_ID, -1); 51 return true; 52 } 53 54 return false; 55 } 56 57 58 // #pragma mark - 59 60 61 int 62 getitimer(int which, struct itimerval* value) 63 { 64 // prepare the respective timer 65 __timer_t timer; 66 if (!prepare_timer(timer, which)) 67 RETURN_AND_SET_ERRNO(EINVAL); 68 69 // let timer_gettime() do the job 70 itimerspec valueSpec; 71 if (timer_gettime(&timer, &valueSpec) != 0) 72 return -1; 73 74 // convert back to itimerval value 75 itimerspec_to_itimerval(valueSpec, *value); 76 77 return 0; 78 } 79 80 81 int 82 setitimer(int which, const struct itimerval* value, struct itimerval* oldValue) 83 { 84 // prepare the respective timer 85 __timer_t timer; 86 if (!prepare_timer(timer, which)) 87 RETURN_AND_SET_ERRNO(EINVAL); 88 89 // convert value to itimerspec 90 itimerspec valueSpec; 91 if (!itimerval_to_itimerspec(*value, valueSpec)) 92 RETURN_AND_SET_ERRNO(EINVAL); 93 94 // let timer_settime() do the job 95 itimerspec oldValueSpec; 96 if (timer_settime(&timer, 0, &valueSpec, 97 oldValue != NULL ? &oldValueSpec : NULL) != 0) { 98 return -1; 99 } 100 101 // convert back to itimerval oldValue 102 if (oldValue != NULL) 103 itimerspec_to_itimerval(oldValueSpec, *oldValue); 104 105 return 0; 106 } 107