From f11904132fd17af42063bf4d1fb37b9286d000d9 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sat, 24 Feb 2024 16:20:55 +0800 Subject: [PATCH] feat add circle_queue --- include/sled/log/log.h | 22 ++++--- include/sled/network/physical_socket_server.h | 4 +- include/sled/queue/circle_queue.h | 63 +++++++++++++++++++ src/network/physical_socket_server.cc | 19 +++--- src/time_utils.cc | 2 +- 5 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 include/sled/queue/circle_queue.h diff --git a/include/sled/log/log.h b/include/sled/log/log.h index a2a30f5..d83f4ed 100644 --- a/include/sled/log/log.h +++ b/include/sled/log/log.h @@ -32,10 +32,10 @@ void Log(LogLevel level, // sled::Log(level, tag, fmt, __FILE__, __FUNCTION__, __VA_ARGS__) #define _SLOG(level, tag, fmt_str, ...) \ - sled::Log(level, tag, fmt::format(fmt_str, #__VA_ARGS__).c_str(), \ + sled::Log(level, tag, fmt::format(fmt_str, ##__VA_ARGS__).c_str(), \ __FILE__, __LINE__, __FUNCTION__) -#define SLOG(level, tag, fmt, ...) _SLOG(level, tag, fmt, #__VA_ARGS__) +#define SLOG(level, tag, fmt, ...) _SLOG(level, tag, fmt, ##__VA_ARGS__) #define SLOG_TRACE(tag, fmt, ...) \ SLOG(sled::LogLevel::kTrace, tag, fmt, __VA_ARGS__) #define SLOG_INFO(tag, fmt, ...) \ @@ -75,12 +75,18 @@ void Log(LogLevel level, #define LOGF_IF(cond, tag, fmt, ...) \ SLOG_IF(cond, sled::LogLevel::kFatal, tag, fmt, __VA_ARGS__) -#define LOGV(tag, fmt, ...) SLOG(sled::LogLevel::kTrace, tag, fmt, #__VA_ARGS__) -#define LOGD(tag, fmt, ...) SLOG(sled::LogLevel::kDebug, tag, fmt, #__VA_ARGS__) -#define LOGI(tag, fmt, ...) SLOG(sled::LogLevel::kInfo, tag, fmt, #__VA_ARGS__) +#define LOGV(tag, fmt, ...) \ + SLOG(sled::LogLevel::kTrace, tag, fmt, ##__VA_ARGS__) +#define LOGD(tag, fmt, ...) \ + SLOG(sled::LogLevel::kDebug, tag, fmt, ##__VA_ARGS__) +#define LOGI(tag, fmt, ...) SLOG(sled::LogLevel::kInfo, tag, fmt, ##__VA_ARGS__) #define LOGW(tag, fmt, ...) \ - SLOG(sled::LogLevel::kWarning, tag, fmt, #__VA_ARGS__) -#define LOGE(tag, fmt, ...) SLOG(sled::LogLevel::kError, tag, fmt, #__VA_ARGS__) -#define LOGF(tag, fmt, ...) SLOG(sled::LogLevel::kFatal, tag, fmt, #__VA_ARGS__) + SLOG(sled::LogLevel::kWarning, tag, fmt, ##__VA_ARGS__) +#define LOGE(tag, fmt, ...) \ + SLOG(sled::LogLevel::kError, tag, fmt, ##__VA_ARGS__) +#define LOGF(tag, fmt, ...) \ + SLOG(sled::LogLevel::kFatal, tag, fmt, ##__VA_ARGS__) + +#define ASSERT(cond, fmt, ...) SLOG_ASSERT(cond, "ASSERT", fmt, ##__VA_ARGS__); #endif// LOG_H diff --git a/include/sled/network/physical_socket_server.h b/include/sled/network/physical_socket_server.h index 2b54a17..c87eca2 100644 --- a/include/sled/network/physical_socket_server.h +++ b/include/sled/network/physical_socket_server.h @@ -53,9 +53,9 @@ public: private: static const int kForeverMs = -1; - static int ToCmsWait(TimeDelta max_wait_duration); + static int ToCusWait(TimeDelta max_wait_duration); - bool WaitSelect(int cmsWait, bool process_io); + bool WaitSelect(int64_t cusWait, bool process_io); uint64_t next_dispatcher_key_ = 0; std::unordered_map dispatcher_by_key_; diff --git a/include/sled/queue/circle_queue.h b/include/sled/queue/circle_queue.h new file mode 100644 index 0000000..5cf41a8 --- /dev/null +++ b/include/sled/queue/circle_queue.h @@ -0,0 +1,63 @@ +/** + * @file : circle_queue + * @created : 星期六 2 24, 2024 16:06:23 CST + * @license : MIT + **/ + +#ifndef CIRCLE_QUEUE_H +#define CIRCLE_QUEUE_H + +#include "sled/log/log.h" +#include + +namespace sled { + +template +class CircleQueue { + static_assert(LEN > 0, "LEN should be greater than 0"); + +public: + void Push(T &&val) + { + ASSERT(size() < LEN, "queue is full"); + queue_.get(tail_) = std::move(val); + ++tail_; + } + + void Push(const T &val) + { + ASSERT(size() < LEN, "queue is full"); + queue_.get(tail_) = val; + ++tail_; + } + + T &Front() + { + ASSERT(!empty()); + return queue_.get(head_); + } + + void Pop() + { + ASSERT(!empty()); + head_ = (head_ + 1) % (LEN + 1); + } + + size_t size() const + { + return tail_ >= head_ ? tail_ - head_ : (LEN + 1) - (head_ - tail_); + } + + bool empty() const { return (tail_ + 1) % (LEN + 1) == head_; } + + bool capacity() const { return LEN; } + +private: + std::array queue_; + size_t head_ = 0; + size_t tail_ = 0; +}; + +}// namespace sled + +#endif// CIRCLE_QUEUE_H diff --git a/src/network/physical_socket_server.cc b/src/network/physical_socket_server.cc index a36d218..d107b73 100644 --- a/src/network/physical_socket_server.cc +++ b/src/network/physical_socket_server.cc @@ -1,4 +1,5 @@ #include "sled/network/physical_socket_server.h" +#include "sled/log/log.h" #include "sled/network/async_resolver.h" #include "sled/network/socket.h" #include "sled/synchronization/event.h" @@ -155,11 +156,11 @@ PhysicalSocketServer::Update(Dispatcher *pdispatcher) } int -PhysicalSocketServer::ToCmsWait(TimeDelta max_wait_duration) +PhysicalSocketServer::ToCusWait(TimeDelta max_wait_duration) { return max_wait_duration == Event::kForever ? kForeverMs - : max_wait_duration.RoundUpTo(TimeDelta::Millis(1)).ms(); + : max_wait_duration.RoundUpTo(TimeDelta::Micros(1)).us(); } bool @@ -167,9 +168,9 @@ PhysicalSocketServer::Wait(TimeDelta max_wait_duration, bool process_io) { ScopedSetTrue s(&waiting_); - const int cmsWait = ToCmsWait(max_wait_duration); + const int64_t cusWait = ToCusWait(max_wait_duration); - return WaitSelect(cmsWait, process_io); + return WaitSelect(cusWait, process_io); } static void @@ -216,16 +217,16 @@ ProcessEvents(Dispatcher *pdispatcher, } bool -PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) +PhysicalSocketServer::WaitSelect(int64_t cusWait, bool process_io) { struct timeval *ptvWait = nullptr; struct timeval tvWait; int64_t stop_us; - if (cmsWait != kForeverMs) { - tvWait.tv_sec = cmsWait / 1000; - tvWait.tv_usec = (cmsWait % 1000) * 1000; + if (cusWait != kForeverMs) { + tvWait.tv_sec = cusWait / sled::kNumMicrosecsPerSec; + tvWait.tv_usec = (cusWait % sled::kNumMicrosecsPerSec); ptvWait = &tvWait; - stop_us = TimeMicros() + cmsWait * 1000; + stop_us = TimeMicros() + cusWait; } fd_set fdsRead; diff --git a/src/time_utils.cc b/src/time_utils.cc index 479eceb..98b46bd 100644 --- a/src/time_utils.cc +++ b/src/time_utils.cc @@ -22,13 +22,13 @@ TimeMillis() int64_t TimeMicros() { - if (g_clock) { return g_clock->TimeNanos(); } return TimeNanos() / kNumNanosecsPerMicrosec; } int64_t TimeNanos() { + if (g_clock) { return g_clock->TimeNanos(); } return SystemTimeNanos(); }