diff options
author | Ciro Nishiguchi <ciro.nishiguchi@ni.com> | 2019-10-23 09:36:27 -0500 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-11-26 12:21:32 -0800 |
commit | 1cd874911db0d4c0463264edd0d46067d072eccb (patch) | |
tree | 24c99f0d0ef05041614c0795e75067b9bae50ead /host/lib/include/uhdlib/utils | |
parent | 3e2cceeaed7c3a2a70318dab695d6adb9ee5602e (diff) | |
download | uhd-1cd874911db0d4c0463264edd0d46067d072eccb.tar.gz uhd-1cd874911db0d4c0463264edd0d46067d072eccb.tar.bz2 uhd-1cd874911db0d4c0463264edd0d46067d072eccb.zip |
rfnoc: Split up offload I/O service into multiple files
Diffstat (limited to 'host/lib/include/uhdlib/utils')
-rw-r--r-- | host/lib/include/uhdlib/utils/semaphore.hpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/host/lib/include/uhdlib/utils/semaphore.hpp b/host/lib/include/uhdlib/utils/semaphore.hpp new file mode 100644 index 000000000..ae77ed102 --- /dev/null +++ b/host/lib/include/uhdlib/utils/semaphore.hpp @@ -0,0 +1,71 @@ +// +// Copyright 2019 Ettus Research, a National Instruments brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <condition_variable> +#include <chrono> +#include <mutex> + +#ifndef INCLUDED_UHDLIB_UTILS_SEMAPHORE_HPP +#define INCLUDED_UHDLIB_UTILS_SEMAPHORE_HPP + +namespace uhd { + +/*! + * A sempahore built using std::condition_variable + */ +class semaphore +{ +public: + void notify() + { + std::unique_lock<std::mutex> lock(_cv_mutex); + _count++; + _cv.notify_one(); + } + + void wait() + { + std::unique_lock<std::mutex> lock(_cv_mutex); + _cv.wait(lock, [this]() { return this->_count != 0; }); + _count--; + } + + bool try_wait() + { + std::unique_lock<std::mutex> lock(_cv_mutex); + if (_count != 0) { + _count--; + return true; + } + return false; + } + + bool wait_for(size_t timeout_ms) + { + std::chrono::milliseconds timeout(timeout_ms); + std::unique_lock<std::mutex> lock(_cv_mutex); + if (_cv.wait_for(lock, timeout, [this]() { return this->_count != 0; })) { + _count--; + return true; + } + return false; + } + + size_t count() + { + std::unique_lock<std::mutex> lock(_cv_mutex); + return _count; + } + +private: + std::condition_variable _cv; + std::mutex _cv_mutex; + size_t _count = 0; +}; + +} // namespace uhd + +#endif /* INCLUDED_UHDLIB_UTILS_SEMAPHORE_HPP */ |