]>
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 */
91 decl_simple_lock_data(,interlock
) /* "hardware" interlock field */
93 read_count
:16, /* No. of accepted readers */
94 want_upgrade
:1, /* Read-to-write upgrade waiting */
95 want_write
:1, /* Writer is waiting, or locked for write */
96 waiting
:1, /* Someone is sleeping on lock */
97 can_sleep
:1; /* Can attempts to lock go to sleep? */
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(long * p
, long delta
);
156 static inline void atomic_incs(short * p
, short delta
);
157 static inline void atomic_incb(char * p
, char delta
);
159 static inline void atomic_decl(long * p
, long delta
);
160 static inline void atomic_decs(short * p
, short delta
);
161 static inline void atomic_decb(char * p
, char delta
);
163 static inline long atomic_getl(long * p
);
164 static inline short atomic_gets(short * p
);
165 static inline char atomic_getb(char * p
);
167 static inline void atomic_setl(long * p
, long value
);
168 static inline void atomic_sets(short * p
, short value
);
169 static inline void atomic_setb(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 uint64_t
203 atomic_load64(uint64_t *quadp
)
208 " lock; cmpxchg8b %1"
210 : "m" (*quadp
), "a" (0), "d" (0), "b" (0), "c" (0));
214 static inline uint64_t
215 atomic_loadstore64(uint64_t *quadp
, uint64_t new)
222 " lock; cmpxchg8b %1 \n\t"
226 "b" ((uint32_t)new), "c" ((uint32_t)(new >> 32)));
230 static inline void atomic_incl(long * p
, long delta
)
232 __asm__
volatile (" lock \n \
235 "r" (delta
), "m" (*(volatile long *)p
));
238 static inline void atomic_incs(short * p
, short delta
)
240 __asm__
volatile (" lock \n \
243 "q" (delta
), "m" (*(volatile short *)p
));
246 static inline void atomic_incb(char * p
, char delta
)
248 __asm__
volatile (" lock \n \
251 "q" (delta
), "m" (*(volatile char *)p
));
254 static inline void atomic_decl(long * p
, long delta
)
256 __asm__
volatile (" lock \n \
259 "r" (delta
), "m" (*(volatile long *)p
));
262 static inline int atomic_decl_and_test(long * p
, long delta
)
270 : "r" (delta
), "m" (*(volatile long *)p
));
274 static inline void atomic_decs(short * p
, short delta
)
276 __asm__
volatile (" lock \n \
279 "q" (delta
), "m" (*(volatile short *)p
));
282 static inline void atomic_decb(char * p
, char delta
)
284 __asm__
volatile (" lock \n \
287 "q" (delta
), "m" (*(volatile char *)p
));
290 static inline long atomic_getl(long * p
)
295 static inline short atomic_gets(short * p
)
300 static inline char atomic_getb(char * p
)
305 static inline void atomic_setl(long * p
, long value
)
310 static inline void atomic_sets(short * p
, short value
)
315 static inline void atomic_setb(char * p
, char value
)
321 #else /* !defined(__GNUC__) */
323 extern void i_bit_set(
327 extern void i_bit_clear(
331 extern void bit_lock(
335 extern void bit_unlock(
340 * All other routines defined in __GNUC__ case lack
341 * definitions otherwise. - XXX
344 #endif /* !defined(__GNUC__) */
346 extern void kernel_preempt_check (void);
348 #endif /* MACH_KERNEL_PRIVATE */
350 #endif /* __APLE_API_PRIVATE */
352 #endif /* _I386_LOCK_H_ */
354 #endif /* KERNEL_PRIVATE */