OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
task_trigger.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 #include <iostream>
5 #include "base/task_trigger.h"
6 
7 #include "base/task.h"
8 
9 class TaskTrigger::WorkerTask : public Task {
10 public:
11  explicit WorkerTask(TaskTrigger *parent)
12  : Task(parent->task_id_, parent->task_instance_), parent_(parent) {
13  }
14  bool Run() {
15  if (parent_->disabled()) {
16  parent_->Reset();
17  return true;
18  }
19  if (parent_->deferred()) {
20  return false;
21  }
22  bool done = (parent_->func_)();
23  if (done) {
24  parent_->Reset();
25  }
26  return done;
27  }
28  std::string Description() const { return "TaskTrigger::WorkerTask"; }
29 
30 private:
32 };
33 
34 TaskTrigger::TaskTrigger(const FunctionPtr& func, int task_id, int task_instance)
35  : func_(func), task_id_(task_id), task_instance_(task_instance) {
36  trigger_ = false;
37  disabled_ = false;
38  deferred_ = false;
39 }
40 
42  assert(!trigger_);
43 }
44 
46  bool current = trigger_.fetch_and_store(true);
47  if (!current) {
48  WorkerTask *task = new WorkerTask(this);
50  scheduler->Enqueue(task);
51  }
52 }
53 
55  trigger_ = false;
56 }
int task_id_
The code path executed by the task.
Definition: task.h:149
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:178
Definition: task_int.h:10
bool Run()
Code to execute. Returns true if task is completed. Return false to reschedule the task...
Definition: task_trigger.cc:14
static TaskScheduler * GetInstance()
Definition: task.cc:547
void Enqueue(Task *task)
Enqueues a task for running. Starts task if all policy rules are met else puts task in waitq...
Definition: task.cc:636
std::string Description() const
Definition: task_trigger.cc:28
tbb::atomic< bool > deferred_
Definition: task_trigger.h:52
FunctionPtr func_
Definition: task_trigger.h:45
void Reset()
Definition: task_trigger.cc:54
tbb::atomic< bool > trigger_
Definition: task_trigger.h:50
bool disabled()
Definition: task_trigger.h:28
bool deferred() const
Definition: task_trigger.h:41
boost::function< bool()> FunctionPtr
Definition: task_trigger.h:13
int task_instance_
The dataset id within a code path.
Definition: task.h:152
WorkerTask(TaskTrigger *parent)
Definition: task_trigger.cc:11
tbb::atomic< bool > disabled_
Definition: task_trigger.h:51
Task is a wrapper over tbb::task to support policies.
Definition: task.h:86
TaskTrigger(const FunctionPtr &func, int task_id, int task_instance)
Definition: task_trigger.cc:34