Format codes.

This commit is contained in:
zhengshuxin 2024-06-26 14:09:34 +08:00
parent fc6a460252
commit 741296c661
3 changed files with 64 additions and 54 deletions

View File

@ -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<typename T> class fiber_tbox;
class wait_group {
class FIBER_CPP_API wait_group {
public:
wait_group(void);
~wait_group(void);

View File

@ -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

View File

@ -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;