Static Webview - RTC - UDP - TCP - WSS
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "lockfree_queue.h"
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
@ -14,61 +15,7 @@ namespace EngineManager {
|
||||
|
||||
// thread communication
|
||||
template<typename T>
|
||||
class LockFreeQueue {
|
||||
private:
|
||||
struct Node {
|
||||
std::atomic<T*> data{nullptr};
|
||||
std::atomic<Node*> next{nullptr};
|
||||
};
|
||||
|
||||
std::atomic<Node*> head_{nullptr};
|
||||
std::atomic<Node*> tail_{nullptr};
|
||||
|
||||
public:
|
||||
LockFreeQueue() {
|
||||
Node* dummy = new Node;
|
||||
head_.store(dummy);
|
||||
tail_.store(dummy);
|
||||
}
|
||||
|
||||
~LockFreeQueue() {
|
||||
while (Node* const old_head = head_.load()) {
|
||||
head_.store(old_head->next);
|
||||
delete old_head->data.load();
|
||||
delete old_head;
|
||||
}
|
||||
}
|
||||
|
||||
void push(T item) {
|
||||
Node* new_node = new Node;
|
||||
T* data = new T(std::move(item));
|
||||
new_node->data.store(data);
|
||||
|
||||
Node* prev_tail = tail_.exchange(new_node);
|
||||
prev_tail->next.store(new_node);
|
||||
}
|
||||
|
||||
bool try_pop(T& result) {
|
||||
Node* head = head_.load();
|
||||
Node* next = head->next.load();
|
||||
|
||||
if (next == nullptr) return false;
|
||||
|
||||
T* data = next->data.load();
|
||||
if (data == nullptr) return false;
|
||||
|
||||
result = *data;
|
||||
delete data;
|
||||
head_.store(next);
|
||||
delete head;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
Node* head = head_.load();
|
||||
return (head->next.load() == nullptr);
|
||||
}
|
||||
};
|
||||
using LockFreeQueue = ::LockFreeQueue<T>;
|
||||
|
||||
// thread pool stealing
|
||||
class ThreadPool {
|
||||
|
||||
Reference in New Issue
Block a user