2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
27 * Purpose: Data structures for the kernel alarm clock
28 * facility. This file is used only by kernel
29 * level clock facility routines.
32 #ifndef _KERN_CLOCK_H_
33 #define _KERN_CLOCK_H_
35 #include <mach/message.h>
36 #include <mach/clock_types.h>
37 #include <mach/mach_time.h>
39 #include <sys/appleapiopts.h>
41 #ifdef __APPLE_API_PRIVATE
43 #ifdef MACH_KERNEL_PRIVATE
45 #include <ipc/ipc_port.h>
48 * Actual clock alarm structure. Used for user clock_sleep() and
49 * clock_alarm() calls. Alarms are allocated from the alarm free
50 * list and entered in time priority order into the active alarm
51 * chain of the target clock.
54 struct alarm
*al_next
; /* next alarm in chain */
55 struct alarm
*al_prev
; /* previous alarm in chain */
56 int al_status
; /* alarm status */
57 mach_timespec_t al_time
; /* alarm time */
58 struct { /* message alarm data */
59 int type
; /* alarm type */
60 ipc_port_t port
; /* alarm port */
62 port_type
; /* alarm port type */
63 struct clock
*clock
; /* alarm clock */
64 void *data
; /* alarm data */
66 #define al_type al_alrm.type
67 #define al_port al_alrm.port
68 #define al_port_type al_alrm.port_type
69 #define al_clock al_alrm.clock
70 #define al_data al_alrm.data
71 long al_seqno
; /* alarm sequence number */
73 typedef struct alarm alarm_data_t
;
76 #define ALARM_FREE 0 /* alarm is on free list */
77 #define ALARM_SLEEP 1 /* active clock_sleep() */
78 #define ALARM_CLOCK 2 /* active clock_alarm() */
79 #define ALARM_DONE 4 /* alarm has expired */
82 * Clock operations list structure. Contains vectors to machine
83 * dependent clock routines. The routines c_config, c_init, and
84 * c_gettime must be implemented for every clock device.
87 int (*c_config
)(void); /* configuration */
89 int (*c_init
)(void); /* initialize */
91 kern_return_t (*c_gettime
)( /* get time */
92 mach_timespec_t
*cur_time
);
94 kern_return_t (*c_settime
)( /* set time */
95 mach_timespec_t
*clock_time
);
97 kern_return_t (*c_getattr
)( /* get attributes */
98 clock_flavor_t flavor
,
100 mach_msg_type_number_t
*count
);
102 kern_return_t (*c_setattr
)( /* set attributes */
103 clock_flavor_t flavor
,
105 mach_msg_type_number_t count
);
107 void (*c_setalrm
)( /* set next alarm */
108 mach_timespec_t
*alarm_time
);
110 typedef struct clock_ops
*clock_ops_t
;
111 typedef struct clock_ops clock_ops_data_t
;
114 * Actual clock object data structure. Contains the machine
115 * dependent operations list, clock operations ports, and a
116 * chain of pending alarms.
119 clock_ops_t cl_ops
; /* operations list */
120 struct ipc_port
*cl_service
; /* service port */
121 struct ipc_port
*cl_control
; /* control port */
122 struct { /* alarm chain head */
123 struct alarm
*al_next
;
126 typedef struct clock clock_data_t
;
129 * Configure the clock system.
131 extern void clock_config(void);
133 * Initialize the clock system.
135 extern void clock_init(void);
138 * Initialize the clock ipc service facility.
140 extern void clock_service_create(void);
143 * Service clock alarm interrupts. Called from machine dependent
144 * layer at splclock(). The clock_id argument specifies the clock,
145 * and the clock_time argument gives that clock's current time.
147 extern void clock_alarm_intr(
149 mach_timespec_t
*clock_time
);
151 extern kern_return_t
clock_sleep_internal(
153 sleep_type_t sleep_type
,
154 mach_timespec_t
*sleep_time
);
156 typedef void (*clock_timer_func_t
)(
159 extern void clock_set_timer_func(
160 clock_timer_func_t func
);
162 extern void clock_set_timer_deadline(
165 extern void mk_timebase_info(
167 uint32_t *abs_to_ns_numer
,
168 uint32_t *abs_to_ns_denom
,
169 uint32_t *proc_to_abs_numer
,
170 uint32_t *proc_to_abs_denom
);
172 extern void clock_adjust_calendar(
175 extern mach_timespec_t
176 clock_get_calendar_offset(void);
178 #endif /* MACH_KERNEL_PRIVATE */
180 extern void clock_set_calendar_value(
181 mach_timespec_t value
);
183 extern int64_t clock_set_calendar_adjtime(
187 extern void clock_initialize_calendar(void);
189 #endif /* __APPLE_API_PRIVATE */
191 #ifdef __APPLE_API_UNSTABLE
193 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
194 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
196 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
197 MACH_TIMESPEC_SEC_MAX, \
198 MACH_TIMESPEC_NSEC_MAX } )
199 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
201 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
203 (t1)->tv_nsec += (clock_res_t)(nsec); \
204 if ((clock_res_t)(nsec) > 0 && \
205 (t1)->tv_nsec >= NSEC_PER_SEC) { \
206 (t1)->tv_nsec -= NSEC_PER_SEC; \
209 else if ((clock_res_t)(nsec) < 0 && \
210 (t1)->tv_nsec < 0) { \
211 (t1)->tv_nsec += NSEC_PER_SEC; \
216 #endif /* __APPLE_API_UNSTABLE */
218 extern mach_timespec_t
clock_get_system_value(void);
220 extern mach_timespec_t
clock_get_calendar_value(void);
222 extern void clock_timebase_info(
223 mach_timebase_info_t info
);
225 extern void clock_get_uptime(
228 extern void clock_interval_to_deadline(
230 uint32_t scale_factor
,
233 extern void clock_interval_to_absolutetime_interval(
235 uint32_t scale_factor
,
238 extern void clock_absolutetime_interval_to_deadline(
242 extern void clock_deadline_for_periodic_event(
247 extern void clock_delay_for_interval(
249 uint32_t scale_factor
);
251 extern void clock_delay_until(
254 extern void absolutetime_to_nanoseconds(
258 extern void nanoseconds_to_absolutetime(
259 uint64_t nanoseconds
,
262 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
264 #include <libkern/OSBase.h>
266 #define clock_get_uptime(a) \
267 clock_get_uptime(__OSAbsoluteTimePtr(a))
269 #define clock_interval_to_deadline(a, b, c) \
270 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
272 #define clock_interval_to_absolutetime_interval(a, b, c) \
273 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
275 #define clock_absolutetime_interval_to_deadline(a, b) \
276 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
278 #define clock_deadline_for_periodic_event(a, b, c) \
279 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
281 #define clock_delay_until(a) \
282 clock_delay_until(__OSAbsoluteTime(a))
284 #define absolutetime_to_nanoseconds(a, b) \
285 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
287 #define nanoseconds_to_absolutetime(a, b) \
288 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
290 #define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
293 #define CMP_ABSOLUTETIME(t1, t2) \
294 (AbsoluteTime_to_scalar(t1) > \
295 AbsoluteTime_to_scalar(t2)? (int)+1 : \
296 (AbsoluteTime_to_scalar(t1) < \
297 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
300 #define ADD_ABSOLUTETIME(t1, t2) \
301 (AbsoluteTime_to_scalar(t1) += \
302 AbsoluteTime_to_scalar(t2))
305 #define SUB_ABSOLUTETIME(t1, t2) \
306 (AbsoluteTime_to_scalar(t1) -= \
307 AbsoluteTime_to_scalar(t2))
309 #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
310 (AbsoluteTime_to_scalar(t1) += \
315 #endif /* _KERN_CLOCK_H_ */