Crates.io | groupex |
lib.rs | groupex |
version | |
source | src |
created_at | 2024-09-06 16:14:18.209537 |
updated_at | 2024-10-27 19:15:23.210238 |
description | Syncronization primitive that allows acquire lock by index. |
homepage | |
repository | https://github.com/nutsalhan87/groupex |
max_upload_size | |
id | 1366112 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Syncronization primitive that allows acquire lock by index.
RawGroupex
RawGroupex
is base on which you can build collections with concurrent access.
It implements basic functions for acquiring and releasing a lock, such as lock
, try_lock
and unlock
.
These functions recieve index on which lock must be acquired/released.
Example:
let groupex = Arc::new(RawGroupex::<1>::new());
thread::scope(|scope| {
for i in 0..10 {
let groupex = &groupex;
scope.spawn(move || {
groupex.lock(i % 2);
// critical section
groupex.unlock(i % 2);
});
}
});
If lock already acquired by other thread, current thread will go to sleep until the lock is released.
RawGroupex
uses parking_lot_core internally for parking and unparking threads.
These functions will panic if index out of range. Range depends on generic parameter BLOCKS
.
One block contains 32 slots so the range of possible indexes is [0; BLOCKS * 32 - 1]. For example, this code will panic:
let groupex = RawGroupex::<8>::new();
groupex.lock(8 * 32);
RawGroupex
also has auxiliary functions is_locked
and elements
.
The first one will be useful to check if index currently acquired.
The second one will return number of indexes and will useful when creating other data structures on top of RawGroupex
.
GroupexMap
and GroupexVec
This crate provides GroupexMap
and GroupexVec
structs - hash table and array.
GroupexMap
GroupexMap
is hash table built on top of HashMap
and RawGroupex
.
Its cells can be locked by keys of any type that implement traits Eq
and Hash
.
Computed hash will be simply divided by Groupex::elements()
's result and remainder of the division will be used as index for locking.
Thus different keys can be resolved to the same index. So it's dangerous to lock another cell when one lock already acquired in the thread.
Take into account that using just HashMap<_, Mutex<_>>
is faster than GroupexMap
.
The second one is your choice only if you need more space-efficient solution.
GroupexVec
GroupexVec
works similar to the previous one but includes Vec
and its cells can be locked just by index.
It's also dangerous to lock another cell when one lock already acquired in the thread because different indexes can be collided due to size of the RawGroupex
.