Skip to content

Commit

Permalink
Simplify codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed Oct 10, 2023
1 parent 7e0ef73 commit 6776da9
Showing 1 changed file with 65 additions and 96 deletions.
161 changes: 65 additions & 96 deletions src/factory/WFTaskFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ struct __NamedObjectList
std::string name;
};

template<typename T>
static T *__get_object_list(const std::string& name, struct rb_root *root,
bool insert)
{
struct rb_node **p = &root->rb_node;
struct rb_node *parent = NULL;
T *objs;

while (*p)
{
parent = *p;
objs = rb_entry(*p, T, rb);
if (name < objs->name)
p = &(*p)->rb_left;
else if (name > objs->name)
p = &(*p)->rb_right;
else
return objs;
}

if (insert)
{
objs = new T(name);
rb_link_node(&objs->rb, parent, p);
rb_insert_color(&objs->rb, root);
return objs;
}

return NULL;
}

/****************** Named Counter ******************/

class __WFNamedCounterTask;
Expand Down Expand Up @@ -169,33 +200,13 @@ WFCounterTask *__NamedCounterMap::create(const std::string& name,
unsigned int target_value,
std::function<void (WFCounterTask *)>&& cb)
{
CounterList *counters;

if (target_value == 0)
return new WFCounterTask(0, std::move(cb));

struct rb_node **p = &root_.rb_node;
struct rb_node *parent = NULL;
CounterList *counters;
std::lock_guard<std::mutex> lock(mutex_);

while (*p)
{
parent = *p;
counters = rb_entry(*p, CounterList, rb);
if (name < counters->name)
p = &(*p)->rb_left;
else if (name > counters->name)
p = &(*p)->rb_right;
else
break;
}

if (*p == NULL)
{
counters = new CounterList(name);
rb_link_node(&counters->rb, parent, p);
rb_insert_color(&counters->rb, &root_);
}

counters = __get_object_list<CounterList>(name, &root_, true);
return new __WFNamedCounterTask(target_value, counters, std::move(cb));
}

Expand Down Expand Up @@ -235,25 +246,13 @@ void __NamedCounterMap::count_n_locked(CounterList *counters, unsigned int n,
void __NamedCounterMap::count_n(const std::string& name, unsigned int n)
{
LIST_HEAD(task_list);
CounterList *counters;
struct __counter_node *node;
struct rb_node *p;
CounterList *counters;

mutex_.lock();
p = root_.rb_node;
while (p)
{
counters = rb_entry(p, CounterList, rb);
if (name < counters->name)
p = p->rb_left;
else if (name > counters->name)
p = p->rb_right;
else
{
count_n_locked(counters, n, &task_list);
break;
}
}
counters = __get_object_list<CounterList>(name, &root_, false);
if (counters)
count_n_locked(counters, n, &task_list);

mutex_.unlock();
while (!list_empty(&task_list))
Expand Down Expand Up @@ -338,7 +337,8 @@ static class __NamedConditionalMap
void remove(ConditionalList *conds, struct __conditional_node *node);

private:
ConditionalList *get_list(const std::string& name, bool insert);
void signal_max_locked(ConditionalList *conds, void *msg, size_t max,
struct list_head *cond_list);
struct rb_root root_;
std::mutex mutex_;

Expand Down Expand Up @@ -389,92 +389,61 @@ class __WFNamedCondtional : public WFConditional
WFConditional *__NamedConditionalMap::create(const std::string& name,
SubTask *task, void **msgbuf)
{
ConditionalList *conds;
std::lock_guard<std::mutex> lock(mutex_);
ConditionalList *conds = get_list(name, true);
conds = __get_object_list<ConditionalList>(name, &root_, true);
return new __WFNamedCondtional(task, msgbuf, conds);
}

WFConditional *__NamedConditionalMap::create(const std::string& name,
SubTask *task)
{
ConditionalList *conds;
std::lock_guard<std::mutex> lock(mutex_);
ConditionalList *conds = get_list(name, true);
conds = __get_object_list<ConditionalList>(name, &root_, true);
return new __WFNamedCondtional(task, conds);
}

__NamedConditionalMap::ConditionalList *
__NamedConditionalMap::get_list(const std::string& name, bool insert)
void __NamedConditionalMap::signal_max_locked(ConditionalList *conds,
void *msg, size_t max,
struct list_head *cond_list)
{
struct rb_node **p = &root_.rb_node;
struct rb_node *parent = NULL;
ConditionalList *conds;
struct list_head *pos, *tmp;

while (*p)
if (max == (size_t)-1)
list_splice(&conds->head, cond_list);
else
{
parent = *p;
conds = rb_entry(*p, ConditionalList, rb);
if (name < conds->name)
p = &(*p)->rb_left;
else if (name > conds->name)
p = &(*p)->rb_right;
else
return conds;
}
list_for_each_safe(pos, tmp, &conds->head)
{
if (max == 0)
return;

if (insert)
{
conds = new ConditionalList(name);
rb_link_node(&conds->rb, parent, p);
rb_insert_color(&conds->rb, &root_);
return conds;
list_move_tail(pos, cond_list);
max--;
}
}

return NULL;
rb_erase(&conds->rb, &root_);
delete conds;
}

void __NamedConditionalMap::signal(const std::string& name, void *msg, size_t max)
{
LIST_HEAD(cond_list);
struct __conditional_node *node;
ConditionalList *conds;
struct list_head *pos;
struct list_head *tmp;

mutex_.lock();
conds = get_list(name, false);
if (!conds)
{
mutex_.unlock();
return;
}

if (max == (size_t)-1)
list_splice(&conds->head, &cond_list);
else
{
list_for_each_safe(pos, tmp, &conds->head)
{
if (max == 0)
{
conds = NULL;
break;
}

list_move_tail(pos, &cond_list);
max--;
}
}

conds = __get_object_list<ConditionalList>(name, &root_, false);
if (conds)
{
rb_erase(&conds->rb, &root_);
delete conds;
}
signal_max_locked(conds, msg, max, &cond_list);

mutex_.unlock();
list_for_each_safe(pos, tmp, &cond_list)
while (!list_empty(&cond_list))
{
node = list_entry(pos, struct __conditional_node, list);
node = list_entry(cond_list.next, struct __conditional_node, list);
list_del(&node->list);
node->cond->WFConditional::signal(msg);
}
}
Expand Down

0 comments on commit 6776da9

Please sign in to comment.