feat update
All checks were successful
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Successful in 1m20s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 1m37s
linux-arm-gcc / linux-gcc-armhf (push) Successful in 1m46s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m45s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m56s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 2m14s

This commit is contained in:
tqcq 2024-04-04 03:17:43 +00:00
parent e7fbe48b7f
commit 3ead7ff87a
4 changed files with 67 additions and 15 deletions

View File

@ -50,9 +50,11 @@ target_include_directories(sled PUBLIC src/ 3party/eigen 3party/inja
3party/rxcpp)
target_sources(
sled
PRIVATE src/sled/async/async.cc
PRIVATE
src/sled/async/async.cc
src/sled/debugging/demangle.cc
src/sled/debugging/symbolize.cc
src/sled/event_bus/event_bus.cc
src/sled/filesystem/path.cc
src/sled/log/log.cc
src/sled/network/async_resolver.cc
@ -61,7 +63,11 @@ target_sources(
src/sled/network/physical_socket_server.cc
src/sled/network/socket_address.cc
src/sled/network/socket_server.cc
src/sled/operations_chain.cc
src/sled/profiling/profiling.cc
src/sled/random.cc
src/sled/sigslot.cc
src/sled/status.cc
src/sled/strings/base64.cc
src/sled/strings/utils.cc
src/sled/synchronization/event.cc
@ -72,21 +78,18 @@ target_sources(
src/sled/system/pid.cc
src/sled/system/thread.cc
src/sled/system/thread_pool.cc
src/sled/system_time.cc
src/sled/task_queue/pending_task_safety_flag.cc
src/sled/task_queue/task_queue_base.cc
src/sled/testing/test.cc
src/sled/testing/benchmark.cc
src/sled/testing/test.cc
src/sled/timer/task_queue_timeout.cc
src/sled/timer/timer.cc
src/sled/time_utils.cc
src/sled/units/time_delta.cc
src/sled/units/timestamp.cc
src/sled/operations_chain.cc
src/sled/random.cc
src/sled/sigslot.cc
src/sled/status.cc
src/sled/system_time.cc
src/sled/time_utils.cc
src/sled/uri.cc)
src/sled/uri.cc
)
# set(BUILD_RTTR_DYNAMIC OFF) set(BUILD_UNIT_TESTS OFF)
# set(BUILD_WITH_STATIC_RUNTIME_LIBS ON) set(BUILD_WITH_DOCUMENTATION OFF)
# add_subdirectory(3party/rttr EXCLUDE_FROM_ALL) include(CheckCCompilerFlag)

View File

@ -0,0 +1,19 @@
#include "sled/event_bus/event_bus.h"
namespace sled {
namespace internal {
static std::atomic<int> g_event_registry_count{0};
void
IncrementEvenetRegistryCount()
{
g_event_registry_count.fetch_add(1, std::memory_order_relaxed);
}
int
GetEventRegistryCount()
{
return g_event_registry_count.load(std::memory_order_acquire);
}
}// namespace internal
}// namespace sled

View File

@ -17,6 +17,8 @@ using RawType = typename std::remove_cv<typename std::remove_reference<Event>::t
template<typename T>
using EnableNotVolatile = typename std::enable_if<!std::is_volatile<T>::value>;
// using RawType = typename std::remove_const<typename std::remove_reference<Event>::type>::type;
void IncrementEvenetRegistryCount();
int GetEventRegistryCount();
}// namespace internal
namespace {
@ -24,9 +26,14 @@ namespace {
template<typename Event>
class EventRegistry {
public:
static_assert(!std::is_const<Event>::value, "Event type must be non-const");
static_assert(!std::is_volatile<Event>::value, "Event type must be non-volatile");
static_assert(!std::is_reference<Event>::value, "Event type must be non-reference");
using Dispatcher = sigslot::signal1<Event>;
using SubscriberTable = std::unordered_map<EventBus *, Dispatcher>;
EventRegistry() { internal::IncrementEvenetRegistryCount(); }
static EventRegistry &Instance()
{
static EventRegistry instance_;
@ -52,13 +59,16 @@ public:
void Subscribe(EventBus *bus, C *instance, F &&method)
{
sled::SharedMutexWriteLock lock(&shared_mutex_);
auto iter = signals_.find(bus);
if (iter == signals_.end()) {
signals_.emplace(bus, Dispatcher());
iter = signals_.find(bus);
bool is_empty = signals_.empty();
{
auto iter = signals_.find(bus);
if (iter == signals_.end()) {
signals_.emplace(bus, Dispatcher());
iter = signals_.find(bus);
}
auto &dispatcher = iter->second;
dispatcher.connect(instance, method);
}
auto &dispatcher = iter->second;
dispatcher.connect(instance, method);
}
template<typename C>
@ -112,6 +122,8 @@ public:
template<typename mt_policy = MultiThreadedLocal>
using Subscriber = sigslot::has_slots<mt_policy>;
static int EventRegistryCount() { return internal::GetEventRegistryCount(); }
EventBus() = default;
~EventBus()

View File

@ -252,4 +252,22 @@ TEST_SUITE("EventBus")
});
CHECK_EQ(subscriber.counter.load(), 2);
}
TEST_CASE("EventRegistryCount")
{
struct EventType1 {};
struct EventType2 {};
sled::EventBus bus;
int current_count = sled::EventBus::EventRegistryCount();
bus.Post(EventType1{});
CHECK_EQ(sled::EventBus::EventRegistryCount(), current_count + 1);
bus.Post(EventType1{});
CHECK_EQ(sled::EventBus::EventRegistryCount(), current_count + 1);
bus.Post(EventType2{});
CHECK_EQ(sled::EventBus::EventRegistryCount(), current_count + 2);
}
}