Skip to content

Commit 79b7529

Browse files
authored
Merge pull request #277 from apple/mad/remove-sys-membarrier
Remove dependency on sys_membarrier on linux
2 parents 993be7a + 93d2dd9 commit 79b7529

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

dispatch/once.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ __BEGIN_DECLS
4040
DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
4141
typedef long dispatch_once_t;
4242

43+
#if defined(__x86_64__) || defined(__i386__) || defined(__s390x__)
44+
#define DISPATCH_ONCE_INLINE_FASTPATH 1
45+
#elif defined(__APPLE__)
46+
#define DISPATCH_ONCE_INLINE_FASTPATH 1
47+
#else
48+
#define DISPATCH_ONCE_INLINE_FASTPATH 0
49+
#endif
50+
4351
/*!
4452
* @function dispatch_once
4553
*
@@ -65,6 +73,7 @@ void
6573
dispatch_once(dispatch_once_t *predicate,
6674
DISPATCH_NOESCAPE dispatch_block_t block);
6775

76+
#if DISPATCH_ONCE_INLINE_FASTPATH
6877
DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
6978
DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
7079
void
@@ -81,6 +90,7 @@ _dispatch_once(dispatch_once_t *predicate,
8190
#undef dispatch_once
8291
#define dispatch_once _dispatch_once
8392
#endif
93+
#endif // DISPATCH_ONCE_INLINE_FASTPATH
8494

8595
API_AVAILABLE(macos(10.6), ios(4.0))
8696
DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
@@ -89,6 +99,7 @@ void
8999
dispatch_once_f(dispatch_once_t *predicate, void *_Nullable context,
90100
dispatch_function_t function);
91101

102+
#if DISPATCH_ONCE_INLINE_FASTPATH
92103
DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL1 DISPATCH_NONNULL3
93104
DISPATCH_NOTHROW
94105
DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
@@ -105,6 +116,7 @@ _dispatch_once_f(dispatch_once_t *predicate, void *_Nullable context,
105116
}
106117
#undef dispatch_once_f
107118
#define dispatch_once_f _dispatch_once_f
119+
#endif // DISPATCH_ONCE_INLINE_FASTPATH
108120

109121
__END_DECLS
110122

src/once.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ dispatch_once(dispatch_once_t *val, dispatch_block_t block)
4040
}
4141
#endif
4242

43-
DISPATCH_NOINLINE
44-
void
45-
dispatch_once_f(dispatch_once_t *val, void *ctxt, dispatch_function_t func)
43+
#if DISPATCH_ONCE_INLINE_FASTPATH
44+
#define DISPATCH_ONCE_SLOW_INLINE inline DISPATCH_ALWAYS_INLINE
45+
#else
46+
#define DISPATCH_ONCE_SLOW_INLINE DISPATCH_NOINLINE
47+
#endif // DISPATCH_ONCE_INLINE_FASTPATH
48+
49+
DISPATCH_ONCE_SLOW_INLINE
50+
static void
51+
dispatch_once_f_slow(dispatch_once_t *val, void *ctxt, dispatch_function_t func)
4652
{
4753
#if DISPATCH_GATE_USE_FOR_DISPATCH_ONCE
4854
dispatch_once_gate_t l = (dispatch_once_gate_t)val;
@@ -95,3 +101,15 @@ dispatch_once_f(dispatch_once_t *val, void *ctxt, dispatch_function_t func)
95101
}
96102
#endif
97103
}
104+
105+
DISPATCH_NOINLINE
106+
void
107+
dispatch_once_f(dispatch_once_t *val, void *ctxt, dispatch_function_t func)
108+
{
109+
#if !DISPATCH_ONCE_INLINE_FASTPATH
110+
if (likely(os_atomic_load(val, acquire) == DLOCK_ONCE_DONE)) {
111+
return;
112+
}
113+
#endif // !DISPATCH_ONCE_INLINE_FASTPATH
114+
return dispatch_once_f_slow(val, ctxt, func);
115+
}

src/shims/lock.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ _dispatch_lock_owner(dispatch_lock lock_value)
5959
#elif defined(__linux__)
6060

6161
#include <linux/futex.h>
62-
#if !defined(__x86_64__) && !defined(__i386__) && !defined(__s390x__)
63-
#include <linux/membarrier.h>
64-
#endif
6562
#include <unistd.h>
6663
#include <sys/syscall.h> /* For SYS_xxx definitions */
6764

@@ -473,28 +470,7 @@ DISPATCH_ALWAYS_INLINE
473470
static inline dispatch_once_t
474471
_dispatch_once_xchg_done(dispatch_once_t *pred)
475472
{
476-
#if defined(__i386__) || defined(__x86_64__) || defined(__s390x__)
477-
// On Intel, any load is a load-acquire, so we don't need to be fancy
478-
// same for s390x
479473
return os_atomic_xchg(pred, DLOCK_ONCE_DONE, release);
480-
#elif defined(__linux__)
481-
if (unlikely(syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0) < 0)) {
482-
/*
483-
* sys_membarrier not supported
484-
*
485-
* Ideally we would call DISPATCH_INTERNAL_CRASH() here, but
486-
* due to ordering constraints in internal.h required by Darwin
487-
* the macro is undefined when this header is included.
488-
* Instead, open-code what would be a call to
489-
* _dispatch_hardware_crash() inside DISPATCH_INTERNAL_CRASH().
490-
*/
491-
__asm__("");
492-
__builtin_trap();
493-
}
494-
return os_atomic_xchg(pred, DLOCK_ONCE_DONE, relaxed);
495-
#else
496-
# error dispatch_once algorithm not available for this port
497-
#endif
498474
}
499475

500476
DISPATCH_ALWAYS_INLINE

0 commit comments

Comments
 (0)