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_RECURSIVE_MUTEX_HPP
12 #define BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_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/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>
34 //!Describes a named shm_named_recursive_mutex class for inter-process synchronization
37 namespace interprocess {
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
41 class interprocess_tester;
42 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
44 class shm_named_recursive_mutex
46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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
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());
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, ... )
64 shm_named_recursive_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
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);
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
77 ~shm_named_recursive_mutex();
79 //!Unlocks a previously locked
80 //!shm_named_recursive_mutex.
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.
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.
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);
97 //!Erases a named recursive mutex
99 static bool remove(const char *name);
101 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
103 friend class interprocess_tester;
104 void dont_close_on_destruction();
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
114 inline shm_named_recursive_mutex::~shm_named_recursive_mutex()
117 inline void shm_named_recursive_mutex::dont_close_on_destruction()
118 { interprocess_tester::dont_close_on_destruction(m_shmem); }
120 inline shm_named_recursive_mutex::shm_named_recursive_mutex(create_only_t, const char *name, const permissions &perm)
121 : m_shmem (create_only
123 ,sizeof(interprocess_recursive_mutex) +
124 open_create_impl_t::ManagedOpenOrCreateUserOffset
127 ,construct_func_t(DoCreate)
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
134 ,sizeof(interprocess_recursive_mutex) +
135 open_create_impl_t::ManagedOpenOrCreateUserOffset
138 ,construct_func_t(DoOpenOrCreate)
142 inline shm_named_recursive_mutex::shm_named_recursive_mutex(open_only_t, const char *name)
147 ,construct_func_t(DoOpen))
150 inline void shm_named_recursive_mutex::lock()
151 { this->mutex()->lock(); }
153 inline void shm_named_recursive_mutex::unlock()
154 { this->mutex()->unlock(); }
156 inline bool shm_named_recursive_mutex::try_lock()
157 { return this->mutex()->try_lock(); }
159 inline bool shm_named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
160 { return this->mutex()->timed_lock(abs_time); }
162 inline bool shm_named_recursive_mutex::remove(const char *name)
163 { return shared_memory_object::remove(name); }
165 } //namespace ipcdetail {
166 } //namespace interprocess {
167 } //namespace boost {
169 #include <boost/interprocess/detail/config_end.hpp>
171 #endif //BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_MUTEX_HPP