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