blob: b626aa5ca2e0b19703e79006c886897ac8b274ac [file] [log] [blame]
<!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="Epoch-based memory reclamation."><meta name="keywords" content="rust, rustlang, rust-lang, crossbeam_epoch"><title>crossbeam_epoch - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[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">&#9776;</button><a class="sidebar-logo" href="../crossbeam_epoch/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../crossbeam_epoch/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate crossbeam_epoch</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.9.14</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li><li><a href="#types">Type Definitions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">crossbeam_epoch</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/crossbeam_epoch/lib.rs.html#1-169">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Epoch-based memory reclamation.</p>
<p>An interesting problem concurrent collections deal with comes from the remove operation.
Suppose that a thread removes an element from a lock-free map, while another thread is reading
that same element at the same time. The first thread must wait until the second thread stops
reading the element. Only then it is safe to destruct it.</p>
<p>Programming languages that come with garbage collectors solve this problem trivially. The
garbage collector will destruct the removed element when no thread can hold a reference to it
anymore.</p>
<p>This crate implements a basic memory reclamation mechanism, which is based on epochs. When an
element gets removed from a concurrent collection, it is inserted into a pile of garbage and
marked with the current epoch. Every time a thread accesses a collection, it checks the current
epoch, attempts to increment it, and destructs some garbage that became so old that no thread
can be referencing it anymore.</p>
<p>That is the general mechanism behind epoch-based memory reclamation, but the details are a bit
more complicated. Anyhow, memory reclamation is designed to be fully automatic and something
users of concurrent collections don’t have to worry much about.</p>
<h2 id="pointers"><a href="#pointers">Pointers</a></h2>
<p>Concurrent collections are built using atomic pointers. This module provides <a href="struct.Atomic.html" title="Atomic"><code>Atomic</code></a>, which
is just a shared atomic pointer to a heap-allocated object. Loading an <a href="struct.Atomic.html" title="Atomic"><code>Atomic</code></a> yields a
<a href="struct.Shared.html" title="Shared"><code>Shared</code></a>, which is an epoch-protected pointer through which the loaded object can be safely
read.</p>
<h2 id="pinning"><a href="#pinning">Pinning</a></h2>
<p>Before an <a href="struct.Atomic.html" title="Atomic"><code>Atomic</code></a> can be loaded, a participant must be <a href="fn.pin.html" title="pin"><code>pin</code></a>ned. By pinning a participant
we declare that any object that gets removed from now on must not be destructed just
yet. Garbage collection of newly removed objects is suspended until the participant gets
unpinned.</p>
<h2 id="garbage"><a href="#garbage">Garbage</a></h2>
<p>Objects that get removed from concurrent collections must be stashed away until all currently
pinned participants get unpinned. Such objects can be stored into a thread-local or global
storage, where they are kept until the right time for their destruction comes.</p>
<p>There is a global shared instance of garbage queue. You can <a href="struct.Guard.html#method.defer"><code>defer</code></a> the execution of an
arbitrary function until the global epoch is advanced enough. Most notably, concurrent data
structures may defer the deallocation of an object.</p>
<h2 id="apis"><a href="#apis">APIs</a></h2>
<p>For majority of use cases, just use the default garbage collector by invoking <a href="fn.pin.html" title="pin"><code>pin</code></a>. If you
want to create your own garbage collector, use the <a href="struct.Collector.html" title="Collector"><code>Collector</code></a> API.</p>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Atomic.html" title="crossbeam_epoch::Atomic struct">Atomic</a></div><div class="item-right docblock-short">An atomic pointer that can be safely shared between threads.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Collector.html" title="crossbeam_epoch::Collector struct">Collector</a></div><div class="item-right docblock-short">An epoch-based garbage collector.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.CompareExchangeError.html" title="crossbeam_epoch::CompareExchangeError struct">CompareExchangeError</a></div><div class="item-right docblock-short">The error returned on failed compare-and-swap operation.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Guard.html" title="crossbeam_epoch::Guard struct">Guard</a></div><div class="item-right docblock-short">A guard that keeps the current thread pinned.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.LocalHandle.html" title="crossbeam_epoch::LocalHandle struct">LocalHandle</a></div><div class="item-right docblock-short">A handle to a garbage collector.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Owned.html" title="crossbeam_epoch::Owned struct">Owned</a></div><div class="item-right docblock-short">An owned heap-allocated object.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Shared.html" title="crossbeam_epoch::Shared struct">Shared</a></div><div class="item-right docblock-short">A pointer to an object protected by the epoch GC.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.CompareAndSetOrdering.html" title="crossbeam_epoch::CompareAndSetOrdering trait">CompareAndSetOrdering</a><span class="stab deprecated" title="">Deprecated</span></div><div class="item-right docblock-short">Memory orderings for compare-and-set operations.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Pointable.html" title="crossbeam_epoch::Pointable trait">Pointable</a></div><div class="item-right docblock-short">Types that are pointed to by a single word.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Pointer.html" title="crossbeam_epoch::Pointer trait">Pointer</a></div><div class="item-right docblock-short">A trait for either <code>Owned</code> or <code>Shared</code> pointers.</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.default_collector.html" title="crossbeam_epoch::default_collector fn">default_collector</a></div><div class="item-right docblock-short">Returns the default global collector.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.is_pinned.html" title="crossbeam_epoch::is_pinned fn">is_pinned</a></div><div class="item-right docblock-short">Returns <code>true</code> if the current thread is pinned.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.pin.html" title="crossbeam_epoch::pin fn">pin</a></div><div class="item-right docblock-short">Pins the current thread.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.unprotected.html" title="crossbeam_epoch::unprotected fn">unprotected</a><sup title="unsafe function"></sup></div><div class="item-right docblock-short">Returns a reference to a dummy guard that allows unprotected access to <a href="struct.Atomic.html"><code>Atomic</code></a>s.</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.CompareAndSetError.html" title="crossbeam_epoch::CompareAndSetError type">CompareAndSetError</a><span class="stab deprecated" title="">Deprecated</span></div><div class="item-right docblock-short">The error returned on failed compare-and-set operation.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="crossbeam_epoch" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>