Added callback for events.

This commit is contained in:
2026-04-10 23:49:29 +02:00
parent 82f58bc24b
commit 8e5381fda2
64 changed files with 148 additions and 38 deletions

72
memqueue.h Normal file
View File

@@ -0,0 +1,72 @@
#ifndef MEMQUEUE_H
#define MEMQUEUE_H
#include <string>
#include <mutex>
class MemQueueItem
{
public:
MemQueueItem *next;
std::string _event;
int _wv;
};
class MemQueue
{
private:
std::mutex _mutex;
int _depth;
MemQueueItem *_first;
MemQueueItem *_last;
public:
void enqueue(int w, std::string s) {
_mutex.lock();
MemQueueItem *item = new MemQueueItem();
item->next = nullptr;
item->_event = s;
item->_wv = w;
if (_last == nullptr) {
_last = item;
_first = item;
} else {
_last->next = item;
_last = item;
}
_depth += 1;
_mutex.unlock();
}
bool dequeue(int &wv, std::string &s) {
_mutex.lock();
if (_first == nullptr) {
_mutex.unlock();
return false;
}
MemQueueItem *item = _first;
_first = _first->next;
if (_first == nullptr) {
_last = nullptr;
}
s = item->_event;
wv = item->_wv;
free(item);
_depth -= 1;
_mutex.unlock();
return true;
}
int depth() {
return _depth;
}
public:
MemQueue() {
_depth = 0;
_first = nullptr;
_last = nullptr;
}
};
#endif // MEMQUEUE_H