/usr/include/uhd/utils/atomic.hpp is in libuhd-dev 3.5.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | //
// Copyright 2012-2013 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef INCLUDED_UHD_UTILS_ATOMIC_HPP
#define INCLUDED_UHD_UTILS_ATOMIC_HPP
#include <uhd/config.hpp>
#include <uhd/types/time_spec.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/version.hpp>
#if BOOST_VERSION >= 104800
# define BOOST_IPC_DETAIL boost::interprocess::ipcdetail
#else
# define BOOST_IPC_DETAIL boost::interprocess::detail
#endif
namespace uhd{
//! A 32-bit integer that can be atomically accessed
class UHD_API atomic_uint32_t{
public:
//! Create a new atomic 32-bit integer, initialized to zero
UHD_INLINE atomic_uint32_t(void){
this->write(0);
}
//! Compare with cmp, swap with newval if same, return old value
UHD_INLINE boost::uint32_t cas(boost::uint32_t newval, boost::uint32_t cmp){
return BOOST_IPC_DETAIL::atomic_cas32(&_num, newval, cmp);
}
//! Sets the atomic integer to a new value
UHD_INLINE void write(const boost::uint32_t newval){
BOOST_IPC_DETAIL::atomic_write32(&_num, newval);
}
//! Gets the current value of the atomic integer
UHD_INLINE boost::uint32_t read(void){
return BOOST_IPC_DETAIL::atomic_read32(&_num);
}
//! Increment by 1 and return the old value
UHD_INLINE boost::uint32_t inc(void){
return BOOST_IPC_DETAIL::atomic_inc32(&_num);
}
//! Decrement by 1 and return the old value
UHD_INLINE boost::uint32_t dec(void){
return BOOST_IPC_DETAIL::atomic_dec32(&_num);
}
private: volatile boost::uint32_t _num;
};
/*!
* A reusable barrier to sync multiple threads.
* All threads spin on wait() until count is reset.
*/
class UHD_API reusable_barrier{
public:
//! Resize the barrier for N threads
void resize(const size_t size){
_size = size;
}
/*!
* Force the barrier wait to throw a boost::thread_interrupted
* The threads were not getting the interruption_point on windows.
*/
void interrupt(void)
{
_done.inc();
}
//! Wait on the barrier condition
UHD_INLINE void wait(void)
{
if (_size == 1) return;
//entry barrier with condition variable
_entry_counter.inc();
_entry_counter.cas(0, _size);
boost::mutex::scoped_lock lock(_mutex);
while (_entry_counter.read() != 0)
{
this->check_interrupt();
_cond.timed_wait(lock, boost::posix_time::milliseconds(1));
}
lock.unlock(); //unlock before notify
_cond.notify_one();
//exit barrier to ensure known condition of entry count
_exit_counter.inc();
_exit_counter.cas(0, _size);
while (_exit_counter.read() != 0) this->check_interrupt();
}
//! Wait on the barrier condition
UHD_INLINE void wait_others(void)
{
while (_entry_counter.read() != (_size-1)) this->check_interrupt();
}
private:
size_t _size;
atomic_uint32_t _entry_counter;
atomic_uint32_t _exit_counter;
atomic_uint32_t _done;
boost::mutex _mutex;
boost::condition_variable _cond;
UHD_INLINE void check_interrupt(void)
{
if (_done.read() != 0) throw boost::thread_interrupted();
boost::this_thread::interruption_point();
boost::this_thread::yield();
}
};
/*!
* Spin-wait on a condition with a timeout.
* \param cond an atomic variable to compare
* \param value compare to atomic for true/false
* \param timeout the timeout in seconds
* \return true for cond == value, false for timeout
*/
UHD_INLINE bool spin_wait_with_timeout(
atomic_uint32_t &cond,
boost::uint32_t value,
const double timeout
){
if (cond.read() == value) return true;
const time_spec_t exit_time = time_spec_t::get_system_time() + time_spec_t(timeout);
while (cond.read() != value){
if (time_spec_t::get_system_time() > exit_time) return false;
boost::this_thread::interruption_point();
boost::this_thread::yield();
}
return true;
}
/*!
* Claimer class to provide synchronization for multi-thread access.
* Claiming enables buffer classes to be used with a buffer queue.
*/
class simple_claimer{
public:
simple_claimer(void){
this->release();
}
UHD_INLINE void release(void){
_locked.write(0);
}
UHD_INLINE bool claim_with_wait(const double timeout){
if (spin_wait_with_timeout(_locked, 0, timeout)){
_locked.write(1);
return true;
}
return false;
}
private:
atomic_uint32_t _locked;
};
} //namespace uhd
#endif /* INCLUDED_UHD_UTILS_ATOMIC_HPP */
|