feat add circle_queue
This commit is contained in:
parent
37cbad0b99
commit
f11904132f
@ -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
|
||||
|
@ -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<uint64_t, Dispatcher *> dispatcher_by_key_;
|
||||
|
63
include/sled/queue/circle_queue.h
Normal file
63
include/sled/queue/circle_queue.h
Normal file
@ -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 <array>
|
||||
|
||||
namespace sled {
|
||||
|
||||
template<typename T, size_t LEN>
|
||||
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<T, LEN + 1> queue_;
|
||||
size_t head_ = 0;
|
||||
size_t tail_ = 0;
|
||||
};
|
||||
|
||||
}// namespace sled
|
||||
|
||||
#endif// CIRCLE_QUEUE_H
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user