Skip to content

Commit a102d19

Browse files
authored
Merge pull request swiftlang#804 from ilya-fedin/fix-lock-contention
2 parents ee39300 + 29babc1 commit a102d19

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

src/shims/yield.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,17 @@ void *_dispatch_wait_for_enqueuer(void **ptr);
9898
#define _dispatch_contention_spins() \
9999
((DISPATCH_CONTENTION_SPINS_MIN) + ((DISPATCH_CONTENTION_SPINS_MAX) - \
100100
(DISPATCH_CONTENTION_SPINS_MIN)) / 2)
101-
#elif defined(_WIN32)
101+
#else
102102
// Use randomness to prevent threads from resonating at the same frequency and
103103
// permanently contending. Windows doesn't provide rand_r(), so use a simple
104104
// LCG. (msvcrt has rand_s(), but its security guarantees aren't optimal here.)
105+
// The implementation of rand() can contain a lock (known with glibc at least).
105106
#define _dispatch_contention_spins() ({ \
106107
static os_atomic(unsigned int) _seed = 1; \
107108
unsigned int _next = os_atomic_load(&_seed, relaxed); \
108109
os_atomic_store(&_seed, _next * 1103515245 + 12345, relaxed); \
109110
((_next >> 24) & (DISPATCH_CONTENTION_SPINS_MAX)) | \
110111
(DISPATCH_CONTENTION_SPINS_MIN); })
111-
#else
112-
// Use randomness to prevent threads from resonating at the same
113-
// frequency and permanently contending.
114-
#define _dispatch_contention_spins() ({ \
115-
((unsigned int)rand() & (DISPATCH_CONTENTION_SPINS_MAX)) | \
116-
(DISPATCH_CONTENTION_SPINS_MIN); })
117112
#endif
118113
#define _dispatch_contention_wait_until(c) ({ \
119114
bool _out = false; \

0 commit comments

Comments
 (0)