Updated copyright
[libcds.git] / cds / sync / injecting_monitor.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef CDSLIB_SYNC_INJECTING_MONITOR_H
32 #define CDSLIB_SYNC_INJECTING_MONITOR_H
33
34 #include <cds/sync/monitor.h>
35 #ifndef CDS_CXX11_INHERITING_CTOR
36 #   include <utility> // std::forward
37 #endif
38
39 namespace cds { namespace sync {
40
41     //@cond
42     struct injecting_monitor_traits {
43         struct empty_stat
44         {};
45     };
46     //@endcond
47
48     /// @ref cds_sync_monitor "Monitor" that injects the lock into each node
49     /**
50         This simple monitor injects the lock object of type \p Lock into each node.
51         The monitor is designed for user-space locking primitives like \ref sync::spin_lock "spin-lock".
52
53         Template arguments:
54         - Lock - lock type like \p std::mutex or \p cds::sync::spin
55     */
56     template <typename Lock>
57     class injecting_monitor
58     {
59     public:
60         typedef Lock lock_type; ///< Lock type
61
62         /// Node injection
63         struct node_injection {
64             mutable lock_type m_Lock;   ///< Node spin-lock
65
66             //@cond
67             CDS_CONSTEXPR bool check_free() const
68             {
69                 return true;
70             }
71             //@endcond
72         };
73
74         /// Makes exclusive access to node \p p
75         template <typename Node>
76         void lock( Node const& p ) const
77         {
78             p.m_SyncMonitorInjection.m_Lock.lock();
79         }
80
81         /// Unlocks the node \p p
82         template <typename Node>
83         void unlock( Node const& p ) const
84         {
85             p.m_SyncMonitorInjection.m_Lock.unlock();
86         }
87
88         //@cond
89         injecting_monitor_traits::empty_stat statistics() const
90         {
91             return injecting_monitor_traits::empty_stat();
92         }
93         //@endcond
94
95         /// Scoped lock
96         template <typename Node>
97         using scoped_lock = monitor_scoped_lock< injecting_monitor, Node >;
98     };
99 }} // namespace cds::sync
100
101 #endif // #ifndef CDSLIB_SYNC_INJECTING_MONITOR_H