USRP Hardware Driver and USRP Manual  Version: 3.11.0.HEAD-0-g13c32cef
UHD and USRP Manual
atomic.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2012-2013,2016-2017 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #ifndef INCLUDED_UHD_UTILS_ATOMIC_HPP
9 #define INCLUDED_UHD_UTILS_ATOMIC_HPP
10 
11 #include <uhd/config.hpp>
12 #include <uhd/types/time_spec.hpp>
13 #include <boost/thread/thread.hpp>
14 #include <atomic>
15 
16 namespace uhd{
17 
26  template<typename T>
28  std::atomic<T> &cond,
29  const T value,
30  const double timeout
31  ){
32  if (cond == value) return true;
33  const time_spec_t exit_time = time_spec_t::get_system_time() + time_spec_t(timeout);
34  while (cond != value) {
35  if (time_spec_t::get_system_time() > exit_time) {
36  return false;
37  }
38  boost::this_thread::interruption_point();
39  boost::this_thread::yield();
40  }
41  return true;
42  }
43 
50  public:
52  this->release();
53  }
54 
55  UHD_INLINE void release(void){
56  _locked = false;
57  }
58 
59  UHD_INLINE bool claim_with_wait(const double timeout){
60  if (spin_wait_with_timeout(_locked, false, timeout)){
61  _locked = true;
62  return true;
63  }
64  return false;
65  }
66 
67  private:
68  std::atomic<bool> _locked;
69  };
70 
71 } //namespace uhd
72 
73 #endif /* INCLUDED_UHD_UTILS_ATOMIC_HPP */
UHD_INLINE void release(void)
Definition: atomic.hpp:55
Definition: time_spec.hpp:29
static time_spec_t get_system_time(void)
Definition: build_info.hpp:14
UHD_INLINE bool claim_with_wait(const double timeout)
Definition: atomic.hpp:59
simple_claimer(void)
Definition: atomic.hpp:51
#define UHD_INLINE
Definition: config.h:53
Definition: atomic.hpp:49
UHD_INLINE bool spin_wait_with_timeout(std::atomic< T > &cond, const T value, const double timeout)
Definition: atomic.hpp:27