• Home
Name Date Size #Lines LOC

..--

.github/workflows/03-May-2024-2317

examples/03-May-2024-2219

patches/03-May-2024-7063

script/03-May-2024-43

src/03-May-2024-3,2371,710

.cargo_vcs_info.jsonD03-May-202474 65

.gitignoreD03-May-202428 43

.travis.ymlD03-May-2024663 3424

Android.bpD03-May-20242.1 KiB8073

CHANGELOG.mdD03-May-20241.3 KiB5737

Cargo.tomlD03-May-20241 KiB3128

Cargo.toml.origD03-May-2024545 2219

LICENSED03-May-20241.1 KiB2117

METADATAD03-May-2024361 2019

MODULE_LICENSE_MITD03-May-20240

OWNERSD03-May-202440 21

README.mdD03-May-20242.9 KiB9767

TEST_MAPPINGD03-May-20241.1 KiB5655

cargo2android.jsonD03-May-2024227 1212

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