]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_spinlock.h
808a417f5889816cde0c07687c4fabb6ae0927a3
[apple/libc.git] / pthreads / pthread_spinlock.h
1 /*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
27 * All Rights Reserved
28 *
29 * Permission to use, copy, modify, and distribute this software and
30 * its documentation for any purpose and without fee is hereby granted,
31 * provided that the above copyright notice appears in all copies and
32 * that both the copyright notice and this permission notice appear in
33 * supporting documentation.
34 *
35 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
36 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 * FOR A PARTICULAR PURPOSE.
38 *
39 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
40 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
41 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
42 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 *
45 */
46 /*
47 * MkLinux
48 */
49
50 /*
51 * POSIX Threads - IEEE 1003.1c
52 */
53
54 #ifndef _POSIX_PTHREAD_SPINLOCK_H
55 #define _POSIX_PTHREAD_SPINLOCK_H
56
57 #include <mach/mach.h>
58 #define __APPLE_API_PRIVATE
59 #include <machine/cpu_capabilities.h>
60
61 #ifndef __POSIX_LIB__
62 #define __POSIX_LIB__
63 #endif
64
65 #include "pthread_machdep.h" /* Machine-dependent definitions. */
66
67 /* Number of times to spin when the lock is unavailable and we are on a
68 multiprocessor. On a uniprocessor we yield the processor immediately. */
69 #define MP_SPIN_TRIES 1000
70 extern int _spin_tries;
71 extern int __is_threaded;
72
73 /* Internal mutex locks for data structures */
74 #define TRY_LOCK(v) (!__is_threaded || _spin_lock_try((pthread_lock_t *)&(v)))
75
76 /* _DO_SPINLOCK_LOCK() takes a (pthread_lock_t *) */
77 #define _DO_SPINLOCK_LOCK(v) _spin_lock(v)
78
79 /* _DO_SPINLOCK_UNLOCK() takes a (pthread_lock_t *) */
80 #define _DO_SPINLOCK_UNLOCK(v) _spin_unlock(v)
81
82 /* LOCK() takes a (pthread_lock_t) */
83 #define LOCK(v) \
84 do { \
85 if (__is_threaded) { \
86 _DO_SPINLOCK_LOCK((pthread_lock_t *)&(v)); \
87 } \
88 } while (0)
89
90 /* UNLOCK() takes a (pthread_lock_t) */
91 #define UNLOCK(v) \
92 do { \
93 if (__is_threaded) { \
94 _DO_SPINLOCK_UNLOCK((pthread_lock_t *)&(v)); \
95 } \
96 } while (0)
97
98 /* Prototypes. */
99
100 /* Functions defined in machine-dependent files. */
101 extern void _spin_lock(pthread_lock_t *lockp);
102 extern int _spin_lock_try(pthread_lock_t *lockp);
103 extern void _spin_unlock(pthread_lock_t *lockp);
104
105 #endif /* _POSIX_PTHREAD_SPINLOCK_H */