1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP
12 #define BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
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>
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>
36 //!Describes a named mutex class for inter-process synchronization
39 namespace interprocess {
42 class named_condition;
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.
49 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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
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());
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, ... )
69 shm_named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
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);
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
84 //!Unlocks a previously locked
85 //!interprocess_mutex.
88 //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked.
89 //!Throws interprocess_exception if a severe error is found
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
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);
102 //!Erases a named mutex from the system.
103 //!Returns false on error. Never throws.
104 static bool remove(const char *name);
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()); }
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
120 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
122 inline void shm_named_mutex::dont_close_on_destruction()
123 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
125 inline shm_named_mutex::~shm_named_mutex()
128 inline shm_named_mutex::shm_named_mutex(create_only_t, const char *name, const permissions &perm)
129 : m_shmem (create_only
131 ,sizeof(interprocess_mutex) +
132 open_create_impl_t::ManagedOpenOrCreateUserOffset
135 ,construct_func_t(ipcdetail::DoCreate)
139 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const char *name, const permissions &perm)
140 : m_shmem (open_or_create
142 ,sizeof(interprocess_mutex) +
143 open_create_impl_t::ManagedOpenOrCreateUserOffset
146 ,construct_func_t(ipcdetail::DoOpenOrCreate)
150 inline shm_named_mutex::shm_named_mutex(open_only_t, const char *name)
155 ,construct_func_t(ipcdetail::DoOpen))
158 inline void shm_named_mutex::lock()
159 { this->internal_mutex().lock(); }
161 inline void shm_named_mutex::unlock()
162 { this->internal_mutex().unlock(); }
164 inline bool shm_named_mutex::try_lock()
165 { return this->internal_mutex().try_lock(); }
167 inline bool shm_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
168 { return this->internal_mutex().timed_lock(abs_time); }
170 inline bool shm_named_mutex::remove(const char *name)
171 { return shared_memory_object::remove(name); }
173 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
175 } //namespace ipcdetail {
176 } //namespace interprocess {
177 } //namespace boost {
179 #include <boost/interprocess/detail/config_end.hpp>
181 #endif //BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP