Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

thread-model

A unified, trait-based abstraction over threading and memory-sharing models for Rust.

thread-model allows library authors to write code that is generic over its concurrency requirements. By programming against the ThreadModel trait, your logic can switch between single-threaded (high performance, no atomic overhead) and multi-threaded (thread-safe, atomic) implementations without changing a single line of business logic.

Key Features

  • Generic Reference Counting: Support for Rc<T> vs Arc<T> (and Weak pointers).
  • Abstracted Locking: Unified interface for Mutex and RwLock via parking_lot's RawMutex / RawRwLock traits.
  • Generic Atomics: Powered by radium, allowing seamless switching between Atom<T> and Radon<T>.
  • Plug-and-Play: Built-in implementations for ThreadSafe (using Arc/Atom/parking_lot) and ThreadLocal (using Rc/Radon/RefCell).

Comparison with archery

If you are familiar with the archery crate:

  • Similarities: Both crates aim to solve the problem of being generic over Rc and Arc.
  • The Difference: archery focuses primarily on the pointer wrapper (SharedPtr<T, P>). thread-model takes a broader approach by abstracting the entire environment.
  • Beyond Pointers: While archery handles reference counting, thread-model also abstracts Locking mechanisms (Mutexes/RwLocks) and Atomic Cells. This makes it a more comprehensive tool for building entire frameworks (like ECS or Actor systems) that need to be portable between sync and async/local-thread contexts.

Installation

Add this to your Cargo.toml:

[dependencies]
thread-model = "0.1.0"

Usage

1. Define your component using ThreadModel

use thread_model::{ThreadModel, RefCounter};

pub struct MyComponent<M: ThreadModel> {
    data: RefCounter<M, String>,
    lock: M::RawMutex,
}

impl<M: ThreadModel> MyComponent<M> {
    pub fn new(val: String) -> Self {
        // Implementation remains the same regardless of the model
    }
}

2. Choose your implementation

Single-threaded (No atomic overhead):

use thread_model::ThreadLocal;
let local_comp = MyComponent::<ThreadLocal>::new("Local".into());

Multi-threaded (Thread-safe):

use thread_model::ThreadSafe;
let safe_comp = MyComponent::<ThreadSafe>::new("Safe".into());

Architecture

The crate provides three core modules:

  • ThreadModel: The core trait defining associated types for RefCounter, AtomicCell, RawMutex, etc.
  • ThreadSafe: An implementation using std::sync::Arc, parking_lot locks, and atomic operations.
  • ThreadLocal: An implementation using std::rc::Rc, RefCell-based "locks", and non-atomic operations (via radium::Radon).

License

Licensed under either of:

at your option.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages