From cab831748a2a9cc18b7f18f3b5e14a4374b7ab68 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期一, 17 五月 2021 18:34:26 +0800 Subject: [PATCH] socket send using abs addr, avoid shm find by id. --- src/robust.h | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/robust.h b/src/robust.h index d2d94e9..c70e2fe 100644 --- a/src/robust.h +++ b/src/robust.h @@ -19,6 +19,7 @@ #ifndef ROBUST_Q31RCWYU #define ROBUST_Q31RCWYU +#include "bh_util.h" #include "log.h" #include <atomic> #include <chrono> @@ -37,8 +38,6 @@ using namespace std::chrono; using namespace std::chrono_literals; -constexpr uint64_t MaskBits(int nbits) { return (uint64_t(1) << nbits) - 1; } - void QuickSleep(); class CasMutex @@ -86,6 +85,8 @@ class NullMutex { public: + template <class... T> + explicit NullMutex(T &&...t) {} // easy test. bool try_lock() { return true; } void lock() {} void unlock() {} @@ -97,7 +98,7 @@ public: typedef uint64_t id_t; FMutex(id_t id) : - id_(id), fd_(Open(id_)) + id_(id), fd_(Open(id_)), count_(0) { if (fd_ == -1) { throw "error create mutex!"; } } @@ -113,11 +114,12 @@ mkdir(dir.c_str(), 0777); return dir + "/fm_" + std::to_string(id); } - static int Open(id_t id) { return open(GetPath(id).c_str(), O_CREAT | O_RDWR, 0666); } + static int Open(id_t id) { return open(GetPath(id).c_str(), O_CREAT | O_RDONLY, 0666); } static int Close(int fd) { return close(fd); } id_t id_; int fd_; std::mutex mtx_; + std::atomic<int32_t> count_; }; union semun { @@ -132,17 +134,11 @@ { public: SemMutex(key_t key) : - key_(key), sem_id_(semget(key, 1, 0666 | IPC_CREAT)) + key_(key), sem_id_(semget(key, 1, 0666)) { if (sem_id_ == -1) { throw "error create semaphore."; } - union semun init_val; - init_val.val = 1; - semctl(sem_id_, 0, SETVAL, init_val); } - ~SemMutex() - { - // semctl(sem_id_, 0, IPC_RMID, semun{}); - } + ~SemMutex() {} bool try_lock() { @@ -312,5 +308,36 @@ AData buf[capacity]; }; +template <class Int> +class AtomicQueue<0, Int> +{ + typedef Int Data; + typedef std::atomic<Data> AData; + static_assert(sizeof(Data) == sizeof(AData)); + +public: + AtomicQueue() { memset(this, 0, sizeof(*this)); } + bool push(const Data d, bool try_more = false) + { + auto cur = buf.load(); + return Empty(cur) && buf.compare_exchange_strong(cur, Enc(d)); + } + bool pop(Data &d, bool try_more = false) + { + Data cur = buf.load(); + bool r = !Empty(cur) && buf.compare_exchange_strong(cur, 0); + if (r) { d = Dec(cur); } + return r; + } + uint32_t head() const { return 0; } + uint32_t tail() const { return 0; } + +private: + static inline bool Empty(const Data d) { return (d & 1) == 0; } // lowest bit 1 means data ok. + static inline Data Enc(const Data d) { return (d << 1) | 1; } // lowest bit 1 means data ok. + static inline Data Dec(const Data d) { return d >> 1; } // lowest bit 1 means data ok. + AData buf; +}; + } // namespace robust #endif // end of include guard: ROBUST_Q31RCWYU -- Gitblit v1.8.0