Split Linux tap receive into two threads: one reader, one processor.
This commit is contained in:
parent
5282e06fd4
commit
2da162bed7
3 changed files with 171 additions and 114 deletions
|
@ -18,8 +18,8 @@
|
|||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
|
||||
#include "Thread.hpp"
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
|
@ -67,7 +67,8 @@ public:
|
|||
inline bool get(T &value)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
if (!r) return false;
|
||||
if (!r)
|
||||
return false;
|
||||
while (q.empty()) {
|
||||
c.wait(lock);
|
||||
if (!r) {
|
||||
|
@ -81,6 +82,16 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
inline std::vector<T> drain()
|
||||
{
|
||||
std::vector<T> v;
|
||||
while (!q.empty()) {
|
||||
v.push_back(q.front());
|
||||
q.pop();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
enum TimedWaitResult
|
||||
{
|
||||
OK,
|
||||
|
@ -92,7 +103,8 @@ public:
|
|||
{
|
||||
const std::chrono::milliseconds ms2{ms};
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
if (!r) return STOP;
|
||||
if (!r)
|
||||
return STOP;
|
||||
while (q.empty()) {
|
||||
if (c.wait_for(lock,ms2) == std::cv_status::timeout)
|
||||
return ((r) ? TIMED_OUT : STOP);
|
||||
|
@ -105,10 +117,10 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
volatile bool r;
|
||||
std::queue<T> q;
|
||||
mutable std::mutex m;
|
||||
mutable std::condition_variable c,gc;
|
||||
std::atomic_bool r;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue