]> git.proxmox.com Git - rustc.git/blob - vendor/crossbeam-epoch-0.8.2/src/lib.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / vendor / crossbeam-epoch-0.8.2 / src / lib.rs
1 //! Epoch-based memory reclamation.
2 //!
3 //! An interesting problem concurrent collections deal with comes from the remove operation.
4 //! Suppose that a thread removes an element from a lock-free map, while another thread is reading
5 //! that same element at the same time. The first thread must wait until the second thread stops
6 //! reading the element. Only then it is safe to destruct it.
7 //!
8 //! Programming languages that come with garbage collectors solve this problem trivially. The
9 //! garbage collector will destruct the removed element when no thread can hold a reference to it
10 //! anymore.
11 //!
12 //! This crate implements a basic memory reclamation mechanism, which is based on epochs. When an
13 //! element gets removed from a concurrent collection, it is inserted into a pile of garbage and
14 //! marked with the current epoch. Every time a thread accesses a collection, it checks the current
15 //! epoch, attempts to increment it, and destructs some garbage that became so old that no thread
16 //! can be referencing it anymore.
17 //!
18 //! That is the general mechanism behind epoch-based memory reclamation, but the details are a bit
19 //! more complicated. Anyhow, memory reclamation is designed to be fully automatic and something
20 //! users of concurrent collections don't have to worry much about.
21 //!
22 //! # Pointers
23 //!
24 //! Concurrent collections are built using atomic pointers. This module provides [`Atomic`], which
25 //! is just a shared atomic pointer to a heap-allocated object. Loading an [`Atomic`] yields a
26 //! [`Shared`], which is an epoch-protected pointer through which the loaded object can be safely
27 //! read.
28 //!
29 //! # Pinning
30 //!
31 //! Before an [`Atomic`] can be loaded, a participant must be [`pin`]ned. By pinning a participant
32 //! we declare that any object that gets removed from now on must not be destructed just
33 //! yet. Garbage collection of newly removed objects is suspended until the participant gets
34 //! unpinned.
35 //!
36 //! # Garbage
37 //!
38 //! Objects that get removed from concurrent collections must be stashed away until all currently
39 //! pinned participants get unpinned. Such objects can be stored into a thread-local or global
40 //! storage, where they are kept until the right time for their destruction comes.
41 //!
42 //! There is a global shared instance of garbage queue. You can [`defer`] the execution of an
43 //! arbitrary function until the global epoch is advanced enough. Most notably, concurrent data
44 //! structures may defer the deallocation of an object.
45 //!
46 //! # APIs
47 //!
48 //! For majority of use cases, just use the default garbage collector by invoking [`pin`]. If you
49 //! want to create your own garbage collector, use the [`Collector`] API.
50 //!
51 //! [`Atomic`]: struct.Atomic.html
52 //! [`Collector`]: struct.Collector.html
53 //! [`Shared`]: struct.Shared.html
54 //! [`pin`]: fn.pin.html
55 //! [`defer`]: struct.Guard.html#method.defer
56
57 #![warn(missing_docs)]
58 #![warn(missing_debug_implementations)]
59 #![cfg_attr(not(feature = "std"), no_std)]
60 #![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))]
61
62 #[macro_use]
63 extern crate cfg_if;
64 #[cfg(feature = "std")]
65 extern crate core;
66
67 extern crate maybe_uninit;
68
69 cfg_if! {
70 if #[cfg(feature = "alloc")] {
71 extern crate alloc;
72 } else if #[cfg(feature = "std")] {
73 extern crate std as alloc;
74 }
75 }
76
77 #[cfg_attr(feature = "nightly", cfg(target_has_atomic = "ptr"))]
78 cfg_if! {
79 if #[cfg(any(feature = "alloc", feature = "std"))] {
80 extern crate crossbeam_utils;
81 #[macro_use]
82 extern crate memoffset;
83 #[macro_use]
84 extern crate scopeguard;
85
86 mod atomic;
87 mod collector;
88 mod deferred;
89 mod epoch;
90 mod guard;
91 mod internal;
92 mod sync;
93
94 pub use self::atomic::{Atomic, CompareAndSetError, CompareAndSetOrdering, Owned, Pointer, Shared};
95 pub use self::collector::{Collector, LocalHandle};
96 pub use self::guard::{unprotected, Guard};
97 }
98 }
99
100 cfg_if! {
101 if #[cfg(feature = "std")] {
102 #[macro_use]
103 extern crate lazy_static;
104
105 mod default;
106 pub use self::default::{default_collector, is_pinned, pin};
107 }
108 }