summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: b26d0b17c4c93c5620a8c23a7bde363b8918c919 (plain)
#![no_std]
#![forbid(unsafe_op_in_unsafe_fn)]

extern crate alloc;

// tier 1: linux, windows, wasm w/o atomics
// tier 2: mac os, android, ios, fuschia, illumos, freebsd, netbsd, solaris, redox, uefi, zkvm, embedded
// not supported: dragonfly, wasm w/ atomics, hermit, teeos, sgx, solid, xous

cfg_if::cfg_if! {
	if #[cfg(any(
		all(target_os = "windows", not(target_vendor = "win7")),
		target_os = "linux",
		target_os = "android",
		//target_os = "freebsd",
		//target_os = "openbsd",
		//target_os = "dragonfly",
		//all(target_family = "wasm", target_feature = "atomics"),
		//target_os = "hermit",
	))] {
		mod futex;
	} else if #[cfg(any(
		target_os = "dragonfly",
		all(target_family = "wasm", target_feature = "atomics"),
		target_os = "hermit"
	))] {
		// merge with above when implemented
	//} else if #[cfg(target_os = "fuchsia")] {
	//	mod fuchsia;
	//  mod unix;
	} else if #[cfg(any(
		target_family = "unix",
	))] {
		extern crate alloc;

		mod lazy_box;

		mod pthread;
		//mod queue;
	} else if #[cfg(target_os = "teeos")] {
		extern crate alloc;

		mod lazy_box;

		mod pthread;
		//mod teeos;
	} else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
		mod windows7;
		//mod queue;
	} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
		//mod sgx;
		//mod queue;
	} else if #[cfg(target_os = "solid_asp3")] {
		//mod itron;
		//mod solid;
	} else if #[cfg(target_os = "xous")] {
		//mod xous;
		//mod queue;
	} else if #[cfg(any(
		target_family = "wasm",
		target_os = "uefi",
		target_os = "zkvm"
	))] {
		mod no_threads;
	} else if #[cfg(target_has_atomic = "8")] {
		mod spin;
	}
}

cfg_if::cfg_if! {
	if #[cfg(any(
		all(target_os = "windows", not(target_vendor = "win7")),
		target_os = "linux",
		target_os = "android",
		//target_os = "freebsd",
		//target_os = "openbsd",
		//target_os = "dragonfly",
		//all(target_family = "wasm", target_feature = "atomics"),
		//target_os = "hermit",
	))] {
		pub use futex::Mutex;
	} else if #[cfg(any(
		target_os = "dragonfly",
		all(target_family = "wasm", target_feature = "atomics"),
		target_os = "hermit"
	))] {
		// merge with above when implemented
	//} else if #[cfg(target_os = "fuchsia")] {
	//	mod fuchsia;
	//	pub use fuchsia::Mutex;
	} else if #[cfg(any(
		target_family = "unix",
		//target_os = "teeos",
	))] {
		pub use pthread::Mutex;
	} else if #[cfg(target_os = "teeos")] {
		// merge with above when implemented
	} else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
		pub use windows7::Mutex;
	} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
		//pub use sgx::Mutex;
	} else if #[cfg(target_os = "solid_asp3")] {
	   //pub use itron::Mutex;
	} else if #[cfg(target_os = "xous")] {
		//pub use xous::Mutex;
	} else if #[cfg(any(
		target_family = "wasm",
		target_os = "uefi",
		target_os = "zkvm"
	))] {
		pub use no_threads::Mutex;
	} else if #[cfg(all(target_os = "none", target_has_atomic = "8"))] {
		pub use spin::Mutex;
	}
}

cfg_if::cfg_if! {
	if #[cfg(any(
		all(target_os = "windows", not(target_vendor = "win7")),
		target_os = "linux",
		target_os = "android",
		//target_os = "freebsd",
		//target_os = "openbsd",
		//target_os = "dragonfly",
		//target_os = "fuchsia",
		//all(target_family = "wasm", target_feature = "atomics"),
		//target_os = "hermit",
	))] {
		pub use futex::RwLock;
	} else if #[cfg(any(
		all(target_family = "wasm", target_feature = "atomics"),
		target_os = "hermit"
	))] {
		// merge with above when implemented
	} else if #[cfg(any(
		target_family = "unix",
		all(target_os = "windows", target_vendor = "win7"),
		//all(target_vendor = "fortanix", target_env = "sgx"),
		//target_os = "xous",
	))] {
		//pub use queue::RwLock;
	} else if #[cfg(any(
		all(target_vendor = "fortanix", target_env = "sgx"),
		target_os = "xous",)
	)] {
		// merge with above when implemented
	} else if #[cfg(target_os = "solid_asp3")] {
		//pub use solid::RwLock;
	} else if #[cfg(target_os = "teeos")] {
		//pub use teeos::RwLock;
	} else if #[cfg(any(
		target_family = "wasm",
		target_os = "uefi",
		target_os = "zkvm"
	))] {
		pub use no_threads::RwLock;
	} else if #[cfg(all(target_os = "none", target_has_atomic = "8"))] {
		//pub use spin::RwLock;
	}
}

unsafe impl lock_api::RawMutex for Mutex {
	#[allow(clippy::declare_interior_mutable_const)]
	const INIT: Self = Self::new();

	type GuardMarker = lock_api::GuardNoSend;

	#[no_panic::no_panic]
	fn lock(&self) {
		// safety: unsafe_lock is disabled
		unsafe { self.lock() }
	}

	#[no_panic::no_panic]
	fn try_lock(&self) -> bool {
		unsafe { self.try_lock() }
	}

	#[no_panic::no_panic]
	unsafe fn unlock(&self) {
		unsafe { self.unlock() }
	}

	fn is_locked(&self) -> bool {
		unsafe { self.is_locked() }
	}
}