2 * Copyright (c) 2000 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@
29 * Revision 1.1.1.1 1998/09/22 21:05:30 wsanchez
30 * Import of Mac OS X kernel (~semeria)
32 * Revision 1.1.1.1 1998/03/07 02:25:45 wsanchez
33 * Import of OSF Mach kernel (~mburg)
35 * Revision 1.1.4.1 1995/06/13 18:20:29 sjs
36 * Merged from flipc_shared.
39 * Revision 1.1.2.3 1995/03/09 19:42:30 rwd
40 * Move yield function out of macro and prototype.
41 * [1995/03/09 19:36:25 rwd]
43 * Revision 1.1.2.2 1995/02/21 17:23:11 randys
44 * Re-indented code to four space indentation
45 * [1995/02/21 16:25:39 randys]
47 * Revision 1.1.2.1 1994/12/20 19:02:06 randys
48 * Moved definition of flipc_simple_lock to flipc_cb.h
49 * [1994/12/20 17:34:44 randys]
51 * Separated the lock macros out into machine dependent and independent files;
52 * this is the machine independent file.
53 * [1994/12/20 16:43:38 randys]
61 * The machine independent part of the flipc_simple_locks definitions.
62 * Most of the locks definitions is in flipc_dep.h, but what isn't
63 * dependent on the platform being used is here.
67 * Note that the locks defined in this file and in flipc_dep.h are only
68 * for use by user level code. The reason why this file is visible to
69 * the kernel is that the kernel section of flipc needs to initialize
73 #ifndef _MACH_FLIPC_LOCKS_H_
74 #define _MACH_FLIPC_LOCKS_H_
76 /* Get the simple lock type. */
77 #include <mach/flipc_cb.h>
80 * Lock function prototypes. This needs to be before any lock definitions
81 * that happen to be macros.
84 /* Initializes lock. Always a macro (so that kernel code can use it without
85 library assistance). */
86 void flipc_simple_lock_init(flipc_simple_lock
*lock
);
88 /* Returns 1 if lock gained, 0 otherwise. */
89 int flipc_simple_lock_try(flipc_simple_lock
*lock
);
91 /* Returns 1 if lock is locked, 0 otherwise. */
92 int flipc_simple_lock_locked(flipc_simple_lock
*lock
);
94 /* Releases the lock. */
95 void flipc_simple_lock_release(flipc_simple_lock
*lock
);
98 void flipc_simple_lock_acquire(flipc_simple_lock
*lock
);
100 /* Take two locks. Does not hold one while spinning on the
102 void flipc_simple_lock_acquire_2(flipc_simple_lock
*lock1
,
103 flipc_simple_lock
*lock2
);
105 /* Get the machine dependent stuff. The things that need to be
106 * defined in a machine dependent fashion are:
108 * flipc_simple_lock_init (must be a macro)
109 * flipc_simple_lock_try
110 * flipc_simple_lock_locked
111 * flipc_simple_lock_release
113 * These last three don't necessarily have to be macros, but if they
114 * aren't definitions must be included in the machine dependent
115 * part of the user level library code.
117 #include <mach/machine/flipc_dep.h>
120 * Set at flipc initialization time to thread_yield argument to
124 extern void (*flipc_simple_lock_yield_fn
)(void);
127 * Machine independent definitions; defined in terms of above routines.
130 /* Take the lock. Assumes an external define saying how long to
131 spin, and an external function to call when we've spun too long. */
132 #define flipc_simple_lock_acquire(lock) \
134 int __spin_count = 0; \
136 while (flipc_simple_lock_locked(lock) \
137 || !flipc_simple_lock_try(lock)) \
138 if (++__spin_count > LOCK_SPIN_LIMIT) { \
139 (*flipc_simple_lock_yield_fn)(); \
144 /* Take two locks. Hold neither while spinning on the other. */
145 #define flipc_simple_lock_acquire_2(lock1, lock2) \
147 int __spin_count = 0; \
150 while (flipc_simple_lock_locked(lock1) \
151 || !flipc_simple_lock_try(lock1)) \
152 if (++__spin_count > LOCK_SPIN_LIMIT) { \
153 (*flipc_simple_lock_yield_fn)(); \
157 if (flipc_simple_lock_try(lock2)) \
159 flipc_simple_lock_release(lock1); \
161 while (flipc_simple_lock_locked(lock2) \
162 || !flipc_simple_lock_try(lock2)) \
163 if (++__spin_count > LOCK_SPIN_LIMIT) { \
164 (*flipc_simple_lock_yield_fn)(); \
168 if (flipc_simple_lock_try(lock1)) \
170 flipc_simple_lock_release(lock2); \
174 #endif /* _MACH_FLIPC_LOCKS_H_ */