<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `spin_sync` crate."><meta name="keywords" content="rust, rustlang, rust-lang, spin_sync"><title>spin_sync - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu.css" disabled ><script id="default-settings"></script><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../favicon.svg">
<link rel="alternate icon" type="image/png" href="../favicon-16x16.png">
<link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../spin_sync/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><p class="location">Crate spin_sync</p><div class="block version"><p>Version 0.3.1</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all spin_sync's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class="location"></p><script>window.sidebarCurrent = {name: "spin_sync", ty: "mod", relpath: "../"};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><span class="help-button">?</span>
<a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../src/spin_sync/lib.rs.html#54-121" title="goto source code">[src]</a></span><span class="in-band">Crate <a class="mod" href="">spin_sync</a></span></h1><div class="docblock"><p><a href="https://circleci.com/gh/wbcchsyn/spin-sync-rs"><img src="https://circleci.com/gh/wbcchsyn/spin-sync-rs.svg?style=svg" alt="CircleCI" /></a>
<a href="https://travis-ci.org/wbcchsyn/spin-sync-rs"><img src="https://travis-ci.org/wbcchsyn/spin-sync-rs.svg?branch=master" alt="Build Status" /></a></p>
<p>spin-sync is a module providing synchronization primitives using spinlock. (<a href="https://en.wikipedia.org/wiki/Spinlock">Wikipedia Spinlock</a>)</p>
<p>The main features are as follows.</p>
<ul>
<li>Declaring public structs <code>Mutex</code> , <code>RwLock</code> , <code>Once</code> , <code>Barrier</code> . The interfaces are resembles those of <code>std::sync</code> .</li>
<li>Ensuring safety as much as <code>std::sync</code> , including poisoning strategy and marker traits.</li>
<li>Declaring public struct <code>Mutex8</code> , which behaves like a set of 8 <code>Mutex</code> instances except for
it gives up poison strategy. It is possible to acquire 2 or more than 2 locks of 1 <code>Mutex8</code>
instance at once.</li>
<li>Unlike to <code>std::sync</code>, the constructors of the public structs are const. For example, it is
possible to declare static <code>Mutex<T></code> as long as T can be build statically.</li>
</ul>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Declare <code>static spin_sync::Mutex<u64></code> variable and update from multi threads.
It is impossible in case of <code>std::sync::Mutex</code> .</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">spin_sync</span>;
<span class="kw">use</span> <span class="ident">spin_sync</span>::<span class="ident">Mutex</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
<span class="comment">// Declare static mut Mutex<u64> variable.</span>
<span class="kw">static</span> <span class="ident">COUNT</span>: <span class="ident">Mutex</span><span class="op"><</span><span class="ident">u64</span><span class="op">></span> <span class="op">=</span> <span class="ident">Mutex</span>::<span class="ident">new</span>(<span class="number">0</span>);
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">num_thread</span> <span class="op">=</span> <span class="number">10</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">handles</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>();
<span class="comment">// Create worker threads to inclement COUNT by 1.</span>
<span class="kw">for</span> <span class="kw">_</span> <span class="kw">in</span> <span class="number">0</span>..<span class="number">10</span> {
<span class="kw">let</span> <span class="ident">handle</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">count</span> <span class="op">=</span> <span class="ident">COUNT</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="kw-2">*</span><span class="ident">count</span> <span class="op">+</span><span class="op">=</span> <span class="number">1</span>;
});
<span class="ident">handles</span>.<span class="ident">push</span>(<span class="ident">handle</span>);
}
<span class="comment">// Wait for all the workers.</span>
<span class="kw">for</span> <span class="ident">handle</span> <span class="kw">in</span> <span class="ident">handles</span> {
<span class="ident">handle</span>.<span class="ident">join</span>().<span class="ident">unwrap</span>();
}
<span class="comment">// Make sure the value is incremented by the worker count.</span>
<span class="kw">let</span> <span class="ident">count</span> <span class="op">=</span> <span class="ident">COUNT</span>.<span class="ident">lock</span>().<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num_thread</span>, <span class="kw-2">*</span><span class="ident">count</span>);
}</pre></div>
</div><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2>
<table><tr class="module-item"><td><a class="struct" href="struct.Barrier.html" title="spin_sync::Barrier struct">Barrier</a></td><td class="docblock-short"><p>A barrier enables multiple threads to synchronize the beginning
of some computation.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.BarrierWaitResult.html" title="spin_sync::BarrierWaitResult struct">BarrierWaitResult</a></td><td class="docblock-short"></td></tr><tr class="module-item"><td><a class="struct" href="struct.Mutex.html" title="spin_sync::Mutex struct">Mutex</a></td><td class="docblock-short"><p>A mutual exclusion primitive useful for protecting shared data.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Mutex8.html" title="spin_sync::Mutex8 struct">Mutex8</a></td><td class="docblock-short"><p><code>Mutex8</code> is a set of mutexes. Each instance includes 8 mutexes.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Mutex8Guard.html" title="spin_sync::Mutex8Guard struct">Mutex8Guard</a></td><td class="docblock-short"><p>An RAII implementation of a "scoped lock(s)" of a <a href="struct.Mutex8.html"><code>Mutex8</code></a> .</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.MutexGuard.html" title="spin_sync::MutexGuard struct">MutexGuard</a></td><td class="docblock-short"><p>An RAII implementation of a "scoped lock" of a mutex.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Once.html" title="spin_sync::Once struct">Once</a></td><td class="docblock-short"><p>A synchronization primitive which can be used to run a one-time global initialization.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.OnceState.html" title="spin_sync::OnceState struct">OnceState</a></td><td class="docblock-short"><p>State yielded to <a href="struct.Once.html#method.call_once_force"><code>call_once_force</code></a> ’s closure parameter. The state can be used to query
the poison status of the <a href="struct.Once.html"><code>Once</code></a></p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.RwLock.html" title="spin_sync::RwLock struct">RwLock</a></td><td class="docblock-short"><p>A reader-writer lock.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.RwLockReadGuard.html" title="spin_sync::RwLockReadGuard struct">RwLockReadGuard</a></td><td class="docblock-short"><p>An RAII implementation of a "scoped shared read lock" of a RwLock.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.RwLockWriteGuard.html" title="spin_sync::RwLockWriteGuard struct">RwLockWriteGuard</a></td><td class="docblock-short"><p>An RAII implementation of a "scoped exclusive write lock" of a RwLock.</p>
</td></tr></table><h2 id="types" class="section-header"><a href="#types">Type Definitions</a></h2>
<table><tr class="module-item"><td><a class="type" href="type.LockResult.html" title="spin_sync::LockResult type">LockResult</a></td><td class="docblock-short"><p>Alias to std::sync::LockResult.</p>
</td></tr><tr class="module-item"><td><a class="type" href="type.PoisonError.html" title="spin_sync::PoisonError type">PoisonError</a></td><td class="docblock-short"><p>Alias to std::sync::PoisonError</p>
</td></tr><tr class="module-item"><td><a class="type" href="type.TryLockError.html" title="spin_sync::TryLockError type">TryLockError</a></td><td class="docblock-short"><p>Alias to std::sync::TryLockError</p>
</td></tr><tr class="module-item"><td><a class="type" href="type.TryLockResult.html" title="spin_sync::TryLockResult type">TryLockResult</a></td><td class="docblock-short"><p>Alias to std::sync::TryLockResult</p>
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../";window.currentCrate = "spin_sync";</script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>