Use atomics instead of mutable statics in slot_hashes (#24091)

This commit is contained in:
Brian Anderson 2022-04-11 16:12:50 -05:00 committed by GitHub
parent 3871c85fd7
commit b38833923d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 7 deletions

View File

@ -5,23 +5,25 @@
pub use crate::clock::Slot;
use {
crate::hash::Hash,
std::{iter::FromIterator, ops::Deref},
std::{
iter::FromIterator,
ops::Deref,
sync::atomic::{AtomicUsize, Ordering},
},
};
pub const MAX_ENTRIES: usize = 512; // about 2.5 minutes to get your vote in
// This is to allow tests with custom slot hash expiry to avoid having to generate
// 512 blocks for such tests.
static mut NUM_ENTRIES: usize = MAX_ENTRIES;
static NUM_ENTRIES: AtomicUsize = AtomicUsize::new(MAX_ENTRIES);
pub fn get_entries() -> usize {
unsafe { NUM_ENTRIES }
NUM_ENTRIES.load(Ordering::Relaxed)
}
pub fn set_entries_for_tests_only(_entries: usize) {
unsafe {
NUM_ENTRIES = _entries;
}
pub fn set_entries_for_tests_only(entries: usize) {
NUM_ENTRIES.store(entries, Ordering::Relaxed);
}
pub type SlotHash = (Slot, Hash);