18 #ifndef INCLUDED_UHD_UTILS_ATOMIC_HPP 19 #define INCLUDED_UHD_UTILS_ATOMIC_HPP 23 #include <boost/thread/thread.hpp> 24 #include <boost/thread/mutex.hpp> 25 #include <boost/thread/condition_variable.hpp> 26 #include <boost/interprocess/detail/atomic.hpp> 28 #include <boost/version.hpp> 29 #if BOOST_VERSION >= 104800 30 # define BOOST_IPC_DETAIL boost::interprocess::ipcdetail 32 # define BOOST_IPC_DETAIL boost::interprocess::detail 47 UHD_INLINE boost::uint32_t
cas(boost::uint32_t newval, boost::uint32_t cmp){
48 return BOOST_IPC_DETAIL::atomic_cas32(&_num, newval, cmp);
53 BOOST_IPC_DETAIL::atomic_write32(&_num, newval);
58 return BOOST_IPC_DETAIL::atomic_read32(&_num);
63 return BOOST_IPC_DETAIL::atomic_inc32(&_num);
68 return BOOST_IPC_DETAIL::atomic_dec32(&_num);
71 private:
volatile boost::uint32_t _num;
102 if (_size == 1)
return;
105 _entry_counter.inc();
106 _entry_counter.cas(0, _size);
107 boost::mutex::scoped_lock lock(_mutex);
108 while (_entry_counter.read() != 0)
110 this->check_interrupt();
111 _cond.timed_wait(lock, boost::posix_time::milliseconds(1));
118 _exit_counter.cas(0, _size);
119 while (_exit_counter.read() != 0) this->check_interrupt();
125 while (_entry_counter.read() != (_size-1)) this->check_interrupt();
134 boost::condition_variable _cond;
138 if (_done.
read() != 0)
throw boost::thread_interrupted();
139 boost::this_thread::interruption_point();
140 boost::this_thread::yield();
153 boost::uint32_t value,
156 if (cond.
read() == value)
return true;
158 while (cond.
read() != value){
160 boost::this_thread::interruption_point();
161 boost::this_thread::yield();
UHD_INLINE boost::uint32_t read(void)
Gets the current value of the atomic integer.
Definition: atomic.hpp:57
UHD_INLINE void wait(void)
Wait on the barrier condition.
Definition: atomic.hpp:100
UHD_INLINE void release(void)
Definition: atomic.hpp:176
Definition: atomic.hpp:78
Definition: time_spec.hpp:39
A 32-bit integer that can be atomically accessed.
Definition: atomic.hpp:38
UHD_INLINE void wait_others(void)
Wait on the barrier condition.
Definition: atomic.hpp:123
UHD_INLINE bool spin_wait_with_timeout(atomic_uint32_t &cond, boost::uint32_t value, const double timeout)
Definition: atomic.hpp:151
static time_spec_t get_system_time(void)
#define UHD_API
Definition: config.hpp:79
Definition: convert.hpp:28
UHD_INLINE boost::uint32_t cas(boost::uint32_t newval, boost::uint32_t cmp)
Compare with cmp, swap with newval if same, return old value.
Definition: atomic.hpp:47
UHD_INLINE bool claim_with_wait(const double timeout)
Definition: atomic.hpp:180
#define UHD_INLINE
Definition: config.hpp:69
UHD_INLINE atomic_uint32_t(void)
Create a new atomic 32-bit integer, initialized to zero.
Definition: atomic.hpp:42
UHD_INLINE void write(const boost::uint32_t newval)
Sets the atomic integer to a new value.
Definition: atomic.hpp:52
UHD_INLINE boost::uint32_t inc(void)
Increment by 1 and return the old value.
Definition: atomic.hpp:62
simple_claimer(void)
Definition: atomic.hpp:172
UHD_INLINE boost::uint32_t dec(void)
Decrement by 1 and return the old value.
Definition: atomic.hpp:67
void resize(const size_t size)
Resize the barrier for N threads.
Definition: atomic.hpp:86
reusable_barrier(const size_t size)
Definition: atomic.hpp:83
Definition: atomic.hpp:170
reusable_barrier()
Definition: atomic.hpp:81
void interrupt(void)
Definition: atomic.hpp:94