-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(thingbuf): hahahaha static storage works
Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information
Showing
5 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use super::*; | ||
use crate::loom::sync::{Condvar, Mutex}; | ||
|
||
pub struct Sender<T> { | ||
inner: Arc<Inner<T>>, | ||
} | ||
|
||
pub struct Receiver<T> { | ||
inner: Arc<Inner<T>>, | ||
} | ||
|
||
struct Inner<T> { | ||
lock: Mutex<bool>, | ||
cv: Condvar, | ||
buf: ThingBuf<T>, | ||
} | ||
|
||
impl<T> Receiver<T> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use std::{fmt::Write, sync::Arc, thread}; | ||
use thingbuf::{Slot, StringBuf, ThingBuf}; | ||
|
||
#[test] | ||
fn static_storage_thingbuf() { | ||
let thingbuf = Arc::new(ThingBuf::from_array([ | ||
Slot::new(0), | ||
Slot::new(0), | ||
Slot::new(0), | ||
Slot::new(0), | ||
Slot::new(0), | ||
Slot::new(0), | ||
Slot::new(0), | ||
Slot::new(0), | ||
])); | ||
|
||
let producer = { | ||
let thingbuf = thingbuf.clone(); | ||
thread::spawn(move || { | ||
for i in 0..32 { | ||
let mut thing = 'write: loop { | ||
match thingbuf.push_ref() { | ||
Ok(thing) => break 'write thing, | ||
_ => thread::yield_now(), | ||
} | ||
}; | ||
thing.with_mut(|thing| *thing = i); | ||
} | ||
}) | ||
}; | ||
|
||
let mut i = 0; | ||
|
||
// While the producer is writing to the queue, push each entry to the | ||
// results string. | ||
while Arc::strong_count(&thingbuf) > 1 { | ||
match thingbuf.pop_ref() { | ||
Some(thing) => thing.with(|thing| { | ||
assert_eq!(*thing, i); | ||
i += 1; | ||
}), | ||
None => thread::yield_now(), | ||
} | ||
} | ||
|
||
producer.join().unwrap(); | ||
|
||
// drain the queue. | ||
while let Some(thing) = thingbuf.pop_ref() { | ||
thing.with(|thing| { | ||
assert_eq!(*thing, i); | ||
i += 1; | ||
}) | ||
} | ||
} | ||
|
||
#[test] | ||
fn static_storage_stringbuf() { | ||
let stringbuf = Arc::new(StringBuf::from_array([ | ||
Slot::new(String::new()), | ||
Slot::new(String::new()), | ||
Slot::new(String::new()), | ||
Slot::new(String::new()), | ||
Slot::new(String::new()), | ||
Slot::new(String::new()), | ||
Slot::new(String::new()), | ||
Slot::new(String::new()), | ||
])); | ||
|
||
let producer = { | ||
let stringbuf = stringbuf.clone(); | ||
thread::spawn(move || { | ||
for i in 0..16 { | ||
let mut string = 'write: loop { | ||
match stringbuf.write() { | ||
Ok(string) => break 'write string, | ||
_ => thread::yield_now(), | ||
} | ||
}; | ||
|
||
write!(&mut string, "{:?}", i).unwrap(); | ||
} | ||
}) | ||
}; | ||
|
||
let mut results = String::new(); | ||
|
||
// While the producer is writing to the queue, push each entry to the | ||
// results string. | ||
while Arc::strong_count(&stringbuf) > 1 { | ||
if let Some(string) = stringbuf.pop_ref() { | ||
writeln!(results, "{}", string).unwrap(); | ||
} | ||
thread::yield_now(); | ||
} | ||
|
||
producer.join().unwrap(); | ||
|
||
// drain the queue. | ||
while let Some(string) = stringbuf.pop_ref() { | ||
writeln!(results, "{}", string).unwrap(); | ||
} | ||
|
||
let results = dbg!(results); | ||
|
||
for (n, ln) in results.lines().enumerate() { | ||
assert_eq!(ln.parse::<usize>(), Ok(n)); | ||
} | ||
} |