2018-10-28 21:14:05 -05:00

311 lines
23 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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 `lock_api` crate."><meta name="keywords" content="rust, rustlang, rust-lang, lock_api"><title>lock_api - 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="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script></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">&#9776;</div><p class='location'>Crate lock_api</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'lock_api', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press S to search, ? for more options…" type="search"><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='in-band'>Crate <a class="mod" href=''>lock_api</a></span><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'>&#x2212;</span>]</a></span><a class='srclink' href='../src/lock_api/lib.rs.html#8-109' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>This library provides type-safe and fully-featured <code>Mutex</code> and <code>RwLock</code>
types which wrap a simple raw mutex or rwlock type. This has several
benefits: not only does it eliminate a large portion of the work in
implementing custom lock types, it also allows users to write code which is
generic with regards to different lock implementations.</p>
<p>Basic usage of this crate is very straightfoward:</p>
<ol>
<li>Create a raw lock type. This should only contain the lock state, not any
data protected by the lock.</li>
<li>Implement the <code>RawMutex</code> trait for your custom lock type.</li>
<li>Export your mutex as a type alias for <code>lock_api::Mutex</code>, and
your mutex guard as a type alias for <code>lock_api::MutexGuard</code>.
See the <a href="#example">example</a> below for details.</li>
</ol>
<p>This process is similar for RwLocks, except that two guards need to be
exported instead of one. (Or 3 guards if your type supports upgradable read
locks, see <a href="#extension-traits">extension traits</a> below for details)</p>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">lock_api</span>::{<span class="ident">RawMutex</span>, <span class="ident">Mutex</span>, <span class="ident">GuardSend</span>};
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::{<span class="ident">AtomicBool</span>, <span class="ident">Ordering</span>, <span class="ident">ATOMIC_BOOL_INIT</span>};
<span class="comment">// 1. Define our raw lock type</span>
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">RawSpinlock</span>(<span class="ident">AtomicBool</span>);
<span class="comment">// 2. Implement RawMutex for this type</span>
<span class="kw">unsafe</span> <span class="kw">impl</span> <span class="ident">RawMutex</span> <span class="kw">for</span> <span class="ident">RawSpinlock</span> {
<span class="kw">const</span> <span class="ident">INIT</span>: <span class="ident">RawSpinlock</span> <span class="op">=</span> <span class="ident">RawSpinlock</span>(<span class="ident">ATOMIC_BOOL_INIT</span>);
<span class="comment">// A spinlock guard can be sent to another thread and unlocked there</span>
<span class="kw">type</span> <span class="ident">GuardMarker</span> <span class="op">=</span> <span class="ident">GuardSend</span>;
<span class="kw">fn</span> <span class="ident">lock</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) {
<span class="comment">// Note: This isn&#39;t the best way of implementing a spinlock, but it</span>
<span class="comment">// suffices for the sake of this example.</span>
<span class="kw">while</span> <span class="op">!</span><span class="self">self</span>.<span class="ident">try_lock</span>() {}
}
<span class="kw">fn</span> <span class="ident">try_lock</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="ident">bool</span> {
<span class="self">self</span>.<span class="number">0</span>.<span class="ident">swap</span>(<span class="bool-val">true</span>, <span class="ident">Ordering</span>::<span class="ident">Acquire</span>)
}
<span class="kw">fn</span> <span class="ident">unlock</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) {
<span class="self">self</span>.<span class="number">0</span>.<span class="ident">store</span>(<span class="bool-val">false</span>, <span class="ident">Ordering</span>::<span class="ident">Release</span>);
}
}
<span class="comment">// 3. Export the wrappers. This are the types that your users will actually use.</span>
<span class="kw">pub</span> <span class="kw">type</span> <span class="ident">Spinlock</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">lock_api</span>::<span class="ident">Mutex</span><span class="op">&lt;</span><span class="ident">RawSpinlock</span>, <span class="ident">T</span><span class="op">&gt;</span>;
<span class="kw">pub</span> <span class="kw">type</span> <span class="ident">SpinlockGuard</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span>, <span class="ident">T</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">lock_api</span>::<span class="ident">MutexGuard</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span>, <span class="ident">RawSpinlock</span>, <span class="ident">T</span><span class="op">&gt;</span>;</pre>
<h1 id="extension-traits" class="section-header"><a href="#extension-traits">Extension traits</a></h1>
<p>In addition to basic locking &amp; unlocking functionality, you have the option
of exposing additional functionality in your lock types by implementing
additional traits for it. Examples of extension features include:</p>
<ul>
<li>Fair unlocking (<code>RawMutexFair</code>, <code>RawRwLockFair</code>)</li>
<li>Lock timeouts (<code>RawMutexTimed</code>, <code>RawRwLockTimed</code>)</li>
<li>Downgradable write locks (<code>RawRwLockDowngradable</code>)</li>
<li>Recursive read locks (<code>RawRwLockRecursive</code>)</li>
<li>Upgradable read locks (<code>RawRwLockUpgrade</code>)</li>
</ul>
<p>The <code>Mutex</code> and <code>RwLock</code> wrappers will automatically expose this additional
functionality if the raw lock type implements these extension traits.</p>
<h1 id="cargo-features" class="section-header"><a href="#cargo-features">Cargo features</a></h1>
<p>This crate supports two cargo features:</p>
<ul>
<li><code>owning_ref</code>: Allows your lock types to be used with the <code>owning_ref</code> crate.</li>
<li><code>nightly</code>: Enables nightly-only features. At the moment the only such
feature is <code>const fn</code> constructors for lock types.</li>
</ul>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.GuardNoSend.html"
title='struct lock_api::GuardNoSend'>GuardNoSend</a></td>
<td class='docblock-short'>
<p>Marker type which indicates that the Guard type for a lock is not <code>Send</code>.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.GuardSend.html"
title='struct lock_api::GuardSend'>GuardSend</a></td>
<td class='docblock-short'>
<p>Marker type which indicates that the Guard type for a lock is <code>Send</code>.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.MappedMutexGuard.html"
title='struct lock_api::MappedMutexGuard'>MappedMutexGuard</a></td>
<td class='docblock-short'>
<p>An RAII mutex guard returned by <code>MutexGuard::map</code>, which can point to a
subfield of the protected data.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.MappedReentrantMutexGuard.html"
title='struct lock_api::MappedReentrantMutexGuard'>MappedReentrantMutexGuard</a></td>
<td class='docblock-short'>
<p>An RAII mutex guard returned by <code>ReentrantMutexGuard::map</code>, which can point to a
subfield of the protected data.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.MappedRwLockReadGuard.html"
title='struct lock_api::MappedRwLockReadGuard'>MappedRwLockReadGuard</a></td>
<td class='docblock-short'>
<p>An RAII read lock guard returned by <code>RwLockReadGuard::map</code>, which can point to a
subfield of the protected data.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.MappedRwLockWriteGuard.html"
title='struct lock_api::MappedRwLockWriteGuard'>MappedRwLockWriteGuard</a></td>
<td class='docblock-short'>
<p>An RAII write lock guard returned by <code>RwLockWriteGuard::map</code>, which can point to a
subfield of the protected data.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Mutex.html"
title='struct lock_api::Mutex'>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.MutexGuard.html"
title='struct lock_api::MutexGuard'>MutexGuard</a></td>
<td class='docblock-short'>
<p>An RAII implementation of a &quot;scoped lock&quot; of a mutex. When this structure is
dropped (falls out of scope), the lock will be unlocked.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.ReentrantMutex.html"
title='struct lock_api::ReentrantMutex'>ReentrantMutex</a></td>
<td class='docblock-short'>
<p>A mutex which can be recursively locked by a single thread.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.ReentrantMutexGuard.html"
title='struct lock_api::ReentrantMutexGuard'>ReentrantMutexGuard</a></td>
<td class='docblock-short'>
<p>An RAII implementation of a &quot;scoped lock&quot; of a reentrant mutex. When this structure
is dropped (falls out of scope), the lock will be unlocked.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.RwLock.html"
title='struct lock_api::RwLock'>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='struct lock_api::RwLockReadGuard'>RwLockReadGuard</a></td>
<td class='docblock-short'>
<p>RAII structure used to release the shared read access of a lock when
dropped.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.RwLockUpgradableReadGuard.html"
title='struct lock_api::RwLockUpgradableReadGuard'>RwLockUpgradableReadGuard</a></td>
<td class='docblock-short'>
<p>RAII structure used to release the upgradable read access of a lock when
dropped.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.RwLockWriteGuard.html"
title='struct lock_api::RwLockWriteGuard'>RwLockWriteGuard</a></td>
<td class='docblock-short'>
<p>RAII structure used to release the exclusive write access of a lock when
dropped.</p>
</td>
</tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table>
<tr class=' module-item'>
<td><a class="trait" href="trait.GetThreadId.html"
title='trait lock_api::GetThreadId'>GetThreadId</a></td>
<td class='docblock-short'>
<p>Helper trait which returns a non-zero thread ID.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawMutex.html"
title='trait lock_api::RawMutex'>RawMutex</a></td>
<td class='docblock-short'>
<p>Basic operations for a mutex.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawMutexFair.html"
title='trait lock_api::RawMutexFair'>RawMutexFair</a></td>
<td class='docblock-short'>
<p>Additional methods for mutexes which support fair unlocking.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawMutexTimed.html"
title='trait lock_api::RawMutexTimed'>RawMutexTimed</a></td>
<td class='docblock-short'>
<p>Additional methods for mutexes which support locking with timeouts.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLock.html"
title='trait lock_api::RawRwLock'>RawRwLock</a></td>
<td class='docblock-short'>
<p>Basic operations for a reader-writer lock.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockDowngrade.html"
title='trait lock_api::RawRwLockDowngrade'>RawRwLockDowngrade</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support atomically downgrading an
exclusive lock to a shared lock.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockFair.html"
title='trait lock_api::RawRwLockFair'>RawRwLockFair</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support fair unlocking.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockRecursive.html"
title='trait lock_api::RawRwLockRecursive'>RawRwLockRecursive</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support recursive read locks.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockRecursiveTimed.html"
title='trait lock_api::RawRwLockRecursiveTimed'>RawRwLockRecursiveTimed</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support recursive read locks and timeouts.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockTimed.html"
title='trait lock_api::RawRwLockTimed'>RawRwLockTimed</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support locking with timeouts.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockUpgrade.html"
title='trait lock_api::RawRwLockUpgrade'>RawRwLockUpgrade</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support atomically upgrading a shared
lock to an exclusive lock.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockUpgradeDowngrade.html"
title='trait lock_api::RawRwLockUpgradeDowngrade'>RawRwLockUpgradeDowngrade</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support upgradable locks and lock
downgrading.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockUpgradeFair.html"
title='trait lock_api::RawRwLockUpgradeFair'>RawRwLockUpgradeFair</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support upgradable locks and fair
unlocking.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="trait" href="trait.RawRwLockUpgradeTimed.html"
title='trait lock_api::RawRwLockUpgradeTimed'>RawRwLockUpgradeTimed</a></td>
<td class='docblock-short'>
<p>Additional methods for RwLocks which support upgradable locks and locking
with timeouts.</p>
</td>
</tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd></kbd></dt><dd>Move up in search results</dd><dt><kbd></kbd></dt><dd>Move down in search results</dd><dt><kbd></kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "lock_api";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html>