2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
32 * Revision 1.1.1.1 1998/09/22 21:05:30 wsanchez
33 * Import of Mac OS X kernel (~semeria)
35 * Revision 1.1.1.1 1998/03/07 02:25:45 wsanchez
36 * Import of OSF Mach kernel (~mburg)
38 * Revision 1.1.4.1 1995/06/13 18:20:29 sjs
39 * Merged from flipc_shared.
42 * Revision 1.1.2.3 1995/03/09 19:42:30 rwd
43 * Move yield function out of macro and prototype.
44 * [1995/03/09 19:36:25 rwd]
46 * Revision 1.1.2.2 1995/02/21 17:23:11 randys
47 * Re-indented code to four space indentation
48 * [1995/02/21 16:25:39 randys]
50 * Revision 1.1.2.1 1994/12/20 19:02:06 randys
51 * Moved definition of flipc_simple_lock to flipc_cb.h
52 * [1994/12/20 17:34:44 randys]
54 * Separated the lock macros out into machine dependent and independent files;
55 * this is the machine independent file.
56 * [1994/12/20 16:43:38 randys]
64 * The machine independent part of the flipc_simple_locks definitions.
65 * Most of the locks definitions is in flipc_dep.h, but what isn't
66 * dependent on the platform being used is here.
70 * Note that the locks defined in this file and in flipc_dep.h are only
71 * for use by user level code. The reason why this file is visible to
72 * the kernel is that the kernel section of flipc needs to initialize
76 #ifndef _MACH_FLIPC_LOCKS_H_
77 #define _MACH_FLIPC_LOCKS_H_
79 /* Get the simple lock type. */
80 #include <mach/flipc_cb.h>
83 * Lock function prototypes. This needs to be before any lock definitions
84 * that happen to be macros.
87 /* Initializes lock. Always a macro (so that kernel code can use it without
88 library assistance). */
89 void flipc_simple_lock_init(flipc_simple_lock
*lock
);
91 /* Returns 1 if lock gained, 0 otherwise. */
92 int flipc_simple_lock_try(flipc_simple_lock
*lock
);
94 /* Returns 1 if lock is locked, 0 otherwise. */
95 int flipc_simple_lock_locked(flipc_simple_lock
*lock
);
97 /* Releases the lock. */
98 void flipc_simple_lock_release(flipc_simple_lock
*lock
);
101 void flipc_simple_lock_acquire(flipc_simple_lock
*lock
);
103 /* Take two locks. Does not hold one while spinning on the
105 void flipc_simple_lock_acquire_2(flipc_simple_lock
*lock1
,
106 flipc_simple_lock
*lock2
);
108 /* Get the machine dependent stuff. The things that need to be
109 * defined in a machine dependent fashion are:
111 * flipc_simple_lock_init (must be a macro)
112 * flipc_simple_lock_try
113 * flipc_simple_lock_locked
114 * flipc_simple_lock_release
116 * These last three don't necessarily have to be macros, but if they
117 * aren't definitions must be included in the machine dependent
118 * part of the user level library code.
120 #include <mach/machine/flipc_dep.h>
123 * Set at flipc initialization time to thread_yield argument to
127 extern void (*flipc_simple_lock_yield_fn
)(void);
130 * Machine independent definitions; defined in terms of above routines.
133 /* Take the lock. Assumes an external define saying how long to
134 spin, and an external function to call when we've spun too long. */
135 #define flipc_simple_lock_acquire(lock) \
137 int __spin_count = 0; \
139 while (flipc_simple_lock_locked(lock) \
140 || !flipc_simple_lock_try(lock)) \
141 if (++__spin_count > LOCK_SPIN_LIMIT) { \
142 (*flipc_simple_lock_yield_fn)(); \
147 /* Take two locks. Hold neither while spinning on the other. */
148 #define flipc_simple_lock_acquire_2(lock1, lock2) \
150 int __spin_count = 0; \
153 while (flipc_simple_lock_locked(lock1) \
154 || !flipc_simple_lock_try(lock1)) \
155 if (++__spin_count > LOCK_SPIN_LIMIT) { \
156 (*flipc_simple_lock_yield_fn)(); \
160 if (flipc_simple_lock_try(lock2)) \
162 flipc_simple_lock_release(lock1); \
164 while (flipc_simple_lock_locked(lock2) \
165 || !flipc_simple_lock_try(lock2)) \
166 if (++__spin_count > LOCK_SPIN_LIMIT) { \
167 (*flipc_simple_lock_yield_fn)(); \
171 if (flipc_simple_lock_try(lock1)) \
173 flipc_simple_lock_release(lock2); \
177 #endif /* _MACH_FLIPC_LOCKS_H_ */