Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / sync / named_sharable_mutex.hpp
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_SHARABLE_MUTEX_HPP
12 #define BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
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/shared_memory_object.hpp>
23 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
24 #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
25 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
26 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
27 #include <boost/interprocess/permissions.hpp>
28
29 //!\file
30 //!Describes a named sharable mutex class for inter-process synchronization
31
32 namespace boost {
33 namespace interprocess {
34
35 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
36 namespace ipcdetail{ class interprocess_tester; }
37 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
38
39 class named_condition;
40
41 //!A sharable mutex with a global name, so it can be found from different
42 //!processes. This mutex can't be placed in shared memory, and
43 //!each process should have it's own named sharable mutex.
44 class named_sharable_mutex
45 {
46    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
47    //Non-copyable
48    named_sharable_mutex();
49    named_sharable_mutex(const named_sharable_mutex &);
50    named_sharable_mutex &operator=(const named_sharable_mutex &);
51    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
52    public:
53
54    //!Creates a global sharable mutex with a name.
55    //!If the sharable mutex can't be created throws interprocess_exception
56    named_sharable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
57
58    //!Opens or creates a global sharable mutex with a name.
59    //!If the sharable mutex is created, this call is equivalent to
60    //!named_sharable_mutex(create_only_t, ...)
61    //!If the sharable mutex is already created, this call is equivalent to
62    //!named_sharable_mutex(open_only_t, ... ).
63    named_sharable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
64
65    //!Opens a global sharable mutex with a name if that sharable mutex
66    //!is previously.
67    //!created. If it is not previously created this function throws
68    //!interprocess_exception.
69    named_sharable_mutex(open_only_t open_only, const char *name);
70
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
76    //!use remove().
77    ~named_sharable_mutex();
78
79    //Exclusive locking
80
81    //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
82    //!   and if another thread has exclusive or sharable ownership of
83    //!   the mutex, it waits until it can obtain the ownership.
84    //!Throws: interprocess_exception on error.
85    void lock();
86
87    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
88    //!   without waiting. If no other thread has exclusive or sharable
89    //!   ownership of the mutex this succeeds.
90    //!Returns: If it can acquire exclusive ownership immediately returns true.
91    //!   If it has to wait, returns false.
92    //!Throws: interprocess_exception on error.
93    bool try_lock();
94
95    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
96    //!   waiting if necessary until no other thread has exclusive, or sharable
97    //!   ownership of the mutex or abs_time is reached.
98    //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
99    //!Throws: interprocess_exception on error.
100    bool timed_lock(const boost::posix_time::ptime &abs_time);
101
102    //!Precondition: The thread must have exclusive ownership of the mutex.
103    //!Effects: The calling thread releases the exclusive ownership of the mutex.
104    //!Throws: An exception derived from interprocess_exception on error.
105    void unlock();
106
107    //Sharable locking
108
109    //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
110    //!   and if another thread has exclusive ownership of the mutex,
111    //!   waits until it can obtain the ownership.
112    //!Throws: interprocess_exception on error.
113    void lock_sharable();
114
115    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
116    //!   without waiting. If no other thread has exclusive ownership
117    //!   of the mutex this succeeds.
118    //!Returns: If it can acquire sharable ownership immediately returns true. If it
119    //!   has to wait, returns false.
120    //!Throws: interprocess_exception on error.
121    bool try_lock_sharable();
122
123    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
124    //!   waiting if necessary until no other thread has exclusive
125    //!   ownership of the mutex or abs_time is reached.
126    //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
127    //!Throws: interprocess_exception on error.
128    bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
129
130    //!Precondition: The thread must have sharable ownership of the mutex.
131    //!Effects: The calling thread releases the sharable ownership of the mutex.
132    //!Throws: An exception derived from interprocess_exception on error.
133    void unlock_sharable();
134
135    //!Erases a named sharable mutex from the system.
136    //!Returns false on error. Never throws.
137    static bool remove(const char *name);
138
139    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
140    private:
141    friend class ipcdetail::interprocess_tester;
142    void dont_close_on_destruction();
143
144    interprocess_sharable_mutex *mutex() const
145    {  return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
146
147    typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
148    open_create_impl_t m_shmem;
149    typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
150    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
151 };
152
153 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
154
155 inline named_sharable_mutex::~named_sharable_mutex()
156 {}
157
158 inline named_sharable_mutex::named_sharable_mutex
159    (create_only_t, const char *name, const permissions &perm)
160    :  m_shmem  (create_only
161                ,name
162                ,sizeof(interprocess_sharable_mutex) +
163                   open_create_impl_t::ManagedOpenOrCreateUserOffset
164                ,read_write
165                ,0
166                ,construct_func_t(ipcdetail::DoCreate)
167                ,perm)
168 {}
169
170 inline named_sharable_mutex::named_sharable_mutex
171    (open_or_create_t, const char *name, const permissions &perm)
172    :  m_shmem  (open_or_create
173                ,name
174                ,sizeof(interprocess_sharable_mutex) +
175                   open_create_impl_t::ManagedOpenOrCreateUserOffset
176                ,read_write
177                ,0
178                ,construct_func_t(ipcdetail::DoOpenOrCreate)
179                ,perm)
180 {}
181
182 inline named_sharable_mutex::named_sharable_mutex
183    (open_only_t, const char *name)
184    :  m_shmem  (open_only
185                ,name
186                ,read_write
187                ,0
188                ,construct_func_t(ipcdetail::DoOpen))
189 {}
190
191 inline void named_sharable_mutex::dont_close_on_destruction()
192 {  ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem);  }
193
194 inline void named_sharable_mutex::lock()
195 {  this->mutex()->lock();  }
196
197 inline void named_sharable_mutex::unlock()
198 {  this->mutex()->unlock();  }
199
200 inline bool named_sharable_mutex::try_lock()
201 {  return this->mutex()->try_lock();  }
202
203 inline bool named_sharable_mutex::timed_lock
204    (const boost::posix_time::ptime &abs_time)
205 {  return this->mutex()->timed_lock(abs_time);  }
206
207 inline void named_sharable_mutex::lock_sharable()
208 {  this->mutex()->lock_sharable();  }
209
210 inline void named_sharable_mutex::unlock_sharable()
211 {  this->mutex()->unlock_sharable();  }
212
213 inline bool named_sharable_mutex::try_lock_sharable()
214 {  return this->mutex()->try_lock_sharable();  }
215
216 inline bool named_sharable_mutex::timed_lock_sharable
217    (const boost::posix_time::ptime &abs_time)
218 {  return this->mutex()->timed_lock_sharable(abs_time);  }
219
220 inline bool named_sharable_mutex::remove(const char *name)
221 {  return shared_memory_object::remove(name); }
222
223 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
224
225 }  //namespace interprocess {
226 }  //namespace boost {
227
228 #include <boost/interprocess/detail/config_end.hpp>
229
230 #endif   //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP