Skip to content

Commit bc06e9c

Browse files
committed
fix comments from 2/24/16
1 parent f2b865b commit bc06e9c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/semaphore.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,6 @@ _dispatch_thread_semaphore_wait(_dispatch_thread_semaphore_t sema)
891891
}
892892

893893
#if USE_FUTEX_SEM
894-
#define DISPATCH_FUTEX_NUM_SPINS 100
895-
#define DISPATCH_FUTEX_VALUE_MAX INT_MAX
896-
#define DISPATCH_FUTEX_NWAITERS_SHIFT 32
897-
#define DISPATCH_FUTEX_VALUE_MASK ((1ull << DISPATCH_FUTEX_NWAITERS_SHIFT) - 1)
898-
899894
static bool _dispatch_futex_wait_slow(dispatch_futex_t dfx,
900895
const struct timespec* timeout);
901896

@@ -918,8 +913,8 @@ DISPATCH_INLINE
918913
bool
919914
_dispatch_futex_trywait(dispatch_futex_t dfx)
920915
{
921-
uint64_t orig;
922-
while ((orig = dfx->dfx_data) & DISPATCH_FUTEX_VALUE_MASK) {
916+
uint64_t orig = dispatch_atomic_load2o(dfx, dfx_data, relaxed);
917+
while (orig & DISPATCH_FUTEX_VALUE_MASK) {
923918
if (dispatch_atomic_cmpxchg2o(dfx, dfx_data, orig, orig - 1, acquire)) {
924919
return true;
925920
}
@@ -933,9 +928,8 @@ DISPATCH_INLINE
933928
bool
934929
_dispatch_futex_signal(dispatch_futex_t dfx)
935930
{
936-
uint64_t orig;
937-
do {
938-
orig = dfx->dfx_data;
931+
uint64_t orig = dispatch_atomic_load2o(dfx, dfx_data, relaxed);
932+
do {
939933
if (slowpath((orig & DISPATCH_FUTEX_VALUE_MASK) ==
940934
DISPATCH_FUTEX_VALUE_MAX)) {
941935
DISPATCH_CRASH("semaphore overflow");
@@ -965,15 +959,16 @@ _dispatch_futex_wait_slow(dispatch_futex_t dfx, const struct timespec *timeout)
965959
{
966960
int spins = DISPATCH_FUTEX_NUM_SPINS;
967961
// Spin for a short time (if there are no waiters).
968-
while (spins-- && !dfx->dfx_data) {
962+
while (spins-- && !dispatch_atomic_load2o(dfx, dfx_data, relaxed)) {
969963
dispatch_hardware_pause();
970964
}
971965
while (!_dispatch_futex_trywait(dfx)) {
972966
dispatch_atomic_add2o(dfx, dfx_data,
973-
1ull << DISPATCH_FUTEX_NWAITERS_SHIFT, relaxed);
974-
int ret = _dispatch_futex_syscall(&dfx->dfx_futex, FUTEX_WAIT, 0, timeout);
967+
1ull << DISPATCH_FUTEX_NWAITERS_SHIFT, relaxed);
968+
int ret = _dispatch_futex_syscall(&dfx->dfx_futex, FUTEX_WAIT,
969+
0, timeout);
975970
dispatch_atomic_sub2o(dfx, dfx_data,
976-
1ull << DISPATCH_FUTEX_NWAITERS_SHIFT, acquire);
971+
1ull << DISPATCH_FUTEX_NWAITERS_SHIFT, acquire);
977972
switch (ret == -1 ? errno : 0) {
978973
case EWOULDBLOCK:
979974
if (!timeout) {

src/semaphore_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ typedef union {
4545
} dispatch_futex_s;
4646
#define DISPATCH_FUTEX_INIT ((dispatch_futex_s){ 0 })
4747
typedef dispatch_futex_s *dispatch_futex_t;
48+
49+
#define DISPATCH_FUTEX_NUM_SPINS 100
50+
#define DISPATCH_FUTEX_VALUE_MAX INT_MAX
51+
#define DISPATCH_FUTEX_NWAITERS_SHIFT 32
52+
#define DISPATCH_FUTEX_VALUE_MASK ((1ull << DISPATCH_FUTEX_NWAITERS_SHIFT) - 1)
4853
#endif /* USE_FUTEX_SEM */
4954

5055
DISPATCH_CLASS_DECL(semaphore);
@@ -85,6 +90,10 @@ void _dispatch_thread_semaphore_dispose(_dispatch_thread_semaphore_t);
8590
void _dispatch_thread_semaphore_wait(_dispatch_thread_semaphore_t);
8691
void _dispatch_thread_semaphore_signal(_dispatch_thread_semaphore_t);
8792

93+
#if USE_FUTEX_SEM
94+
#define _dispatch_get_thread_semaphore() _dispatch_thread_semaphore_create()
95+
#define _dispatch_put_thread_semaphore(s) _dispatch_thread_semaphore_dispose(s)
96+
#else
8897
DISPATCH_ALWAYS_INLINE
8998
static inline _dispatch_thread_semaphore_t
9099
_dispatch_get_thread_semaphore(void)
@@ -109,5 +118,6 @@ _dispatch_put_thread_semaphore(_dispatch_thread_semaphore_t sema)
109118
return _dispatch_thread_semaphore_dispose(old_sema);
110119
}
111120
}
121+
#endif
112122

113123
#endif

0 commit comments

Comments
 (0)