Merge branch 'dev' of github.com:khizmax/libcds into dev
[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     //@cond
14     struct injecting_monitor_traits {
15         struct empty_stat
16         {};
17     };
18     //@endcond
19
20     /// @ref cds_sync_monitor "Monitor" that injects the lock into each node
21     /**
22         This simple monitor injects the lock object of type \p Lock into each node.
23         The monitor is designed for user-space locking primitives like \ref sync::spin_lock "spin-lock".
24
25         Template arguments:
26         - Lock - lock type like \p std::mutex or \p cds::sync::spin
27     */
28     template <typename Lock>
29     class injecting_monitor
30     {
31     public:
32         typedef Lock lock_type; ///< Lock type
33
34         /// Node injection
35         struct node_injection {
36             mutable lock_type m_Lock;   ///< Node spin-lock
37
38             //@cond
39             CDS_CONSTEXPR bool check_free() const
40             {
41                 return true;
42             }
43             //@endcond
44         };
45
46         /// Makes exclusive access to node \p p
47         template <typename Node>
48         void lock( Node const& p ) const
49         {
50             p.m_SyncMonitorInjection.m_Lock.lock();
51         }
52
53         /// Unlocks the node \p p
54         template <typename Node>
55         void unlock( Node const& p ) const
56         {
57             p.m_SyncMonitorInjection.m_Lock.unlock();
58         }
59
60         //@cond
61         injecting_monitor_traits::empty_stat statistics() const
62         {
63             return injecting_monitor_traits::empty_stat();
64         }
65         //@endcond
66
67         /// Scoped lock
68         template <typename Node>
69         using scoped_lock = monitor_scoped_lock< injecting_monitor, Node >;
70     };
71 }} // namespace cds::sync
72
73 #endif // #ifndef CDSLIB_SYNC_INJECTING_MONITOR_H