xref: /haiku/src/system/libroot/os/time.cpp (revision 22440f4105cafc95cc1d49f9bc65bb395c527d86)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <syslog.h>
11 
12 #include <FindDirectory.h>
13 #include <OS.h>
14 
15 #include <commpage_defs.h>
16 #include <errno_private.h>
17 #include <libroot_private.h>
18 #include <real_time_data.h>
19 #include <user_timer_defs.h>
20 #include <syscalls.h>
21 
22 
23 static struct real_time_data* sRealTimeData;
24 
25 
26 void
27 __init_time(addr_t commPageTable)
28 {
29 	sRealTimeData = (struct real_time_data*)
30 		(((addr_t*)commPageTable)[COMMPAGE_ENTRY_REAL_TIME_DATA]
31 			+ commPageTable);
32 
33 	__arch_init_time(sRealTimeData, false);
34 }
35 
36 
37 bigtime_t
38 __get_system_time_offset()
39 {
40 	return __arch_get_system_time_offset(sRealTimeData);
41 }
42 
43 
44 //	#pragma mark - public API
45 
46 
47 uint32
48 real_time_clock(void)
49 {
50 	return (__arch_get_system_time_offset(sRealTimeData) + system_time())
51 		/ 1000000;
52 }
53 
54 
55 bigtime_t
56 real_time_clock_usecs(void)
57 {
58 	return __arch_get_system_time_offset(sRealTimeData) + system_time();
59 }
60 
61 
62 void
63 set_real_time_clock(uint32 secs)
64 {
65 	_kern_set_real_time_clock((bigtime_t)secs * 1000000);
66 }
67 
68 
69 status_t
70 set_timezone(const char* /*timezone*/)
71 {
72 	/* There's nothing we can do here, since we no longer support named
73 	 * timezones.
74 	 *
75 	 * TODO: should we keep this around for compatibility or get rid of it?
76 	 */
77 	return B_OK;
78 }
79 
80 
81 bigtime_t
82 set_alarm(bigtime_t when, uint32 mode)
83 {
84 	// prepare the values to be passed to the kernel
85 	bigtime_t interval = 0;
86 	uint32 flags = B_RELATIVE_TIMEOUT;
87 
88 	if (when == B_INFINITE_TIMEOUT) {
89 		when = B_INFINITE_TIMEOUT;
90 	} else {
91 		switch (mode) {
92 			case B_PERIODIC_ALARM:
93 				interval = when;
94 				break;
95 			case B_ONE_SHOT_ABSOLUTE_ALARM:
96 				flags = B_ABSOLUTE_TIMEOUT;
97 				break;
98 			case B_ONE_SHOT_RELATIVE_ALARM:
99 			default:
100 				break;
101 		}
102 	}
103 
104 	// set the timer
105 	user_timer_info oldInfo;
106 	status_t error = _kern_set_timer(USER_TIMER_REAL_TIME_ID, find_thread(NULL),
107 		when, interval, flags, &oldInfo);
108 	if (error != B_OK)
109 		return 0;
110 
111 	// A remaining time of B_INFINITE_TIMEOUT means not scheduled.
112 	return oldInfo.remaining_time != B_INFINITE_TIMEOUT
113 		? oldInfo.remaining_time : 0;
114 }
115