Split Linux tap receive into two threads: one reader, one processor.

This commit is contained in:
Adam Ierymenko 2020-11-16 21:40:25 -05:00
parent 5282e06fd4
commit 2da162bed7
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
3 changed files with 171 additions and 114 deletions

View file

@ -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