From 88c56243345966d1384df0a2890e326290b97b9e Mon Sep 17 00:00:00 2001 From: azw Date: Wed, 27 Sep 2023 14:18:05 +0000 Subject: [PATCH] 1 --- a8/queue.cc | 30 ++++++++++++++++++++++++++++++ a8/queue.h | 23 +++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 a8/queue.cc create mode 100644 a8/queue.h diff --git a/a8/queue.cc b/a8/queue.cc new file mode 100644 index 0000000..cec712e --- /dev/null +++ b/a8/queue.cc @@ -0,0 +1,30 @@ +#include + +namespace a8 +{ + + Queue::Queue() + { + INIT_LIST_HEAD(&msg_list_); + INIT_LIST_HEAD(&work_list_); + } + + void Queue::Push(list_head* node) + { + msg_mutex_.lock(); + list_add_tail(node, &msg_list_); + msg_mutex_.unlock(); + } + + void Queue::Fetch() + { + if (list_empty(&work_list_)) { + msg_mutex_.lock(); + if (!list_empty(&msg_list_)) { + list_replace_init(&msg_list_, &work_list_); + } + msg_mutex_.unlock(); + } + } + +} diff --git a/a8/queue.h b/a8/queue.h new file mode 100644 index 0000000..f0b16b4 --- /dev/null +++ b/a8/queue.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace a8 +{ + + class Queue + { + public: + + Queue(); + list_head* GetWorkList() { return &work_list_; } + void Push(list_head* node); + void Fetch(); + + private: + list_head msg_list_; + list_head work_list_; + std::mutex msg_mutex_; + }; + +}