mirror of
https://github.com/NotAShelf/mpvrc.git
synced 2026-04-16 16:03:48 +00:00
67 lines
No EOL
8 KiB
HTML
67 lines
No EOL
8 KiB
HTML
<!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="A mutual exclusion primitive useful for protecting shared data"><title>Mutex in parking_lot - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="parking_lot" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc type"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../parking_lot/index.html">parking_<wbr>lot</a><span class="version">0.12.3</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Mutex</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#fairness" title="Fairness">Fairness</a></li><li><a href="#differences-from-the-standard-library-mutex" title="Differences from the standard library `Mutex`">Differences from the standard library <code>Mutex</code></a></li><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#aliased-type">Aliased type</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate parking_<wbr>lot</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">parking_lot</a></span><h1>Type Alias <span class="type">Mutex</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/parking_lot/mutex.rs.html#86">Source</a> </span></div><pre class="rust item-decl"><code>pub type Mutex<T> = <a class="struct" href="../lock_api/mutex/struct.Mutex.html" title="struct lock_api::mutex::Mutex">Mutex</a><<a class="struct" href="struct.RawMutex.html" title="struct parking_lot::RawMutex">RawMutex</a>, T>;</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A mutual exclusion primitive useful for protecting shared data</p>
|
||
<p>This mutex will block threads waiting for the lock to become available. The
|
||
mutex can be statically initialized or created by the <code>new</code>
|
||
constructor. Each mutex has a type parameter which represents the data that
|
||
it is protecting. The data can only be accessed through the RAII guards
|
||
returned from <code>lock</code> and <code>try_lock</code>, which guarantees that the data is only
|
||
ever accessed when the mutex is locked.</p>
|
||
<h2 id="fairness"><a class="doc-anchor" href="#fairness">§</a>Fairness</h2>
|
||
<p>A typical unfair lock can often end up in a situation where a single thread
|
||
quickly acquires and releases the same mutex in succession, which can starve
|
||
other threads waiting to acquire the mutex. While this improves throughput
|
||
because it doesn’t force a context switch when a thread tries to re-acquire
|
||
a mutex it has just released, this can starve other threads.</p>
|
||
<p>This mutex uses <a href="https://trac.webkit.org/changeset/203350">eventual fairness</a>
|
||
to ensure that the lock will be fair on average without sacrificing
|
||
throughput. This is done by forcing a fair unlock on average every 0.5ms,
|
||
which will force the lock to go to the next thread waiting for the mutex.</p>
|
||
<p>Additionally, any critical section longer than 1ms will always use a fair
|
||
unlock, which has a negligible impact on throughput considering the length
|
||
of the critical section.</p>
|
||
<p>You can also force a fair unlock by calling <code>MutexGuard::unlock_fair</code> when
|
||
unlocking a mutex instead of simply dropping the <code>MutexGuard</code>.</p>
|
||
<h2 id="differences-from-the-standard-library-mutex"><a class="doc-anchor" href="#differences-from-the-standard-library-mutex">§</a>Differences from the standard library <code>Mutex</code></h2>
|
||
<ul>
|
||
<li>No poisoning, the lock is released normally on panic.</li>
|
||
<li>Only requires 1 byte of space, whereas the standard library boxes the
|
||
<code>Mutex</code> due to platform limitations.</li>
|
||
<li>Can be statically constructed.</li>
|
||
<li>Does not require any drop glue when dropped.</li>
|
||
<li>Inline fast path for the uncontended case.</li>
|
||
<li>Efficient handling of micro-contention using adaptive spinning.</li>
|
||
<li>Allows raw locking & unlocking without a guard.</li>
|
||
<li>Supports eventual fairness so that the mutex is fair on average.</li>
|
||
<li>Optionally allows making the mutex fair by calling <code>MutexGuard::unlock_fair</code>.</li>
|
||
</ul>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>parking_lot::Mutex;
|
||
<span class="kw">use </span>std::sync::{Arc, mpsc::channel};
|
||
<span class="kw">use </span>std::thread;
|
||
|
||
<span class="kw">const </span>N: usize = <span class="number">10</span>;
|
||
|
||
<span class="comment">// Spawn a few threads to increment a shared variable (non-atomically), and
|
||
// let the main thread know once all increments are done.
|
||
//
|
||
// Here we're using an Arc to share memory among threads, and the data inside
|
||
// the Arc is protected with a mutex.
|
||
</span><span class="kw">let </span>data = Arc::new(Mutex::new(<span class="number">0</span>));
|
||
|
||
<span class="kw">let </span>(tx, rx) = channel();
|
||
<span class="kw">for _ in </span><span class="number">0</span>..<span class="number">10 </span>{
|
||
<span class="kw">let </span>(data, tx) = (Arc::clone(<span class="kw-2">&</span>data), tx.clone());
|
||
thread::spawn(<span class="kw">move </span>|| {
|
||
<span class="comment">// The shared state can only be accessed once the lock is held.
|
||
// Our non-atomic increment is safe because we're the only thread
|
||
// which can access the shared state when the lock is held.
|
||
</span><span class="kw">let </span><span class="kw-2">mut </span>data = data.lock();
|
||
<span class="kw-2">*</span>data += <span class="number">1</span>;
|
||
<span class="kw">if </span><span class="kw-2">*</span>data == N {
|
||
tx.send(()).unwrap();
|
||
}
|
||
<span class="comment">// the lock is unlocked here when `data` goes out of scope.
|
||
</span>});
|
||
}
|
||
|
||
rx.recv().unwrap();</code></pre></div>
|
||
</div></details><h2 id="aliased-type" class="section-header">Aliased Type<a href="#aliased-type" class="anchor">§</a></h2><pre class="rust item-decl"><code>struct Mutex<T> { <span class="comment">/* private fields */</span> }</code></pre><script src="../type.impl/lock_api/mutex/struct.Mutex.js" data-self-path="parking_lot::mutex::Mutex" async></script></section></div></main></body></html> |