|
|
|
|
<!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><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="../favicon.ico"><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='../lock_api/index.html'><div class='logo-container'><img src='../rust-logo.png' alt='logo'></div></a><p class='location'>Crate lock_api</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all lock_api's items</p></a><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"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><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/lock_api/lib.rs.html#8-109' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>lock_api</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 straightforward:</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>
|
|
|
|
|
<div class="example-wrap"><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">&</span><span class="self">self</span>) {
|
|
|
|
|
<span class="comment">// Note: This isn'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">&</span><span class="self">self</span>) <span class="op">-</span><span class="op">></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">&</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"><</span><span class="ident">T</span><span class="op">></span> <span class="op">=</span> <span class="ident">lock_api</span>::<span class="ident">Mutex</span><span class="op"><</span><span class="ident">RawSpinlock</span>, <span class="ident">T</span><span class="op">></span>;
|
|
|
|
|
<span class="kw">pub</span> <span class="kw">type</span> <span class="ident">SpinlockGuard</span><span class="op"><</span><span class="lifetime">'a</span>, <span class="ident">T</span><span class="op">></span> <span class="op">=</span> <span class="ident">lock_api</span>::<span class="ident">MutexGuard</span><span class="op"><</span><span class="lifetime">'a</span>, <span class="ident">RawSpinlock</span>, <span class="ident">T</span><span class="op">></span>;</pre></div>
|
|
|
|
|
<h1 id="extension-traits" class="section-header"><a href="#extension-traits">Extension traits</a></h1>
|
|
|
|
|
<p>In addition to basic locking & 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='lock_api::GuardNoSend struct'>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='lock_api::GuardSend struct'>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='lock_api::MappedMutexGuard struct'>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='lock_api::MappedReentrantMutexGuard struct'>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='lock_api::MappedRwLockReadGuard struct'>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='lock_api::MappedRwLockWriteGuard struct'>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='lock_api::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.MutexGuard.html" title='lock_api::MutexGuard struct'>MutexGuard</a></td><td class='docblock-short'><p>An RAII implementation of a "scoped lock" 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='lock_api::ReentrantMutex struct'>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='lock_api::ReentrantMutexGuard struct'>ReentrantMutexGuard</a></td><td class='docblock-short'><p>An RAII implementation of a "scoped lock" 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='lock_api::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='lock_api::RwLockReadGuard struct'>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='lock_api::RwLockUpgradableReadGuard struct'>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='lock_api::RwLockWriteGuard struct'>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='lock_api::GetThreadId trait'>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='lock_api::RawMutex trait'>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='lock_api::RawMutexFair trait'>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='lock_api::RawMutexTimed trait'>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='lock_api::RawRwLock trait'>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='lock_api::RawRwLockDowngrade trait'>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='lock_api::RawRwLockFair trait'>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='lock_api::RawRwLockRecursive trait'>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='lock_api::RawRwLockRecursiveTimed trait'>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='lock_api::RawRwLockTimed trait'>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='lock_api::RawRwLockUpgrade trait'>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='lock_api::RawRwLockUpgradeDowngrade trait'>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='lock_api::RawRwLockUpgradeFair trait'>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='lock_api::RawRwLockUpgradeTimed trait'>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>⏎</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>
|