Skip to content

Commit

Permalink
use a global atomic for non scheduler threads
Browse files Browse the repository at this point in the history
to get some randomness
  • Loading branch information
sverker committed Mar 4, 2024
1 parent f65a659 commit 7e66483
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 4 additions & 1 deletion erts/emulator/beam/erl_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ Eterm erts_system_profile;
struct erts_system_profile_flags_t erts_system_profile_flags;
int erts_system_profile_ts_type = ERTS_TRACE_FLG_NOW_TIMESTAMP;

erts_atomic_t erts_sched_local_random_nosched_state;

#if ERTS_MAX_PROCESSES > 0x7fffffff
#error "Need to store process_count in another type"
#endif
Expand Down Expand Up @@ -6201,7 +6203,8 @@ erts_init_scheduling(int no_schedulers, int no_schedulers_online, int no_poll_th
erts_atomic32_init_relb(&erts_halt_progress, -1);
erts_halt_code = 0;


erts_atomic_init_nob(&erts_sched_local_random_nosched_state,
(erts_aint_t)&erts_sched_local_random_nosched_state >> 3);
}

ErtsRunQueue *
Expand Down
16 changes: 9 additions & 7 deletions erts/emulator/beam/erl_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -2804,15 +2804,17 @@ Uint32 erts_sched_local_random_hash_64_to_32_shift(Uint64 key)
ERTS_GLB_INLINE
Uint32 erts_sched_local_random(Uint additional_seed)
{
extern erts_atomic_t erts_sched_local_random_nosched_state;
ErtsSchedulerData *esdp = erts_get_scheduler_data();
if(ERTS_UNLIKELY(esdp == NULL))
return erts_sched_local_random_hash_64_to_32_shift(((Uint64)(UWord)&additional_seed)
+ additional_seed);
else {
esdp->rand_state++;
return erts_sched_local_random_hash_64_to_32_shift(esdp->rand_state
+ additional_seed);
Uint64 rand_state;

if(ERTS_UNLIKELY(esdp == NULL)) {
rand_state = erts_atomic_inc_read_nob(&erts_sched_local_random_nosched_state);
} else {
rand_state = esdp->rand_state++;
}
return erts_sched_local_random_hash_64_to_32_shift(rand_state
+ additional_seed);
}

#ifdef DEBUG
Expand Down

0 comments on commit 7e66483

Please sign in to comment.