From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期一, 10 五月 2021 16:05:28 +0800 Subject: [PATCH] test lock code. --- src/bh_util.h | 32 +++++++++++++++++++++++++------- 1 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/bh_util.h b/src/bh_util.h index e3ab70b..a1c0d84 100644 --- a/src/bh_util.h +++ b/src/bh_util.h @@ -18,6 +18,7 @@ #ifndef BH_UTIL_SOXWOK67 #define BH_UTIL_SOXWOK67 +#include "macro.h" #include <functional> #include <mutex> #include <stdint.h> @@ -143,12 +144,29 @@ } }; -// macro helper -#define JOIN_IMPL(a, b) a##b -#define JOIN(a, b) JOIN_IMPL(a, b) -// defer function / lambda. -#define DEFERF(func) ExitCall JOIN(defer_, __LINE__)(func) -// defer simple expression -#define DEFER1(expr) DEFERF([&]() { expr; }) +template <class T, class Tag> +class StaticDataRef +{ + typedef T *Ptr; + static inline Ptr &ptr() + { + static Ptr sp(nullptr); + return sp; + } + +protected: + static inline T &GetData() + { + if (!ptr()) { throw std::string("Must set ShmMsg shm before use!"); } + return *ptr(); + } + +public: + static bool SetData(T &t) + { + auto Bind = [&]() { ptr() = &t; return true; }; + return ptr() ? false : Bind(); + } +}; #endif /* end of include guard: BH_UTIL_SOXWOK67 */ -- Gitblit v1.8.0