Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10 //
|
Chris@16
|
11 // Parts of the pthread code come from Boost Threads code:
|
Chris@16
|
12 //
|
Chris@16
|
13 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
14 //
|
Chris@16
|
15 // Copyright (C) 2001-2003
|
Chris@16
|
16 // William E. Kempf
|
Chris@16
|
17 //
|
Chris@16
|
18 // Permission to use, copy, modify, distribute and sell this software
|
Chris@16
|
19 // and its documentation for any purpose is hereby granted without fee,
|
Chris@16
|
20 // provided that the above copyright notice appear in all copies and
|
Chris@16
|
21 // that both that copyright notice and this permission notice appear
|
Chris@16
|
22 // in supporting documentation. William E. Kempf makes no representations
|
Chris@16
|
23 // about the suitability of this software for any purpose.
|
Chris@16
|
24 // It is provided "as is" without express or implied warranty.
|
Chris@16
|
25 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
26
|
Chris@16
|
27 #ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
|
Chris@16
|
28 #define BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
|
Chris@16
|
29
|
Chris@101
|
30 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
31 # include <boost/config.hpp>
|
Chris@101
|
32 #endif
|
Chris@101
|
33 #
|
Chris@101
|
34 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
35 # pragma once
|
Chris@16
|
36 #endif
|
Chris@16
|
37
|
Chris@16
|
38 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
39 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
40
|
Chris@16
|
41 #include <pthread.h>
|
Chris@16
|
42 #include <errno.h>
|
Chris@16
|
43 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
44 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
|
Chris@16
|
45 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
46 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
47 #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
|
Chris@16
|
48
|
Chris@16
|
49 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
50 # include <boost/interprocess/detail/os_thread_functions.hpp>
|
Chris@101
|
51 # include <boost/interprocess/sync/detail/common_algorithms.hpp>
|
Chris@16
|
52 #endif
|
Chris@16
|
53 #include <boost/assert.hpp>
|
Chris@16
|
54
|
Chris@16
|
55 namespace boost {
|
Chris@16
|
56 namespace interprocess {
|
Chris@16
|
57 namespace ipcdetail {
|
Chris@16
|
58
|
Chris@16
|
59 class posix_condition;
|
Chris@16
|
60
|
Chris@16
|
61 class posix_mutex
|
Chris@16
|
62 {
|
Chris@16
|
63 posix_mutex(const posix_mutex &);
|
Chris@16
|
64 posix_mutex &operator=(const posix_mutex &);
|
Chris@16
|
65 public:
|
Chris@16
|
66
|
Chris@16
|
67 posix_mutex();
|
Chris@16
|
68 ~posix_mutex();
|
Chris@16
|
69
|
Chris@16
|
70 void lock();
|
Chris@16
|
71 bool try_lock();
|
Chris@16
|
72 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
73 void unlock();
|
Chris@16
|
74
|
Chris@16
|
75 friend class posix_condition;
|
Chris@16
|
76
|
Chris@16
|
77 private:
|
Chris@16
|
78 pthread_mutex_t m_mut;
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 inline posix_mutex::posix_mutex()
|
Chris@16
|
82 {
|
Chris@16
|
83 mutexattr_wrapper mut_attr;
|
Chris@16
|
84 mutex_initializer mut(m_mut, mut_attr);
|
Chris@16
|
85 mut.release();
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 inline posix_mutex::~posix_mutex()
|
Chris@16
|
89 {
|
Chris@16
|
90 int res = pthread_mutex_destroy(&m_mut);
|
Chris@16
|
91 BOOST_ASSERT(res == 0);(void)res;
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 inline void posix_mutex::lock()
|
Chris@16
|
95 {
|
Chris@16
|
96 if (pthread_mutex_lock(&m_mut) != 0)
|
Chris@16
|
97 throw lock_exception();
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 inline bool posix_mutex::try_lock()
|
Chris@16
|
101 {
|
Chris@16
|
102 int res = pthread_mutex_trylock(&m_mut);
|
Chris@16
|
103 if (!(res == 0 || res == EBUSY))
|
Chris@16
|
104 throw lock_exception();
|
Chris@16
|
105 return res == 0;
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
109 {
|
Chris@101
|
110 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@101
|
111 //Posix does not support infinity absolute time so handle it here
|
Chris@16
|
112 if(abs_time == boost::posix_time::pos_infin){
|
Chris@16
|
113 this->lock();
|
Chris@16
|
114 return true;
|
Chris@16
|
115 }
|
Chris@16
|
116 timespec ts = ptime_to_timespec(abs_time);
|
Chris@16
|
117 int res = pthread_mutex_timedlock(&m_mut, &ts);
|
Chris@16
|
118 if (res != 0 && res != ETIMEDOUT)
|
Chris@16
|
119 throw lock_exception();
|
Chris@16
|
120 return res == 0;
|
Chris@16
|
121
|
Chris@16
|
122 #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
123
|
Chris@101
|
124 return ipcdetail::try_based_timed_lock(*this, abs_time);
|
Chris@16
|
125
|
Chris@16
|
126 #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 inline void posix_mutex::unlock()
|
Chris@16
|
130 {
|
Chris@16
|
131 int res = 0;
|
Chris@16
|
132 res = pthread_mutex_unlock(&m_mut);
|
Chris@16
|
133 (void)res;
|
Chris@16
|
134 BOOST_ASSERT(res == 0);
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 } //namespace ipcdetail {
|
Chris@16
|
138 } //namespace interprocess {
|
Chris@16
|
139 } //namespace boost {
|
Chris@16
|
140
|
Chris@16
|
141 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
142
|
Chris@16
|
143 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
|