Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tokio locks pr #82

Merged
merged 4 commits into from
Jan 9, 2024
Merged

Conversation

dan-da
Copy link
Collaborator

@dan-da dan-da commented Jan 9, 2024

This PR adds crate::util_types::sync::tokio::{AtomicRw, AtomicMutex}. These are wrappers around tokio::sync::{RwLock, Mutex} and complement twenty_first::sync::{AtomicRw, AtomicMutex} which wrap the std library lock types.

This PR just introduces the locks without using them in the App yet.

This is extracted from, and is a building block for, a much larger neptune-core PR to follow that reworks lock handling substantially.

These async tokio locks are very similar to the sync counterparts except that:

  1. calls to lock() and lock_mut() are async, so would typically be followed by .await.
  2. additional methods lock_async() and lock_async_mut() are provided. These enable the caller to pass in an async callback fn. Unfortunately the usage/ergonomics is a bit awkward (see doctest examples) and has a runtime cost so in practice I have tended to just use lock_guard() and lock_guard_mut() instead.

Like the sync types in twenty-first, these types support named locks with lock-event callbacks, which enables detailed loggining/tracing for detecting deadlocks, and lock usage by thread or tokio task.

Adds util_types::sync::tokio::{AtomicMutex, AtomicRw} which are tokio
counterparts of twenty_first::sync::{AtomicMutex, AtomicRw}.
Adds `with_async()` and `with_mut_async()` to AtomicRw and AtomicMutex
Renames locking methods in crate::sync to include the word "lock".

The word "with" is a bit vague, and when reading code that is using
the locks, it is helpful to clearly see where locks occur, and which
type.

The renames are:
 with()      --> lock()
 with_mut()  --> lock_mut()
 guard()     --> lock_guard()
 guard_mut() --> lock_guard_mut()

These match the naming in twenty_first::sync::*
Atomic lock types in util_types::sync::tokio now accept
an optional name and lock-event callback fn.

This enables the app to be notified of lock events and take appropriate
action, such as logging/tracing.

These features are optional and the API is backwards compatible
with code that does not need lock-event notifications.
@dan-da dan-da merged commit 2ce0378 into Neptune-Crypto:master Jan 9, 2024
3 checks passed
@dan-da
Copy link
Collaborator Author

dan-da commented Jan 9, 2024

In the interest of saving time, I merged without a review. Anyway, this doesn't really change anything, just adds new, as-yet unused types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant