Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/interprocess/sync/posix/recursive_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_RECURSIVE_MUTEX_HPP | |
28 #define BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP | |
29 | |
30 #include <boost/interprocess/detail/config_begin.hpp> | |
31 #include <boost/interprocess/detail/workaround.hpp> | |
32 | |
33 #include <pthread.h> | |
34 #include <errno.h> | |
35 #include <boost/interprocess/sync/posix/pthread_helpers.hpp> | |
36 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp> | |
37 #include <boost/interprocess/detail/posix_time_types_wrk.hpp> | |
38 #include <boost/interprocess/exceptions.hpp> | |
39 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
40 # include <boost/interprocess/detail/os_thread_functions.hpp> | |
41 # include <boost/interprocess/sync/spin/wait.hpp> | |
42 #endif | |
43 #include <boost/assert.hpp> | |
44 | |
45 namespace boost { | |
46 namespace interprocess { | |
47 namespace ipcdetail { | |
48 | |
49 class posix_recursive_mutex | |
50 { | |
51 posix_recursive_mutex(const posix_recursive_mutex &); | |
52 posix_recursive_mutex &operator=(const posix_recursive_mutex &); | |
53 public: | |
54 | |
55 posix_recursive_mutex(); | |
56 ~posix_recursive_mutex(); | |
57 | |
58 void lock(); | |
59 bool try_lock(); | |
60 bool timed_lock(const boost::posix_time::ptime &abs_time); | |
61 void unlock(); | |
62 | |
63 private: | |
64 pthread_mutex_t m_mut; | |
65 }; | |
66 | |
67 inline posix_recursive_mutex::posix_recursive_mutex() | |
68 { | |
69 mutexattr_wrapper mut_attr(true); | |
70 mutex_initializer mut(m_mut, mut_attr); | |
71 mut.release(); | |
72 } | |
73 | |
74 inline posix_recursive_mutex::~posix_recursive_mutex() | |
75 { | |
76 int res = pthread_mutex_destroy(&m_mut); | |
77 BOOST_ASSERT(res == 0);(void)res; | |
78 } | |
79 | |
80 inline void posix_recursive_mutex::lock() | |
81 { | |
82 if (pthread_mutex_lock(&m_mut) != 0) | |
83 throw lock_exception(); | |
84 } | |
85 | |
86 inline bool posix_recursive_mutex::try_lock() | |
87 { | |
88 int res = pthread_mutex_trylock(&m_mut); | |
89 if (!(res == 0 || res == EBUSY)) | |
90 throw lock_exception(); | |
91 return res == 0; | |
92 } | |
93 | |
94 inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time) | |
95 { | |
96 if(abs_time == boost::posix_time::pos_infin){ | |
97 this->lock(); | |
98 return true; | |
99 } | |
100 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
101 | |
102 timespec ts = ptime_to_timespec(abs_time); | |
103 int res = pthread_mutex_timedlock(&m_mut, &ts); | |
104 if (res != 0 && res != ETIMEDOUT) | |
105 throw lock_exception(); | |
106 return res == 0; | |
107 | |
108 #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
109 | |
110 //Obtain current count and target time | |
111 boost::posix_time::ptime now = microsec_clock::universal_time(); | |
112 spin_wait swait; | |
113 do{ | |
114 if(this->try_lock()){ | |
115 break; | |
116 } | |
117 now = microsec_clock::universal_time(); | |
118 | |
119 if(now >= abs_time){ | |
120 return false; | |
121 } | |
122 // relinquish current time slice | |
123 swait.yield(); | |
124 }while (true); | |
125 return true; | |
126 | |
127 #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS | |
128 } | |
129 | |
130 inline void posix_recursive_mutex::unlock() | |
131 { | |
132 int res = 0; | |
133 res = pthread_mutex_unlock(&m_mut); | |
134 BOOST_ASSERT(res == 0); (void)res; | |
135 } | |
136 | |
137 } //namespace ipcdetail { | |
138 } //namespace interprocess { | |
139 } //namespace boost { | |
140 | |
141 #include <boost/interprocess/detail/config_end.hpp> | |
142 | |
143 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP |