2 #ifndef CDSLIB_SYNC_MONITOR_H
3 #define CDSLIB_SYNC_MONITOR_H
4 #include <cds/details/defs.h>
5 namespace cds { namespace sync {
7 @page cds_sync_monitor Synchronization monitor
8 A <a href="http://en.wikipedia.org/wiki/Monitor_%28synchronization%29">monitor</a> is synchronization construct
9 that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true.
10 Some blocking data structure algoritms like the trees require per-node locking.
11 For huge trees containing millions of nodes it can be very inefficient to inject
12 the lock object into each node. Moreover, some operating systems may not support
13 the millions of system objects like mutexes per user process.
14 The monitor strategy is intended to solve that problem.
15 When the node should be locked, the monitor is called to allocate appropriate
16 lock object for the node if needed, and to lock the node.
17 The monitor strategy can significantly reduce the number of system objects
18 required for data structure.
20 \p libcds contains several monitor implementations:
21 - \p sync::injecting_monitor injects the lock object into each node.
22 That mock monitor is designed for user-space locking primitive like
23 \ref sync::spin_lock "spin-lock".
24 - \p sync::pool_monitor is the monitor that allocates a lock object
25 for a node from the pool when needed. When the node is unlocked
26 the lock assigned to it is given back to the pool if no thread
27 references to that node.
29 If you use a container from \p libcds that requires a monitor, you should just
30 specify required monitor type in container's traits. Usually, the monitor
31 is specified by \p traits::sync_monitor typedef, or by \p cds::opt::sync_monitor
32 option for container's \p make_traits metafunction.
33 If you're developing a new container algorithm, you should know internal monitor
38 // Monitor's injection into the Node class
39 template <typename Node>
40 struct node_injection: public Node
42 // Monitor data to inject into container's node
46 template <typename Node>
47 void lock( Node& node );
49 template <typename Node>
50 void unlock( Node& node );
51 // Scoped lock applyes RAII to Monitor
52 template <typename Node>
53 using scoped_lock = monitor_scoped_lock< pool_monitor, Node >;
56 Monitor's data must be inject into container's node as \p m_SyncMonitorInjection data member:
58 template <typename SyncMonitor>
62 typename SyncMonitor::node_injection m_SyncMonitorInjection;
65 The monitor must be a member of your container:
67 template <typename GC, typename T, typename Traits>
70 typedef typename Traits::sync_monitor sync_monitor;
71 typedef my_node<sync_monitor> node_type;
72 sync_monitor m_Monitor;
77 /// Monitor scoped lock (RAII)
80 - \p Monitor - monitor type
83 template <typename Monitor, typename Node>
84 struct monitor_scoped_lock
87 typedef Monitor monitor_type; ///< Monitor type
88 typedef Node node_type; ///< Node type
91 monitor_type& m_Monitor; ///< Monitor
92 node_type const& m_Node; ///< Our locked node
95 /// Makes exclusive access to the node \p p by \p monitor
96 monitor_scoped_lock( monitor_type& monitor, node_type const& p )
97 : m_Monitor( monitor )
103 ~monitor_scoped_lock()
105 m_Monitor.unlock( m_Node );
108 }} // namespace cds::sync
109 #endif // #ifndef CDSLIB_SYNC_MONITOR_H