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