2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
30 * Purpose: Data structures for the kernel alarm clock
31 * facility. This file is used only by kernel
32 * level clock facility routines.
35 #ifndef _KERN_CLOCK_H_
36 #define _KERN_CLOCK_H_
38 #include <mach/message.h>
39 #include <mach/clock_types.h>
40 #include <mach/mach_time.h>
42 #include <sys/appleapiopts.h>
44 #ifdef __APPLE_API_PRIVATE
46 #ifdef MACH_KERNEL_PRIVATE
48 #include <ipc/ipc_port.h>
51 * Actual clock alarm structure. Used for user clock_sleep() and
52 * clock_alarm() calls. Alarms are allocated from the alarm free
53 * list and entered in time priority order into the active alarm
54 * chain of the target clock.
57 struct alarm
*al_next
; /* next alarm in chain */
58 struct alarm
*al_prev
; /* previous alarm in chain */
59 int al_status
; /* alarm status */
60 mach_timespec_t al_time
; /* alarm time */
61 struct { /* message alarm data */
62 int type
; /* alarm type */
63 ipc_port_t port
; /* alarm port */
65 port_type
; /* alarm port type */
66 struct clock
*clock
; /* alarm clock */
67 void *data
; /* alarm data */
69 #define al_type al_alrm.type
70 #define al_port al_alrm.port
71 #define al_port_type al_alrm.port_type
72 #define al_clock al_alrm.clock
73 #define al_data al_alrm.data
74 long al_seqno
; /* alarm sequence number */
76 typedef struct alarm alarm_data_t
;
79 #define ALARM_FREE 0 /* alarm is on free list */
80 #define ALARM_SLEEP 1 /* active clock_sleep() */
81 #define ALARM_CLOCK 2 /* active clock_alarm() */
82 #define ALARM_DONE 4 /* alarm has expired */
85 * Clock operations list structure. Contains vectors to machine
86 * dependent clock routines. The routines c_config, c_init, and
87 * c_gettime must be implemented for every clock device.
90 int (*c_config
)(void); /* configuration */
92 int (*c_init
)(void); /* initialize */
94 kern_return_t (*c_gettime
)( /* get time */
95 mach_timespec_t
*cur_time
);
97 kern_return_t (*c_settime
)( /* set time */
98 mach_timespec_t
*clock_time
);
100 kern_return_t (*c_getattr
)( /* get attributes */
101 clock_flavor_t flavor
,
103 mach_msg_type_number_t
*count
);
105 kern_return_t (*c_setattr
)( /* set attributes */
106 clock_flavor_t flavor
,
108 mach_msg_type_number_t count
);
110 void (*c_setalrm
)( /* set next alarm */
111 mach_timespec_t
*alarm_time
);
113 typedef struct clock_ops
*clock_ops_t
;
114 typedef struct clock_ops clock_ops_data_t
;
117 * Actual clock object data structure. Contains the machine
118 * dependent operations list, clock operations ports, and a
119 * chain of pending alarms.
122 clock_ops_t cl_ops
; /* operations list */
123 struct ipc_port
*cl_service
; /* service port */
124 struct ipc_port
*cl_control
; /* control port */
125 struct { /* alarm chain head */
126 struct alarm
*al_next
;
129 typedef struct clock clock_data_t
;
132 * Configure the clock system.
134 extern void clock_config(void);
136 * Initialize the clock system.
138 extern void clock_init(void);
141 * Initialize the clock ipc service facility.
143 extern void clock_service_create(void);
146 * Service clock alarm interrupts. Called from machine dependent
147 * layer at splclock(). The clock_id argument specifies the clock,
148 * and the clock_time argument gives that clock's current time.
150 extern void clock_alarm_intr(
152 mach_timespec_t
*clock_time
);
154 extern kern_return_t
clock_sleep_internal(
156 sleep_type_t sleep_type
,
157 mach_timespec_t
*sleep_time
);
159 typedef void (*clock_timer_func_t
)(
162 extern void clock_set_timer_func(
163 clock_timer_func_t func
);
165 extern void clock_set_timer_deadline(
168 extern void mk_timebase_info(
170 uint32_t *abs_to_ns_numer
,
171 uint32_t *abs_to_ns_denom
,
172 uint32_t *proc_to_abs_numer
,
173 uint32_t *proc_to_abs_denom
);
175 extern void clock_adjust_calendar(
178 extern mach_timespec_t
179 clock_get_calendar_offset(void);
181 #endif /* MACH_KERNEL_PRIVATE */
183 extern void clock_set_calendar_value(
184 mach_timespec_t value
);
186 extern int64_t clock_set_calendar_adjtime(
190 extern void clock_initialize_calendar(void);
192 extern void clock_gettimeofday(
194 uint32_t *microsecs
);
196 #endif /* __APPLE_API_PRIVATE */
198 #ifdef __APPLE_API_UNSTABLE
200 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
201 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
203 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
204 MACH_TIMESPEC_SEC_MAX, \
205 MACH_TIMESPEC_NSEC_MAX } )
206 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
208 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
210 (t1)->tv_nsec += (clock_res_t)(nsec); \
211 if ((clock_res_t)(nsec) > 0 && \
212 (t1)->tv_nsec >= NSEC_PER_SEC) { \
213 (t1)->tv_nsec -= NSEC_PER_SEC; \
216 else if ((clock_res_t)(nsec) < 0 && \
217 (t1)->tv_nsec < 0) { \
218 (t1)->tv_nsec += NSEC_PER_SEC; \
223 #endif /* __APPLE_API_UNSTABLE */
225 extern mach_timespec_t
clock_get_system_value(void);
227 extern mach_timespec_t
clock_get_calendar_value(void);
229 extern void clock_timebase_info(
230 mach_timebase_info_t info
);
232 extern void clock_get_uptime(
235 extern void clock_interval_to_deadline(
237 uint32_t scale_factor
,
240 extern void clock_interval_to_absolutetime_interval(
242 uint32_t scale_factor
,
245 extern void clock_absolutetime_interval_to_deadline(
249 extern void clock_deadline_for_periodic_event(
254 extern void clock_delay_for_interval(
256 uint32_t scale_factor
);
258 extern void clock_delay_until(
261 extern void absolutetime_to_nanoseconds(
265 extern void nanoseconds_to_absolutetime(
266 uint64_t nanoseconds
,
269 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
271 #include <libkern/OSBase.h>
273 #define clock_get_uptime(a) \
274 clock_get_uptime(__OSAbsoluteTimePtr(a))
276 #define clock_interval_to_deadline(a, b, c) \
277 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
279 #define clock_interval_to_absolutetime_interval(a, b, c) \
280 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
282 #define clock_absolutetime_interval_to_deadline(a, b) \
283 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
285 #define clock_deadline_for_periodic_event(a, b, c) \
286 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
288 #define clock_delay_until(a) \
289 clock_delay_until(__OSAbsoluteTime(a))
291 #define absolutetime_to_nanoseconds(a, b) \
292 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
294 #define nanoseconds_to_absolutetime(a, b) \
295 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
297 #define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
300 #define CMP_ABSOLUTETIME(t1, t2) \
301 (AbsoluteTime_to_scalar(t1) > \
302 AbsoluteTime_to_scalar(t2)? (int)+1 : \
303 (AbsoluteTime_to_scalar(t1) < \
304 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
307 #define ADD_ABSOLUTETIME(t1, t2) \
308 (AbsoluteTime_to_scalar(t1) += \
309 AbsoluteTime_to_scalar(t2))
312 #define SUB_ABSOLUTETIME(t1, t2) \
313 (AbsoluteTime_to_scalar(t1) -= \
314 AbsoluteTime_to_scalar(t2))
316 #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
317 (AbsoluteTime_to_scalar(t1) += \
322 #endif /* _KERN_CLOCK_H_ */