Added sync monitor statistics
[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
39         /// Makes exclusive access to node \p p
40         template <typename Node>
41         void lock( Node const& p ) const
42         {
43             p.m_SyncMonitorInjection.m_Lock.lock();
44         }
45
46         /// Unlocks the node \p p
47         template <typename Node>
48         void unlock( Node const& p ) const
49         {
50             p.m_SyncMonitorInjection.m_Lock.unlock();
51         }
52
53         //@cond
54         injecting_monitor_traits::empty_stat statistics() const
55         {
56             return injecting_monitor_traits::empty_stat();
57         }
58         //@endcond
59
60         /// Scoped lock
61         template <typename Node>
62         using scoped_lock = monitor_scoped_lock< injecting_monitor, Node > ;
63     };
64 }} // namespace cds::sync
65
66 #endif // #ifndef CDSLIB_SYNC_INJECTING_MONITOR_H