#mutex #raii #rust #criticalsection #threadsync

aloe-critical-section

Aloe Critical Section provides RAII-based thread synchronization tools for scoped locking in Rust. It includes constructs for critical sections and mutexes with automatic lock management. This crate is a translation of the c++ juce module serving the same function.

2 releases

Uses new Rust 2024

new 0.1.2 Apr 2, 2025
0.1.0 Apr 2, 2025

#17 in #raii

Download history 91/week @ 2025-03-28

91 downloads per month
Used in 199 crates (61 directly)

GPL-3.0 license

51KB
350 lines

Aloe Critical Section

Aloe Critical Section is a Rust crate designed for managing thread synchronization through scoped locking using a Resource Acquisition Is Initialization (RAII) pattern. It encompasses a suite of generic constructs for safely handling critical sections, ensuring that shared resources are consistently accessed in a thread-safe manner.

Features

  • Scoped Locking for Critical Sections: Provides RAII-based constructs like ScopedLock, ScopedUnlock, and ScopedTryLock for automatic locking and unlocking of critical section resources.
  • Generic Template Support: Utilize GenericScopedLock, GenericScopedUnlock, and GenericScopedTryLock for mutex objects, supporting various locking primitives such as CriticalSection, SpinLock, etc.
  • Reentrant Mutex: Supports re-entrant locking through the CriticalSection structure.
  • Dummy Critical Section: Implements a dummy lock, useful for templated systems where locking can be optimized away.

Usage

A simple usage of ScopedLock for a critical section is as follows:

struct MyObject {
    object_lock: CriticalSection,
}

impl MyObject {
    pub fn foo(&self) {
        let _scoped_lock = ScopedLock::new(&self.object_lock);
        // object_lock is now locked..
        // Thread-safe operations
        // object_lock is unlocked as _scoped_lock goes out of scope
    }
}

Mutex Handling with Generic Scoped Locks

Use GenericScopedLock for RAII-based mutex management:

let critical_section = CriticalSection::new();
{
    let _lock = GenericScopedLock::new(&critical_section);
    // Perform operations with the lock held
}
// Lock released at the end of the block

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the GPL-3.0 License.

Acknowledgments

This crate was developed with contributions from klebs.


This README was generated by an AI model and may not be 100% accurate, but it should be pretty good.

This crate is a translation of the JUCE module.

JUCE is a c++ software framework for developing high performance audio applications.

Usage falls under the GPLv3 as well as the JUCE commercial license.

See github.com/juce-framework/JUCE and the JUCE license page for details.

This crate is in the process of being translated from c++ to rust. For progress updates, please see the workspacer rust project. designed specifically for rust projects.

Dependencies

~13–23MB
~371K SLoC