xref: /haiku/src/system/libroot/os/time.cpp (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
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 unsigned long
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(unsigned long secs)
64 {
65 	_kern_set_real_time_clock((bigtime_t)secs * 1000000);
66 }
67 
68 
69 bigtime_t
70 set_alarm(bigtime_t when, uint32 mode)
71 {
72 	// prepare the values to be passed to the kernel
73 	bigtime_t interval = 0;
74 	uint32 flags = B_RELATIVE_TIMEOUT;
75 
76 	if (when == B_INFINITE_TIMEOUT) {
77 		when = B_INFINITE_TIMEOUT;
78 	} else {
79 		switch (mode) {
80 			case B_PERIODIC_ALARM:
81 				interval = when;
82 				break;
83 			case B_ONE_SHOT_ABSOLUTE_ALARM:
84 				flags = B_ABSOLUTE_TIMEOUT;
85 				break;
86 			case B_ONE_SHOT_RELATIVE_ALARM:
87 			default:
88 				break;
89 		}
90 	}
91 
92 	// set the timer
93 	user_timer_info oldInfo;
94 	status_t error = _kern_set_timer(USER_TIMER_REAL_TIME_ID, find_thread(NULL),
95 		when, interval, flags, &oldInfo);
96 	if (error != B_OK)
97 		return 0;
98 
99 	// A remaining time of B_INFINITE_TIMEOUT means not scheduled.
100 	return oldInfo.remaining_time != B_INFINITE_TIMEOUT
101 		? oldInfo.remaining_time : 0;
102 }
103 
104 
105 #ifdef __HAIKU_BEOS_COMPATIBLE
106 extern "C" status_t
107 set_timezone(const char* /*timezone*/)
108 {
109 	// There's nothing we can do here, since we no longer support named timezones.
110 	return ENOSYS;
111 }
112 #endif
113