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 <libkern/OSBase.h>
37 #include <mach/message.h>
38 #include <mach/clock_types.h>
39 #include <mach/mach_time.h>
41 #ifdef MACH_KERNEL_PRIVATE
42 #include <ipc/ipc_port.h>
45 * Actual clock alarm structure. Used for user clock_sleep() and
46 * clock_alarm() calls. Alarms are allocated from the alarm free
47 * list and entered in time priority order into the active alarm
48 * chain of the target clock.
51 struct alarm
*al_next
; /* next alarm in chain */
52 struct alarm
*al_prev
; /* previous alarm in chain */
53 int al_status
; /* alarm status */
54 mach_timespec_t al_time
; /* alarm time */
55 struct { /* message alarm data */
56 int type
; /* alarm type */
57 ipc_port_t port
; /* alarm port */
59 port_type
; /* alarm port type */
60 struct clock
*clock
; /* alarm clock */
61 void *data
; /* alarm data */
63 #define al_type al_alrm.type
64 #define al_port al_alrm.port
65 #define al_port_type al_alrm.port_type
66 #define al_clock al_alrm.clock
67 #define al_data al_alrm.data
68 long al_seqno
; /* alarm sequence number */
70 typedef struct alarm alarm_data_t
;
73 #define ALARM_FREE 0 /* alarm is on free list */
74 #define ALARM_SLEEP 1 /* active clock_sleep() */
75 #define ALARM_CLOCK 2 /* active clock_alarm() */
76 #define ALARM_DONE 4 /* alarm has expired */
79 * Clock operations list structure. Contains vectors to machine
80 * dependent clock routines. The routines c_config, c_init, and
81 * c_gettime must be implemented for every clock device.
84 int (*c_config
)(void); /* configuration */
86 int (*c_init
)(void); /* initialize */
88 kern_return_t (*c_gettime
)( /* get time */
89 mach_timespec_t
*cur_time
);
91 kern_return_t (*c_settime
)( /* set time */
92 mach_timespec_t
*clock_time
);
94 kern_return_t (*c_getattr
)( /* get attributes */
95 clock_flavor_t flavor
,
97 mach_msg_type_number_t
*count
);
99 kern_return_t (*c_setattr
)( /* set attributes */
100 clock_flavor_t flavor
,
102 mach_msg_type_number_t count
);
104 void (*c_setalrm
)( /* set next alarm */
105 mach_timespec_t
*alarm_time
);
107 typedef struct clock_ops
*clock_ops_t
;
108 typedef struct clock_ops clock_ops_data_t
;
111 * Actual clock object data structure. Contains the machine
112 * dependent operations list, clock operations ports, and a
113 * chain of pending alarms.
116 clock_ops_t cl_ops
; /* operations list */
117 struct ipc_port
*cl_service
; /* service port */
118 struct ipc_port
*cl_control
; /* control port */
119 struct { /* alarm chain head */
120 struct alarm
*al_next
;
123 typedef struct clock clock_data_t
;
126 * Configure the clock system.
128 extern void clock_config(void);
130 * Initialize the clock system.
132 extern void clock_init(void);
135 * Initialize the clock ipc service facility.
137 extern void clock_service_create(void);
140 * Service clock alarm interrupts. Called from machine dependent
141 * layer at splclock(). The clock_id argument specifies the clock,
142 * and the clock_time argument gives that clock's current time.
144 extern void clock_alarm_intr(
146 mach_timespec_t
*clock_time
);
148 extern kern_return_t
clock_sleep_internal(
150 sleep_type_t sleep_type
,
151 mach_timespec_t
*sleep_time
);
153 typedef void (*clock_timer_func_t
)(
154 AbsoluteTime timestamp
);
156 extern void clock_set_timer_func(
157 clock_timer_func_t func
);
159 extern void clock_set_timer_deadline(
160 AbsoluteTime deadline
);
162 extern void mk_timebase_info(
164 uint32_t *abs_to_ns_numer
,
165 uint32_t *abs_to_ns_denom
,
166 uint32_t *proc_to_abs_numer
,
167 uint32_t *proc_to_abs_denom
);
169 #define scalar_to_AbsoluteTime(x) (*(AbsoluteTime *)(x))
171 #endif /* MACH_KERNEL_PRIVATE */
173 #define MACH_TIMESPEC_SEC_MAX (0 - 1)
174 #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
176 #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
177 MACH_TIMESPEC_SEC_MAX, \
178 MACH_TIMESPEC_NSEC_MAX } )
179 #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
181 #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
183 (t1)->tv_nsec += (clock_res_t)(nsec); \
184 if ((clock_res_t)(nsec) > 0 && \
185 (t1)->tv_nsec >= NSEC_PER_SEC) { \
186 (t1)->tv_nsec -= NSEC_PER_SEC; \
189 else if ((clock_res_t)(nsec) < 0 && \
190 (t1)->tv_nsec < 0) { \
191 (t1)->tv_nsec += NSEC_PER_SEC; \
196 extern mach_timespec_t
clock_get_system_value(void);
198 extern mach_timespec_t
clock_get_calendar_value(void);
200 extern void clock_set_calendar_value(
201 mach_timespec_t value
);
203 extern void clock_adjust_calendar(
206 extern void clock_initialize_calendar(void);
208 extern mach_timespec_t
clock_get_calendar_offset(void);
210 #define AbsoluteTime_to_scalar(x) (*(uint64_t *)(x))
213 #define CMP_ABSOLUTETIME(t1, t2) \
214 (AbsoluteTime_to_scalar(t1) > \
215 AbsoluteTime_to_scalar(t2)? (int)+1 : \
216 (AbsoluteTime_to_scalar(t1) < \
217 AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
220 #define ADD_ABSOLUTETIME(t1, t2) \
221 (AbsoluteTime_to_scalar(t1) += \
222 AbsoluteTime_to_scalar(t2))
225 #define SUB_ABSOLUTETIME(t1, t2) \
226 (AbsoluteTime_to_scalar(t1) -= \
227 AbsoluteTime_to_scalar(t2))
229 #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
230 (AbsoluteTime_to_scalar(t1) += \
233 extern void clock_timebase_info(
234 mach_timebase_info_t info
);
236 extern void clock_get_uptime(
237 AbsoluteTime
*result
);
239 extern void clock_interval_to_deadline(
241 natural_t scale_factor
,
242 AbsoluteTime
*result
);
244 extern void clock_interval_to_absolutetime_interval(
246 natural_t scale_factor
,
247 AbsoluteTime
*result
);
249 extern void clock_absolutetime_interval_to_deadline(
250 AbsoluteTime abstime
,
251 AbsoluteTime
*result
);
253 extern void clock_deadline_for_periodic_event(
254 AbsoluteTime interval
,
255 AbsoluteTime abstime
,
256 AbsoluteTime
*deadline
);
258 extern void clock_delay_for_interval(
260 natural_t scale_factor
);
262 extern void clock_delay_until(
263 AbsoluteTime deadline
);
265 extern void absolutetime_to_nanoseconds(
266 AbsoluteTime abstime
,
269 extern void nanoseconds_to_absolutetime(
271 AbsoluteTime
*result
);
273 #endif /* _KERN_CLOCK_H_ */