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_NAMED_RECURSIVE_MUTEX_HPP
12 #define BOOST_INTERPROCESS_NAMED_RECURSIVE_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/detail/posix_time_types_wrk.hpp>
26 #include <boost/interprocess/permissions.hpp>
27 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
28    #include <boost/interprocess/sync/windows/named_recursive_mutex.hpp>
29    #define BOOST_INTERPROCESS_USE_WINDOWS
30 #else
31    #include <boost/interprocess/sync/shm/named_recursive_mutex.hpp>
32 #endif
33 
34 //!\file
35 //!Describes a named named_recursive_mutex class for inter-process synchronization
36 
37 namespace boost {
38 namespace interprocess {
39 
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
41 namespace ipcdetail{ class interprocess_tester; }
42 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
43 
44 //!A recursive 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_recursive_mutex.
47 class named_recursive_mutex
48 {
49    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
50    //Non-copyable
51    named_recursive_mutex();
52    named_recursive_mutex(const named_recursive_mutex &);
53    named_recursive_mutex &operator=(const named_recursive_mutex &);
54    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
55    public:
56 
57    //!Creates a global recursive_mutex with a name.
58    //!If the recursive_mutex can't be created throws interprocess_exception
59    named_recursive_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
60 
61    //!Opens or creates a global recursive_mutex with a name.
62    //!If the recursive_mutex is created, this call is equivalent to
63    //!named_recursive_mutex(create_only_t, ... )
64    //!If the recursive_mutex is already created, this call is equivalent
65    //!named_recursive_mutex(open_only_t, ... )
66    //!Does not throw
67    named_recursive_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
68 
69    //!Opens a global recursive_mutex with a name if that recursive_mutex is previously
70    //!created. If it is not previously created this function throws
71    //!interprocess_exception.
72    named_recursive_mutex(open_only_t open_only, const char *name);
73 
74    //!Destroys *this and indicates that the calling process is finished using
75    //!the resource. The destructor function will deallocate
76    //!any system resources allocated by the system for use by this process for
77    //!this resource. The resource can still be opened again calling
78    //!the open constructor overload. To erase the resource from the system
79    //!use remove().
80    ~named_recursive_mutex();
81 
82    //!Unlocks a previously locked
83    //!named_recursive_mutex.
84    void unlock();
85 
86    //!Locks named_recursive_mutex, sleeps when named_recursive_mutex is already locked.
87    //!Throws interprocess_exception if a severe error is found.
88    void lock();
89 
90    //!Tries to lock the named_recursive_mutex, returns false when named_recursive_mutex
91    //!is already locked, returns true when success.
92    //!Throws interprocess_exception if a severe error is found.
93    bool try_lock();
94 
95    //!Tries to lock the named_recursive_mutex until time abs_time,
96    //!Returns false when timeout expires, returns true when locks.
97    //!Throws interprocess_exception if a severe error is found
98    bool timed_lock(const boost::posix_time::ptime &abs_time);
99 
100    //!Erases a named recursive mutex
101    //!from the system
102    static bool remove(const char *name);
103 
104    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
105    private:
106    friend class ipcdetail::interprocess_tester;
107    void dont_close_on_destruction();
108 
109    #if defined(BOOST_INTERPROCESS_USE_WINDOWS)
110       typedef ipcdetail::windows_named_recursive_mutex   impl_t;
111       #undef BOOST_INTERPROCESS_USE_WINDOWS
112    #else
113       typedef ipcdetail::shm_named_recursive_mutex impl_t;
114    #endif
115    impl_t m_mut;
116 
117    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
118 };
119 
120 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
121 
~named_recursive_mutex()122 inline named_recursive_mutex::~named_recursive_mutex()
123 {}
124 
dont_close_on_destruction()125 inline void named_recursive_mutex::dont_close_on_destruction()
126 {  ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut);  }
127 
named_recursive_mutex(create_only_t,const char * name,const permissions & perm)128 inline named_recursive_mutex::named_recursive_mutex(create_only_t, const char *name, const permissions &perm)
129    :  m_mut  (create_only, name, perm)
130 {}
131 
named_recursive_mutex(open_or_create_t,const char * name,const permissions & perm)132 inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm)
133    :  m_mut  (open_or_create, name, perm)
134 {}
135 
named_recursive_mutex(open_only_t,const char * name)136 inline named_recursive_mutex::named_recursive_mutex(open_only_t, const char *name)
137    :  m_mut   (open_only, name)
138 {}
139 
lock()140 inline void named_recursive_mutex::lock()
141 {  m_mut.lock();  }
142 
unlock()143 inline void named_recursive_mutex::unlock()
144 {  m_mut.unlock();  }
145 
try_lock()146 inline bool named_recursive_mutex::try_lock()
147 {  return m_mut.try_lock();  }
148 
timed_lock(const boost::posix_time::ptime & abs_time)149 inline bool named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
150 {  return m_mut.timed_lock(abs_time);  }
151 
remove(const char * name)152 inline bool named_recursive_mutex::remove(const char *name)
153 {  return impl_t::remove(name); }
154 
155 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
156 
157 }  //namespace interprocess {
158 }  //namespace boost {
159 
160 #include <boost/interprocess/detail/config_end.hpp>
161 
162 #endif   //BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_HPP
163