]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/lock.h
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (C) 1998 Apple Computer
36 * Mach Operating System
37 * Copyright (c) 1991,1990 Carnegie Mellon University
38 * All Rights Reserved.
40 * Permission to use, copy, modify and distribute this software and its
41 * documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
48 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
50 * Carnegie Mellon requests users of this software to return to
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
57 * any improvements or extensions that they make and grant Carnegie Mellon
58 * the rights to redistribute these changes.
65 * Machine-dependent simple locks for the i386.
72 #include <sys/appleapiopts.h>
74 #ifdef __APPLE_API_PRIVATE
76 #ifdef MACH_KERNEL_PRIVATE
78 #include <kern/macro_help.h>
79 #include <kern/assert.h>
80 #include <i386/hw_lock_types.h>
81 #include <i386/locks.h>
84 #include <mach_ldebug.h>
87 lck_mtx_t lck_mtx
; /* inlined lck_mtx, need to be first */
90 #define MUTEX_TAG 0x4d4d
93 #endif /* MACH_LDEBUG */
96 typedef lck_rw_t lock_t
;
98 extern unsigned int LockTimeOut
; /* Number of hardware ticks of a lock timeout */
101 #if defined(__GNUC__)
104 * General bit-lock routines.
107 #define bit_lock(bit,l) \
108 __asm__ volatile(" jmp 1f \n \
115 "r" (bit), "m" (*(volatile int *)(l)) : \
118 #define bit_unlock(bit,l) \
119 __asm__ volatile(" lock \n \
122 "r" (bit), "m" (*(volatile int *)(l)));
125 * Set or clear individual bits in a long word.
126 * The locked access is needed only to lock access
127 * to the word, not to individual bits.
130 #define i_bit_set(bit,l) \
131 __asm__ volatile(" lock \n \
134 "r" (bit), "m" (*(volatile int *)(l)));
136 #define i_bit_clear(bit,l) \
137 __asm__ volatile(" lock \n \
140 "r" (bit), "m" (*(volatile int *)(l)));
142 static inline unsigned long i_bit_isset(unsigned int test
, volatile unsigned long *word
)
146 __asm__
volatile("btl %2,%1\n\tsbbl %0,%0" : "=r" (bit
)
147 : "m" (word
), "ir" (test
));
151 static inline char xchgb(volatile char * cp
, char new);
153 static inline void atomic_incl(volatile long * p
, long delta
);
154 static inline void atomic_incs(volatile short * p
, short delta
);
155 static inline void atomic_incb(volatile char * p
, char delta
);
157 static inline void atomic_decl(volatile long * p
, long delta
);
158 static inline void atomic_decs(volatile short * p
, short delta
);
159 static inline void atomic_decb(volatile char * p
, char delta
);
161 static inline long atomic_getl(const volatile long * p
);
162 static inline short atomic_gets(const volatile short * p
);
163 static inline char atomic_getb(const volatile char * p
);
165 static inline void atomic_setl(volatile long * p
, long value
);
166 static inline void atomic_sets(volatile short * p
, short value
);
167 static inline void atomic_setb(volatile char * p
, char value
);
169 static inline char xchgb(volatile char * cp
, char new)
171 register char old
= new;
173 __asm__
volatile (" xchgb %0,%2" :
175 "0" (new), "m" (*(volatile char *)cp
) : "memory");
180 * Compare and exchange:
181 * - returns failure (0) if the location did not contain the old value,
182 * - returns success (1) if the location was set to the new value.
184 static inline uint32_t
185 atomic_cmpxchg(uint32_t *p
, uint32_t old
, uint32_t new)
190 "lock; cmpxchgl %1,%2; \n\t"
193 : "+a" (res
) /* %0: old value to compare, returns success */
194 : "r" (new), /* %1: new value to set */
195 "m" (*(p
)) /* %2: memory address */
200 static inline void atomic_incl(volatile long * p
, long delta
)
202 __asm__
volatile (" lock \n \
205 "r" (delta
), "m" (*(volatile long *)p
));
208 static inline void atomic_incs(volatile short * p
, short delta
)
210 __asm__
volatile (" lock \n \
213 "q" (delta
), "m" (*(volatile short *)p
));
216 static inline void atomic_incb(volatile char * p
, char delta
)
218 __asm__
volatile (" lock \n \
221 "q" (delta
), "m" (*(volatile char *)p
));
224 static inline void atomic_decl(volatile long * p
, long delta
)
226 __asm__
volatile (" lock \n \
229 "r" (delta
), "m" (*(volatile long *)p
));
232 static inline int atomic_decl_and_test(volatile long * p
, long delta
)
240 : "r" (delta
), "m" (*(volatile long *)p
));
244 static inline void atomic_decs(volatile short * p
, short delta
)
246 __asm__
volatile (" lock \n \
249 "q" (delta
), "m" (*(volatile short *)p
));
252 static inline void atomic_decb(volatile char * p
, char delta
)
254 __asm__
volatile (" lock \n \
257 "q" (delta
), "m" (*(volatile char *)p
));
260 static inline long atomic_getl(const volatile long * p
)
265 static inline short atomic_gets(const volatile short * p
)
270 static inline char atomic_getb(const volatile char * p
)
275 static inline void atomic_setl(volatile long * p
, long value
)
280 static inline void atomic_sets(volatile short * p
, short value
)
285 static inline void atomic_setb(volatile char * p
, char value
)
291 #else /* !defined(__GNUC__) */
293 extern void i_bit_set(
297 extern void i_bit_clear(
301 extern void bit_lock(
305 extern void bit_unlock(
310 * All other routines defined in __GNUC__ case lack
311 * definitions otherwise. - XXX
314 #endif /* !defined(__GNUC__) */
316 extern void kernel_preempt_check (void);
318 #endif /* MACH_KERNEL_PRIVATE */
320 #endif /* __APLE_API_PRIVATE */
322 #endif /* _I386_LOCK_H_ */
324 #endif /* KERNEL_PRIVATE */