Added more measures to sync monitor statistics
[libcds.git] / cds / sync / monitor.h
1 //$$CDS-header$$
2 #ifndef CDSLIB_SYNC_MONITOR_H
3 #define CDSLIB_SYNC_MONITOR_H
4 #include <cds/details/defs.h>
5 namespace cds { namespace sync {
6     /**
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.
19         <b>Implemetations</b>
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.
28         <b>How to use</b>
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
34         interface:
35         \code
36         class Monitor {
37         public:
38             // Monitor's injection into the Node class
39             template <typename Node>
40             struct node_injection: public Node
41             {
42                 // Monitor data to inject into container's node
43                 // ...
44             };
45             // Locks the node
46             template <typename Node>
47             void lock( Node& node );
48             // Unlocks the 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 >;
54         };
55         \endcode
56         Monitor's data must be inject into container's node as \p m_SyncMonitorInjection data member:
57         \code
58         template <typename SyncMonitor>
59         struct my_node
60         {
61             // ...
62             typename SyncMonitor::node_injection m_SyncMonitorInjection;
63         };
64         \endcode
65         The monitor must be a member of your container:
66         \code
67         template <typename GC, typename T, typename Traits>
68         class my_container {
69             // ...
70             typedef typename Traits::sync_monitor   sync_monitor;
71             typedef my_node<sync_monitor> node_type;
72             sync_monitor m_Monitor;
73             //...
74         };
75         \endcode
76     */
77     /// Monitor scoped lock (RAII)
78     /**
79         Template arguments:
80         - \p Monitor - monitor type
81         - \p Node - node type
82     */
83     template <typename Monitor, typename Node>
84     struct monitor_scoped_lock
85     {
86     public:
87         typedef Monitor monitor_type;   ///< Monitor type
88         typedef Node    node_type;      ///< Node type
89     private:
90         //@cond
91         monitor_type&    m_Monitor; ///< Monitor
92         node_type const& m_Node;    ///< Our locked node
93         //@endcond
94     public:
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 )
98             , m_Node( p )
99         {
100             monitor.lock( p );
101         }
102         /// Unlocks the node
103         ~monitor_scoped_lock()
104         {
105             m_Monitor.unlock( m_Node );
106         }
107     };
108 }} // namespace cds::sync
109 #endif // #ifndef CDSLIB_SYNC_MONITOR_H
110