check state before wait

This commit is contained in:
白喵 2024-06-25 12:39:04 +08:00
parent 25b59ab47b
commit 919c8a68fa

View File

@ -17,8 +17,7 @@ wait_group::~wait_group(void)
void wait_group::add(int n)
{
state_ += (long long)n << 32;
long long state = state_;
long long state = state_.add_fetch((long long)n << 32);
int c = (int)(state >> 32);
uint32_t w = (uint32_t)state;
if(c < 0){
@ -52,11 +51,12 @@ void wait_group::done(void)
void wait_group::wait(void)
{
for(;;){
long long state = state_;
int c = (int)(state >> 32);
uint32_t w = (uint32_t)state;
if(c == 0) return;
state_++;
if(state_.cas(state, state + 1) == state){
bool found;
#ifdef _DEBUG
unsigned long* tid = box_->pop(-1, &found);
@ -69,6 +69,9 @@ void wait_group::wait(void)
if(state_ != 0){
acl_msg_fatal("wait_group: wait_group is reused before previous wait has returned");
}
return;
}
}
}
} // namespace acl