]>
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_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 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.
59 * Machine-dependent simple locks for the i386.
66 #include <sys/appleapiopts.h>
68 #ifdef __APPLE_API_PRIVATE
70 #ifdef MACH_KERNEL_PRIVATE
72 #include <kern/macro_help.h>
73 #include <kern/assert.h>
74 #include <i386/hw_lock_types.h>
75 #include <i386/locks.h>
78 #include <mach_ldebug.h>
81 lck_mtx_t lck_mtx
; /* inlined lck_mtx, need to be first */
84 #define MUTEX_TAG 0x4d4d
87 #endif /* MACH_LDEBUG */
90 typedef lck_rw_t lock_t
;
92 extern unsigned int LockTimeOut
; /* Number of hardware ticks of a lock timeout */
98 * General bit-lock routines.
101 #define bit_lock(bit,l) \
102 __asm__ volatile(" jmp 1f \n \
109 "r" (bit), "m" (*(volatile int *)(l)) : \
112 #define bit_unlock(bit,l) \
113 __asm__ volatile(" lock \n \
116 "r" (bit), "m" (*(volatile int *)(l)));
119 * Set or clear individual bits in a long word.
120 * The locked access is needed only to lock access
121 * to the word, not to individual bits.
124 #define i_bit_set(bit,l) \
125 __asm__ volatile(" lock \n \
128 "r" (bit), "m" (*(volatile int *)(l)));
130 #define i_bit_clear(bit,l) \
131 __asm__ volatile(" lock \n \
134 "r" (bit), "m" (*(volatile int *)(l)));
136 static inline unsigned long i_bit_isset(unsigned int test
, volatile unsigned long *word
)
140 __asm__
volatile("btl %2,%1\n\tsbbl %0,%0" : "=r" (bit
)
141 : "m" (word
), "ir" (test
));
145 static inline char xchgb(volatile char * cp
, char new);
147 static inline void atomic_incl(volatile long * p
, long delta
);
148 static inline void atomic_incs(volatile short * p
, short delta
);
149 static inline void atomic_incb(volatile char * p
, char delta
);
151 static inline void atomic_decl(volatile long * p
, long delta
);
152 static inline void atomic_decs(volatile short * p
, short delta
);
153 static inline void atomic_decb(volatile char * p
, char delta
);
155 static inline long atomic_getl(const volatile long * p
);
156 static inline short atomic_gets(const volatile short * p
);
157 static inline char atomic_getb(const volatile char * p
);
159 static inline void atomic_setl(volatile long * p
, long value
);
160 static inline void atomic_sets(volatile short * p
, short value
);
161 static inline void atomic_setb(volatile char * p
, char value
);
163 static inline char xchgb(volatile char * cp
, char new)
165 register char old
= new;
167 __asm__
volatile (" xchgb %0,%2" :
169 "0" (new), "m" (*(volatile char *)cp
) : "memory");
174 * Compare and exchange:
175 * - returns failure (0) if the location did not contain the old value,
176 * - returns success (1) if the location was set to the new value.
178 static inline uint32_t
179 atomic_cmpxchg(uint32_t *p
, uint32_t old
, uint32_t new)
184 "lock; cmpxchgl %1,%2; \n\t"
187 : "+a" (res
) /* %0: old value to compare, returns success */
188 : "r" (new), /* %1: new value to set */
189 "m" (*(p
)) /* %2: memory address */
194 static inline void atomic_incl(volatile long * p
, long delta
)
196 __asm__
volatile (" lock \n \
199 "r" (delta
), "m" (*(volatile long *)p
));
202 static inline void atomic_incs(volatile short * p
, short delta
)
204 __asm__
volatile (" lock \n \
207 "q" (delta
), "m" (*(volatile short *)p
));
210 static inline void atomic_incb(volatile char * p
, char delta
)
212 __asm__
volatile (" lock \n \
215 "q" (delta
), "m" (*(volatile char *)p
));
218 static inline void atomic_decl(volatile long * p
, long delta
)
220 __asm__
volatile (" lock \n \
223 "r" (delta
), "m" (*(volatile long *)p
));
226 static inline int atomic_decl_and_test(volatile long * p
, long delta
)
234 : "r" (delta
), "m" (*(volatile long *)p
));
238 static inline void atomic_decs(volatile short * p
, short delta
)
240 __asm__
volatile (" lock \n \
243 "q" (delta
), "m" (*(volatile short *)p
));
246 static inline void atomic_decb(volatile char * p
, char delta
)
248 __asm__
volatile (" lock \n \
251 "q" (delta
), "m" (*(volatile char *)p
));
254 static inline long atomic_getl(const volatile long * p
)
259 static inline short atomic_gets(const volatile short * p
)
264 static inline char atomic_getb(const volatile char * p
)
269 static inline void atomic_setl(volatile long * p
, long value
)
274 static inline void atomic_sets(volatile short * p
, short value
)
279 static inline void atomic_setb(volatile char * p
, char value
)
285 #else /* !defined(__GNUC__) */
287 extern void i_bit_set(
291 extern void i_bit_clear(
295 extern void bit_lock(
299 extern void bit_unlock(
304 * All other routines defined in __GNUC__ case lack
305 * definitions otherwise. - XXX
308 #endif /* !defined(__GNUC__) */
310 extern void kernel_preempt_check (void);
312 #endif /* MACH_KERNEL_PRIVATE */
314 #endif /* __APLE_API_PRIVATE */
316 #endif /* _I386_LOCK_H_ */
318 #endif /* KERNEL_PRIVATE */