1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2011. 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 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/interprocess/exceptions.hpp>
22 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
23 #include <boost/interprocess/shared_memory_object.hpp>
24 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
25 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
26 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
27 #include <boost/interprocess/permissions.hpp>
30 //!Describes a named shm_named_recursive_mutex class for inter-process synchronization
33 namespace interprocess {
37 class interprocess_tester;
40 class shm_named_recursive_mutex
44 shm_named_recursive_mutex();
45 shm_named_recursive_mutex(const shm_named_recursive_mutex &);
46 shm_named_recursive_mutex &operator=(const shm_named_recursive_mutex &);
50 //!Creates a global recursive_mutex with a name.
51 //!If the recursive_mutex can't be created throws interprocess_exception
52 shm_named_recursive_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
54 //!Opens or creates a global recursive_mutex with a name.
55 //!If the recursive_mutex is created, this call is equivalent to
56 //!shm_named_recursive_mutex(create_only_t, ... )
57 //!If the recursive_mutex is already created, this call is equivalent
58 //!shm_named_recursive_mutex(open_only_t, ... )
60 shm_named_recursive_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
62 //!Opens a global recursive_mutex with a name if that recursive_mutex is previously
63 //!created. If it is not previously created this function throws
64 //!interprocess_exception.
65 shm_named_recursive_mutex(open_only_t open_only, const char *name);
67 //!Destroys *this and indicates that the calling process is finished using
68 //!the resource. The destructor function will deallocate
69 //!any system resources allocated by the system for use by this process for
70 //!this resource. The resource can still be opened again calling
71 //!the open constructor overload. To erase the resource from the system
73 ~shm_named_recursive_mutex();
75 //!Unlocks a previously locked
76 //!shm_named_recursive_mutex.
79 //!Locks shm_named_recursive_mutex, sleeps when shm_named_recursive_mutex is already locked.
80 //!Throws interprocess_exception if a severe error is found.
83 //!Tries to lock the shm_named_recursive_mutex, returns false when shm_named_recursive_mutex
84 //!is already locked, returns true when success.
85 //!Throws interprocess_exception if a severe error is found.
88 //!Tries to lock the shm_named_recursive_mutex until time abs_time,
89 //!Returns false when timeout expires, returns true when locks.
90 //!Throws interprocess_exception if a severe error is found
91 bool timed_lock(const boost::posix_time::ptime &abs_time);
93 //!Erases a named recursive mutex
95 static bool remove(const char *name);
99 friend class interprocess_tester;
100 void dont_close_on_destruction();
102 interprocess_recursive_mutex *mutex() const
103 { return static_cast<interprocess_recursive_mutex*>(m_shmem.get_user_address()); }
105 managed_open_or_create_impl<shared_memory_object> m_shmem;
106 typedef named_creation_functor<interprocess_recursive_mutex> construct_func_t;
110 inline shm_named_recursive_mutex::~shm_named_recursive_mutex()
113 inline void shm_named_recursive_mutex::dont_close_on_destruction()
114 { interprocess_tester::dont_close_on_destruction(m_shmem); }
116 inline shm_named_recursive_mutex::shm_named_recursive_mutex(create_only_t, const char *name, const permissions &perm)
117 : m_shmem (create_only
119 ,sizeof(interprocess_recursive_mutex) +
120 managed_open_or_create_impl<shared_memory_object>::
121 ManagedOpenOrCreateUserOffset
124 ,construct_func_t(DoCreate)
128 inline shm_named_recursive_mutex::shm_named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm)
129 : m_shmem (open_or_create
131 ,sizeof(interprocess_recursive_mutex) +
132 managed_open_or_create_impl<shared_memory_object>::
133 ManagedOpenOrCreateUserOffset
136 ,construct_func_t(DoOpenOrCreate)
140 inline shm_named_recursive_mutex::shm_named_recursive_mutex(open_only_t, const char *name)
145 ,construct_func_t(DoOpen))
148 inline void shm_named_recursive_mutex::lock()
149 { this->mutex()->lock(); }
151 inline void shm_named_recursive_mutex::unlock()
152 { this->mutex()->unlock(); }
154 inline bool shm_named_recursive_mutex::try_lock()
155 { return this->mutex()->try_lock(); }
157 inline bool shm_named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
159 if(abs_time == boost::posix_time::pos_infin){
163 return this->mutex()->timed_lock(abs_time);
166 inline bool shm_named_recursive_mutex::remove(const char *name)
167 { return shared_memory_object::remove(name); }
169 } //namespace ipcdetail {
170 } //namespace interprocess {
171 } //namespace boost {
173 #include <boost/interprocess/detail/config_end.hpp>
175 #endif //BOOST_INTERPROCESS_SHM_NAMED_RECURSIVE_MUTEX_HPP