]>
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_MUTEX_HPP | |
12 | #define BOOST_INTERPROCESS_SHM_NAMED_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/interprocess_tester.hpp> | |
27 | #include <boost/interprocess/permissions.hpp> | |
28 | #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | |
29 | ||
30 | #include <boost/interprocess/shared_memory_object.hpp> | |
31 | #include <boost/interprocess/sync/interprocess_mutex.hpp> | |
32 | #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> | |
33 | #include <boost/interprocess/sync/shm/named_creation_functor.hpp> | |
34 | ||
35 | //!\file | |
36 | //!Describes a named mutex class for inter-process synchronization | |
37 | ||
38 | namespace boost { | |
39 | namespace interprocess { | |
40 | namespace ipcdetail { | |
41 | ||
42 | class named_condition; | |
43 | ||
44 | //!A mutex with a global name, so it can be found from different | |
45 | //!processes. This mutex can't be placed in shared memory, and | |
46 | //!each process should have it's own named mutex. | |
47 | class shm_named_mutex | |
48 | { | |
49 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
50 | ||
51 | //Non-copyable | |
52 | shm_named_mutex(); | |
53 | shm_named_mutex(const shm_named_mutex &); | |
54 | shm_named_mutex &operator=(const shm_named_mutex &); | |
55 | friend class named_condition; | |
56 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
57 | ||
58 | public: | |
59 | //!Creates a global interprocess_mutex with a name. | |
60 | //!Throws interprocess_exception on error. | |
61 | shm_named_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions()); | |
62 | ||
63 | //!Opens or creates a global mutex with a name. | |
64 | //!If the mutex is created, this call is equivalent to | |
65 | //!shm_named_mutex(create_only_t, ... ) | |
66 | //!If the mutex is already created, this call is equivalent | |
67 | //!shm_named_mutex(open_only_t, ... ) | |
68 | //!Does not throw | |
69 | shm_named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions()); | |
70 | ||
71 | //!Opens a global mutex with a name if that mutex is previously | |
72 | //!created. If it is not previously created this function throws | |
73 | //!interprocess_exception. | |
74 | shm_named_mutex(open_only_t open_only, const char *name); | |
75 | ||
76 | //!Destroys *this and indicates that the calling process is finished using | |
77 | //!the resource. The destructor function will deallocate | |
78 | //!any system resources allocated by the system for use by this process for | |
79 | //!this resource. The resource can still be opened again calling | |
80 | //!the open constructor overload. To erase the resource from the system | |
81 | //!use remove(). | |
82 | ~shm_named_mutex(); | |
83 | ||
84 | //!Unlocks a previously locked | |
85 | //!interprocess_mutex. | |
86 | void unlock(); | |
87 | ||
88 | //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked. | |
89 | //!Throws interprocess_exception if a severe error is found | |
90 | void lock(); | |
91 | ||
92 | //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex | |
93 | //!is already locked, returns true when success. | |
94 | //!Throws interprocess_exception if a severe error is found | |
95 | bool try_lock(); | |
96 | ||
97 | //!Tries to lock the interprocess_mutex until time abs_time, | |
98 | //!Returns false when timeout expires, returns true when locks. | |
99 | //!Throws interprocess_exception if a severe error is found | |
100 | bool timed_lock(const boost::posix_time::ptime &abs_time); | |
101 | ||
102 | //!Erases a named mutex from the system. | |
103 | //!Returns false on error. Never throws. | |
104 | static bool remove(const char *name); | |
105 | ||
106 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
107 | typedef interprocess_mutex internal_mutex_type; | |
108 | interprocess_mutex &internal_mutex() | |
109 | { return *static_cast<interprocess_mutex*>(m_shmem.get_user_address()); } | |
110 | ||
111 | private: | |
112 | friend class ipcdetail::interprocess_tester; | |
113 | void dont_close_on_destruction(); | |
114 | typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t; | |
115 | open_create_impl_t m_shmem; | |
116 | typedef ipcdetail::named_creation_functor<interprocess_mutex> construct_func_t; | |
117 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
118 | }; | |
119 | ||
120 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
121 | ||
122 | inline void shm_named_mutex::dont_close_on_destruction() | |
123 | { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); } | |
124 | ||
125 | inline shm_named_mutex::~shm_named_mutex() | |
126 | {} | |
127 | ||
128 | inline shm_named_mutex::shm_named_mutex(create_only_t, const char *name, const permissions &perm) | |
129 | : m_shmem (create_only | |
130 | ,name | |
131 | ,sizeof(interprocess_mutex) + | |
132 | open_create_impl_t::ManagedOpenOrCreateUserOffset | |
133 | ,read_write | |
134 | ,0 | |
135 | ,construct_func_t(ipcdetail::DoCreate) | |
136 | ,perm) | |
137 | {} | |
138 | ||
139 | inline shm_named_mutex::shm_named_mutex(open_or_create_t, const char *name, const permissions &perm) | |
140 | : m_shmem (open_or_create | |
141 | ,name | |
142 | ,sizeof(interprocess_mutex) + | |
143 | open_create_impl_t::ManagedOpenOrCreateUserOffset | |
144 | ,read_write | |
145 | ,0 | |
146 | ,construct_func_t(ipcdetail::DoOpenOrCreate) | |
147 | ,perm) | |
148 | {} | |
149 | ||
150 | inline shm_named_mutex::shm_named_mutex(open_only_t, const char *name) | |
151 | : m_shmem (open_only | |
152 | ,name | |
153 | ,read_write | |
154 | ,0 | |
155 | ,construct_func_t(ipcdetail::DoOpen)) | |
156 | {} | |
157 | ||
158 | inline void shm_named_mutex::lock() | |
159 | { this->internal_mutex().lock(); } | |
160 | ||
161 | inline void shm_named_mutex::unlock() | |
162 | { this->internal_mutex().unlock(); } | |
163 | ||
164 | inline bool shm_named_mutex::try_lock() | |
165 | { return this->internal_mutex().try_lock(); } | |
166 | ||
167 | inline bool shm_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time) | |
168 | { return this->internal_mutex().timed_lock(abs_time); } | |
169 | ||
170 | inline bool shm_named_mutex::remove(const char *name) | |
171 | { return shared_memory_object::remove(name); } | |
172 | ||
173 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
174 | ||
175 | } //namespace ipcdetail { | |
176 | } //namespace interprocess { | |
177 | } //namespace boost { | |
178 | ||
179 | #include <boost/interprocess/detail/config_end.hpp> | |
180 | ||
181 | #endif //BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP |