-
Notifications
You must be signed in to change notification settings - Fork 10
/
EnumerateCache.cpp
68 lines (60 loc) · 1.55 KB
/
EnumerateCache.cpp
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
// Copyright (c) 2017-2017 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Config.hpp>
#include <SoapySDR/Device.hpp>
#include <condition_variable>
#include <chrono>
#include <thread>
#include <atomic>
#include <mutex>
/*!
* A singleton thread that polls for device enumeration.
* The device enumeration is fetched in the background
* so that the overlay operation does not block for devices.
*/
class SDRBlockBgEnumerator
{
public:
SDRBlockBgEnumerator(void):
_done(false),
_cache(SoapySDR::Device::enumerate()), //force a synchronous read before backgrounding
_bgThread(&SDRBlockBgEnumerator::pollingLoop, this)
{
return;
}
~SDRBlockBgEnumerator(void)
{
_done = true;
_cv.notify_one();
_bgThread.join();
}
SoapySDR::KwargsList getCache(void)
{
std::lock_guard<std::mutex> lock(_mutex);
return _cache;
}
private:
void pollingLoop(void)
{
std::unique_lock<std::mutex> lock(_mutex);
while (not _done)
{
_cv.wait_for(lock, std::chrono::milliseconds(3000));
if (_done) break;
lock.unlock();
auto result = SoapySDR::Device::enumerate();
lock.lock();
_cache = result;
}
}
std::mutex _mutex;
std::condition_variable _cv;
std::atomic<bool> _done;
SoapySDR::KwargsList _cache;
std::thread _bgThread;
};
SoapySDR::KwargsList cachedEnumerate(void)
{
static SDRBlockBgEnumerator instance;
return instance.getCache();
}