]> git.saurik.com Git - apple/xnu.git/blame - osfmk/mach/flipc_locks.h
xnu-792.6.76.tar.gz
[apple/xnu.git] / osfmk / mach / flipc_locks.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 *
25 */
26/*
27 * HISTORY
28 *
29 * Revision 1.1.1.1 1998/09/22 21:05:30 wsanchez
30 * Import of Mac OS X kernel (~semeria)
31 *
32 * Revision 1.1.1.1 1998/03/07 02:25:45 wsanchez
33 * Import of OSF Mach kernel (~mburg)
34 *
35 * Revision 1.1.4.1 1995/06/13 18:20:29 sjs
36 * Merged from flipc_shared.
37 * [95/06/07 sjs]
38 *
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]
42 *
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]
46 *
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]
50 *
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]
54 *
55 * $EndLog$
56 */
57
58/*
59 * mach/flipc_locks.h
60 *
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.
64 */
65
66/*
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
70 * these locks.
71 */
72
73#ifndef _MACH_FLIPC_LOCKS_H_
74#define _MACH_FLIPC_LOCKS_H_
75
76/* Get the simple lock type. */
77#include <mach/flipc_cb.h>
78
79/*
80 * Lock function prototypes. This needs to be before any lock definitions
81 * that happen to be macros.
82 */
83
84/* Initializes lock. Always a macro (so that kernel code can use it without
85 library assistance). */
86void flipc_simple_lock_init(flipc_simple_lock *lock);
87
88/* Returns 1 if lock gained, 0 otherwise. */
89int flipc_simple_lock_try(flipc_simple_lock *lock);
90
91/* Returns 1 if lock is locked, 0 otherwise. */
92int flipc_simple_lock_locked(flipc_simple_lock *lock);
93
94/* Releases the lock. */
95void flipc_simple_lock_release(flipc_simple_lock *lock);
96
97/* Take the lock. */
98void flipc_simple_lock_acquire(flipc_simple_lock *lock);
99
100/* Take two locks. Does not hold one while spinning on the
101 other. */
102void flipc_simple_lock_acquire_2(flipc_simple_lock *lock1,
103 flipc_simple_lock *lock2);
104
105/* Get the machine dependent stuff. The things that need to be
106 * defined in a machine dependent fashion are:
107 *
108 * flipc_simple_lock_init (must be a macro)
109 * flipc_simple_lock_try
110 * flipc_simple_lock_locked
111 * flipc_simple_lock_release
112 *
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.
116 */
117#include <mach/machine/flipc_dep.h>
118
119/*
120 * Set at flipc initialization time to thread_yield argument to
121 * FLIPC_domain_init
122 */
123
124extern void (*flipc_simple_lock_yield_fn)(void);
125
126/*
127 * Machine independent definitions; defined in terms of above routines.
128 */
129
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) \
133do { \
134 int __spin_count = 0; \
135 \
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)(); \
140 __spin_count = 0; \
141 } \
142} while (0)
143
144/* Take two locks. Hold neither while spinning on the other. */
145#define flipc_simple_lock_acquire_2(lock1, lock2) \
146do { \
147 int __spin_count = 0; \
148 \
149 while (1) { \
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)(); \
154 __spin_count = 0; \
155 } \
156 \
157 if (flipc_simple_lock_try(lock2)) \
158 break; \
159 flipc_simple_lock_release(lock1); \
160 \
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)(); \
165 __spin_count = 0; \
166 } \
167 \
168 if (flipc_simple_lock_try(lock1)) \
169 break; \
170 flipc_simple_lock_release(lock2); \
171 } \
172} while (0)
173
174#endif /* _MACH_FLIPC_LOCKS_H_ */