@@ -891,11 +891,6 @@ _dispatch_thread_semaphore_wait(_dispatch_thread_semaphore_t sema)
891
891
}
892
892
893
893
#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
-
899
894
static bool _dispatch_futex_wait_slow (dispatch_futex_t dfx ,
900
895
const struct timespec * timeout );
901
896
@@ -918,8 +913,8 @@ DISPATCH_INLINE
918
913
bool
919
914
_dispatch_futex_trywait (dispatch_futex_t dfx )
920
915
{
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 ) {
923
918
if (dispatch_atomic_cmpxchg2o (dfx , dfx_data , orig , orig - 1 , acquire )) {
924
919
return true;
925
920
}
@@ -933,9 +928,8 @@ DISPATCH_INLINE
933
928
bool
934
929
_dispatch_futex_signal (dispatch_futex_t dfx )
935
930
{
936
- uint64_t orig ;
937
- do {
938
- orig = dfx -> dfx_data ;
931
+ uint64_t orig = dispatch_atomic_load2o (dfx , dfx_data , relaxed );
932
+ do {
939
933
if (slowpath ((orig & DISPATCH_FUTEX_VALUE_MASK ) ==
940
934
DISPATCH_FUTEX_VALUE_MAX )) {
941
935
DISPATCH_CRASH ("semaphore overflow" );
@@ -965,15 +959,16 @@ _dispatch_futex_wait_slow(dispatch_futex_t dfx, const struct timespec *timeout)
965
959
{
966
960
int spins = DISPATCH_FUTEX_NUM_SPINS ;
967
961
// 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 ) ) {
969
963
dispatch_hardware_pause ();
970
964
}
971
965
while (!_dispatch_futex_trywait (dfx )) {
972
966
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 );
975
970
dispatch_atomic_sub2o (dfx , dfx_data ,
976
- 1ull << DISPATCH_FUTEX_NWAITERS_SHIFT , acquire );
971
+ 1ull << DISPATCH_FUTEX_NWAITERS_SHIFT , acquire );
977
972
switch (ret == -1 ? errno : 0 ) {
978
973
case EWOULDBLOCK :
979
974
if (!timeout ) {
0 commit comments