]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/lock.h
e3d4649ff8a77803fcdf71ce777a3ae5067c2554
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (C) 1998 Apple Computer
38 * Mach Operating System
39 * Copyright (c) 1991,1990 Carnegie Mellon University
40 * All Rights Reserved.
42 * Permission to use, copy, modify and distribute this software and its
43 * documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
59 * any improvements or extensions that they make and grant Carnegie Mellon
60 * the rights to redistribute these changes.
67 * Machine-dependent simple locks for the i386.
74 #include <sys/appleapiopts.h>
76 #ifdef __APPLE_API_PRIVATE
78 #ifdef MACH_KERNEL_PRIVATE
80 #include <kern/macro_help.h>
81 #include <kern/assert.h>
82 #include <i386/hw_lock_types.h>
83 #include <i386/locks.h>
86 #include <mach_ldebug.h>
89 lck_mtx_t lck_mtx
; /* inlined lck_mtx, need to be first */
92 #define MUTEX_TAG 0x4d4d
95 #endif /* MACH_LDEBUG */
98 typedef lck_rw_t lock_t
;
100 extern unsigned int LockTimeOut
; /* Number of hardware ticks of a lock timeout */
103 #if defined(__GNUC__)
106 * General bit-lock routines.
109 #define bit_lock(bit,l) \
110 __asm__ volatile(" jmp 1f \n \
117 "r" (bit), "m" (*(volatile int *)(l)) : \
120 #define bit_unlock(bit,l) \
121 __asm__ volatile(" lock \n \
124 "r" (bit), "m" (*(volatile int *)(l)));
127 * Set or clear individual bits in a long word.
128 * The locked access is needed only to lock access
129 * to the word, not to individual bits.
132 #define i_bit_set(bit,l) \
133 __asm__ volatile(" lock \n \
136 "r" (bit), "m" (*(volatile int *)(l)));
138 #define i_bit_clear(bit,l) \
139 __asm__ volatile(" lock \n \
142 "r" (bit), "m" (*(volatile int *)(l)));
144 static inline unsigned long i_bit_isset(unsigned int test
, volatile unsigned long *word
)
148 __asm__
volatile("btl %2,%1\n\tsbbl %0,%0" : "=r" (bit
)
149 : "m" (word
), "ir" (test
));
153 static inline char xchgb(volatile char * cp
, char new);
155 static inline void atomic_incl(volatile long * p
, long delta
);
156 static inline void atomic_incs(volatile short * p
, short delta
);
157 static inline void atomic_incb(volatile char * p
, char delta
);
159 static inline void atomic_decl(volatile long * p
, long delta
);
160 static inline void atomic_decs(volatile short * p
, short delta
);
161 static inline void atomic_decb(volatile char * p
, char delta
);
163 static inline long atomic_getl(const volatile long * p
);
164 static inline short atomic_gets(const volatile short * p
);
165 static inline char atomic_getb(const volatile char * p
);
167 static inline void atomic_setl(volatile long * p
, long value
);
168 static inline void atomic_sets(volatile short * p
, short value
);
169 static inline void atomic_setb(volatile char * p
, char value
);
171 static inline char xchgb(volatile char * cp
, char new)
173 register char old
= new;
175 __asm__
volatile (" xchgb %0,%2" :
177 "0" (new), "m" (*(volatile char *)cp
) : "memory");
182 * Compare and exchange:
183 * - returns failure (0) if the location did not contain the old value,
184 * - returns success (1) if the location was set to the new value.
186 static inline uint32_t
187 atomic_cmpxchg(uint32_t *p
, uint32_t old
, uint32_t new)
192 "lock; cmpxchgl %1,%2; \n\t"
195 : "+a" (res
) /* %0: old value to compare, returns success */
196 : "r" (new), /* %1: new value to set */
197 "m" (*(p
)) /* %2: memory address */
202 static inline void atomic_incl(volatile long * p
, long delta
)
204 __asm__
volatile (" lock \n \
207 "r" (delta
), "m" (*(volatile long *)p
));
210 static inline void atomic_incs(volatile short * p
, short delta
)
212 __asm__
volatile (" lock \n \
215 "q" (delta
), "m" (*(volatile short *)p
));
218 static inline void atomic_incb(volatile char * p
, char delta
)
220 __asm__
volatile (" lock \n \
223 "q" (delta
), "m" (*(volatile char *)p
));
226 static inline void atomic_decl(volatile long * p
, long delta
)
228 __asm__
volatile (" lock \n \
231 "r" (delta
), "m" (*(volatile long *)p
));
234 static inline int atomic_decl_and_test(volatile long * p
, long delta
)
242 : "r" (delta
), "m" (*(volatile long *)p
));
246 static inline void atomic_decs(volatile short * p
, short delta
)
248 __asm__
volatile (" lock \n \
251 "q" (delta
), "m" (*(volatile short *)p
));
254 static inline void atomic_decb(volatile char * p
, char delta
)
256 __asm__
volatile (" lock \n \
259 "q" (delta
), "m" (*(volatile char *)p
));
262 static inline long atomic_getl(const volatile long * p
)
267 static inline short atomic_gets(const volatile short * p
)
272 static inline char atomic_getb(const volatile char * p
)
277 static inline void atomic_setl(volatile long * p
, long value
)
282 static inline void atomic_sets(volatile short * p
, short value
)
287 static inline void atomic_setb(volatile char * p
, char value
)
293 #else /* !defined(__GNUC__) */
295 extern void i_bit_set(
299 extern void i_bit_clear(
303 extern void bit_lock(
307 extern void bit_unlock(
312 * All other routines defined in __GNUC__ case lack
313 * definitions otherwise. - XXX
316 #endif /* !defined(__GNUC__) */
318 extern void kernel_preempt_check (void);
320 #endif /* MACH_KERNEL_PRIVATE */
322 #endif /* __APLE_API_PRIVATE */
324 #endif /* _I386_LOCK_H_ */
326 #endif /* KERNEL_PRIVATE */