]> git.saurik.com Git - apple/libplatform.git/blame - internal/os/yield.h
libplatform-177.200.16.tar.gz
[apple/libplatform.git] / internal / os / yield.h
CommitLineData
ada7c492
A
1/*
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21/*
22 * IMPORTANT: This header file describes INTERNAL interfaces to libplatform
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
25 */
26
27#ifndef __OS_YIELD__
28#define __OS_YIELD__
29
30#include <TargetConditionals.h>
31
32#pragma mark -
33#pragma mark _os_wait_until
34
35#if OS_ATOMIC_UP
36#define _os_wait_until(c) do { \
37 int _spins = 0; \
38 while (unlikely(!(c))) { \
39 _spins++; \
40 _os_preemption_yield(_spins); \
41 } } while (0)
438624e0 42#else
ada7c492
A
43// <rdar://problem/15508918>
44#ifndef OS_WAIT_SPINS
45#define OS_WAIT_SPINS 1024
46#endif
47#define _os_wait_until(c) do { \
48 int _spins = -(OS_WAIT_SPINS); \
49 while (unlikely(!(c))) { \
50 if (unlikely(_spins++ >= 0)) { \
51 _os_preemption_yield(_spins); \
52 } else { \
53 os_hardware_pause(); \
54 } \
55 } } while (0)
ada7c492
A
56#endif
57
58#pragma mark -
59#pragma mark os_hardware_pause
60
61#if defined(__x86_64__) || defined(__i386__)
62#define os_hardware_pause() __asm__("pause")
63#elif (defined(__arm__) && defined(_ARM_ARCH_7) && defined(__thumb__)) || \
64 defined(__arm64__)
65#define os_hardware_pause() __asm__("yield")
66#define os_hardware_wfe() __asm__("wfe")
67#else
68#define os_hardware_pause() __asm__("")
69#endif
70
71#pragma mark -
72#pragma mark _os_preemption_yield
73
ada7c492 74#define _os_preemption_yield(n) thread_switch(MACH_PORT_NULL, \
438624e0 75 SWITCH_OPTION_OSLOCK_DEPRESS, (mach_msg_timeout_t)(n))
ada7c492
A
76
77#endif // __OS_YIELD__