]>
git.saurik.com Git - apple/libdispatch.git/blob - src/shims/yield.h
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
27 #ifndef __DISPATCH_SHIMS_YIELD__
28 #define __DISPATCH_SHIMS_YIELD__
31 #pragma mark _dispatch_wait_until
33 #if DISPATCH_HW_CONFIG_UP
34 #define _dispatch_wait_until(c) ({ \
38 if (likely(_c = (c))) break; \
40 _dispatch_preemption_yield(_spins); \
43 #elif TARGET_OS_EMBEDDED
44 // <rdar://problem/15440575>
45 #ifndef DISPATCH_WAIT_SPINS
46 #define DISPATCH_WAIT_SPINS 1024
48 #define _dispatch_wait_until(c) ({ \
50 int _spins = -(DISPATCH_WAIT_SPINS); \
52 if (likely(_c = (c))) break; \
53 if (slowpath(_spins++ >= 0)) { \
54 _dispatch_preemption_yield(_spins); \
56 dispatch_hardware_pause(); \
61 #define _dispatch_wait_until(c) ({ \
64 if (likely(_c = (c))) break; \
65 dispatch_hardware_pause(); \
71 #pragma mark _dispatch_contention_wait_until
73 #if DISPATCH_HW_CONFIG_UP
74 #define _dispatch_contention_wait_until(c) false
76 #ifndef DISPATCH_CONTENTION_SPINS_MAX
77 #define DISPATCH_CONTENTION_SPINS_MAX (128 - 1)
79 #ifndef DISPATCH_CONTENTION_SPINS_MIN
80 #define DISPATCH_CONTENTION_SPINS_MIN (32 - 1)
82 #if TARGET_OS_EMBEDDED
83 #define _dispatch_contention_spins() \
84 ((DISPATCH_CONTENTION_SPINS_MIN) + ((DISPATCH_CONTENTION_SPINS_MAX) - \
85 (DISPATCH_CONTENTION_SPINS_MIN)) / 2)
87 // Use randomness to prevent threads from resonating at the same
88 // frequency and permanently contending. All threads sharing the same
89 // seed value is safe with the FreeBSD rand_r implementation.
90 #define _dispatch_contention_spins() ({ \
91 static unsigned int _seed; \
92 ((unsigned int)rand_r(&_seed) & (DISPATCH_CONTENTION_SPINS_MAX)) | \
93 (DISPATCH_CONTENTION_SPINS_MIN); })
95 #define _dispatch_contention_wait_until(c) ({ \
97 unsigned int _spins = _dispatch_contention_spins(); \
99 dispatch_hardware_pause(); \
100 if ((_out = fastpath(c))) break; \
105 #pragma mark dispatch_hardware_pause
107 #if defined(__x86_64__) || defined(__i386__)
108 #define dispatch_hardware_pause() __asm__("pause")
109 #elif (defined(__arm__) && defined(_ARM_ARCH_7) && defined(__thumb__)) || \
111 #define dispatch_hardware_pause() __asm__("yield")
112 #define dispatch_hardware_wfe() __asm__("wfe")
114 #define dispatch_hardware_pause() __asm__("")
118 #pragma mark _dispatch_preemption_yield
121 #if defined(SWITCH_OPTION_OSLOCK_DEPRESS)
122 #define DISPATCH_YIELD_THREAD_SWITCH_OPTION SWITCH_OPTION_OSLOCK_DEPRESS
124 #define DISPATCH_YIELD_THREAD_SWITCH_OPTION SWITCH_OPTION_DEPRESS
126 #define _dispatch_preemption_yield(n) thread_switch(MACH_PORT_NULL, \
127 DISPATCH_YIELD_THREAD_SWITCH_OPTION, (mach_msg_timeout_t)(n))
128 #define _dispatch_preemption_yield_to(th, n) thread_switch(th, \
129 DISPATCH_YIELD_THREAD_SWITCH_OPTION, (mach_msg_timeout_t)(n))
131 #define _dispatch_preemption_yield(n) pthread_yield_np()
132 #define _dispatch_preemption_yield_to(th, n) pthread_yield_np()
136 #pragma mark _dispatch_contention_usleep
138 #ifndef DISPATCH_CONTENTION_USLEEP_START
139 #define DISPATCH_CONTENTION_USLEEP_START 500
141 #ifndef DISPATCH_CONTENTION_USLEEP_MAX
142 #define DISPATCH_CONTENTION_USLEEP_MAX 100000
146 #if defined(SWITCH_OPTION_DISPATCH_CONTENTION)
147 #define _dispatch_contention_usleep(u) thread_switch(MACH_PORT_NULL, \
148 SWITCH_OPTION_DISPATCH_CONTENTION, (u))
150 #define _dispatch_contention_usleep(u) thread_switch(MACH_PORT_NULL, \
151 SWITCH_OPTION_WAIT, (((u)-1)/1000)+1)
154 #define _dispatch_contention_usleep(u) usleep((u))
157 #endif // __DISPATCH_SHIMS_YIELD__