Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | - | - | ||||
.github/workflows/ | 03-May-2024 | - | 23 | 17 | ||
examples/ | 03-May-2024 | - | 22 | 19 | ||
patches/ | 03-May-2024 | - | 70 | 63 | ||
script/ | 03-May-2024 | - | 4 | 3 | ||
src/ | 03-May-2024 | - | 3,237 | 1,710 | ||
.cargo_vcs_info.json | D | 03-May-2024 | 74 | 6 | 5 | |
.gitignore | D | 03-May-2024 | 28 | 4 | 3 | |
.travis.yml | D | 03-May-2024 | 663 | 34 | 24 | |
Android.bp | D | 03-May-2024 | 2.1 KiB | 80 | 73 | |
CHANGELOG.md | D | 03-May-2024 | 1.3 KiB | 57 | 37 | |
Cargo.toml | D | 03-May-2024 | 1 KiB | 31 | 28 | |
Cargo.toml.orig | D | 03-May-2024 | 545 | 22 | 19 | |
LICENSE | D | 03-May-2024 | 1.1 KiB | 21 | 17 | |
METADATA | D | 03-May-2024 | 361 | 20 | 19 | |
MODULE_LICENSE_MIT | D | 03-May-2024 | 0 | |||
OWNERS | D | 03-May-2024 | 40 | 2 | 1 | |
README.md | D | 03-May-2024 | 2.9 KiB | 97 | 67 | |
TEST_MAPPING | D | 03-May-2024 | 1.1 KiB | 56 | 55 | |
cargo2android.json | D | 03-May-2024 | 227 | 12 | 12 |
README.md
1# spin-rs 2 3[![Crates.io version](https://img.shields.io/crates/v/spin.svg)](https://crates.io/crates/spin) 4[![docs.rs](https://docs.rs/spin/badge.svg)](https://docs.rs/spin/) 5[![Build Status](https://travis-ci.org/mvdnes/spin-rs.svg)](https://travis-ci.org/mvdnes/spin-rs) 6 7Spin-based synchronization primitives. 8 9This crate provides [spin-based](https://en.wikipedia.org/wiki/Spinlock) 10versions of the primitives in `std::sync`. Because synchronization is done 11through spinning, the primitives are suitable for use in `no_std` environments. 12 13Before deciding to use `spin`, we recommend reading 14[this superb blog post](https://matklad.github.io/2020/01/02/spinlocks-considered-harmful.html) 15by [@matklad](https://github.com/matklad/) that discusses the pros and cons of 16spinlocks. If you have access to `std`, it's likely that the primitives in 17`std::sync` will serve you better except in very specific circumstances. 18 19## Features 20 21- `Mutex`, `RwLock` and `Once` equivalents 22- Support for `no_std` environments 23- [`lock_api`](https://crates.io/crates/lock_api) compatibility 24- Upgradeable `RwLock` guards 25- Guards can be sent and shared between threads 26- Guard leaking 27- `std` feature to enable yield to the OS scheduler in busy loops 28- `Mutex` can become a ticket lock 29 30## Usage 31 32Include the following under the `[dependencies]` section in your `Cargo.toml` file. 33 34```toml 35spin = "x.y" 36``` 37 38## Example 39 40When calling `lock` on a `Mutex` you will get a guard value that provides access 41to the data. When this guard is dropped, the lock will be unlocked. 42 43```rust 44extern crate spin; 45use std::{sync::Arc, thread}; 46 47fn main() { 48 let counter = Arc::new(spin::Mutex::new(0)); 49 50 let thread = thread::spawn({ 51 let counter = counter.clone(); 52 move || { 53 for _ in 0..10 { 54 *counter.lock() += 1; 55 } 56 } 57 }); 58 59 for _ in 0..10 { 60 *counter.lock() += 1; 61 } 62 63 thread.join().unwrap(); 64 65 assert_eq!(*counter.lock(), 20); 66} 67``` 68 69## Feature flags 70 71The crate comes with a few feature flags that you may wish to use. 72 73- `lock_api` enabled support for [`lock_api`](https://crates.io/crates/lock_api) 74 75- `ticket_mutex` uses a ticket lock for the implementation of `Mutex` 76 77- `std` enables support for thread yielding instead of spinning 78 79## Remarks 80 81It is often desirable to have a lock shared between threads. Wrapping the lock in an 82`std::sync::Arc` is route through which this might be achieved. 83 84Locks provide zero-overhead access to their data when accessed through a mutable 85reference by using their `get_mut` methods. 86 87The behaviour of these lock is similar to their namesakes in `std::sync`. they 88differ on the following: 89 90- Locks will not be poisoned in case of failure. 91- Threads will not yield to the OS scheduler when encounter a lock that cannot be 92accessed. Instead, they will 'spin' in a busy loop until the lock becomes available. 93 94## License 95 96`spin` is distributed under the MIT License, (See `LICENSE`). 97