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