1//! Async synchronization primitives.
2//!
3//! This crate provides the following primitives:
4//!
5//! * [`Barrier`] - enables tasks to synchronize all together at the same time.
6//! * [`Mutex`] - a mutual exclusion lock.
7//! * [`RwLock`] - a reader-writer lock, allowing any number of readers or a single writer.
8//! * [`Semaphore`] - limits the number of concurrent operations.
9//!
10//! ## Relationship with `std::sync`
11//!
12//! In general, you should consider using [`std::sync`] types over types from this crate.
13//!
14//! There are two primary use cases for types from this crate:
15//!
16//! - You need to use a synchronization primitive in a `no_std` environment.
17//! - You need to hold a lock across an `.await` point.
18//! (Holding an [`std::sync`] lock guard across an `.await` will make your future non-`Send`,
19//! and is also highly likely to cause deadlocks.)
20//!
21//! If you already use `libstd` and you aren't holding locks across await points (there is a
22//! Clippy lint called [`await_holding_lock`] that emits warnings for this scenario), you should
23//! consider [`std::sync`] instead of this crate. Those types are optimized for the currently
24//! running operating system, are less complex and are generally much faster.
25//!
26//! In contrast, `async-lock`'s notification system uses `std::sync::Mutex` under the hood if
27//! the `std` feature is enabled, and will fall back to a significantly slower strategy if it is
28//! not. So, there are few cases where `async-lock` is a win for performance over [`std::sync`].
29//!
30//! [`std::sync`]: https://doc.rust-lang.org/std/sync/index.html
31//! [`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/stable/index.html#/await_holding_lock
32
33#![cfg_attr(not(feature = "std"), no_std)]
34#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
35#![doc(
36 html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
37)]
38#![doc(
39 html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
40)]
41
42extern crate alloc;
43
44/// Simple macro to extract the value of `Poll` or return `Pending`.
45///
46/// TODO: Drop in favor of `core::task::ready`, once MSRV is bumped to 1.64.
47macro_rules! ready {
48 ($e:expr) => {{
49 use ::core::task::Poll;
50
51 match $e {
52 Poll::Ready(v) => v,
53 Poll::Pending => return Poll::Pending,
54 }
55 }};
56}
57
58/// Pins a variable on the stack.
59///
60/// TODO: Drop in favor of `core::pin::pin`, once MSRV is bumped to 1.68.
61macro_rules! pin {
62 ($($x:ident),* $(,)?) => {
63 $(
64 let mut $x = $x;
65 #[allow(unused_mut)]
66 let mut $x = unsafe {
67 core::pin::Pin::new_unchecked(&mut $x)
68 };
69 )*
70 }
71}
72
73mod barrier;
74mod mutex;
75mod once_cell;
76mod rwlock;
77mod semaphore;
78
79pub use barrier::{Barrier, BarrierWaitResult};
80pub use mutex::{Mutex, MutexGuard, MutexGuardArc};
81pub use once_cell::OnceCell;
82pub use rwlock::{
83 RwLock, RwLockReadGuard, RwLockReadGuardArc, RwLockUpgradableReadGuard,
84 RwLockUpgradableReadGuardArc, RwLockWriteGuard, RwLockWriteGuardArc,
85};
86pub use semaphore::{Semaphore, SemaphoreGuard, SemaphoreGuardArc};
87
88pub mod futures {
89 //! Named futures for use with `async_lock` primitives.
90
91 pub use crate::barrier::BarrierWait;
92 pub use crate::mutex::{Lock, LockArc};
93 pub use crate::rwlock::futures::{
94 Read, ReadArc, UpgradableRead, UpgradableReadArc, Upgrade, UpgradeArc, Write, WriteArc,
95 };
96 pub use crate::semaphore::{Acquire, AcquireArc};
97}
98
99#[cold]
100fn abort() -> ! {
101 // For no_std targets, panicking while panicking is defined as an abort
102 #[cfg(not(feature = "std"))]
103 {
104 struct Bomb;
105
106 impl Drop for Bomb {
107 fn drop(&mut self) {
108 panic!("Panicking while panicking to abort")
109 }
110 }
111
112 let _bomb = Bomb;
113 panic!("Panicking while panicking to abort")
114 }
115
116 // For libstd targets, abort using std::process::abort
117 #[cfg(feature = "std")]
118 std::process::abort()
119}
120