Merge pull request #239 from Barenboim/master

fix named counter dismiss bug
This commit is contained in:
xiehan
2021-02-02 19:21:55 +08:00
committed by GitHub
2 changed files with 22 additions and 1 deletions

View File

@@ -76,6 +76,7 @@ public:
void count_n(const std::string& name, unsigned int n);
void count(struct __CounterList *counters, struct __counter_node *node);
void remove(struct __CounterList *counters, struct __counter_node *node);
virtual ~__CounterMap();
@@ -104,6 +105,12 @@ public:
counters_->push_back(&node_);
}
virtual ~__WFCounterTask()
{
if (this->value != 0)
__CounterMap::get_instance()->remove(counters_, &node_);
}
virtual void count()
{
__CounterMap::get_instance()->count(counters_, &node_);
@@ -259,6 +266,20 @@ void __CounterMap::count(struct __CounterList *counters,
task->WFCounterTask::count();
}
void __CounterMap::remove(struct __CounterList *counters,
struct __counter_node *node)
{
mutex_.lock();
counters->del(node);
if (counters->empty())
{
rb_erase(&counters->rb, &counters_map_);
delete counters;
}
mutex_.unlock();
}
WFCounterTask *WFTaskFactory::create_counter_task(const std::string& counter_name,
unsigned int target_value,
counter_callback_t callback)

View File

@@ -36,7 +36,7 @@ TEST(memory_unittest, dismiss)
auto *timer_task = WFTaskFactory::create_timer_task(0, nullptr);
tasks.push_back(timer_task);
auto *counter_task = WFTaskFactory::create_counter_task("", 0, nullptr);
auto *counter_task = WFTaskFactory::create_counter_task("", 1, nullptr);
tasks.push_back(counter_task);
auto *go_task = WFTaskFactory::create_go_task("", [](){});