2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
55 * Author: Avadis Tevanian, Jr., Michael Wayne Young
58 * Higher Level Locking primitives definitions
65 * Configuration variables:
68 * MACH_LDEBUG: record pc and thread of callers, turn on
72 * ETAP: The Event Trace Analysis Package (ETAP) monitors
73 * and records micro-kernel lock behavior and general
74 * kernel events. ETAP supports two levels of
76 * - cumulative (ETAP_LOCK_ACCUMULATE)
77 * - monitored (ETAP_LOCK_MONITOR)
79 * Note: If either level of tracing is configured then
80 * ETAP_LOCK_TRACE is automatically defined to
83 * Several macros are added throughout the lock code to
84 * allow for convenient configuration.
87 #include <kern/simple_lock.h>
88 #include <machine/lock.h>
89 #include <mach/etap_events.h>
90 #include <mach/etap.h>
93 * The Mach lock package exports the following high-level
96 * Lock Type Properties
97 * mutex blocking mutual exclusion lock, intended for
98 * SMP synchronization (vanishes on a uniprocessor);
99 * supports debugging, statistics, and pre-emption
100 * lock blocking synchronization permitting multiple
101 * simultaneous readers or a single writer; supports
102 * debugging and statistics but not pre-emption
104 * In general, mutex locks are preferred over all others, as the
105 * mutex supports pre-emption and relinquishes the processor
110 #include <sys/appleapiopts.h>
112 #ifdef __APPLE_API_PRIVATE
114 #ifdef MACH_KERNEL_PRIVATE
117 * A simple mutex lock.
118 * Do not change the order of the fields in this structure without
119 * changing the machine-dependent assembler routines which depend
123 #include <mach_ldebug.h>
124 #include <kern/etap_options.h>
125 #include <kern/etap_pool.h>
128 hw_lock_data_t interlock
;
129 hw_lock_data_t locked
;
131 uint16_t promoted_pri
;
134 #define MUTEX_TAG 0x4d4d
137 #endif /* MACH_LDEBUG */
139 union { /* Must be overlaid on the event_tablep */
140 struct event_table_chain event_table_chain
;
142 event_table_t event_tablep
; /* ptr to event table entry */
143 etap_time_t start_hold_time
; /* Time of last acquistion */
146 #endif /* ETAP_LOCK_TRACE */
147 #if ETAP_LOCK_ACCUMULATE
148 cbuff_entry_t cbuff_entry
; /* cumulative buffer entry */
149 #endif /* ETAP_LOCK_ACCUMULATE */
150 #if ETAP_LOCK_MONITOR
151 vm_offset_t start_pc
; /* pc where lock operation began */
152 vm_offset_t end_pc
; /* pc where lock operation ended */
153 #endif /* ETAP_LOCK_MONITOR */
156 #define decl_mutex_data(class,name) class mutex_t name;
157 #define mutex_addr(m) (&(m))
159 extern void mutex_init (mutex_t
*, etap_event_t
);
160 extern void mutex_lock_wait (mutex_t
*, thread_act_t
);
161 extern int mutex_lock_acquire (mutex_t
*);
162 extern void mutex_unlock_wakeup (mutex_t
*, thread_act_t
);
163 extern void interlock_unlock (hw_lock_t
);
165 #endif /* MACH_KERNEL_PRIVATE */
167 extern void mutex_pause (void);
169 #endif /* __APPLE_API_PRIVATE */
171 #if !defined(MACH_KERNEL_PRIVATE)
173 typedef struct __mutex__ mutex_t
;
175 #endif /* MACH_KERNEL_PRIVATE */
177 extern mutex_t
*mutex_alloc (etap_event_t
);
178 extern void mutex_free (mutex_t
*);
179 extern void mutex_lock (mutex_t
*);
180 extern void mutex_unlock (mutex_t
*);
181 extern boolean_t
mutex_try (mutex_t
*);
183 #ifdef __APPLE_API_PRIVATE
185 #ifdef MACH_KERNEL_PRIVATE
188 * The general lock structure. Provides for multiple readers,
189 * upgrading from read to write, and sleeping until the lock
192 * On some architectures, assembly language code in the 'inline'
193 * program fiddles the lock structures. It must be changed in
194 * concert with the structure layout.
196 * Only the "interlock" field is used for hardware exclusion;
197 * other fields are modified with normal instructions after
198 * acquiring the interlock bit.
202 decl_simple_lock_data(,interlock
) /* "hardware" interlock field */
203 volatile unsigned int
204 read_count
:16, /* No. of accepted readers */
205 want_upgrade
:1, /* Read-to-write upgrade waiting */
206 want_write
:1, /* Writer is waiting, or
208 waiting
:1, /* Someone is sleeping on lock */
209 can_sleep
:1; /* Can attempts to lock go to sleep? */
211 union { /* Must be overlaid on the event_tablep */
212 struct event_table_chain event_table_chain
;
214 event_table_t event_tablep
; /* ptr to event table entry */
215 start_data_node_t start_list
; /* linked list of start times
219 #endif /* ETAP_LOCK_TRACE */
220 #if ETAP_LOCK_ACCUMULATE
221 cbuff_entry_t cbuff_write
; /* write cumulative buffer entry */
222 cbuff_entry_t cbuff_read
; /* read cumulative buffer entry */
223 #endif /* ETAP_LOCK_ACCUMULATE */
226 /* Sleep locks must work even if no multiprocessing */
229 * Complex lock operations
234 * Locks have a pointer into an event_table entry that names the
235 * corresponding lock event and controls whether it is being traced.
236 * Initially this pointer is into a read-only table event_table_init[].
237 * Once dynamic allocation becomes possible a modifiable copy of the table
238 * is allocated and pointers are set to within this copy. The pointers
239 * that were already in place at that point need to be switched to point
240 * into the copy. To do this we overlay the event_table_chain structure
241 * onto sufficiently-big elements of the various lock structures so we
242 * can sweep down this list switching the pointers. The assumption is
243 * that we will not want to enable tracing before this is done (which is
244 * after all during kernel bootstrap, before any user tasks are launched).
246 * This is admittedly rather ugly but so were the alternatives:
247 * - record the event_table pointers in a statically-allocated array
248 * (dynamic allocation not yet being available) -- but there were
250 * - add a new link field to each lock structure;
251 * - change pointers to array indices -- this adds quite a bit of
252 * arithmetic to every lock operation that might be traced.
254 #define lock_event_table(lockp) ((lockp)->u.s.event_tablep)
255 #define lock_start_hold_time(lockp) ((lockp)->u.s.start_hold_time)
256 #endif /* ETAP_LOCK_TRACE */
258 extern void lock_init (lock_t
*,
263 #endif /* MACH_KERNEL_PRIVATE */
265 extern unsigned int LockTimeOut
; /* Standard lock timeout value */
267 #endif /* __APPLE_API_PRIVATE */
269 #if !defined(MACH_KERNEL_PRIVATE)
271 typedef struct __lock__ lock_t
;
272 extern lock_t
*lock_alloc(boolean_t
, etap_event_t
, etap_event_t
);
273 void lock_free(lock_t
*);
275 #endif /* MACH_KERNEL_PRIVATE */
277 extern void lock_write (lock_t
*);
278 extern void lock_read (lock_t
*);
279 extern void lock_done (lock_t
*);
280 extern void lock_write_to_read (lock_t
*);
282 #define lock_read_done(l) lock_done(l)
283 #define lock_write_done(l) lock_done(l)
285 extern boolean_t
lock_read_to_write (lock_t
*); /* vm_map is only user */
287 #endif /* _KERN_LOCK_H_ */