Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / sync / interprocess_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 // Parts of the pthread code come from Boost Threads code.
12 //
13 //////////////////////////////////////////////////////////////////////////////
14
15 #ifndef BOOST_INTERPROCESS_MUTEX_HPP
16 #define BOOST_INTERPROCESS_MUTEX_HPP
17
18 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
19
20 #if defined(_MSC_VER)
21 #  pragma once
22 #endif
23
24 #include <boost/interprocess/detail/config_begin.hpp>
25 #include <boost/interprocess/exceptions.hpp>
26 #include <boost/interprocess/detail/workaround.hpp>
27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
28 #include <boost/assert.hpp>
29
30 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
31    #include <boost/interprocess/sync/posix/mutex.hpp>
32    #define BOOST_INTERPROCESS_USE_POSIX
33 //Experimental...
34 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
35    #include <boost/interprocess/sync/windows/mutex.hpp>
36    #define BOOST_INTERPROCESS_USE_WINDOWS
37 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
38    #include <boost/interprocess/sync/spin/mutex.hpp>
39    #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
40
41 namespace boost {
42 namespace interprocess {
43 namespace ipcdetail{
44 namespace robust_emulation_helpers {
45
46 template<class T>
47 class mutex_traits;
48
49 }}}}
50
51 #endif
52
53 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
54
55 //!\file
56 //!Describes a mutex class that can be placed in memory shared by
57 //!several processes.
58
59 namespace boost {
60 namespace interprocess {
61
62 class interprocess_condition;
63
64 //!Wraps a interprocess_mutex that can be placed in shared memory and can be
65 //!shared between processes. Allows timed lock tries
66 class interprocess_mutex
67 {
68    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
69    //Non-copyable
70    interprocess_mutex(const interprocess_mutex &);
71    interprocess_mutex &operator=(const interprocess_mutex &);
72    friend class interprocess_condition;
73
74    public:
75    #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
76       #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
77       typedef ipcdetail::spin_mutex internal_mutex_type;
78       private:
79       friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
80       void take_ownership(){ m_mutex.take_ownership(); }
81       public:
82    #elif defined(BOOST_INTERPROCESS_USE_POSIX)
83       #undef BOOST_INTERPROCESS_USE_POSIX
84       typedef ipcdetail::posix_mutex internal_mutex_type;
85    #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
86       #undef BOOST_INTERPROCESS_USE_WINDOWS
87       typedef ipcdetail::windows_mutex internal_mutex_type;
88    #else
89       #error "Unknown platform for interprocess_mutex"
90    #endif
91
92    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
93    public:
94
95    //!Constructor.
96    //!Throws interprocess_exception on error.
97    interprocess_mutex();
98
99    //!Destructor. If any process uses the mutex after the destructor is called
100    //!the result is undefined. Does not throw.
101    ~interprocess_mutex();
102
103    //!Effects: The calling thread tries to obtain ownership of the mutex, and
104    //!   if another thread has ownership of the mutex, it waits until it can
105    //!   obtain the ownership. If a thread takes ownership of the mutex the
106    //!   mutex must be unlocked by the same mutex.
107    //!Throws: interprocess_exception on error.
108    void lock();
109
110    //!Effects: The calling thread tries to obtain ownership of the mutex, and
111    //!   if another thread has ownership of the mutex returns immediately.
112    //!Returns: If the thread acquires ownership of the mutex, returns true, if
113    //!   the another thread has ownership of the mutex, returns false.
114    //!Throws: interprocess_exception on error.
115    bool try_lock();
116
117    //!Effects: The calling thread will try to obtain exclusive ownership of the
118    //!   mutex if it can do so in until the specified time is reached. If the
119    //!   mutex supports recursive locking, the mutex must be unlocked the same
120    //!   number of times it is locked.
121    //!Returns: If the thread acquires ownership of the mutex, returns true, if
122    //!   the timeout expires returns false.
123    //!Throws: interprocess_exception on error.
124    bool timed_lock(const boost::posix_time::ptime &abs_time);
125
126    //!Effects: The calling thread releases the exclusive ownership of the mutex.
127    //!Throws: interprocess_exception on error.
128    void unlock();
129
130    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
131    internal_mutex_type &internal_mutex()
132    {  return m_mutex;   }
133
134    const internal_mutex_type &internal_mutex() const
135    {  return m_mutex;   }
136
137    private:
138    internal_mutex_type m_mutex;
139    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
140 };
141
142 }  //namespace interprocess {
143 }  //namespace boost {
144
145
146 namespace boost {
147 namespace interprocess {
148
149 inline interprocess_mutex::interprocess_mutex(){}
150
151 inline interprocess_mutex::~interprocess_mutex(){}
152
153 inline void interprocess_mutex::lock()
154 {
155    #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
156       boost::posix_time::ptime wait_time
157          = boost::posix_time::microsec_clock::universal_time()
158          + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
159       if (!m_mutex.timed_lock(wait_time))
160       {
161          throw interprocess_exception(timeout_when_locking_error
162                                      , "Interprocess mutex timeout when locking. Possible deadlock: "
163                                        "owner died without unlocking?");
164       }
165    #else
166       m_mutex.lock();
167    #endif
168 }
169
170 inline bool interprocess_mutex::try_lock()
171 { return m_mutex.try_lock(); }
172
173 inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
174 { return m_mutex.timed_lock(abs_time); }
175
176 inline void interprocess_mutex::unlock()
177 { m_mutex.unlock(); }
178
179 }  //namespace interprocess {
180 }  //namespace boost {
181
182 #include <boost/interprocess/detail/config_end.hpp>
183
184 #endif   //BOOST_INTERPROCESS_MUTEX_HPP