2 * Copyright (c) 2004-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _OSSPINLOCK_DEPRECATED_H_
25 #define _OSSPINLOCK_DEPRECATED_H_
28 * These are deprecated legacy interfaces for userspace spinlocks.
30 * These interfaces should no longer be used, particularily in situations where
31 * threads of differing priorities may contend on the same spinlock.
33 * The interfaces in <os/lock.h> should be used instead in cases where a very
34 * low-level lock primitive is required. In general however, using higher level
35 * synchronization primitives such as those provided by the pthread or dispatch
36 * subsystems should be preferred.
38 * Define OSSPINLOCK_USE_INLINED=1 to get inline implementations of these
39 * interfaces in terms of the <os/lock.h> primitives. This is intended as a
40 * transition convenience, direct use of those primitives is preferred.
43 #ifndef OSSPINLOCK_DEPRECATED
44 #define OSSPINLOCK_DEPRECATED 1
45 #define OSSPINLOCK_DEPRECATED_MSG(_r) "Use " #_r "() from <os/lock.h> instead"
46 #define OSSPINLOCK_DEPRECATED_REPLACE_WITH(_r) \
47 __OS_AVAILABILITY_MSG(macosx, deprecated=10.12, OSSPINLOCK_DEPRECATED_MSG(_r)) \
48 __OS_AVAILABILITY_MSG(ios, deprecated=10.0, OSSPINLOCK_DEPRECATED_MSG(_r)) \
49 __OS_AVAILABILITY_MSG(tvos, deprecated=10.0, OSSPINLOCK_DEPRECATED_MSG(_r)) \
50 __OS_AVAILABILITY_MSG(watchos, deprecated=3.0, OSSPINLOCK_DEPRECATED_MSG(_r))
52 #undef OSSPINLOCK_DEPRECATED
53 #define OSSPINLOCK_DEPRECATED 0
54 #define OSSPINLOCK_DEPRECATED_REPLACE_WITH(_r)
57 #if !(defined(OSSPINLOCK_USE_INLINED) && OSSPINLOCK_USE_INLINED)
59 #include <sys/cdefs.h>
63 #include <Availability.h>
67 /*! @abstract The default value for an <code>OSSpinLock</code>.
69 The convention is that unlocked is zero, locked is nonzero.
71 #define OS_SPINLOCK_INIT 0
74 /*! @abstract Data type for a spinlock.
76 You should always initialize a spinlock to {@link OS_SPINLOCK_INIT} before
79 typedef int32_t OSSpinLock
OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock
);
82 /*! @abstract Locks a spinlock if it would not block
84 Returns <code>false</code> if the lock was already held by another thread,
85 <code>true</code> if it took the lock successfully.
87 OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock_trylock
)
88 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_2_0
)
89 bool OSSpinLockTry( volatile OSSpinLock
*__lock
);
92 /*! @abstract Locks a spinlock
94 Although the lock operation spins, it employs various strategies to back
95 off if the lock is held.
97 OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock_lock
)
98 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_2_0
)
99 void OSSpinLockLock( volatile OSSpinLock
*__lock
);
102 /*! @abstract Unlocks a spinlock */
103 OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock_unlock
)
104 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_2_0
)
105 void OSSpinLockUnlock( volatile OSSpinLock
*__lock
);
109 #else /* OSSPINLOCK_USE_INLINED */
112 * Inline implementations of the legacy OSSpinLock interfaces in terms of the
113 * of the <os/lock.h> primitives. Direct use of those primitives is preferred.
115 * NOTE: the locked value of os_unfair_lock is implementation defined and
116 * subject to change, code that relies on the specific locked value used by the
117 * legacy OSSpinLock interface WILL break when using these inline
118 * implementations in terms of os_unfair_lock.
121 #if !OSSPINLOCK_USE_INLINED_TRANSPARENT
127 #if __has_attribute(always_inline)
128 #define OSSPINLOCK_INLINE static __inline
130 #define OSSPINLOCK_INLINE static __inline __attribute__((__always_inline__))
133 #define OS_SPINLOCK_INIT 0
134 typedef int32_t OSSpinLock
;
136 #if __has_extension(c_static_assert)
137 _Static_assert(sizeof(OSSpinLock
) == sizeof(os_unfair_lock
),
138 "Incompatible os_unfair_lock type");
143 OSSpinLockLock(volatile OSSpinLock
*__lock
)
145 os_unfair_lock_t lock
= (os_unfair_lock_t
)__lock
;
146 return os_unfair_lock_lock(lock
);
151 OSSpinLockTry(volatile OSSpinLock
*__lock
)
153 os_unfair_lock_t lock
= (os_unfair_lock_t
)__lock
;
154 return os_unfair_lock_trylock(lock
);
159 OSSpinLockUnlock(volatile OSSpinLock
*__lock
)
161 os_unfair_lock_t lock
= (os_unfair_lock_t
)__lock
;
162 return os_unfair_lock_unlock(lock
);
165 #undef OSSPINLOCK_INLINE
169 #else /* OSSPINLOCK_USE_INLINED_TRANSPARENT */
171 #include <sys/cdefs.h>
175 #include <Availability.h>
177 #define OS_NOSPIN_LOCK_AVAILABILITY \
178 __OSX_AVAILABLE(10.12) __IOS_AVAILABLE(10.0) \
179 __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
183 #define OS_SPINLOCK_INIT 0
184 typedef int32_t OSSpinLock
OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock
);
185 typedef volatile OSSpinLock
*_os_nospin_lock_t
186 OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock_t
);
188 OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock_lock
)
189 OS_NOSPIN_LOCK_AVAILABILITY
190 void _os_nospin_lock_lock(_os_nospin_lock_t lock
);
191 #undef OSSpinLockLock
192 #define OSSpinLockLock(lock) _os_nospin_lock_lock(lock)
194 OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock_trylock
)
195 OS_NOSPIN_LOCK_AVAILABILITY
196 bool _os_nospin_lock_trylock(_os_nospin_lock_t lock
);
198 #define OSSpinLockTry(lock) _os_nospin_lock_trylock(lock)
200 OSSPINLOCK_DEPRECATED_REPLACE_WITH(os_unfair_lock_unlock
)
201 OS_NOSPIN_LOCK_AVAILABILITY
202 void _os_nospin_lock_unlock(_os_nospin_lock_t lock
);
203 #undef OSSpinLockUnlock
204 #define OSSpinLockUnlock(lock) _os_nospin_lock_unlock(lock)
208 #endif /* OSSPINLOCK_USE_INLINED_TRANSPARENT */
210 #endif /* OSSPINLOCK_USE_INLINED */
212 #endif /* _OSSPINLOCK_DEPRECATED_H_ */