]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/lock.h
xnu-344.tar.gz
[apple/xnu.git] / osfmk / kern / lock.h
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 * File: kern/lock.h
52 * Author: Avadis Tevanian, Jr., Michael Wayne Young
53 * Date: 1985
54 *
55 * Higher Level Locking primitives definitions
56 */
57
58 #ifndef _KERN_LOCK_H_
59 #define _KERN_LOCK_H_
60
61 /*
62 * Configuration variables:
63 *
64 *
65 * MACH_LDEBUG: record pc and thread of callers, turn on
66 * all lock debugging.
67 *
68 *
69 * ETAP: The Event Trace Analysis Package (ETAP) monitors
70 * and records micro-kernel lock behavior and general
71 * kernel events. ETAP supports two levels of
72 * tracing for locks:
73 * - cumulative (ETAP_LOCK_ACCUMULATE)
74 * - monitored (ETAP_LOCK_MONITOR)
75 *
76 * Note: If either level of tracing is configured then
77 * ETAP_LOCK_TRACE is automatically defined to
78 * equal one.
79 *
80 * Several macros are added throughout the lock code to
81 * allow for convenient configuration.
82 */
83
84 #include <kern/simple_lock.h>
85 #include <machine/lock.h>
86 #include <mach/etap_events.h>
87 #include <mach/etap.h>
88
89 /*
90 * The Mach lock package exports the following high-level
91 * lock abstractions:
92 *
93 * Lock Type Properties
94 * mutex blocking mutual exclusion lock, intended for
95 * SMP synchronization (vanishes on a uniprocessor);
96 * supports debugging, statistics, and pre-emption
97 * lock blocking synchronization permitting multiple
98 * simultaneous readers or a single writer; supports
99 * debugging and statistics but not pre-emption
100 *
101 * In general, mutex locks are preferred over all others, as the
102 * mutex supports pre-emption and relinquishes the processor
103 * upon contention.
104 *
105 */
106
107 #include <sys/appleapiopts.h>
108
109 #ifdef __APPLE_API_PRIVATE
110
111 #ifdef MACH_KERNEL_PRIVATE
112
113 /*
114 * A simple mutex lock.
115 * Do not change the order of the fields in this structure without
116 * changing the machine-dependent assembler routines which depend
117 * on them.
118 */
119
120 #include <mach_ldebug.h>
121 #include <kern/etap_options.h>
122 #include <kern/etap_pool.h>
123
124 typedef struct {
125 hw_lock_data_t interlock;
126 hw_lock_data_t locked;
127 uint16_t waiters;
128 uint16_t promoted_pri;
129 #if MACH_LDEBUG
130 int type;
131 #define MUTEX_TAG 0x4d4d
132 vm_offset_t pc;
133 vm_offset_t thread;
134 #endif /* MACH_LDEBUG */
135 #if ETAP_LOCK_TRACE
136 union { /* Must be overlaid on the event_tablep */
137 struct event_table_chain event_table_chain;
138 struct {
139 event_table_t event_tablep; /* ptr to event table entry */
140 etap_time_t start_hold_time; /* Time of last acquistion */
141 } s;
142 } u;
143 #endif /* ETAP_LOCK_TRACE */
144 #if ETAP_LOCK_ACCUMULATE
145 cbuff_entry_t cbuff_entry; /* cumulative buffer entry */
146 #endif /* ETAP_LOCK_ACCUMULATE */
147 #if ETAP_LOCK_MONITOR
148 vm_offset_t start_pc; /* pc where lock operation began */
149 vm_offset_t end_pc; /* pc where lock operation ended */
150 #endif /* ETAP_LOCK_MONITOR */
151 } mutex_t;
152
153 #define decl_mutex_data(class,name) class mutex_t name;
154 #define mutex_addr(m) (&(m))
155
156 extern void mutex_init (mutex_t*, etap_event_t);
157 extern void mutex_lock_wait (mutex_t *, thread_act_t);
158 extern int mutex_lock_acquire (mutex_t *);
159 extern void mutex_unlock_wakeup (mutex_t*, thread_act_t);
160 extern void interlock_unlock (hw_lock_t);
161
162 #endif /* MACH_KERNEL_PRIVATE */
163
164 extern void mutex_pause (void);
165
166 #endif /* __APPLE_API_PRIVATE */
167
168 #if !defined(MACH_KERNEL_PRIVATE)
169
170 typedef struct __mutex__ mutex_t;
171
172 #endif /* MACH_KERNEL_PRIVATE */
173
174 extern mutex_t *mutex_alloc (etap_event_t);
175 extern void mutex_free (mutex_t*);
176 extern void mutex_lock (mutex_t*);
177 extern void mutex_unlock (mutex_t*);
178 extern boolean_t mutex_try (mutex_t*);
179
180 #ifdef __APPLE_API_PRIVATE
181
182 #ifdef MACH_KERNEL_PRIVATE
183
184 /*
185 * The general lock structure. Provides for multiple readers,
186 * upgrading from read to write, and sleeping until the lock
187 * can be gained.
188 *
189 * On some architectures, assembly language code in the 'inline'
190 * program fiddles the lock structures. It must be changed in
191 * concert with the structure layout.
192 *
193 * Only the "interlock" field is used for hardware exclusion;
194 * other fields are modified with normal instructions after
195 * acquiring the interlock bit.
196 */
197
198 typedef struct {
199 decl_simple_lock_data(,interlock) /* "hardware" interlock field */
200 volatile unsigned int
201 read_count:16, /* No. of accepted readers */
202 want_upgrade:1, /* Read-to-write upgrade waiting */
203 want_write:1, /* Writer is waiting, or
204 locked for write */
205 waiting:1, /* Someone is sleeping on lock */
206 can_sleep:1; /* Can attempts to lock go to sleep? */
207 #if ETAP_LOCK_TRACE
208 union { /* Must be overlaid on the event_tablep */
209 struct event_table_chain event_table_chain;
210 struct {
211 event_table_t event_tablep; /* ptr to event table entry */
212 start_data_node_t start_list; /* linked list of start times
213 and pcs */
214 } s;
215 } u;
216 #endif /* ETAP_LOCK_TRACE */
217 #if ETAP_LOCK_ACCUMULATE
218 cbuff_entry_t cbuff_write; /* write cumulative buffer entry */
219 cbuff_entry_t cbuff_read; /* read cumulative buffer entry */
220 #endif /* ETAP_LOCK_ACCUMULATE */
221 } lock_t;
222
223 /* Sleep locks must work even if no multiprocessing */
224
225 /*
226 * Complex lock operations
227 */
228
229 #if ETAP
230 /*
231 * Locks have a pointer into an event_table entry that names the
232 * corresponding lock event and controls whether it is being traced.
233 * Initially this pointer is into a read-only table event_table_init[].
234 * Once dynamic allocation becomes possible a modifiable copy of the table
235 * is allocated and pointers are set to within this copy. The pointers
236 * that were already in place at that point need to be switched to point
237 * into the copy. To do this we overlay the event_table_chain structure
238 * onto sufficiently-big elements of the various lock structures so we
239 * can sweep down this list switching the pointers. The assumption is
240 * that we will not want to enable tracing before this is done (which is
241 * after all during kernel bootstrap, before any user tasks are launched).
242 *
243 * This is admittedly rather ugly but so were the alternatives:
244 * - record the event_table pointers in a statically-allocated array
245 * (dynamic allocation not yet being available) -- but there were
246 * over 8000 of them;
247 * - add a new link field to each lock structure;
248 * - change pointers to array indices -- this adds quite a bit of
249 * arithmetic to every lock operation that might be traced.
250 */
251 #define lock_event_table(lockp) ((lockp)->u.s.event_tablep)
252 #define lock_start_hold_time(lockp) ((lockp)->u.s.start_hold_time)
253 #endif /* ETAP_LOCK_TRACE */
254
255 extern void lock_init (lock_t*,
256 boolean_t,
257 etap_event_t,
258 etap_event_t);
259
260 #endif /* MACH_KERNEL_PRIVATE */
261
262 extern unsigned int LockTimeOut; /* Standard lock timeout value */
263
264 #endif /* __APPLE_API_PRIVATE */
265
266 #if !defined(MACH_KERNEL_PRIVATE)
267
268 typedef struct __lock__ lock_t;
269 extern lock_t *lock_alloc(boolean_t, etap_event_t, etap_event_t);
270 void lock_free(lock_t *);
271
272 #endif /* MACH_KERNEL_PRIVATE */
273
274 extern void lock_write (lock_t*);
275 extern void lock_read (lock_t*);
276 extern void lock_done (lock_t*);
277 extern void lock_write_to_read (lock_t*);
278
279 #define lock_read_done(l) lock_done(l)
280 #define lock_write_done(l) lock_done(l)
281
282 extern boolean_t lock_read_to_write (lock_t*); /* vm_map is only user */
283
284 #endif /* _KERN_LOCK_H_ */