From 2df2fcb68740fd0d24edca1c364dccf874228592 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 13 Mar 2024 21:19:34 +0800 Subject: [PATCH 1/4] feat Add Any --- CMakeLists.txt | 1 + include/sled/any.h | 86 ++++++++++++++++++++++++++++++++++++++++++++++ src/any_test.cc | 4 +++ 3 files changed, 91 insertions(+) create mode 100644 src/any_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 170222d..5becad9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,7 @@ if(SLED_BUILD_TESTS) ) FetchContent_MakeAvailable(googletest) add_executable(sled_tests + src/any_test.cc src/filesystem/path_test.cc src/strings/base64_test.cc src/cleanup_test.cc diff --git a/include/sled/any.h b/include/sled/any.h index 1eabe1d..cfe3aee 100644 --- a/include/sled/any.h +++ b/include/sled/any.h @@ -332,6 +332,92 @@ any_cast(any &&operand) return any_cast(operand); } +class Any final { +public: + inline Any() {} + + inline Any(const Any &other) : value_(other.value_) {} + + inline Any(Any &&other) noexcept : value_(std::move(other.value_)) {} + + template + inline Any(const ValueType &value) : value_(value) + {} + + template + inline Any(ValueType &&value) : value_(std::move(value)) + {} + + // ~Any() noexcept {} + + Any &operator=(const Any &rhs) + { + Any(rhs).swap(*this); + return *this; + } + + Any &operator=(Any &&rhs) noexcept + { + rhs.swap(*this); + Any().swap(rhs); + return *this; + } + + template + Any &operator=(ValueType &&rhs) + { + Any(static_cast(rhs)).swap(*this); + return *this; + } + + template + inline auto Cast() const -> ValueType + { + return any_cast(value_); + } + + template + inline auto Cast() -> ValueType + { + return any_cast(value_); + } + + template + inline auto CastOr(U &&default_value) const -> ValueType + { + try { + return any_cast(value_); + } catch (const bad_any_cast &e) { + return static_cast(default_value); + } + } + + template + inline auto CastOr(U &&default_value) -> ValueType + { + try { + return any_cast(value_); + } catch (const bad_any_cast &e) { + return static_cast(default_value); + } + } + + void Reset() noexcept { Any().swap(*this); } + + bool HasValue() const noexcept { return value_.has_value(); } + + const std::type_info &Type() const noexcept { return value_.type(); } + + Any &swap(Any &rhs) noexcept + { + std::swap(value_, rhs.value_); + return *this; + } + +private: + any value_; +}; + }// namespace sled #endif /* SLED_ANY_H */ diff --git a/src/any_test.cc b/src/any_test.cc new file mode 100644 index 0000000..1bbbc44 --- /dev/null +++ b/src/any_test.cc @@ -0,0 +1,4 @@ +#include +#include + +TEST(Any, Assign) {} From 3ca77d457b17b51b0e40c9d983c6926cf7abf5c0 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 13 Mar 2024 22:09:48 +0800 Subject: [PATCH 2/4] feat add attributes --- include/sled/any.h | 14 ++++++--- include/sled/lang/attributes.h | 5 +++ include/sled/synchronization/mutex.h | 15 ++++----- src/any_test.cc | 46 +++++++++++++++++++++++++++- 4 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 include/sled/lang/attributes.h diff --git a/include/sled/any.h b/include/sled/any.h index cfe3aee..25b89f0 100644 --- a/include/sled/any.h +++ b/include/sled/any.h @@ -345,7 +345,12 @@ public: {} template - inline Any(ValueType &&value) : value_(std::move(value)) + inline Any( + ValueType &&value, + typename std::enable_if::value>::type + * = 0, + typename std::enable_if::value>::type * = 0) + : value_(std::forward(value)) {} // ~Any() noexcept {} @@ -371,13 +376,13 @@ public: } template - inline auto Cast() const -> ValueType + inline auto Cast() const -> ValueType const { return any_cast(value_); } template - inline auto Cast() -> ValueType + inline ValueType Cast() const && { return any_cast(value_); } @@ -414,8 +419,9 @@ public: return *this; } -private: any value_; + +private: }; }// namespace sled diff --git a/include/sled/lang/attributes.h b/include/sled/lang/attributes.h new file mode 100644 index 0000000..6e06b92 --- /dev/null +++ b/include/sled/lang/attributes.h @@ -0,0 +1,5 @@ +#pragma once +#ifndef SLED_LANG_ATTRIBUTES_H +#define SLED_LANG_ATTRIBUTES_H +#define SLED_DEPRECATED __attribute__((deprecated)) +#endif// SLED_LANG_ATTRIBUTES_H diff --git a/include/sled/synchronization/mutex.h b/include/sled/synchronization/mutex.h index dc2cccf..3ff109c 100644 --- a/include/sled/synchronization/mutex.h +++ b/include/sled/synchronization/mutex.h @@ -9,6 +9,7 @@ #ifndef SLED_SYNCHRONIZATION_MUTEX_H #define SLED_SYNCHRONIZATION_MUTEX_H +#include "sled/lang/attributes.h" #include "sled/units/time_delta.h" #include #include @@ -75,7 +76,7 @@ private: template::value, TLock>::type * = nullptr> -class LockGuard final { +class SLED_DEPRECATED LockGuard final { public: LockGuard(const LockGuard &) = delete; LockGuard &operator=(const LockGuard &) = delete; @@ -89,23 +90,23 @@ private: friend class ConditionVariable; }; -class MutexGuard final { +class MutexLock final { public: - MutexGuard(Mutex *mutex) : lock_(*mutex) {} + MutexLock(Mutex *mutex) : lock_(*mutex) {} - MutexGuard(const MutexGuard &) = delete; - MutexGuard &operator=(const MutexGuard &) = delete; + MutexLock(const MutexLock &) = delete; + MutexLock &operator=(const MutexLock &) = delete; private: friend class ConditionVariable; marl::lock lock_; }; -using MutexLock = MutexGuard; +using MutexGuard SLED_DEPRECATED = MutexLock; // using MutexGuard = marl::lock; // using MutexLock = LockGuard; // using MutexGuard = LockGuard; -using RecursiveMutexLock = LockGuard; +using RecursiveMutexLock SLED_DEPRECATED = LockGuard; // class MutexLock final { // public: diff --git a/src/any_test.cc b/src/any_test.cc index 1bbbc44..e7ee97a 100644 --- a/src/any_test.cc +++ b/src/any_test.cc @@ -1,4 +1,48 @@ #include #include +#include -TEST(Any, Assign) {} +TEST(Any, Assign) +{ + sled::Any any1; + sled::Any any2(any1); + sled::Any any3(1); + sled::Any any4(any3); + sled::Any any5 = 1; + EXPECT_FALSE(any1.HasValue()); + EXPECT_FALSE(any2.HasValue()); + EXPECT_TRUE(any3.HasValue()); + EXPECT_TRUE(any4.HasValue()); + EXPECT_TRUE(any5.HasValue()); + EXPECT_EQ(any3.Cast(), 1); + EXPECT_EQ(any4.Cast(), 1); + EXPECT_EQ(any5.Cast(), 1); + EXPECT_EQ(any3.CastOr("def"), "def"); + EXPECT_EQ(any4.CastOr("def"), "def"); + EXPECT_EQ(any5.CastOr("def"), "def"); + EXPECT_EQ(any3.CastOr(11), 1); +} + +TEST(Any, std_swap) +{ + sled::Any a; + sled::Any b = 2; + EXPECT_FALSE(a.HasValue()); + EXPECT_TRUE(b.HasValue()); + std::swap(a, b); + EXPECT_TRUE(a.HasValue()); + EXPECT_FALSE(b.HasValue()); + EXPECT_EQ(a.Cast(), 2); +} + +TEST(Any, custom_swap) +{ + sled::Any a; + sled::Any b = 2; + EXPECT_FALSE(a.HasValue()); + EXPECT_TRUE(b.HasValue()); + a.swap(b); + EXPECT_TRUE(a.HasValue()); + EXPECT_FALSE(b.HasValue()); + EXPECT_EQ(a.Cast(), 2); +} From 016eadb3d9ff8f2546d7788b6853257a667f34ec Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 13 Mar 2024 22:44:23 +0800 Subject: [PATCH 3/4] feat update --- include/sled/lang/attributes.h | 38 ++++++++++++++++++- include/sled/synchronization/event.h | 2 +- include/sled/synchronization/mutex.h | 19 ++++++---- include/sled/synchronization/one_time_event.h | 2 +- include/sled/system/thread.h | 9 +++-- 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/include/sled/lang/attributes.h b/include/sled/lang/attributes.h index 6e06b92..b91befa 100644 --- a/include/sled/lang/attributes.h +++ b/include/sled/lang/attributes.h @@ -1,5 +1,41 @@ #pragma once #ifndef SLED_LANG_ATTRIBUTES_H #define SLED_LANG_ATTRIBUTES_H -#define SLED_DEPRECATED __attribute__((deprecated)) + +#define SLED_DEPRECATED() __attribute__((deprecated)) +#if defined(__GNUC__) && defined(__SUPPORT_TS_ANNOTATION__) && !defined(SWIG) +#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#elif defined(__clang__) +#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) +#else +#define THREAD_ANNOTATION_ATTRIBUTE__(x) +#endif + +#if defined(GUARDED_BY) +#undef GUARDED_BY +#endif +#define GUARDED_BY(x) __attribute__((guarded_by(x))) + +#if defined(__clang__) +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock_function(__VA_ARGS__)) + +#define EXCLUSIVE_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock_function(__VA_ARGS__)) + +#define UNLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(unlock_function(__VA_ARGS__)) + +#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) + +#else + +#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_trylock(__VA_ARGS__)) +#define EXCLUSIVE_LOCK_FUNCTION(...) \ + THREAD_ANNOTATION_ATTRIBUTE__(exclusive_lock(__VA_ARGS__)) +#define UNLOCK_FUNCTION(...) THREAD_ANNOTATION_ATTRIBUTE__(unlock(__VA_ARGS__)) +#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(point_to_guarded_by(x)) +#endif + #endif// SLED_LANG_ATTRIBUTES_H diff --git a/include/sled/synchronization/event.h b/include/sled/synchronization/event.h index 1b4c4bc..fd0881f 100644 --- a/include/sled/synchronization/event.h +++ b/include/sled/synchronization/event.h @@ -36,7 +36,7 @@ public: Mutex mutex_; ConditionVariable cv_; const bool is_manual_reset_; - bool event_status_; + bool event_status_ GUARDED_BY(mutex_); }; }// namespace sled diff --git a/include/sled/synchronization/mutex.h b/include/sled/synchronization/mutex.h index 3ff109c..673addc 100644 --- a/include/sled/synchronization/mutex.h +++ b/include/sled/synchronization/mutex.h @@ -5,10 +5,10 @@ **/ #pragma once -#include "marl/conditionvariable.h" #ifndef SLED_SYNCHRONIZATION_MUTEX_H #define SLED_SYNCHRONIZATION_MUTEX_H +#include "marl/conditionvariable.h" #include "sled/lang/attributes.h" #include "sled/units/time_delta.h" #include @@ -61,13 +61,13 @@ public: RecursiveMutex(const RecursiveMutex &) = delete; RecursiveMutex &operator=(const RecursiveMutex &) = delete; - inline void Lock() { impl_.lock(); } + inline void Lock() EXCLUSIVE_LOCK_FUNCTION() { impl_.lock(); } inline bool TryLock() { return impl_.try_lock(); } inline void AssertHeld() {} - inline void Unlock() { impl_.unlock(); } + inline void Unlock() UNLOCK_FUNCTION() { impl_.unlock(); } private: std::recursive_mutex impl_; @@ -76,14 +76,17 @@ private: template::value, TLock>::type * = nullptr> -class SLED_DEPRECATED LockGuard final { +class SLED_DEPRECATED() LockGuard final { public: LockGuard(const LockGuard &) = delete; LockGuard &operator=(const LockGuard &) = delete; - explicit LockGuard(TLock *lock) : mutex_(lock) { mutex_->Lock(); }; + explicit LockGuard(TLock *lock) EXCLUSIVE_LOCK_FUNCTION() : mutex_(lock) + { + mutex_->Lock(); + }; - ~LockGuard() { mutex_->Unlock(); }; + ~LockGuard() UNLOCK_FUNCTION() { mutex_->Unlock(); }; private: TLock *mutex_; @@ -102,11 +105,11 @@ private: marl::lock lock_; }; -using MutexGuard SLED_DEPRECATED = MutexLock; +using MutexGuard SLED_DEPRECATED() = MutexLock; // using MutexGuard = marl::lock; // using MutexLock = LockGuard; // using MutexGuard = LockGuard; -using RecursiveMutexLock SLED_DEPRECATED = LockGuard; +using RecursiveMutexLock SLED_DEPRECATED() = LockGuard; // class MutexLock final { // public: diff --git a/include/sled/synchronization/one_time_event.h b/include/sled/synchronization/one_time_event.h index 4bd31e9..a1a50ed 100644 --- a/include/sled/synchronization/one_time_event.h +++ b/include/sled/synchronization/one_time_event.h @@ -25,7 +25,7 @@ public: } private: - bool happended_ = false; + bool happended_ GUARDED_BY(mutex_) = false; Mutex mutex_; }; diff --git a/include/sled/system/thread.h b/include/sled/system/thread.h index 1466893..084f1ae 100644 --- a/include/sled/system/thread.h +++ b/include/sled/system/thread.h @@ -159,9 +159,9 @@ private: void ClearCurrentTaskQueue(); mutable Mutex mutex_; - std::queue> messages_; - std::priority_queue delayed_messages_; - uint32_t delayed_next_num_; + std::queue> messages_ GUARDED_BY(mutex_); + std::priority_queue delayed_messages_ GUARDED_BY(mutex_); + uint32_t delayed_next_num_ GUARDED_BY(mutex_); bool fInitialized_; bool fDestroyed_; std::atomic stop_; @@ -183,8 +183,9 @@ public: AutoSocketServerThread(const AutoSocketServerThread &) = delete; AutoSocketServerThread &operator=(const AutoSocketServerThread &) = delete; + private: - Thread* old_thread_; + Thread *old_thread_; }; }// namespace sled From 6b2bade8b4ec51741f9bd482434acb1238bd34cc Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Wed, 13 Mar 2024 23:14:06 +0800 Subject: [PATCH 4/4] fat update --- CMakeLists.txt | 8 ++------ include/sled/ref_counted_object.h | 1 + 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5becad9..38071ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,8 @@ if (SLED_LOCATION_PATH) target_compile_definitions(sled PRIVATE __SLED_LOCATION_PATH="${SLED_LOCATION_PATH}") endif() # add_subdirectory(3party/eigen EXCLUDE_FROM_ALL) -target_include_directories(sled PUBLIC 3party/eigen 3party/inja) +target_include_directories(sled + PUBLIC 3party/eigen 3party/inja include) target_sources( sled PRIVATE @@ -69,11 +70,6 @@ target_sources( # set(BUILD_WITH_STATIC_RUNTIME_LIBS ON) set(BUILD_WITH_DOCUMENTATION OFF) # add_subdirectory(3party/rttr EXCLUDE_FROM_ALL) -target_include_directories( - sled - PUBLIC include - PRIVATE src) - target_link_libraries(sled PUBLIC rpc_core fmt marl protobuf::libprotobuf) if(SLED_BUILD_BENCHMARK) diff --git a/include/sled/ref_counted_object.h b/include/sled/ref_counted_object.h index a93e8cf..c36f289 100644 --- a/include/sled/ref_counted_object.h +++ b/include/sled/ref_counted_object.h @@ -10,6 +10,7 @@ #include "sled/ref_count.h" #include "sled/ref_counter.h" +#include namespace sled {