index.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="generator" content="rustdoc">
  7. <meta name="description" content="API documentation for the Rust `epoch` mod in crate `crossbeam`.">
  8. <meta name="keywords" content="rust, rustlang, rust-lang, epoch">
  9. <title>crossbeam::epoch - Rust</title>
  10. <link rel="stylesheet" type="text/css" href="../../normalize.css">
  11. <link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle">
  12. <link rel="stylesheet" type="text/css" href="../../dark.css">
  13. <link rel="stylesheet" type="text/css" href="../../main.css" id="themeStyle">
  14. <script src="../../storage.js"></script>
  15. </head>
  16. <body class="rustdoc mod">
  17. <!--[if lte IE 8]>
  18. <div class="warning">
  19. This old browser is unsupported and will most likely display funky
  20. things.
  21. </div>
  22. <![endif]-->
  23. <nav class="sidebar">
  24. <div class="sidebar-menu">&#9776;</div>
  25. <p class='location'>Module epoch</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'><a href='../index.html'>crossbeam</a></p><script>window.sidebarCurrent = {name: 'epoch', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div>
  26. </nav>
  27. <div class="theme-picker">
  28. <button id="theme-picker" aria-label="Pick another theme!">
  29. <img src="../../brush.svg" width="18" alt="Pick another theme!">
  30. </button>
  31. <div id="theme-choices"></div>
  32. </div>
  33. <script src="../../theme.js"></script>
  34. <nav class="sub">
  35. <form class="search-form js-only">
  36. <div class="search-container">
  37. <input class="search-input" name="search"
  38. autocomplete="off"
  39. placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
  40. type="search">
  41. </div>
  42. </form>
  43. </nav>
  44. <section id='main' class="content">
  45. <h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>crossbeam</a>::<wbr><a class="mod" href=''>epoch</a></span><span class='out-of-band'><span id='render-detail'>
  46. <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
  47. [<span class='inner'>&#x2212;</span>]
  48. </a>
  49. </span><a class='srclink' href='../../src/crossbeam/epoch/mod.rs.html#1-265' title='goto source code'>[src]</a></span></h1>
  50. <div class='docblock'><p>Epoch-based memory management</p>
  51. <p>This module provides fast, easy to use memory management for lock free data
  52. structures. It's inspired by <a href="https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-579.pdf">Keir Fraser's <em>epoch-based
  53. reclamation</em></a>.</p>
  54. <p>The basic problem this is solving is the fact that when one thread has
  55. removed a node from a data structure, other threads may still have pointers
  56. to that node (in the form of snapshots that will be validated through things
  57. like compare-and-swap), so the memory cannot be immediately freed. Put differently:</p>
  58. <ol>
  59. <li>
  60. <p>There are two sources of reachability at play -- the data structure, and
  61. the snapshots in threads accessing it. Before we delete a node, we need to know
  62. that it cannot be reached in either of these ways.</p>
  63. </li>
  64. <li>
  65. <p>Once a node has been unlinked from the data structure, no <em>new</em> snapshots
  66. reaching it will be created.</p>
  67. </li>
  68. </ol>
  69. <p>Using the epoch scheme is fairly straightforward, and does not require
  70. understanding any of the implementation details:</p>
  71. <ul>
  72. <li>
  73. <p>When operating on a shared data structure, a thread must &quot;pin the current
  74. epoch&quot;, which is done by calling <code>pin()</code>. This function returns a <code>Guard</code>
  75. which unpins the epoch when destroyed.</p>
  76. </li>
  77. <li>
  78. <p>When the thread subsequently reads from a lock-free data structure, the
  79. pointers it extracts act like references with lifetime tied to the
  80. <code>Guard</code>. This allows threads to safely read from snapshotted data, being
  81. guaranteed that the data will remain allocated until they exit the epoch.</p>
  82. </li>
  83. </ul>
  84. <p>To put the <code>Guard</code> to use, Crossbeam provides a set of three pointer types meant to work together:</p>
  85. <ul>
  86. <li>
  87. <p><code>Owned&lt;T&gt;</code>, akin to <code>Box&lt;T&gt;</code>, which points to uniquely-owned data that has
  88. not yet been published in a concurrent data structure.</p>
  89. </li>
  90. <li>
  91. <p><code>Shared&lt;'a, T&gt;</code>, akin to <code>&amp;'a T</code>, which points to shared data that may or may
  92. not be reachable from a data structure, but it guaranteed not to be freed
  93. during lifetime <code>'a</code>.</p>
  94. </li>
  95. <li>
  96. <p><code>Atomic&lt;T&gt;</code>, akin to <code>std::sync::atomic::AtomicPtr</code>, which provides atomic
  97. updates to a pointer using the <code>Owned</code> and <code>Shared</code> types, and connects them
  98. to a <code>Guard</code>.</p>
  99. </li>
  100. </ul>
  101. <p>Each of these types provides further documentation on usage.</p>
  102. <h1 id="example" class="section-header"><a href="#example">Example</a></h1>
  103. <pre class="rust rust-example-rendered">
  104. <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">atomic</span>::<span class="ident">Ordering</span>::{<span class="ident">Acquire</span>, <span class="ident">Release</span>, <span class="ident">Relaxed</span>};
  105. <span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ptr</span>;
  106. <span class="kw">use</span> <span class="ident">crossbeam</span>::<span class="ident">epoch</span>::{<span class="self">self</span>, <span class="ident">Atomic</span>, <span class="ident">Owned</span>};
  107. <span class="kw">struct</span> <span class="ident">TreiberStack</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
  108. <span class="ident">head</span>: <span class="ident">Atomic</span><span class="op">&lt;</span><span class="ident">Node</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;&gt;</span>,
  109. }
  110. <span class="kw">struct</span> <span class="ident">Node</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
  111. <span class="ident">data</span>: <span class="ident">T</span>,
  112. <span class="ident">next</span>: <span class="ident">Atomic</span><span class="op">&lt;</span><span class="ident">Node</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;&gt;</span>,
  113. }
  114. <span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">TreiberStack</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
  115. <span class="kw">fn</span> <span class="ident">new</span>() <span class="op">-&gt;</span> <span class="ident">TreiberStack</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
  116. <span class="ident">TreiberStack</span> {
  117. <span class="ident">head</span>: <span class="ident">Atomic</span>::<span class="ident">null</span>()
  118. }
  119. }
  120. <span class="kw">fn</span> <span class="ident">push</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">t</span>: <span class="ident">T</span>) {
  121. <span class="comment">// allocate the node via Owned</span>
  122. <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">n</span> <span class="op">=</span> <span class="ident">Owned</span>::<span class="ident">new</span>(<span class="ident">Node</span> {
  123. <span class="ident">data</span>: <span class="ident">t</span>,
  124. <span class="ident">next</span>: <span class="ident">Atomic</span>::<span class="ident">null</span>(),
  125. });
  126. <span class="comment">// become active</span>
  127. <span class="kw">let</span> <span class="ident">guard</span> <span class="op">=</span> <span class="ident">epoch</span>::<span class="ident">pin</span>();
  128. <span class="kw">loop</span> {
  129. <span class="comment">// snapshot current head</span>
  130. <span class="kw">let</span> <span class="ident">head</span> <span class="op">=</span> <span class="self">self</span>.<span class="ident">head</span>.<span class="ident">load</span>(<span class="ident">Relaxed</span>, <span class="kw-2">&amp;</span><span class="ident">guard</span>);
  131. <span class="comment">// update `next` pointer with snapshot</span>
  132. <span class="ident">n</span>.<span class="ident">next</span>.<span class="ident">store_shared</span>(<span class="ident">head</span>, <span class="ident">Relaxed</span>);
  133. <span class="comment">// if snapshot is still good, link in the new node</span>
  134. <span class="kw">match</span> <span class="self">self</span>.<span class="ident">head</span>.<span class="ident">cas_and_ref</span>(<span class="ident">head</span>, <span class="ident">n</span>, <span class="ident">Release</span>, <span class="kw-2">&amp;</span><span class="ident">guard</span>) {
  135. <span class="prelude-val">Ok</span>(_) <span class="op">=&gt;</span> <span class="kw">return</span>,
  136. <span class="prelude-val">Err</span>(<span class="ident">owned</span>) <span class="op">=&gt;</span> <span class="ident">n</span> <span class="op">=</span> <span class="ident">owned</span>,
  137. }
  138. }
  139. }
  140. <span class="kw">fn</span> <span class="ident">pop</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
  141. <span class="comment">// become active</span>
  142. <span class="kw">let</span> <span class="ident">guard</span> <span class="op">=</span> <span class="ident">epoch</span>::<span class="ident">pin</span>();
  143. <span class="kw">loop</span> {
  144. <span class="comment">// take a snapshot</span>
  145. <span class="kw">match</span> <span class="self">self</span>.<span class="ident">head</span>.<span class="ident">load</span>(<span class="ident">Acquire</span>, <span class="kw-2">&amp;</span><span class="ident">guard</span>) {
  146. <span class="comment">// the stack is non-empty</span>
  147. <span class="prelude-val">Some</span>(<span class="ident">head</span>) <span class="op">=&gt;</span> {
  148. <span class="comment">// read through the snapshot, *safely*!</span>
  149. <span class="kw">let</span> <span class="ident">next</span> <span class="op">=</span> <span class="ident">head</span>.<span class="ident">next</span>.<span class="ident">load</span>(<span class="ident">Relaxed</span>, <span class="kw-2">&amp;</span><span class="ident">guard</span>);
  150. <span class="comment">// if snapshot is still good, update from `head` to `next`</span>
  151. <span class="kw">if</span> <span class="self">self</span>.<span class="ident">head</span>.<span class="ident">cas_shared</span>(<span class="prelude-val">Some</span>(<span class="ident">head</span>), <span class="ident">next</span>, <span class="ident">Release</span>) {
  152. <span class="kw">unsafe</span> {
  153. <span class="comment">// mark the node as unlinked</span>
  154. <span class="ident">guard</span>.<span class="ident">unlinked</span>(<span class="ident">head</span>);
  155. <span class="comment">// extract out the data from the now-unlinked node</span>
  156. <span class="kw">return</span> <span class="prelude-val">Some</span>(<span class="ident">ptr</span>::<span class="ident">read</span>(<span class="kw-2">&amp;</span>(<span class="kw-2">*</span><span class="ident">head</span>).<span class="ident">data</span>))
  157. }
  158. }
  159. }
  160. <span class="comment">// we observed the stack empty</span>
  161. <span class="prelude-val">None</span> <span class="op">=&gt;</span> <span class="kw">return</span> <span class="prelude-val">None</span>
  162. }
  163. }
  164. }
  165. }</pre>
  166. </div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
  167. <table>
  168. <tr class=' module-item'>
  169. <td><a class="struct" href="struct.Atomic.html"
  170. title='struct crossbeam::epoch::Atomic'>Atomic</a></td>
  171. <td class='docblock-short'>
  172. <p>Like <code>std::sync::atomic::AtomicPtr</code>.</p>
  173. </td>
  174. </tr>
  175. <tr class=' module-item'>
  176. <td><a class="struct" href="struct.Guard.html"
  177. title='struct crossbeam::epoch::Guard'>Guard</a></td>
  178. <td class='docblock-short'>
  179. <p>An RAII-style guard for pinning the current epoch.</p>
  180. </td>
  181. </tr>
  182. <tr class=' module-item'>
  183. <td><a class="struct" href="struct.Owned.html"
  184. title='struct crossbeam::epoch::Owned'>Owned</a></td>
  185. <td class='docblock-short'>
  186. <p>Like <code>Box&lt;T&gt;</code>: an owned, heap-allocated data value of type <code>T</code>.</p>
  187. </td>
  188. </tr>
  189. <tr class=' module-item'>
  190. <td><a class="struct" href="struct.Shared.html"
  191. title='struct crossbeam::epoch::Shared'>Shared</a></td>
  192. <td class='docblock-short'>
  193. <p>Like <code>&amp;'a T</code>: a shared reference valid for lifetime <code>'a</code>.</p>
  194. </td>
  195. </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
  196. <table>
  197. <tr class=' module-item'>
  198. <td><a class="fn" href="fn.pin.html"
  199. title='fn crossbeam::epoch::pin'>pin</a></td>
  200. <td class='docblock-short'>
  201. <p>Pin the current epoch.</p>
  202. </td>
  203. </tr></table></section>
  204. <section id='search' class="content hidden"></section>
  205. <section class="footer"></section>
  206. <aside id="help" class="hidden">
  207. <div>
  208. <h1 class="hidden">Help</h1>
  209. <div class="shortcuts">
  210. <h2>Keyboard Shortcuts</h2>
  211. <dl>
  212. <dt><kbd>?</kbd></dt>
  213. <dd>Show this help dialog</dd>
  214. <dt><kbd>S</kbd></dt>
  215. <dd>Focus the search field</dd>
  216. <dt><kbd>↑</kbd></dt>
  217. <dd>Move up in search results</dd>
  218. <dt><kbd>↓</kbd></dt>
  219. <dd>Move down in search results</dd>
  220. <dt><kbd>↹</kbd></dt>
  221. <dd>Switch tab</dd>
  222. <dt><kbd>&#9166;</kbd></dt>
  223. <dd>Go to active search result</dd>
  224. <dt><kbd>+</kbd></dt>
  225. <dd>Expand all sections</dd>
  226. <dt><kbd>-</kbd></dt>
  227. <dd>Collapse all sections</dd>
  228. </dl>
  229. </div>
  230. <div class="infos">
  231. <h2>Search Tricks</h2>
  232. <p>
  233. Prefix searches with a type followed by a colon (e.g.
  234. <code>fn:</code>) to restrict the search to a given type.
  235. </p>
  236. <p>
  237. Accepted types are: <code>fn</code>, <code>mod</code>,
  238. <code>struct</code>, <code>enum</code>,
  239. <code>trait</code>, <code>type</code>, <code>macro</code>,
  240. and <code>const</code>.
  241. </p>
  242. <p>
  243. Search functions by type signature (e.g.
  244. <code>vec -> usize</code> or <code>* -> vec</code>)
  245. </p>
  246. </div>
  247. </div>
  248. </aside>
  249. <script>
  250. window.rootPath = "../../";
  251. window.currentCrate = "crossbeam";
  252. </script>
  253. <script src="../../main.js"></script>
  254. <script defer src="../../search-index.js"></script>
  255. </body>
  256. </html>