fix build.yml, concurrency concerns, build.rs concerns

This commit is contained in:
l1npengtul
2022-11-04 15:36:18 +09:00
parent 80fef5a01c
commit 36edc445d1
6 changed files with 98 additions and 165 deletions

View File

@@ -4,31 +4,94 @@
//!
//! See [`set_rhai_ahash_seed`] for more.
//!
//! Alternatively, You can also set this at compile time by disabling `stable_hash` and setting the `RHAI_AHASH_SEED`
//! Alternatively, You can also set this at compile time by setting the `RHAI_AHASH_SEED`
//! environment variable instead.
//!
//! E.g. `env RHAI_AHASH_SEED ="[236,800,954,213]"`
// [236,800,954,213], haha funny yume nikki reference epic uboachan face numberworld nexus moment 100
use crate::config::hashing_env;
use core::{
cell::UnsafeCell,
marker::PhantomData,
mem::MaybeUninit,
panic::{RefUnwindSafe, UnwindSafe},
sync::atomic::{AtomicBool, Ordering},
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};
// Safety: lol
// omg its hokma from record team here to record our locks
// what does this do?
// so what this does is keep track of a global address in memory that acts as a global lock
// i stole this from crossbeam so read their docs for more
struct HokmaLock {
lock: AtomicUsize,
}
impl HokmaLock {
pub const fn new() -> Self {
Self {
lock: AtomicUsize::new(0),
}
}
pub fn write(&'static self) -> WhenTheHokmaSupression {
loop {
let previous = self.lock.swap(1, Ordering::SeqCst);
if previous != 1 {
return WhenTheHokmaSupression {
hokma: self,
state: previous,
};
}
}
}
}
struct WhenTheHokmaSupression {
hokma: &'static HokmaLock,
state: usize,
}
impl WhenTheHokmaSupression {
pub fn the_price_of_silence(self) {
self.hokma.lock.store(self.state, Ordering::SeqCst);
core::mem::forget(self)
}
}
impl Drop for WhenTheHokmaSupression {
fn drop(&mut self) {
self.hokma
.lock
.store(self.state.wrapping_add(2), Ordering::SeqCst)
}
}
fn hokmalock(address: usize) -> &'static HokmaLock {
const LEN: usize = 787;
const LCK: HokmaLock = HokmaLock::new();
static RECORDS: [HokmaLock; LEN] = [LCK; LEN];
&RECORDS[address % LEN]
}
// Safety: lol, there is a reason its called "SusLock<T>"
struct SusLock<T>
where
T: 'static,
T: 'static + Copy,
{
initalized: AtomicBool,
data: UnsafeCell<MaybeUninit<T>>,
_marker: PhantomData<T>,
}
impl<T> SusLock<T> {
impl<T> SusLock<T>
where
T: 'static + Copy,
{
pub const fn new() -> SusLock<T> {
SusLock {
initalized: AtomicBool::new(false),
@@ -37,21 +100,31 @@ impl<T> SusLock<T> {
}
}
pub fn get(&self) -> Option<&T> {
pub fn get(&self) -> Option<T> {
if self.initalized.load(Ordering::SeqCst) {
Some(unsafe { (&*self.data.get()).assume_init_ref() })
let hokma = hokmalock(unsafe { core::mem::transmute(self.data.get()) });
// we forgo the optimistic read, because we don't really care
let guard = hokma.write();
let val = {
let cast: *const T = self.data.get().cast();
unsafe { cast.read() }
};
guard.the_price_of_silence();
Some(val)
} else {
return None;
}
}
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> Option<&T> {
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> Option<T> {
let value = f();
if !self.initalized.load(Ordering::SeqCst) {
self.initalized.store(true, Ordering::SeqCst);
let hokma = hokmalock(unsafe { core::mem::transmute(self.data.get()) });
hokma.write();
unsafe {
self.data.get().write(MaybeUninit::new(value));
}
self.initalized.store(true, Ordering::SeqCst);
}
self.get()
@@ -67,11 +140,14 @@ impl<T> SusLock<T> {
}
}
unsafe impl<T: Sync + Send> Sync for SusLock<T> {}
unsafe impl<T: Send> Send for SusLock<T> {}
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for SusLock<T> {}
unsafe impl<T: Sync + Send> Sync for SusLock<T> where T: 'static + Copy {}
unsafe impl<T: Send> Send for SusLock<T> where T: 'static + Copy {}
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for SusLock<T> where T: 'static + Copy {}
impl<T> Drop for SusLock<T> {
impl<T> Drop for SusLock<T>
where
T: 'static + Copy,
{
fn drop(&mut self) {
if self.initalized.load(Ordering::SeqCst) {
unsafe { (&mut *self.data.get()).assume_init_drop() };
@@ -103,10 +179,5 @@ pub fn set_ahash_seed(new_seed: Option<[u64; 4]>) -> Result<(), Option<[u64; 4]>
///
/// See [`set_rhai_ahash_seed`] for more.
pub fn get_ahash_seed() -> Option<[u64; 4]> {
const FUNNY_YUMENIKKI_REFERENCE: Option<[u64; 4]> = None;
AHASH_SEED
.get_or_init(|| FUNNY_YUMENIKKI_REFERENCE)
.map(|x| *x)
.flatten()
AHASH_SEED.get_or_init(|| hashing_env::AHASH_SEED).flatten()
}