summaryrefslogtreecommitdiff
path: root/src/poisonable.rs
diff options
context:
space:
mode:
authorMica White <botahamec@gmail.com>2024-07-17 16:37:21 -0400
committerMica White <botahamec@gmail.com>2024-07-21 12:55:32 -0400
commitbd64ff98530ea5f92ce528009d65203f0f6676fe (patch)
tree27e6c6a2153e3a9c4ecf4087f473b89a01b25033 /src/poisonable.rs
parent32f972a26a0066291873445088718deec3ed4233 (diff)
Create Poisonable API
Diffstat (limited to 'src/poisonable.rs')
-rw-r--r--src/poisonable.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/poisonable.rs b/src/poisonable.rs
new file mode 100644
index 0000000..49979e5
--- /dev/null
+++ b/src/poisonable.rs
@@ -0,0 +1,46 @@
+#[cfg(not(panic = "unwind"))]
+use std::convert::Infallible;
+use std::marker::PhantomData;
+use std::sync::atomic::AtomicBool;
+
+use crate::lockable::{Lockable, RawLock};
+
+mod error;
+mod flag;
+mod guard;
+mod poisonable;
+
+#[derive(Debug, Default)]
+struct PoisonFlag(#[cfg(panic = "unwind")] AtomicBool);
+
+#[derive(Debug, Default)]
+pub struct Poisonable<L: Lockable + RawLock> {
+ inner: L,
+ poisoned: PoisonFlag,
+}
+
+pub struct PoisonRef<'flag, G> {
+ guard: G,
+ #[cfg(panic = "unwind")]
+ flag: &'flag PoisonFlag,
+}
+
+pub struct PoisonGuard<'flag, 'key, G, Key> {
+ guard: PoisonRef<'flag, G>,
+ key: Key,
+ _phantom: PhantomData<&'key ()>,
+}
+
+pub struct PoisonError<Guard> {
+ guard: Guard,
+}
+
+pub enum TryLockPoisonableError<'flag, 'key, G, Key: 'key> {
+ Poisoned(PoisonError<PoisonGuard<'flag, 'key, G, Key>>),
+ WouldBlock(Key),
+}
+
+pub type PoisonResult<Guard> = Result<Guard, PoisonError<Guard>>;
+
+pub type TryLockPoisonableResult<'flag, 'key, G, Key> =
+ Result<PoisonGuard<'flag, 'key, G, Key>, TryLockPoisonableError<'flag, 'key, G, Key>>;