]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/timer.c
xnu-792.24.17.tar.gz
[apple/xnu.git] / osfmk / kern / timer.c
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 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52
53 #include <stat_time.h>
54 #include <machine_timer_routines.h>
55
56 #include <mach/kern_return.h>
57 #include <mach/port.h>
58 #include <kern/queue.h>
59 #include <kern/processor.h>
60 #include <kern/thread.h>
61 #include <kern/sched_prim.h>
62 #include <kern/timer.h>
63
64 /*
65 * timer_init initializes a timer.
66 */
67 void
68 timer_init(
69 timer_t timer)
70 {
71 timer->low_bits = 0;
72 timer->high_bits = 0;
73 timer->high_bits_check = 0;
74 #if !STAT_TIME
75 timer->tstamp = 0;
76 #endif /* STAT_TIME */
77 }
78
79 /*
80 * Calculate the difference between a timer
81 * and saved value, and update the saved value.
82 */
83 uint64_t
84 timer_delta(
85 timer_t timer,
86 uint64_t *save)
87 {
88 uint64_t new, old = *save;
89
90 *save = new = timer_grab(timer);
91
92 return (new - old);
93 }
94
95 #if !STAT_TIME
96
97 /*
98 * Update the current timer (if any)
99 * and start the new timer, which
100 * could be either the same or NULL.
101 *
102 * Called with interrupts disabled.
103 */
104 void
105 timer_switch(
106 uint32_t tstamp,
107 timer_t new_timer)
108 {
109 processor_t processor = current_processor();
110 timer_t timer;
111 uint32_t old_low, low;
112
113 /*
114 * Update current timer.
115 */
116 timer = PROCESSOR_DATA(processor, current_timer);
117 if (timer != NULL) {
118 old_low = timer->low_bits;
119 low = old_low + tstamp - timer->tstamp;
120 if (low < old_low)
121 timer_update(timer, timer->high_bits + 1, low);
122 else
123 timer->low_bits = low;
124 }
125
126 /*
127 * Start new timer.
128 */
129 PROCESSOR_DATA(processor, current_timer) = new_timer;
130 if (new_timer != NULL)
131 new_timer->tstamp = tstamp;
132 }
133
134 #if MACHINE_TIMER_ROUTINES
135
136 /*
137 * Machine-dependent code implements the timer event routine.
138 */
139
140 #else /* MACHINE_TIMER_ROUTINES */
141
142 /*
143 * Update the current timer and start
144 * the new timer. Requires a current
145 * and new timer.
146 *
147 * Called with interrupts disabled.
148 */
149 void
150 timer_event(
151 uint32_t tstamp,
152 timer_t new_timer)
153 {
154 processor_t processor = current_processor();
155 timer_t timer;
156 uint32_t old_low, low;
157
158 /*
159 * Update current timer.
160 */
161 timer = PROCESSOR_DATA(processor, current_timer);
162 old_low = timer->low_bits;
163 low = old_low + tstamp - timer->tstamp;
164 if (low < old_low)
165 timer_update(timer, timer->high_bits + 1, low);
166 else
167 timer->low_bits = low;
168
169 /*
170 * Start new timer.
171 */
172 PROCESSOR_DATA(processor, current_timer) = new_timer;
173 new_timer->tstamp = tstamp;
174 }
175
176 #endif /* MACHINE_TIMER_ROUTINES */
177
178 #endif /* STAT_TIME */