Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/interprocess/sync/posix/mutex.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children | c530137014c0 |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
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 // Copyright (C) 2001-2003 | |
16 // William E. Kempf | |
17 // | |
18 // Permission to use, copy, modify, distribute and sell this software | |
19 // and its documentation for any purpose is hereby granted without fee, | |
20 // provided that the above copyright notice appear in all copies and | |
21 // that both that copyright notice and this permission notice appear | |
22 // in supporting documentation. William E. Kempf makes no representations | |
23 // about the suitability of this software for any purpose. | |
24 // It is provided "as is" without express or implied warranty. | |
25 ////////////////////////////////////////////////////////////////////////////// | |
26 | |
27 #ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP | |
28 #define BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP | |
29 | |
30 #if (defined _MSC_VER) && (_MSC_VER >= 1200) | |
31 # pragma once | |
32 #endif | |
33 | |
34 #include <boost/interprocess/detail/config_begin.hpp> | |
35 #include <boost/interprocess/detail/workaround.hpp> | |
36 | |
37 #include <pthread.h> | |
38 #include <errno.h> | |
39 #include <boost/interprocess/exceptions.hpp> | |
40 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp> | |
41 #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | |
42 #include <boost/interprocess/exceptions.hpp> | |
43 #include <boost/interprocess/sync/posix/pthread_helpers.hpp> | |
44 | |
45 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
46 # include <boost/interprocess/detail/os_thread_functions.hpp> | |
47 # include <boost/interprocess/sync/spin/wait.hpp> | |
48 #endif | |
49 #include <boost/assert.hpp> | |
50 | |
51 namespace boost { | |
52 namespace interprocess { | |
53 namespace ipcdetail { | |
54 | |
55 class posix_condition; | |
56 | |
57 class posix_mutex | |
58 { | |
59 posix_mutex(const posix_mutex &); | |
60 posix_mutex &operator=(const posix_mutex &); | |
61 public: | |
62 | |
63 posix_mutex(); | |
64 ~posix_mutex(); | |
65 | |
66 void lock(); | |
67 bool try_lock(); | |
68 bool timed_lock(const boost::posix_time::ptime &abs_time); | |
69 void unlock(); | |
70 | |
71 friend class posix_condition; | |
72 | |
73 private: | |
74 pthread_mutex_t m_mut; | |
75 }; | |
76 | |
77 inline posix_mutex::posix_mutex() | |
78 { | |
79 mutexattr_wrapper mut_attr; | |
80 mutex_initializer mut(m_mut, mut_attr); | |
81 mut.release(); | |
82 } | |
83 | |
84 inline posix_mutex::~posix_mutex() | |
85 { | |
86 int res = pthread_mutex_destroy(&m_mut); | |
87 BOOST_ASSERT(res == 0);(void)res; | |
88 } | |
89 | |
90 inline void posix_mutex::lock() | |
91 { | |
92 if (pthread_mutex_lock(&m_mut) != 0) | |
93 throw lock_exception(); | |
94 } | |
95 | |
96 inline bool posix_mutex::try_lock() | |
97 { | |
98 int res = pthread_mutex_trylock(&m_mut); | |
99 if (!(res == 0 || res == EBUSY)) | |
100 throw lock_exception(); | |
101 return res == 0; | |
102 } | |
103 | |
104 inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time) | |
105 { | |
106 if(abs_time == boost::posix_time::pos_infin){ | |
107 this->lock(); | |
108 return true; | |
109 } | |
110 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
111 | |
112 timespec ts = ptime_to_timespec(abs_time); | |
113 int res = pthread_mutex_timedlock(&m_mut, &ts); | |
114 if (res != 0 && res != ETIMEDOUT) | |
115 throw lock_exception(); | |
116 return res == 0; | |
117 | |
118 #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
119 | |
120 //Obtain current count and target time | |
121 boost::posix_time::ptime now = microsec_clock::universal_time(); | |
122 | |
123 spin_wait swait; | |
124 do{ | |
125 if(this->try_lock()){ | |
126 break; | |
127 } | |
128 now = microsec_clock::universal_time(); | |
129 | |
130 if(now >= abs_time){ | |
131 return false; | |
132 } | |
133 // relinquish current time slice | |
134 swait.yield(); | |
135 }while (true); | |
136 return true; | |
137 | |
138 #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
139 } | |
140 | |
141 inline void posix_mutex::unlock() | |
142 { | |
143 int res = 0; | |
144 res = pthread_mutex_unlock(&m_mut); | |
145 (void)res; | |
146 BOOST_ASSERT(res == 0); | |
147 } | |
148 | |
149 } //namespace ipcdetail { | |
150 } //namespace interprocess { | |
151 } //namespace boost { | |
152 | |
153 #include <boost/interprocess/detail/config_end.hpp> | |
154 | |
155 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP |