Expand description
§ARCHIVED ARCHIVED ARCHIVED
This crate is archived and will not be updated.
The code is now at
safina::sync
in the
safina
crate.
§safina-sync
Structs for sharing or sending data between async tasks.
It is part of safina
, a safe async runtime.
§Features
Mutex
with an async lock methodoneshot
andsync_channel
with async and blocking methodsforbid(unsafe_code)
- Depends only on
std
- 100% test coverage
- Works with
safina-executor
or any async executor
§Limitations
- Allocates
- No const constructor.
Uses
std::sync::Mutex
internally, which does not have a const constructor. See rust#66806 and const-eval#3. You can work around this with unstablecore::lazy::OnceCell
or variousunsafe
crates:lazy_static
,once_cell
,lazycell
, andconquer-once
.
§Documentation
§Examples
use std::sync::Arc;
use safina_async_test::async_test;
use safina_sync::Mutex;
let shared_counter: Arc<Mutex<u32>> = get_shared_data();
{
let mut counter_guard = shared_counter.lock().await;
*counter_guard += 1;
// some_async_fn().await; // Cannot await while holding a MutexGuard.
}
some_async_fn().await; // Await is ok after releasing MutexGuard.
§Alternatives
- async-lock
- Contains a little
unsafe
code
- Contains a little
- futures-locks
- Contains a little
unsafe
code
- Contains a little
- futures-util
- Very popular
- Full of
unsafe
- tokio-sync
- Very popular
- Fast
- Internally incredibly complicated
- Full of
unsafe
§Changelog
- v0.2.4
- Implement
Eq
andPartialEq
forReceiver
,OneSender
, andSyncSender
. - Fix bug where
await
onReceiver
would not wake senders.
- Implement
- v0.2.3 Fix race condition.
- v0.2.2 Fix deadlock on Linux.
- v0.2.1
- Add
sync_channel
andSyncSender
. - Add
Receiver::async_recv
to let users await without writing ugly(&mut receiver).await
. - Remove
Receiver::blocking
and addtry_recv
,recv
, etc.
- Add
- v0.2.0 - Replace
Promise
withoneshot
,OneSender
, andReceiver
that supports async and blocking reads. - v0.1.6 - Update docs.
- v0.1.5 - Update docs
- v0.1.4 - Update docs, make
MutexGuard::new
non-public - v0.1.3 - Fix Promise type parameter
- v0.1.2 - Add Promise
- v0.1.1 - Improve Mutex performance when there are many waiters
- v0.1.0 - First published version
§TO DO
- Add
Barrier
- Add
RwLock
- Add
WaitableBool
- Add
UnboundedChannel
- Add
WaitableQueue
(multiple receivers) - Add
UnboundedWaitableQueue
- Add
Topic
(copies message to every receiver)
§Release Process
- Edit
Cargo.toml
and bump version number. - Run
./release.sh
Structs§
- Inner
- Into
Iter - An owning iterator over messages on a
Receiver
, created byinto_iter
. - Iter
- An iterator over messages on a
Receiver
, created byiter
. - Mutex
- A wrapper around
std::sync::Mutex
with an asynclock
method. - Mutex
Guard - An RAII
scoped lock of a
Mutex
. It automatically unlocks the mutex when dropped (falls out of scope). - OneSender
- Receiver
- The receiving half of a channel. This half can only be owned by one thread.
- SendFut
- Sync
Sender - TryIter
- An iterator that attempts to yield all pending values for a
Receiver
, created bytry_iter
.
Functions§
- oneshot
- Creates a channel that can be used to send a single value.
- sync_
channel - Creates a new synchronous, bounded channel.
All data sent on the
SyncSender
will become available on theReceiver
in the same order as it was sent. TheReceiver
will block until a message becomes available.