OpenSDN source code
task_tbbkeepawake.h
Go to the documentation of this file.
1 #include <mutex>
2 
3 #include "base/task.h"
4 #include "base/timer.h"
5 #include <boost/bind/bind.hpp>
6 #include "io/event_manager.h"
7 
8 using namespace boost::placeholders;
9 
11 public:
12  TaskTbbKeepAwake() : tbb_awake_count_(0), tbb_awake_val_(0),
13  timeout_changed_(false), tbb_awake_timer_(NULL) { }
14 
16  const std::string task_name,
17  uint32_t tbbKeepawakeTimeout = 1000) {
18  uint32_t task_id = ts->GetTaskId(task_name);
19  tbb_awake_timer_ = TimerManager::CreateTimer(*event_mgr->io_service(),
20  "TBB Keep Awake",
21  task_id, 0);
22  tbb_awake_val_ = tbbKeepawakeTimeout;
23  bool ret = tbb_awake_timer_->Start(tbbKeepawakeTimeout,
24  boost::bind(&TaskTbbKeepAwake::TbbKeepAwake, this));
25  return ret;
26  }
27 
28  void ModifyTbbKeepAwakeTimeout(uint32_t timeout) {
29  std::scoped_lock lock(mutex_);
30  if (tbb_awake_val_ != timeout) {
31  timeout_changed_ = true;
32  tbb_awake_val_ = timeout;
33  }
34  }
35 
36  bool TbbKeepAwake() {
37  tbb_awake_count_++;
38  if (timeout_changed_) {
39  tbb_awake_timer_->Reschedule(tbb_awake_val_);
40  timeout_changed_ = false;
41  }
42  return true;
43  }
44 
46  if (tbb_awake_timer_) {
47  tbb_awake_timer_->Cancel();
48  TimerManager::DeleteTimer(tbb_awake_timer_);
49  }
50  }
51 
52  uint32_t tbb_awake_val() const { return tbb_awake_val_; }
53 private:
54  uint64_t tbb_awake_count_;
55  uint32_t tbb_awake_val_;
58  std::mutex mutex_;
59 };
boost::asio::io_context * io_service()
Definition: event_manager.h:42
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:304
int GetTaskId(const std::string &name)
Definition: task.cc:861
bool StartTbbKeepAwakeTask(TaskScheduler *ts, EventManager *event_mgr, const std::string task_name, uint32_t tbbKeepawakeTimeout=1000)
void ModifyTbbKeepAwakeTimeout(uint32_t timeout)
uint32_t tbb_awake_val() const
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:221
static Timer * CreateTimer(boost::asio::io_context &service, const std::string &name, int task_id=Timer::GetTimerTaskId(), int task_instance=Timer::GetTimerInstanceId(), bool delete_on_completion=false)
Definition: timer.cc:200
Definition: timer.h:57