From 741296c661b80f66464affd30aaf918f62066700 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Wed, 26 Jun 2024 14:09:34 +0800 Subject: [PATCH] Format codes. --- lib_fiber/cpp/include/fiber/wait_group.hpp | 6 +- lib_fiber/cpp/src/wait_group.cpp | 95 ++++++++++++---------- lib_fiber/samples/wait_group/main.cpp | 17 ++-- 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/lib_fiber/cpp/include/fiber/wait_group.hpp b/lib_fiber/cpp/include/fiber/wait_group.hpp index e8f11411a..c3c7a3b2f 100644 --- a/lib_fiber/cpp/include/fiber/wait_group.hpp +++ b/lib_fiber/cpp/include/fiber/wait_group.hpp @@ -1,11 +1,13 @@ #pragma once -#include "acl_cpp/stdlib/atomic.hpp" +#include "fiber_cpp_define.hpp" + +//#include "acl_cpp/stdlib/atomic.hpp" namespace acl { template class fiber_tbox; -class wait_group { +class FIBER_CPP_API wait_group { public: wait_group(void); ~wait_group(void); diff --git a/lib_fiber/cpp/src/wait_group.cpp b/lib_fiber/cpp/src/wait_group.cpp index ed492f723..ea45dc559 100644 --- a/lib_fiber/cpp/src/wait_group.cpp +++ b/lib_fiber/cpp/src/wait_group.cpp @@ -17,68 +17,81 @@ wait_group::~wait_group(void) void wait_group::add(int n) { - long long state = state_.add_fetch((long long)n << 32); + long long state = state_.add_fetch((long long)n << 32); + //高32位为任务数量 - int c = (int)(state >> 32); + int c = (int)(state >> 32); + //低32位为等待者数量 - uint32_t w = (uint32_t)state; + unsigned w = (unsigned)state; + //count不能小于0 - if(c < 0){ - acl_msg_fatal("wait_group: negative wait_group counter"); - } - if(w != 0 && n > 0 && c == n){ - acl_msg_fatal("wait_group: add called concurrently with wait"); - } - if(c > 0 || w ==0){ - return; - } + if (c < 0){ + logger_fatal("Negative wait_group counter"); + } + + if (w != 0 && n > 0 && c == n){ + logger_fatal("Add called concurrently with wait"); + } + + if (c > 0 || w == 0) { + return; + } + //检查state是否被修改 - if(state_ != state){ - acl_msg_fatal("wait_group: add called concurrently with wait"); - } + if (state_ != state) { + logger_fatal("Add called concurrently with wait"); + } + //这里count为0了,清空state并唤醒所有等待者 - state_ = 0; - for (size_t i = 0; i < w; i++) { + state_ = 0; + + for (size_t i = 0; i < w; i++) { #ifdef _DEBUG - unsigned long* tid = new unsigned long; - *tid = acl::thread::self(); - box_->push(tid); + unsigned long* tid = new unsigned long; + *tid = acl::thread::self(); + box_->push(tid); #else - box_->push(NULL); + box_->push(NULL); #endif - } + } } void wait_group::done(void) { - add(-1); + add(-1); } void wait_group::wait(void) { - for(;;){ - long long state = state_; - int c = (int)(state >> 32); - uint32_t w = (uint32_t)state; + for(;;) { + long long state = state_; + int c = (int) (state >> 32); + //没有任务直接返回 - if(c == 0) return; + if (c == 0) { + return; + } + //等待者数量加一,失败的话重新获取state - if(state_.cas(state, state + 1) == state){ - bool found; + if (state_.cas(state, state + 1) == state) { + bool found; #ifdef _DEBUG - unsigned long* tid = box_->pop(-1, &found); - assert(found); - delete tid; + unsigned long* tid = box_->pop(-1, &found); + assert(found); + delete tid; #else - (void) box_->pop(-1, &found); - assert(found); + (void) box_->pop(-1, &found); + assert(found); #endif - if(state_ != 0){ - acl_msg_fatal("wait_group: wait_group is reused before previous wait has returned"); - } - return; - } - } + if(state_ == 0) { + return; + } + + logger_fatal("Reused before previous wait has returned"); + return; + } + } } } // namespace acl diff --git a/lib_fiber/samples/wait_group/main.cpp b/lib_fiber/samples/wait_group/main.cpp index 8ad42b4e5..1f23f591f 100644 --- a/lib_fiber/samples/wait_group/main.cpp +++ b/lib_fiber/samples/wait_group/main.cpp @@ -6,8 +6,7 @@ static int __delay = 1; ////////////////////////////////////////////////////////////////////////////// -class fiber_notifier : public acl::fiber -{ +class fiber_notifier : public acl::fiber { public: fiber_notifier(acl::wait_group& sync) : sync_(sync) {} @@ -32,8 +31,7 @@ private: ////////////////////////////////////////////////////////////////////////////// -class thread_notifier : public acl::thread -{ +class thread_notifier : public acl::thread { public: thread_notifier(acl::wait_group& sync) : sync_(sync) {} @@ -59,8 +57,7 @@ private: ////////////////////////////////////////////////////////////////////////////// -class fiber_waiter : public acl::fiber -{ +class fiber_waiter : public acl::fiber { public: fiber_waiter(acl::wait_group& sync) : sync_(sync) {} ~fiber_waiter(void) {} @@ -70,15 +67,14 @@ private: // @override void run(void) { - size_t ret = sync_.wait(); + sync_.wait(); printf("All threads and fibers were done\r\n"); } }; ////////////////////////////////////////////////////////////////////////////// -static void usage(const char* procname) -{ +static void usage(const char* procname) { printf("usage: %s -h [help]\r\n" " -e event_type[kernel|poll|select|io_uring]\r\n" " -t threads_count[default: 1]\r\n" @@ -87,8 +83,7 @@ static void usage(const char* procname) , procname); } -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { int ch, threads_count = 1, fibers_count = 1; acl::fiber_event_t event_type = acl::FIBER_EVENT_T_KERNEL;