| 1 | // vim: tw=80 |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 4 | syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#}, |
| 5 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/, |
| 6 | timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/, |
| 7 | }; |
| 8 | |
| 9 | use divbufrust-analyzer cargo divbuf 0.4.1 crate/::*; |
| 10 | use lazy_staticrust-analyzer cargo lazy_static 1.5.0 crate/::lazy_staticrust-analyzer cargo lazy_static 1.5.0 lazy_static!; |
| 11 | |
| 12 | lazy_staticrust-analyzer cargo lazy_static 1.5.0 lazy_static!! { |
| 13 | pub static ref DBSrust-analyzer cargo divbuf 0.4.1 DBS#: DivBufSharedrust-analyzer cargo divbuf 0.4.1 divbuf/DivBufShared# = DivBufSharedrust-analyzer cargo divbuf 0.4.1 divbuf/DivBufShared#::fromrust-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBufShared][`From<Vec<u8>>`]from().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; 4096]); |
| 14 | pub static ref SHUTDOWNrust-analyzer cargo divbuf 0.4.1 SHUTDOWN#: AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool# = AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]new().(false); |
| 15 | } |
| 16 | |
| 17 | fn readfuncrust-analyzer cargo divbuf 0.4.1 readfunc().() { |
| 18 | let mut losses: u64 = 0; |
| 19 | let mut wins: u64 = 0; |
| 20 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().SHUTDOWNrust-analyzer cargo divbuf 0.4.1 SHUTDOWN..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) { |
| 21 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(db) = (DBSrust-analyzer cargo divbuf 0.4.1 DBS.).try_construst-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBufShared]try_const().() { |
| 22 | let mut db0 = db.slicerust-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBuf]slice().(0, 1024); |
| 23 | let db1 = db.slicerust-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBuf]slice().(1024, 2048); |
| 24 | db0.unsplitrust-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBuf]unsplit().(db1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 25 | wins rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1; |
| 26 | } else { |
| 27 | losses rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1; |
| 28 | } |
| 29 | } |
| 30 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("reader won {} races and lost {}", wins, losses); |
| 31 | } |
| 32 | |
| 33 | fn writefuncrust-analyzer cargo divbuf 0.4.1 writefunc().() { |
| 34 | let mut losses: u64 = 0; |
| 35 | let mut wins: u64 = 0; |
| 36 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().SHUTDOWNrust-analyzer cargo divbuf 0.4.1 SHUTDOWN..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) { |
| 37 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mut dbm) = (DBSrust-analyzer cargo divbuf 0.4.1 DBS.).try_mutrust-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBufShared]try_mut().() { |
| 38 | let dbm1 = dbm.split_offrust-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBufMut]split_off().(2048); |
| 39 | dbm.unsplitrust-analyzer cargo divbuf 0.4.1 divbuf/impl#[DivBufMut]unsplit().(dbm1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 40 | wins rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1; |
| 41 | } else { |
| 42 | losses rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1; |
| 43 | } |
| 44 | } |
| 45 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("writer won {} races and lost {}", wins, losses); |
| 46 | } |
| 47 | |
| 48 | /// Create a multitude of threads that each try to divide a common static |
| 49 | /// buffer. They run for a fixed time. Success happens if nobody panics. |
| 50 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 51 | fn test_thread_racerust-analyzer cargo divbuf 0.4.1 test_thread_race().() { |
| 52 | let reader0 = threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(readfuncrust-analyzer cargo divbuf 0.4.1 readfunc().); |
| 53 | let reader1 = threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(readfuncrust-analyzer cargo divbuf 0.4.1 readfunc().); |
| 54 | let writer0 = threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(writefuncrust-analyzer cargo divbuf 0.4.1 writefunc().); |
| 55 | let writer1 = threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(writefuncrust-analyzer cargo divbuf 0.4.1 writefunc().); |
| 56 | threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(1)); |
| 57 | SHUTDOWNrust-analyzer cargo divbuf 0.4.1 SHUTDOWN..storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]store().(true, Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#); |
| 58 | reader0.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 59 | reader1.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 60 | writer0.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 61 | writer1.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 62 | } |