|
|
1.1 root 1: /*
2: * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
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.
11: *
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
18: * under the License.
19: *
20: * @APPLE_LICENSE_HEADER_END@
21: */
22: /*
23: * @OSF_COPYRIGHT@
24: */
25: /*
26: * File: kern/clock.h
27: * Purpose: Data structures for the kernel alarm clock
28: * facility. This file is used only by kernel
29: * level clock facility routines.
30: */
31:
32: #ifndef _KERN_CLOCK_H_
33: #define _KERN_CLOCK_H_
34:
35: #include <libkern/OSBase.h>
36:
37: #include <mach/message.h>
38: #include <mach/clock_types.h>
39: #include <mach/mach_types.h>
40:
41: #ifdef MACH_KERNEL_PRIVATE
42: #include <ipc/ipc_port.h>
43:
44: /*
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.
49: */
50: struct alarm {
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 */
58: mach_msg_type_name_t
59: port_type; /* alarm port type */
60: struct clock *clock; /* alarm clock */
61: void *data; /* alarm data */
62: } al_alrm;
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: int al_policy; /* sched policy to notify */
69: long al_seqno; /* alarm sequence number */
70: };
71: typedef struct alarm alarm_data_t;
72:
73: /* alarm status */
74: #define ALARM_FREE 0 /* alarm is on free list */
75: #define ALARM_SLEEP 1 /* active clock_sleep() */
76: #define ALARM_CLOCK 2 /* active clock_alarm() */
77: #define ALARM_DONE 4 /* alarm has expired */
78:
79: /*
80: * Clock operations list structure. Contains vectors to machine
81: * dependent clock routines. The routines c_config, c_init, and
82: * c_gettime must be implemented for every clock device.
83: */
84: struct clock_ops {
85: int (*c_config)(void); /* configuration */
86:
87: int (*c_init)(void); /* initialize */
88:
89: kern_return_t (*c_gettime)( /* get time */
90: mach_timespec_t *cur_time);
91:
92: kern_return_t (*c_settime)( /* set time */
93: mach_timespec_t *clock_time);
94:
95: kern_return_t (*c_getattr)( /* get attributes */
96: clock_flavor_t flavor,
97: clock_attr_t attr,
98: mach_msg_type_number_t *count);
99:
100: kern_return_t (*c_setattr)( /* set attributes */
101: clock_flavor_t flavor,
102: clock_attr_t attr,
103: mach_msg_type_number_t count);
104:
105: void (*c_setalrm)( /* set next alarm */
106: mach_timespec_t *alarm_time);
107: };
108: typedef struct clock_ops *clock_ops_t;
109: typedef struct clock_ops clock_ops_data_t;
110:
111: /*
112: * Actual clock object data structure. Contains the machine
113: * dependent operations list, clock operations ports, and a
114: * chain of pending alarms.
115: */
116: struct clock {
117: clock_ops_t cl_ops; /* operations list */
118: struct ipc_port *cl_service; /* service port */
119: struct ipc_port *cl_control; /* control port */
120: struct { /* alarm chain head */
121: struct alarm *al_next;
122: } cl_alarm;
123: };
124: typedef struct clock clock_data_t;
125:
126: /*
127: * Configure the clock system.
128: */
129: extern void clock_config(void);
130: /*
131: * Initialize the clock system.
132: */
133: extern void clock_init(void);
134:
135: /*
136: * Initialize the clock ipc service facility.
137: */
138: extern void clock_service_create(void);
139:
140: /*
141: * Service clock alarm interrupts. Called from machine dependent
142: * layer at splclock(). The clock_id argument specifies the clock,
143: * and the clock_time argument gives that clock's current time.
144: */
145: extern void clock_alarm_intr(
146: clock_id_t clock_id,
147: mach_timespec_t *clock_time);
148:
149: /*
150: * Set a clock alarm for a scheduling policy.
151: */
152: extern kern_return_t clock_alarm_sp(
153: alarm_type_t alarm_type,
154: mach_timespec_t alarm_time,
155: long *alarm_id,
156: int policy,
157: void *alarm_data,
158: alarm_t alarm);
159:
160: /*
161: * Cancel a clock alarm on behalf of a scheduling policy.
162: */
163: extern kern_return_t clock_alarm_cancel_sp(
164: long alarm_id);
165:
166: extern kern_return_t clock_sleep_internal(
167: clock_t clock,
168: sleep_type_t sleep_type,
169: mach_timespec_t *sleep_time);
170:
171: typedef void (*clock_timer_func_t)(
172: AbsoluteTime timestamp);
173:
174: extern void clock_set_timer_func(
175: clock_timer_func_t func);
176:
177: extern void clock_set_timer_deadline(
178: AbsoluteTime deadline);
179:
180: #endif /* MACH_KERNEL_PRIVATE */
181:
182: #define MACH_TIMESPEC_SEC_MAX (0 - 1)
183: #define MACH_TIMESPEC_NSEC_MAX (NSEC_PER_SEC - 1)
184:
185: #define MACH_TIMESPEC_MAX ((mach_timespec_t) { \
186: MACH_TIMESPEC_SEC_MAX, \
187: MACH_TIMESPEC_NSEC_MAX } )
188: #define MACH_TIMESPEC_ZERO ((mach_timespec_t) { 0, 0 } )
189:
190: #define ADD_MACH_TIMESPEC_NSEC(t1, nsec) \
191: do { \
192: (t1)->tv_nsec += (clock_res_t)(nsec); \
193: if ((clock_res_t)(nsec) > 0 && \
194: (t1)->tv_nsec >= NSEC_PER_SEC) { \
195: (t1)->tv_nsec -= NSEC_PER_SEC; \
196: (t1)->tv_sec += 1; \
197: } \
198: else if ((clock_res_t)(nsec) < 0 && \
199: (t1)->tv_nsec < 0) { \
200: (t1)->tv_nsec += NSEC_PER_SEC; \
201: (t1)->tv_sec -= 1; \
202: } \
203: } while (0)
204:
205: extern mach_timespec_t clock_get_system_value(void);
206:
207: extern mach_timespec_t clock_get_calendar_value(void);
208:
209: extern void clock_set_calendar_value(
210: mach_timespec_t value);
211:
212: extern void clock_adjust_calendar(
213: clock_res_t nsec);
214:
215: extern void clock_initialize_calendar(void);
216:
217: extern mach_timespec_t clock_get_calendar_offset(void);
218:
219: typedef unsigned long long abstime_scalar_t;
220:
221: #define AbsoluteTime_to_scalar(x) \
222: (*(abstime_scalar_t *)(x))
223:
224: /* t1 < = > t2 */
225: #define CMP_ABSOLUTETIME(t1, t2) \
226: (AbsoluteTime_to_scalar(t1) > \
227: AbsoluteTime_to_scalar(t2)? (int)+1 : \
228: (AbsoluteTime_to_scalar(t1) < \
229: AbsoluteTime_to_scalar(t2)? (int)-1 : 0))
230:
231: /* t1 += t2 */
232: #define ADD_ABSOLUTETIME(t1, t2) \
233: (AbsoluteTime_to_scalar(t1) += \
234: AbsoluteTime_to_scalar(t2))
235:
236: /* t1 -= t2 */
237: #define SUB_ABSOLUTETIME(t1, t2) \
238: (AbsoluteTime_to_scalar(t1) -= \
239: AbsoluteTime_to_scalar(t2))
240:
241: #define ADD_ABSOLUTETIME_TICKS(t1, ticks) \
242: (AbsoluteTime_to_scalar(t1) += \
243: (integer_t)(ticks))
244:
245: extern void clock_get_timebase_info(
246: natural_t *delta,
247: natural_t *abs_to_ns_num,
248: natural_t *abs_to_ns_denom,
249: natural_t *proc_to_abs_num,
250: natural_t *proc_to_abs_denom);
251:
252: extern void clock_get_uptime(
253: AbsoluteTime *result);
254:
255: extern void clock_interval_to_deadline(
256: natural_t interval,
257: natural_t scale_factor,
258: AbsoluteTime *result);
259:
260: extern void clock_interval_to_absolutetime_interval(
261: natural_t interval,
262: natural_t scale_factor,
263: AbsoluteTime *result);
264:
265: extern void clock_absolutetime_interval_to_deadline(
266: AbsoluteTime abstime,
267: AbsoluteTime *result);
268:
269: extern void clock_deadline_for_periodic_event(
270: AbsoluteTime interval,
271: AbsoluteTime abstime,
272: AbsoluteTime *deadline);
273:
274: extern void clock_delay_for_interval(
275: natural_t interval,
276: natural_t scale_factor);
277:
278: extern void clock_delay_until(
279: AbsoluteTime deadline);
280:
281: extern void absolutetime_to_nanoseconds(
282: AbsoluteTime abstime,
283: UInt64 *result);
284:
285: extern void nanoseconds_to_absolutetime(
286: UInt64 nanoseconds,
287: AbsoluteTime *result);
288:
289: #endif /* _KERN_CLOCK_H_ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.