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@
23 * Copyright (C) 1998 Apple Computer
30 * Mach Operating System
31 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
32 * All Rights Reserved.
34 * Permission to use, copy, modify and distribute this software and its
35 * documentation is hereby granted, provided that both the copyright
36 * notice and this permission notice appear in all copies of the
37 * software, derivative works or modified versions, and any portions
38 * thereof, and that both notices appear in supporting documentation.
40 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
41 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
42 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
44 * Carnegie Mellon requests users of this software to return to
46 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
47 * School of Computer Science
48 * Carnegie Mellon University
49 * Pittsburgh PA 15213-3890
51 * any improvements or extensions that they make and grant Carnegie Mellon
52 * the rights to redistribute these changes.
56 * Author: Avadis Tevanian, Jr., Michael Wayne Young
59 * Higher Level Locking primitives definitions
66 * Configuration variables:
69 * MACH_LDEBUG: record pc and thread of callers, turn on
73 * ETAP: The Event Trace Analysis Package (ETAP) monitors
74 * and records micro-kernel lock behavior and general
75 * kernel events. ETAP supports two levels of
77 * - cumulative (ETAP_LOCK_ACCUMULATE)
78 * - monitored (ETAP_LOCK_MONITOR)
80 * Note: If either level of tracing is configured then
81 * ETAP_LOCK_TRACE is automatically defined to
84 * Several macros are added throughout the lock code to
85 * allow for convenient configuration.
88 #include <kern/simple_lock.h>
89 #include <machine/lock.h>
90 #include <mach/etap_events.h>
91 #include <mach/etap.h>
94 * The Mach lock package exports the following high-level
97 * Lock Type Properties
98 * mutex blocking mutual exclusion lock, intended for
99 * SMP synchronization (vanishes on a uniprocessor);
100 * supports debugging, statistics, and pre-emption
101 * lock blocking synchronization permitting multiple
102 * simultaneous readers or a single writer; supports
103 * debugging and statistics but not pre-emption
105 * In general, mutex locks are preferred over all others, as the
106 * mutex supports pre-emption and relinquishes the processor
112 * A simple mutex lock.
113 * Do not change the order of the fields in this structure without
114 * changing the machine-dependent assembler routines which depend
117 #ifdef MACH_KERNEL_PRIVATE
118 #include <mach_ldebug.h>
119 #include <kern/etap_options.h>
120 #include <kern/etap_pool.h>
123 hw_lock_data_t interlock
;
124 hw_lock_data_t locked
;
128 #define MUTEX_TAG 0x4d4d
131 #endif /* MACH_LDEBUG */
133 union { /* Must be overlaid on the event_tablep */
134 struct event_table_chain event_table_chain
;
136 event_table_t event_tablep
; /* ptr to event table entry */
137 etap_time_t start_hold_time
; /* Time of last acquistion */
140 #endif /* ETAP_LOCK_TRACE */
141 #if ETAP_LOCK_ACCUMULATE
142 cbuff_entry_t cbuff_entry
; /* cumulative buffer entry */
143 #endif /* ETAP_LOCK_ACCUMULATE */
144 #if ETAP_LOCK_MONITOR
145 vm_offset_t start_pc
; /* pc where lock operation began */
146 vm_offset_t end_pc
; /* pc where lock operation ended */
147 #endif /* ETAP_LOCK_MONITOR */
150 #define decl_mutex_data(class,name) class mutex_t name;
151 #define mutex_addr(m) (&(m))
153 extern void mutex_init (mutex_t
*, etap_event_t
);
154 extern void mutex_lock_wait (mutex_t
*);
155 extern void mutex_unlock_wakeup (mutex_t
*);
156 extern void interlock_unlock (hw_lock_t
);
158 #else /* MACH_KERNEL_PRIVATE */
160 typedef struct __mutex__ mutex_t
;
163 extern void _mutex_lock (mutex_t
*);
164 extern boolean_t
_mutex_try (mutex_t
*);
166 #endif /* !MACH_KERNEL_PRIVATE */
168 extern mutex_t
*mutex_alloc (etap_event_t
);
169 extern void mutex_free (mutex_t
*);
170 extern void mutex_lock (mutex_t
*);
171 extern void mutex_unlock (mutex_t
*);
172 extern boolean_t
mutex_try (mutex_t
*);
174 extern void mutex_pause (void);
177 * The general lock structure. Provides for multiple readers,
178 * upgrading from read to write, and sleeping until the lock
181 * On some architectures, assembly language code in the 'inline'
182 * program fiddles the lock structures. It must be changed in
183 * concert with the structure layout.
185 * Only the "interlock" field is used for hardware exclusion;
186 * other fields are modified with normal instructions after
187 * acquiring the interlock bit.
189 #ifdef MACH_KERNEL_PRIVATE
191 decl_simple_lock_data(,interlock
) /* "hardware" interlock field */
192 volatile unsigned int
193 read_count
:16, /* No. of accepted readers */
194 want_upgrade
:1, /* Read-to-write upgrade waiting */
195 want_write
:1, /* Writer is waiting, or
197 waiting
:1, /* Someone is sleeping on lock */
198 can_sleep
:1; /* Can attempts to lock go to sleep? */
200 union { /* Must be overlaid on the event_tablep */
201 struct event_table_chain event_table_chain
;
203 event_table_t event_tablep
; /* ptr to event table entry */
204 start_data_node_t start_list
; /* linked list of start times
208 #endif /* ETAP_LOCK_TRACE */
209 #if ETAP_LOCK_ACCUMULATE
210 cbuff_entry_t cbuff_write
; /* write cumulative buffer entry */
211 cbuff_entry_t cbuff_read
; /* read cumulative buffer entry */
212 #endif /* ETAP_LOCK_ACCUMULATE */
215 /* Sleep locks must work even if no multiprocessing */
218 * Complex lock operations
223 * Locks have a pointer into an event_table entry that names the
224 * corresponding lock event and controls whether it is being traced.
225 * Initially this pointer is into a read-only table event_table_init[].
226 * Once dynamic allocation becomes possible a modifiable copy of the table
227 * is allocated and pointers are set to within this copy. The pointers
228 * that were already in place at that point need to be switched to point
229 * into the copy. To do this we overlay the event_table_chain structure
230 * onto sufficiently-big elements of the various lock structures so we
231 * can sweep down this list switching the pointers. The assumption is
232 * that we will not want to enable tracing before this is done (which is
233 * after all during kernel bootstrap, before any user tasks are launched).
235 * This is admittedly rather ugly but so were the alternatives:
236 * - record the event_table pointers in a statically-allocated array
237 * (dynamic allocation not yet being available) -- but there were
239 * - add a new link field to each lock structure;
240 * - change pointers to array indices -- this adds quite a bit of
241 * arithmetic to every lock operation that might be traced.
243 #define lock_event_table(lockp) ((lockp)->u.s.event_tablep)
244 #define lock_start_hold_time(lockp) ((lockp)->u.s.start_hold_time)
245 #endif /* ETAP_LOCK_TRACE */
247 extern void lock_init (lock_t
*,
252 #else /* MACH_KERNEL_PRIVATE */
254 typedef struct __lock__ lock_t
;
255 extern lock_t
*lock_alloc(boolean_t
, etap_event_t
, etap_event_t
);
256 void lock_free(lock_t
*);
258 #endif /* !MACH_KERNEL_PRIVATE */
260 extern void lock_write (lock_t
*);
261 extern void lock_read (lock_t
*);
262 extern void lock_done (lock_t
*);
263 extern void lock_write_to_read (lock_t
*);
265 #define lock_read_done(l) lock_done(l)
266 #define lock_write_done(l) lock_done(l)
268 extern boolean_t
lock_read_to_write (lock_t
*); /* vm_map is only user */
269 extern unsigned int LockTimeOut
; /* Standard lock timeout value */
271 #endif /* _KERN_LOCK_H_ */