spin_sync/
lib.rs

1// Copyright 2020 Shin Yoshida
2//
3// "LGPL-3.0-or-later OR Apache-2.0 OR BSD-2-Clause"
4//
5// This is part of spin-sync
6//
7//  spin-sync is free software: you can redistribute it and/or modify
8//  it under the terms of the GNU Lesser General Public License as published by
9//  the Free Software Foundation, either version 3 of the License, or
10//  (at your option) any later version.
11//
12//  spin-sync is distributed in the hope that it will be useful,
13//  but WITHOUT ANY WARRANTY; without even the implied warranty of
14//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//  GNU Lesser General Public License for more details.
16//
17//  You should have received a copy of the GNU Lesser General Public License
18//  along with spin-sync.  If not, see <http://www.gnu.org/licenses/>.
19//
20//
21// Licensed under the Apache License, Version 2.0 (the "License");
22// you may not use this file except in compliance with the License.
23// You may obtain a copy of the License at
24//
25//     http://www.apache.org/licenses/LICENSE-2.0
26//
27// Unless required by applicable law or agreed to in writing, software
28// distributed under the License is distributed on an "AS IS" BASIS,
29// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30// See the License for the specific language governing permissions and
31// limitations under the License.
32//
33//
34// Redistribution and use in source and binary forms, with or without modification, are permitted
35// provided that the following conditions are met:
36//
37// 1. Redistributions of source code must retain the above copyright notice, this list of
38//    conditions and the following disclaimer.
39// 2. Redistributions in binary form must reproduce the above copyright notice, this
40//    list of conditions and the following disclaimer in the documentation and/or other
41//    materials provided with the distribution.
42//
43// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
44// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
47// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
50// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52// POSSIBILITY OF SUCH DAMAGE.
53
54//! [![CircleCI](https://circleci.com/gh/wbcchsyn/spin-sync-rs.svg?style=svg)](https://circleci.com/gh/wbcchsyn/spin-sync-rs)
55//! [![Build Status](https://travis-ci.org/wbcchsyn/spin-sync-rs.svg?branch=master)](https://travis-ci.org/wbcchsyn/spin-sync-rs)
56//!
57//! spin-sync is a module providing synchronization primitives using spinlock. ([Wikipedia Spinlock](https://en.wikipedia.org/wiki/Spinlock))
58//!
59//! The main features are as follows.
60//!
61//! * Declaring public structs `Mutex` , `RwLock` , `Once` , `Barrier` . The interfaces are resembles those of `std::sync` .
62//! * Ensuring safety as much as `std::sync` , including poisoning strategy and marker traits.
63//! * Declaring public struct `Mutex8` , which behaves like a set of 8 `Mutex` instances except for
64//!   it gives up poison strategy. It is possible to acquire 2 or more than 2 locks of 1 `Mutex8`
65//!   instance at once.
66//! * Unlike to `std::sync`, the constructors of the public structs are const. For example, it is
67//!   possible to declare static `Mutex<T>` as long as T can be build statically.
68//!
69//! # Examples
70//!
71//! Declare `static spin_sync::Mutex<u64>` variable and update from multi threads.
72//! It is impossible in case of `std::sync::Mutex` .
73//!
74//! ```
75//! extern crate spin_sync;
76//!
77//! use spin_sync::Mutex;
78//! use std::thread;
79//!
80//! // Declare static mut Mutex<u64> variable.
81//! static COUNT: Mutex<u64> = Mutex::new(0);
82//!
83//! fn main() {
84//!     let num_thread = 10;
85//!     let mut handles = Vec::new();
86//!     
87//!     // Create worker threads to inclement COUNT by 1.
88//!     for _ in 0..10 {
89//!         let handle = thread::spawn(move || {
90//!             let mut count = COUNT.lock().unwrap();
91//!             *count += 1;
92//!         });
93//!
94//!         handles.push(handle);
95//!     }
96//!
97//!     // Wait for all the workers.
98//!     for handle in handles {
99//!         handle.join().unwrap();
100//!     }
101//!
102//!     // Make sure the value is incremented by the worker count.
103//!     let count = COUNT.lock().unwrap();
104//!     assert_eq!(num_thread, *count);
105//! }
106//! ```
107
108mod barrier;
109mod misc;
110mod mutex;
111mod mutex8;
112mod once;
113mod result;
114mod rwlock;
115
116pub use crate::barrier::{Barrier, BarrierWaitResult};
117pub use crate::mutex::{Mutex, MutexGuard};
118pub use crate::mutex8::{Mutex8, Mutex8Guard};
119pub use crate::once::{Once, OnceState};
120pub use crate::result::{LockResult, PoisonError, TryLockError, TryLockResult};
121pub use crate::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};