Skip to content

Commit 7e66483

Browse files
committed
use a global atomic for non scheduler threads
to get some randomness
1 parent f65a659 commit 7e66483

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

erts/emulator/beam/erl_process.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ Eterm erts_system_profile;
437437
struct erts_system_profile_flags_t erts_system_profile_flags;
438438
int erts_system_profile_ts_type = ERTS_TRACE_FLG_NOW_TIMESTAMP;
439439

440+
erts_atomic_t erts_sched_local_random_nosched_state;
441+
440442
#if ERTS_MAX_PROCESSES > 0x7fffffff
441443
#error "Need to store process_count in another type"
442444
#endif
@@ -6201,7 +6203,8 @@ erts_init_scheduling(int no_schedulers, int no_schedulers_online, int no_poll_th
62016203
erts_atomic32_init_relb(&erts_halt_progress, -1);
62026204
erts_halt_code = 0;
62036205

6204-
6206+
erts_atomic_init_nob(&erts_sched_local_random_nosched_state,
6207+
(erts_aint_t)&erts_sched_local_random_nosched_state >> 3);
62056208
}
62066209

62076210
ErtsRunQueue *

erts/emulator/beam/erl_process.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,15 +2804,17 @@ Uint32 erts_sched_local_random_hash_64_to_32_shift(Uint64 key)
28042804
ERTS_GLB_INLINE
28052805
Uint32 erts_sched_local_random(Uint additional_seed)
28062806
{
2807+
extern erts_atomic_t erts_sched_local_random_nosched_state;
28072808
ErtsSchedulerData *esdp = erts_get_scheduler_data();
2808-
if(ERTS_UNLIKELY(esdp == NULL))
2809-
return erts_sched_local_random_hash_64_to_32_shift(((Uint64)(UWord)&additional_seed)
2810-
+ additional_seed);
2811-
else {
2812-
esdp->rand_state++;
2813-
return erts_sched_local_random_hash_64_to_32_shift(esdp->rand_state
2814-
+ additional_seed);
2809+
Uint64 rand_state;
2810+
2811+
if(ERTS_UNLIKELY(esdp == NULL)) {
2812+
rand_state = erts_atomic_inc_read_nob(&erts_sched_local_random_nosched_state);
2813+
} else {
2814+
rand_state = esdp->rand_state++;
28152815
}
2816+
return erts_sched_local_random_hash_64_to_32_shift(rand_state
2817+
+ additional_seed);
28162818
}
28172819

28182820
#ifdef DEBUG

0 commit comments

Comments
 (0)