]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
d7e50217 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
d7e50217 A |
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 | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
d7e50217 A |
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. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * @OSF_COPYRIGHT@ | |
27 | * | |
28 | */ | |
29 | /* | |
30 | * HISTORY | |
31 | * | |
32 | * Revision 1.1.1.1 1998/09/22 21:05:30 wsanchez | |
33 | * Import of Mac OS X kernel (~semeria) | |
34 | * | |
35 | * Revision 1.1.1.1 1998/03/07 02:25:45 wsanchez | |
36 | * Import of OSF Mach kernel (~mburg) | |
37 | * | |
38 | * Revision 1.1.4.1 1995/06/13 18:20:29 sjs | |
39 | * Merged from flipc_shared. | |
40 | * [95/06/07 sjs] | |
41 | * | |
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] | |
45 | * | |
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] | |
49 | * | |
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] | |
53 | * | |
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] | |
57 | * | |
58 | * $EndLog$ | |
59 | */ | |
60 | ||
61 | /* | |
62 | * mach/flipc_locks.h | |
63 | * | |
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. | |
67 | */ | |
68 | ||
69 | /* | |
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 | |
73 | * these locks. | |
74 | */ | |
75 | ||
76 | #ifndef _MACH_FLIPC_LOCKS_H_ | |
77 | #define _MACH_FLIPC_LOCKS_H_ | |
78 | ||
79 | /* Get the simple lock type. */ | |
80 | #include <mach/flipc_cb.h> | |
81 | ||
82 | /* | |
83 | * Lock function prototypes. This needs to be before any lock definitions | |
84 | * that happen to be macros. | |
85 | */ | |
86 | ||
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); | |
90 | ||
91 | /* Returns 1 if lock gained, 0 otherwise. */ | |
92 | int flipc_simple_lock_try(flipc_simple_lock *lock); | |
93 | ||
94 | /* Returns 1 if lock is locked, 0 otherwise. */ | |
95 | int flipc_simple_lock_locked(flipc_simple_lock *lock); | |
96 | ||
97 | /* Releases the lock. */ | |
98 | void flipc_simple_lock_release(flipc_simple_lock *lock); | |
99 | ||
100 | /* Take the lock. */ | |
101 | void flipc_simple_lock_acquire(flipc_simple_lock *lock); | |
102 | ||
103 | /* Take two locks. Does not hold one while spinning on the | |
104 | other. */ | |
105 | void flipc_simple_lock_acquire_2(flipc_simple_lock *lock1, | |
106 | flipc_simple_lock *lock2); | |
107 | ||
108 | /* Get the machine dependent stuff. The things that need to be | |
109 | * defined in a machine dependent fashion are: | |
110 | * | |
111 | * flipc_simple_lock_init (must be a macro) | |
112 | * flipc_simple_lock_try | |
113 | * flipc_simple_lock_locked | |
114 | * flipc_simple_lock_release | |
115 | * | |
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. | |
119 | */ | |
120 | #include <mach/machine/flipc_dep.h> | |
121 | ||
122 | /* | |
123 | * Set at flipc initialization time to thread_yield argument to | |
124 | * FLIPC_domain_init | |
125 | */ | |
126 | ||
127 | extern void (*flipc_simple_lock_yield_fn)(void); | |
128 | ||
129 | /* | |
130 | * Machine independent definitions; defined in terms of above routines. | |
131 | */ | |
132 | ||
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) \ | |
136 | do { \ | |
137 | int __spin_count = 0; \ | |
138 | \ | |
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)(); \ | |
143 | __spin_count = 0; \ | |
144 | } \ | |
145 | } while (0) | |
146 | ||
147 | /* Take two locks. Hold neither while spinning on the other. */ | |
148 | #define flipc_simple_lock_acquire_2(lock1, lock2) \ | |
149 | do { \ | |
150 | int __spin_count = 0; \ | |
151 | \ | |
152 | while (1) { \ | |
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)(); \ | |
157 | __spin_count = 0; \ | |
158 | } \ | |
159 | \ | |
160 | if (flipc_simple_lock_try(lock2)) \ | |
161 | break; \ | |
162 | flipc_simple_lock_release(lock1); \ | |
163 | \ | |
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)(); \ | |
168 | __spin_count = 0; \ | |
169 | } \ | |
170 | \ | |
171 | if (flipc_simple_lock_try(lock1)) \ | |
172 | break; \ | |
173 | flipc_simple_lock_release(lock2); \ | |
174 | } \ | |
175 | } while (0) | |
176 | ||
177 | #endif /* _MACH_FLIPC_LOCKS_H_ */ |