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