896e5a9b4c9705c8372bf657b511cfc5a1cffeac
[libcds.git] / cds / sync / monitor.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_SYNC_MONITOR_H
4 #define CDSLIB_SYNC_MONITOR_H
5
6 #include <cds/details/defs.h>
7
8 namespace cds { namespace sync {
9
10     /**
11         @page cds_sync_monitor Synchronization monitor
12
13         A <a href="http://en.wikipedia.org/wiki/Monitor_%28synchronization%29">monitor</a> is synchronization construct
14         that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true.
15
16         Some blocking data structure algoritms like the trees require per-node locking.
17         For huge trees containing millions of nodes it can be very inefficient to inject
18         the lock object into each node. Moreover, some operating systems may not support
19         the millions of system objects like mutexes per user process.
20
21         The monitor strategy is intended to solve that problem.
22         When the node should be locked, the monitor is called to allocate appropriate
23         lock object for the node if it's needed, and to lock the node.
24         The monitor strategy can significantly reduce the number of system objects
25         required for data structure.
26
27         <b>Implemetations</b>
28
29         \p libcds contains several monitor implementations:
30         - \p sync::injecting_monitor injects the lock object into each node.
31             That mock monitor is designed for user-space locking primitive like
32             \ref sync::spin_lock "spin-lock".
33         - \p sync::pool_monitor is the monitor that allocates the lock object
34             for the node from the pool when needed. When the node is unlocked
35             the lock assigned to it is given back to the pool if no thread
36             references to that node.
37
38         <b>How to use</b>
39
40         If you use a container from \p libcds that requires a monitor, you should just
41         specify required monitor type in container's traits. Usually, the monitor 
42         is specified by \p traits::sync_monitor typedef, or by \p cds::opt::sync_monitor
43         option for container's \p make_traits metafunction.
44
45         If you're developing a new container algorithm, you should know internal monitor 
46         interface:
47         \code
48         class Monitor {
49         public:
50             // Monitor's injection into the Node class
51             template <typename Node>
52             struct node_injection: public Node
53             {
54                 // Monitor data to inject into container's node
55                 // ... 
56             };
57
58             // Locks the node 
59             template <typename Node>
60             void lock( Node& node );
61
62             // Unlocks the node
63             template <typename Node>
64             void unlock( Node& node );
65
66             // Scoped lock applyes RAII to Monitor
67             template <typename Node>
68             using scoped_lock = monitor_scoped_lock< pool_monitor, Node >;
69         };
70         \endcode
71         The monitor should be a member of your container:
72         \code
73         template <typename GC, typename T, typename Traits>
74         class my_container {
75             // ...
76             typedef typename Traits::sync_monitor   sync_monitor;
77             sync_monitor m_Monitor;
78             //...
79         };
80         \endcode
81     */
82
83     /// Monitor scoped lock (RAII)
84     /**
85         Template arguments:
86         - \p Monitor - monitor type
87         - \p Node - node type
88     */
89     template <typename Monitor, typename Node>
90     struct monitor_scoped_lock
91     {
92     public:
93         typedef Monitor monitor_type;   ///< Monitor type
94         typedef Node    node_type;      ///< Node type
95
96     private:
97         //@cond
98         monitor_type&    m_Monitor; ///< Monitor
99         node_type const& m_Node;    ///< Our locked node
100         //@endcond
101
102     public:
103         /// Makes exclusive access to the node \p p by \p monitor
104         monitor_scoped_lock( monitor_type& monitor, node_type const& p )
105             : m_Monitor( monitor )
106             , m_Node( p )
107         {
108             monitor.lock( p );
109         }
110
111         /// Unlocks the node
112         ~monitor_scoped_lock()
113         {
114             m_Monitor.unlock( m_Node );
115         }
116     };
117
118 }} // namespace cds::sync
119
120 #endif // #ifndef CDSLIB_SYNC_MONITOR_H
121