]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost | |
4 | // Software License, Version 1.0. (See accompanying file | |
5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | // See http://www.boost.org/libs/interprocess for documentation. | |
8 | // | |
9 | ////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_MUTEX_HPP | |
12 | #define BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_MUTEX_HPP | |
13 | ||
14 | #ifndef BOOST_CONFIG_HPP | |
15 | # include <boost/config.hpp> | |
16 | #endif | |
17 | # | |
18 | #if defined(BOOST_HAS_PRAGMA_ONCE) | |
19 | # pragma once | |
20 | #endif | |
21 | ||
22 | #include <boost/interprocess/detail/config_begin.hpp> | |
23 | #include <boost/interprocess/detail/workaround.hpp> | |
24 | #include <boost/interprocess/creation_tags.hpp> | |
25 | #include <boost/interprocess/exceptions.hpp> | |
26 | #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | |
27 | #include <boost/interprocess/shared_memory_object.hpp> | |
28 | #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> | |
29 | #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp> | |
30 | #include <boost/interprocess/sync/shm/named_creation_functor.hpp> | |
31 | #include <boost/interprocess/permissions.hpp> | |
32 | ||
33 | //!\file | |
34 | //!Describes a named shm_named_recursive_mutex class for inter-process synchronization | |
35 | ||
36 | namespace boost { | |
37 | namespace interprocess { | |
38 | namespace ipcdetail { | |
39 | ||
40 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
41 | class interprocess_tester; | |
42 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
43 | ||
44 | class shm_named_recursive_mutex | |
45 | { | |
46 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
47 | //Non-copyable | |
48 | shm_named_recursive_mutex(); | |
49 | shm_named_recursive_mutex(const shm_named_recursive_mutex &); | |
50 | shm_named_recursive_mutex &operator=(const shm_named_recursive_mutex &); | |
51 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
52 | public: | |
53 | ||
54 | //!Creates a global recursive_mutex with a name. | |
55 | //!If the recursive_mutex can't be created throws interprocess_exception | |
56 | shm_named_recursive_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions()); | |
57 | ||
58 | //!Opens or creates a global recursive_mutex with a name. | |
59 | //!If the recursive_mutex is created, this call is equivalent to | |
60 | //!shm_named_recursive_mutex(create_only_t, ... ) | |
61 | //!If the recursive_mutex is already created, this call is equivalent | |
62 | //!shm_named_recursive_mutex(open_only_t, ... ) | |
63 | //!Does not throw | |
64 | shm_named_recursive_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions()); | |
65 | ||
66 | //!Opens a global recursive_mutex with a name if that recursive_mutex is previously | |
67 | //!created. If it is not previously created this function throws | |
68 | //!interprocess_exception. | |
69 | shm_named_recursive_mutex(open_only_t open_only, const char *name); | |
70 | ||
71 | //!Destroys *this and indicates that the calling process is finished using | |
72 | //!the resource. The destructor function will deallocate | |
73 | //!any system resources allocated by the system for use by this process for | |
74 | //!this resource. The resource can still be opened again calling | |
75 | //!the open constructor overload. To erase the resource from the system | |
76 | //!use remove(). | |
77 | ~shm_named_recursive_mutex(); | |
78 | ||
79 | //!Unlocks a previously locked | |
80 | //!shm_named_recursive_mutex. | |
81 | void unlock(); | |
82 | ||
83 | //!Locks shm_named_recursive_mutex, sleeps when shm_named_recursive_mutex is already locked. | |
84 | //!Throws interprocess_exception if a severe error is found. | |
85 | void lock(); | |
86 | ||
87 | //!Tries to lock the shm_named_recursive_mutex, returns false when shm_named_recursive_mutex | |
88 | //!is already locked, returns true when success. | |
89 | //!Throws interprocess_exception if a severe error is found. | |
90 | bool try_lock(); | |
91 | ||
92 | //!Tries to lock the shm_named_recursive_mutex until time abs_time, | |
93 | //!Returns false when timeout expires, returns true when locks. | |
94 | //!Throws interprocess_exception if a severe error is found | |
95 | bool timed_lock(const boost::posix_time::ptime &abs_time); | |
96 | ||
97 | //!Erases a named recursive mutex | |
98 | //!from the system | |
99 | static bool remove(const char *name); | |
100 | ||
101 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
102 | private: | |
103 | friend class interprocess_tester; | |
104 | void dont_close_on_destruction(); | |
105 | ||
106 | interprocess_recursive_mutex *mutex() const | |
107 | { return static_cast<interprocess_recursive_mutex*>(m_shmem.get_user_address()); } | |
108 | typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t; | |
109 | open_create_impl_t m_shmem; | |
110 | typedef named_creation_functor<interprocess_recursive_mutex> construct_func_t; | |
111 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
112 | }; | |
113 | ||
114 | inline shm_named_recursive_mutex::~shm_named_recursive_mutex() | |
115 | {} | |
116 | ||
117 | inline void shm_named_recursive_mutex::dont_close_on_destruction() | |
118 | { interprocess_tester::dont_close_on_destruction(m_shmem); } | |
119 | ||
120 | inline shm_named_recursive_mutex::shm_named_recursive_mutex(create_only_t, const char *name, const permissions &perm) | |
121 | : m_shmem (create_only | |
122 | ,name | |
123 | ,sizeof(interprocess_recursive_mutex) + | |
124 | open_create_impl_t::ManagedOpenOrCreateUserOffset | |
125 | ,read_write | |
126 | ,0 | |
127 | ,construct_func_t(DoCreate) | |
128 | ,perm) | |
129 | {} | |
130 | ||
131 | inline shm_named_recursive_mutex::shm_named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm) | |
132 | : m_shmem (open_or_create | |
133 | ,name | |
134 | ,sizeof(interprocess_recursive_mutex) + | |
135 | open_create_impl_t::ManagedOpenOrCreateUserOffset | |
136 | ,read_write | |
137 | ,0 | |
138 | ,construct_func_t(DoOpenOrCreate) | |
139 | ,perm) | |
140 | {} | |
141 | ||
142 | inline shm_named_recursive_mutex::shm_named_recursive_mutex(open_only_t, const char *name) | |
143 | : m_shmem (open_only | |
144 | ,name | |
145 | ,read_write | |
146 | ,0 | |
147 | ,construct_func_t(DoOpen)) | |
148 | {} | |
149 | ||
150 | inline void shm_named_recursive_mutex::lock() | |
151 | { this->mutex()->lock(); } | |
152 | ||
153 | inline void shm_named_recursive_mutex::unlock() | |
154 | { this->mutex()->unlock(); } | |
155 | ||
156 | inline bool shm_named_recursive_mutex::try_lock() | |
157 | { return this->mutex()->try_lock(); } | |
158 | ||
159 | inline bool shm_named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time) | |
160 | { return this->mutex()->timed_lock(abs_time); } | |
161 | ||
162 | inline bool shm_named_recursive_mutex::remove(const char *name) | |
163 | { return shared_memory_object::remove(name); } | |
164 | ||
165 | } //namespace ipcdetail { | |
166 | } //namespace interprocess { | |
167 | } //namespace boost { | |
168 | ||
169 | #include <boost/interprocess/detail/config_end.hpp> | |
170 | ||
171 | #endif //BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_MUTEX_HPP |