]>
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 LockTimeOutTSC
; /* Lock timeout in TSC ticks */
99 extern unsigned int LockTimeOut
; /* Lock timeout in absolute time */
102 #if defined(__GNUC__)
105 * General bit-lock routines.
108 #define bit_lock(bit,l) \
109 __asm__ volatile(" jmp 1f \n \
116 "r" (bit), "m" (*(volatile int *)(l)) : \
119 #define bit_unlock(bit,l) \
120 __asm__ volatile(" lock \n \
123 "r" (bit), "m" (*(volatile int *)(l)));
126 * Set or clear individual bits in a long word.
127 * The locked access is needed only to lock access
128 * to the word, not to individual bits.
131 #define i_bit_set(bit,l) \
132 __asm__ volatile(" lock \n \
135 "r" (bit), "m" (*(volatile int *)(l)));
137 #define i_bit_clear(bit,l) \
138 __asm__ volatile(" lock \n \
141 "r" (bit), "m" (*(volatile int *)(l)));
143 static inline unsigned long i_bit_isset(unsigned int test
, volatile unsigned long *word
)
147 __asm__
volatile("btl %2,%1\n\tsbbl %0,%0" : "=r" (bit
)
148 : "m" (word
), "ir" (test
));
152 static inline char xchgb(volatile char * cp
, char new);
154 static inline void atomic_incl(volatile long * p
, long delta
);
155 static inline void atomic_incs(volatile short * p
, short delta
);
156 static inline void atomic_incb(volatile char * p
, char delta
);
158 static inline void atomic_decl(volatile long * p
, long delta
);
159 static inline void atomic_decs(volatile short * p
, short delta
);
160 static inline void atomic_decb(volatile char * p
, char delta
);
162 static inline long atomic_getl(const volatile long * p
);
163 static inline short atomic_gets(const volatile short * p
);
164 static inline char atomic_getb(const volatile char * p
);
166 static inline void atomic_setl(volatile long * p
, long value
);
167 static inline void atomic_sets(volatile short * p
, short value
);
168 static inline void atomic_setb(volatile char * p
, char value
);
170 static inline char xchgb(volatile char * cp
, char new)
172 register char old
= new;
174 __asm__
volatile (" xchgb %0,%2" :
176 "0" (new), "m" (*(volatile char *)cp
) : "memory");
181 * Compare and exchange:
182 * - returns failure (0) if the location did not contain the old value,
183 * - returns success (1) if the location was set to the new value.
185 static inline uint32_t
186 atomic_cmpxchg(uint32_t *p
, uint32_t old
, uint32_t new)
191 "lock; cmpxchgl %1,%2; \n\t"
194 : "+a" (res
) /* %0: old value to compare, returns success */
195 : "r" (new), /* %1: new value to set */
196 "m" (*(p
)) /* %2: memory address */
201 static inline void atomic_incl(volatile long * p
, long delta
)
203 __asm__
volatile (" lock \n \
206 "r" (delta
), "m" (*(volatile long *)p
));
209 static inline void atomic_incs(volatile short * p
, short delta
)
211 __asm__
volatile (" lock \n \
214 "q" (delta
), "m" (*(volatile short *)p
));
217 static inline void atomic_incb(volatile char * p
, char delta
)
219 __asm__
volatile (" lock \n \
222 "q" (delta
), "m" (*(volatile char *)p
));
225 static inline void atomic_decl(volatile long * p
, long delta
)
227 __asm__
volatile (" lock \n \
230 "r" (delta
), "m" (*(volatile long *)p
));
233 static inline int atomic_decl_and_test(volatile long * p
, long delta
)
241 : "r" (delta
), "m" (*(volatile long *)p
));
245 static inline void atomic_decs(volatile short * p
, short delta
)
247 __asm__
volatile (" lock \n \
250 "q" (delta
), "m" (*(volatile short *)p
));
253 static inline void atomic_decb(volatile char * p
, char delta
)
255 __asm__
volatile (" lock \n \
258 "q" (delta
), "m" (*(volatile char *)p
));
261 static inline long atomic_getl(const volatile long * p
)
266 static inline short atomic_gets(const volatile short * p
)
271 static inline char atomic_getb(const volatile char * p
)
276 static inline void atomic_setl(volatile long * p
, long value
)
281 static inline void atomic_sets(volatile short * p
, short value
)
286 static inline void atomic_setb(volatile char * p
, char value
)
292 #else /* !defined(__GNUC__) */
294 extern void i_bit_set(
298 extern void i_bit_clear(
302 extern void bit_lock(
306 extern void bit_unlock(
311 * All other routines defined in __GNUC__ case lack
312 * definitions otherwise. - XXX
315 #endif /* !defined(__GNUC__) */
317 extern void kernel_preempt_check (void);
319 #endif /* MACH_KERNEL_PRIVATE */
321 #endif /* __APLE_API_PRIVATE */
323 #endif /* _I386_LOCK_H_ */
325 #endif /* KERNEL_PRIVATE */