Changed sync monitor internal interface from base hook to member hook
[libcds.git] / cds / sync / injecting_monitor.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_SYNC_INJECTING_MONITOR_H
4 #define CDSLIB_SYNC_INJECTING_MONITOR_H
5
6 #include <cds/sync/monitor.h>
7 #ifndef CDS_CXX11_INHERITING_CTOR
8 #   include <utility> // std::forward
9 #endif
10
11 namespace cds { namespace sync {
12
13     /// @ref cds_sync_monitor "Monitor" that injects the lock into each node
14     /**
15         This simple monitor injects the lock object of type \p Lock into each node. 
16         The monitor is designed for user-space locking primitives like \ref sync::spin_lock "spin-lock".
17
18         Template arguments:
19         - Lock - lock type like \p std::mutex or \p cds::sync::spin
20     */
21     template <typename Lock>
22     class injecting_monitor
23     {
24     public:
25         typedef Lock lock_type; ///< Lock type
26
27         /// Node injection
28         struct node_injection {
29             mutable lock_type m_Lock;   ///< Node spin-lock
30         };
31
32         /// Makes exclusive access to node \p p
33         template <typename Node>
34         void lock( Node const& p ) const
35         {
36             p.m_SyncMonitorInjection.m_Lock.lock();
37         }
38
39         /// Unlocks the node \p p
40         template <typename Node>
41         void unlock( Node const& p ) const
42         {
43             p.m_SyncMonitorInjection.m_Lock.unlock();
44         }
45
46         /// Scoped lock
47         template <typename Node>
48         using scoped_lock = monitor_scoped_lock< injecting_monitor, Node > ;
49     };
50 }} // namespace cds::sync
51
52 #endif // #ifndef CDSLIB_SYNC_INJECTING_MONITOR_H