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 #ifdef MACH_KERNEL_PRIVATE
40 #include <ipc/ipc_port.h>
43 * Actual clock alarm structure. Used for user clock_sleep() and
44 * clock_alarm() calls. Alarms are allocated from the alarm free
45 * list and entered in time priority order into the active alarm
46 * chain of the target clock.
49 struct alarm
*al_next
; /* next alarm in chain */
50 struct alarm
*al_prev
; /* previous alarm in chain */
51 int al_status
; /* alarm status */
52 mach_timespec_t al_time
; /* alarm time */
53 struct { /* message alarm data */
54 int type
; /* alarm type */
55 ipc_port_t port
; /* alarm port */
57 port_type
; /* alarm port type */
58 struct clock
*clock
; /* alarm clock */
59 void *data
; /* alarm data */
61 #define al_type al_alrm.type
62 #define al_port al_alrm.port
63 #define al_port_type al_alrm.port_type
64 #define al_clock al_alrm.clock
65 #define al_data al_alrm.data
66 long al_seqno
; /* alarm sequence number */
68 typedef struct alarm alarm_data_t
;
71 #define ALARM_FREE 0 /* alarm is on free list */
72 #define ALARM_SLEEP 1 /* active clock_sleep() */
73 #define ALARM_CLOCK 2 /* active clock_alarm() */
74 #define ALARM_DONE 4 /* alarm has expired */
77 * Clock operations list structure. Contains vectors to machine
78 * dependent clock routines. The routines c_config, c_init, and
79 * c_gettime must be implemented for every clock device.
82 int (*c_config
)(void); /* configuration */
84 int (*c_init
)(void); /* initialize */
86 kern_return_t (*c_gettime
)( /* get time */
87 mach_timespec_t
*cur_time
);
89 kern_return_t (*c_settime
)( /* set time */
90 mach_timespec_t
*clock_time
);
92 kern_return_t (*c_getattr
)( /* get attributes */
93 clock_flavor_t flavor
,
95 mach_msg_type_number_t
*count
);
97 kern_return_t (*c_setattr
)( /* set attributes */
98 clock_flavor_t flavor
,
100 mach_msg_type_number_t count
);
102 void (*c_setalrm
)( /* set next alarm */
103 mach_timespec_t
*alarm_time
);
105 typedef struct clock_ops
*clock_ops_t
;
106 typedef struct clock_ops clock_ops_data_t
;
109 * Actual clock object data structure. Contains the machine
110 * dependent operations list, clock operations ports, and a
111 * chain of pending alarms.
114 clock_ops_t cl_ops
; /* operations list */
115 struct ipc_port
*cl_service
; /* service port */
116 struct ipc_port
*cl_control
; /* control port */
117 struct { /* alarm chain head */
118 struct alarm
*al_next
;
121 typedef struct clock clock_data_t
;
124 * Configure the clock system.
126 extern void clock_config(void);
128 * Initialize the clock system.
130 extern void clock_init(void);
133 * Initialize the clock ipc service facility.
135 extern void clock_service_create(void);
138 * Service clock alarm interrupts. Called from machine dependent
139 * layer at splclock(). The clock_id argument specifies the clock,
140 * and the clock_time argument gives that clock's current time.
142 extern void clock_alarm_intr(
144 mach_timespec_t
*clock_time
);
146 extern kern_return_t
clock_sleep_internal(
148 sleep_type_t sleep_type
,
149 mach_timespec_t
*sleep_time
);
151 typedef void (*clock_timer_func_t
)(
154 extern void clock_set_timer_func(
155 clock_timer_func_t func
);
157 extern void clock_set_timer_deadline(
160 extern void mk_timebase_info(
162 uint32_t *abs_to_ns_numer
,
163 uint32_t *abs_to_ns_denom
,
164 uint32_t *proc_to_abs_numer
,
165 uint32_t *proc_to_abs_denom
);
167 #endif /* MACH_KERNEL_PRIVATE */
169 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
170 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
172 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
173 MACH_TIMESPEC_SEC_MAX, \
174 MACH_TIMESPEC_NSEC_MAX } )
175 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
177 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
179 (t1)->tv_nsec += (clock_res_t)(nsec); \
180 if ((clock_res_t)(nsec) > 0 && \
181 (t1)->tv_nsec >= NSEC_PER_SEC) { \
182 (t1)->tv_nsec -= NSEC_PER_SEC; \
185 else if ((clock_res_t)(nsec) < 0 && \
186 (t1)->tv_nsec < 0) { \
187 (t1)->tv_nsec += NSEC_PER_SEC; \
192 extern mach_timespec_t
clock_get_system_value(void);
194 extern mach_timespec_t
clock_get_calendar_value(void);
196 extern void clock_set_calendar_value(
197 mach_timespec_t value
);
199 extern void clock_adjust_calendar(
202 extern void clock_initialize_calendar(void);
204 extern mach_timespec_t
clock_get_calendar_offset(void);
206 extern void clock_timebase_info(
207 mach_timebase_info_t info
);
209 extern void clock_get_uptime(
212 extern void clock_interval_to_deadline(
214 uint32_t scale_factor
,
217 extern void clock_interval_to_absolutetime_interval(
219 uint32_t scale_factor
,
222 extern void clock_absolutetime_interval_to_deadline(
226 extern void clock_deadline_for_periodic_event(
231 extern void clock_delay_for_interval(
233 uint32_t scale_factor
);
235 extern void clock_delay_until(
238 extern void absolutetime_to_nanoseconds(
242 extern void nanoseconds_to_absolutetime(
243 uint64_t nanoseconds
,
246 #if !defined(MACH_KERNEL_PRIVATE) && !defined(ABSOLUTETIME_SCALAR_TYPE)
248 #include <libkern/OSBase.h>
250 #define clock_get_uptime(a) \
251 clock_get_uptime(__OSAbsoluteTimePtr(a))
253 #define clock_interval_to_deadline(a, b, c) \
254 clock_interval_to_deadline((a), (b), __OSAbsoluteTimePtr(c))
256 #define clock_interval_to_absolutetime_interval(a, b, c) \
257 clock_interval_to_absolutetime_interval((a), (b), __OSAbsoluteTimePtr(c))
259 #define clock_absolutetime_interval_to_deadline(a, b) \
260 clock_absolutetime_interval_to_deadline(__OSAbsoluteTime(a), __OSAbsoluteTimePtr(b))
262 #define clock_deadline_for_periodic_event(a, b, c) \
263 clock_deadline_for_periodic_event(__OSAbsoluteTime(a), __OSAbsoluteTime(b), __OSAbsoluteTimePtr(c))
265 #define clock_delay_until(a) \
266 clock_delay_until(__OSAbsoluteTime(a))
268 #define absolutetime_to_nanoseconds(a, b) \
269 absolutetime_to_nanoseconds(__OSAbsoluteTime(a), (b))
271 #define nanoseconds_to_absolutetime(a, b) \
272 nanoseconds_to_absolutetime((a), __OSAbsoluteTimePtr(b))
274 #define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
277 #define CMP_ABSOLUTETIME(t1, t2) \
278 (AbsoluteTime_to_scalar(t1) > \
279 AbsoluteTime_to_scalar(t2)? (int)+1 : \
280 (AbsoluteTime_to_scalar(t1) < \
281 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
284 #define ADD_ABSOLUTETIME(t1, t2) \
285 (AbsoluteTime_to_scalar(t1) += \
286 AbsoluteTime_to_scalar(t2))
289 #define SUB_ABSOLUTETIME(t1, t2) \
290 (AbsoluteTime_to_scalar(t1) -= \
291 AbsoluteTime_to_scalar(t2))
293 #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
294 (AbsoluteTime_to_scalar(t1) += \
299 #endif /* _KERN_CLOCK_H_ */