comparison DEPENDENCIES/generic/include/boost/interprocess/sync/spin/mutex.hpp @ 101:c530137014c0

Update Boost headers (1.58.0)
author Chris Cannam
date Mon, 07 Sep 2015 11:12:49 +0100
parents 2665513ce2d3
children
comparison
equal deleted inserted replaced
100:793467b5e61c 101:c530137014c0
9 ////////////////////////////////////////////////////////////////////////////// 9 //////////////////////////////////////////////////////////////////////////////
10 10
11 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP 11 #ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
12 #define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP 12 #define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
13 13
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200) 14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
15 # pragma once 19 # pragma once
16 #endif 20 #endif
17 21
18 #include <boost/interprocess/detail/config_begin.hpp> 22 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp> 23 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/detail/posix_time_types_wrk.hpp> 24 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
21 #include <boost/assert.hpp> 25 #include <boost/assert.hpp>
22 #include <boost/interprocess/detail/atomic.hpp> 26 #include <boost/interprocess/detail/atomic.hpp>
23 #include <boost/cstdint.hpp> 27 #include <boost/cstdint.hpp>
24 #include <boost/interprocess/detail/os_thread_functions.hpp> 28 #include <boost/interprocess/detail/os_thread_functions.hpp>
25 #include <boost/interprocess/sync/spin/wait.hpp> 29 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
26 30
27 namespace boost { 31 namespace boost {
28 namespace interprocess { 32 namespace interprocess {
29 namespace ipcdetail { 33 namespace ipcdetail {
30 34
58 { 62 {
59 //Trivial destructor 63 //Trivial destructor
60 } 64 }
61 65
62 inline void spin_mutex::lock(void) 66 inline void spin_mutex::lock(void)
63 { 67 { return ipcdetail::try_based_lock(*this); }
64 spin_wait swait;
65 do{
66 boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
67
68 if (m_s == 1 && prev_s == 0){
69 break;
70 }
71 // relinquish current timeslice
72 swait.yield();
73 }while (true);
74 }
75 68
76 inline bool spin_mutex::try_lock(void) 69 inline bool spin_mutex::try_lock(void)
77 { 70 {
78 boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0); 71 boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
79 return m_s == 1 && prev_s == 0; 72 return m_s == 1 && prev_s == 0;
80 } 73 }
81 74
82 inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time) 75 inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
83 { 76 { return ipcdetail::try_based_timed_lock(*this, abs_time); }
84 if(abs_time == boost::posix_time::pos_infin){
85 this->lock();
86 return true;
87 }
88 //Obtain current count and target time
89 boost::posix_time::ptime now = microsec_clock::universal_time();
90
91 spin_wait swait;
92 do{
93 if(this->try_lock()){
94 break;
95 }
96 now = microsec_clock::universal_time();
97
98 if(now >= abs_time){
99 return false;
100 }
101 // relinquish current time slice
102 swait.yield();
103 }while (true);
104
105 return true;
106 }
107 77
108 inline void spin_mutex::unlock(void) 78 inline void spin_mutex::unlock(void)
109 { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); } 79 { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
110 80
111 } //namespace ipcdetail { 81 } //namespace ipcdetail {