diff --git a/src/factory/WFTaskFactory.cc b/src/factory/WFTaskFactory.cc index 8e43008671c..cac4f08bb0d 100644 --- a/src/factory/WFTaskFactory.cc +++ b/src/factory/WFTaskFactory.cc @@ -176,13 +176,16 @@ class __WFNamedTimerTask : public __WFTimerTask { public: __WFNamedTimerTask(time_t seconds, long nanoseconds, - __NamedTimerMap::TimerList *timers, CommScheduler *scheduler, timer_callback_t&& cb) : __WFTimerTask(seconds, nanoseconds, scheduler, std::move(cb)), flag_(false) { node_.task = this; + } + + void push_to(__NamedTimerMap::TimerList *timers) + { timers->push_back(&node_); timers_ = timers; } @@ -245,11 +248,12 @@ WFTimerTask *__NamedTimerMap::create(const std::string& name, CommScheduler *scheduler, timer_callback_t&& cb) { - TimerList *timers; - std::lock_guard lock(mutex_); - timers = __get_object_list(name, &root_, true); - return new __WFNamedTimerTask(seconds, nanoseconds, timers, - scheduler, std::move(cb)); + auto *task = new __WFNamedTimerTask(seconds, nanoseconds, scheduler, + std::move(cb)); + mutex_.lock(); + task->push_to(__get_object_list(name, &root_, true)); + mutex_.unlock(); + return task; } void __NamedTimerMap::cancel(const std::string& name, size_t max) @@ -340,21 +344,17 @@ static class __NamedCounterMap class __WFNamedCounterTask : public WFCounterTask { public: - __WFNamedCounterTask(unsigned int target_value, - __NamedCounterMap::CounterList *counters, - counter_callback_t&& cb) : + __WFNamedCounterTask(unsigned int target_value, counter_callback_t&& cb) : WFCounterTask(1, std::move(cb)) { node_.target_value = target_value; node_.task = this; - counters->push_back(&node_); - counters_ = counters; } - virtual ~__WFNamedCounterTask() + void push_to(__NamedCounterMap::CounterList *counters) { - if (this->value != 0) - __counter_map.remove(counters_, &node_); + counters->push_back(&node_); + counters_ = counters; } virtual void count() @@ -362,6 +362,12 @@ class __WFNamedCounterTask : public WFCounterTask __counter_map.count(counters_, &node_); } + virtual ~__WFNamedCounterTask() + { + if (this->value != 0) + __counter_map.remove(counters_, &node_); + } + private: struct __counter_node node_; __NamedCounterMap::CounterList *counters_; @@ -371,14 +377,14 @@ WFCounterTask *__NamedCounterMap::create(const std::string& name, unsigned int target_value, counter_callback_t&& cb) { - CounterList *counters; - if (target_value == 0) return new WFCounterTask(0, std::move(cb)); - std::lock_guard lock(mutex_); - counters = __get_object_list(name, &root_, true); - return new __WFNamedCounterTask(target_value, counters, std::move(cb)); + auto *task = new __WFNamedCounterTask(target_value, std::move(cb)); + mutex_.lock(); + task->push_to(__get_object_list(name, &root_, true)); + mutex_.unlock(); + return task; } void __NamedCounterMap::count_n_locked(CounterList *counters, unsigned int n, @@ -505,28 +511,22 @@ static class __NamedConditionalMap class __WFNamedCondtional : public WFConditional { public: - __WFNamedCondtional(SubTask *task, void **msgbuf, - __NamedConditionalMap::ConditionalList *conds) : + __WFNamedCondtional(SubTask *task, void **msgbuf) : WFConditional(task, msgbuf) { node_.cond = this; - conds->push_back(&node_); - conds_ = conds; } - __WFNamedCondtional(SubTask *task, - __NamedConditionalMap::ConditionalList *conds) : + __WFNamedCondtional(SubTask *task) : WFConditional(task) { node_.cond = this; - conds->push_back(&node_); - conds_ = conds; } - virtual ~__WFNamedCondtional() + void push_to(__NamedConditionalMap::ConditionalList *conds) { - if (!this->flag) - __conditional_map.remove(conds_, &node_); + conds->push_back(&node_); + conds_ = conds; } virtual void signal(void *msg) @@ -534,6 +534,12 @@ class __WFNamedCondtional : public WFConditional __conditional_map.signal(conds_, &node_, msg); } + virtual ~__WFNamedCondtional() + { + if (!this->flag) + __conditional_map.remove(conds_, &node_); + } + private: struct __conditional_node node_; __NamedConditionalMap::ConditionalList *conds_; @@ -542,19 +548,19 @@ class __WFNamedCondtional : public WFConditional WFConditional *__NamedConditionalMap::create(const std::string& name, SubTask *task, void **msgbuf) { - ConditionalList *conds; - std::lock_guard lock(mutex_); - conds = __get_object_list(name, &root_, true); - return new __WFNamedCondtional(task, msgbuf, conds); + auto *cond = new __WFNamedCondtional(task, msgbuf); + mutex_.lock(); + cond->push_to(__get_object_list(name, &root_, true)); + mutex_.unlock(); + return cond; } WFConditional *__NamedConditionalMap::create(const std::string& name, SubTask *task) { - ConditionalList *conds; + auto *cond = new __WFNamedCondtional(task); std::lock_guard lock(mutex_); - conds = __get_object_list(name, &root_, true); - return new __WFNamedCondtional(task, conds); + cond->push_to(__get_object_list(name, &root_, true)); } void __NamedConditionalMap::signal_max_locked(ConditionalList *conds,