ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::exec::WorkQueue Class Reference

#include <WorkQueue.h>

Public Types

enum class  State { ONLINE , FINISHING , FORCE_FINISHING }
 

Public Member Functions

 WorkQueue ()=default
 Create WorkQueue object.
 
 ~WorkQueue ()
 Destroy WorkQueue object.
 
void operator() ()
 Thread entry function.
 
void enqueue (std::unique_ptr< IFunction > &&fn)
 Push the given Task to the job queue.
 
void terminate ()
 Flag as terminating so all the worker threads can terminate.
 
void finish ()
 Flag as terminating so all the worker threads can terminate.
 
uint32_t numJobsInQueue ()
 Check if it has pending jobs. Even if this returns fals, WorkQueue threads may be still running.
 

Detailed Description

Definition at line 32 of file WorkQueue.h.

Member Enumeration Documentation

◆ State

enum class onert::exec::WorkQueue::State
strong
Enumerator
ONLINE 
FINISHING 
FORCE_FINISHING 

Definition at line 35 of file WorkQueue.h.

Constructor & Destructor Documentation

◆ WorkQueue()

onert::exec::WorkQueue::WorkQueue ( )
default

Create WorkQueue object.

◆ ~WorkQueue()

onert::exec::WorkQueue::~WorkQueue ( )

Destroy WorkQueue object.

Definition at line 26 of file WorkQueue.cc.

27{
28 {
29 std::unique_lock<std::mutex> lock(_mu);
31 }
32 _cv.notify_all();
33}

References FORCE_FINISHING.

Member Function Documentation

◆ enqueue()

void onert::exec::WorkQueue::enqueue ( std::unique_ptr< IFunction > &&  fn)

Push the given Task to the job queue.

Parameters
fnFunction to be executed(a job)

Definition at line 70 of file WorkQueue.cc.

71{
72 {
73 std::unique_lock<std::mutex> lock{_mu};
74 _functions.emplace(std::move(fn));
75 }
76 _cv.notify_one();
77}

Referenced by onert::exec::ThreadPool::enqueue().

◆ finish()

void onert::exec::WorkQueue::finish ( )

Flag as terminating so all the worker threads can terminate.

Definition at line 88 of file WorkQueue.cc.

89{
90 {
91 std::unique_lock<std::mutex> lock{_mu};
92 _state = State::FINISHING;
93 }
94 _cv.notify_all();
95}

References FINISHING.

Referenced by onert::exec::ThreadPool::finish().

◆ numJobsInQueue()

uint32_t onert::exec::WorkQueue::numJobsInQueue ( )

Check if it has pending jobs. Even if this returns fals, WorkQueue threads may be still running.

Returns
true if the job queue not empty otherwise false

Definition at line 97 of file WorkQueue.cc.

98{
99 std::unique_lock<std::mutex> lock{_mu};
100 return _functions.size();
101}

Referenced by onert::exec::ThreadPool::numJobsInQueue().

◆ operator()()

void onert::exec::WorkQueue::operator() ( )

Thread entry function.

Definition at line 35 of file WorkQueue.cc.

36{
37 while (true)
38 {
39 std::unique_ptr<IFunction> fn = nullptr;
40
41 {
42 std::unique_lock<std::mutex> lock{_mu};
43 _cv.wait(lock, [this] {
44 return (_state == State::FORCE_FINISHING) || (_state == State::FINISHING) ||
45 (_state == State::ONLINE && !_functions.empty());
46 });
47
48 if (_state == State::FORCE_FINISHING)
49 {
50 assert(_functions.empty() && "Terminating with unfinished jobs");
51 return;
52 }
53 else if (_state == State::FINISHING && _functions.empty())
54 {
55 return;
56 }
57 else
58 {
59 assert(((_state == State::FINISHING) || (_state == State::ONLINE)) && !_functions.empty());
60 fn = std::move(_functions.front());
61 _functions.pop();
62 }
63 }
64
65 assert(fn);
66 fn->run();
67 }
68}

References FINISHING, FORCE_FINISHING, and ONLINE.

◆ terminate()

void onert::exec::WorkQueue::terminate ( )

Flag as terminating so all the worker threads can terminate.

Definition at line 79 of file WorkQueue.cc.

80{
81 {
82 std::unique_lock<std::mutex> lock{_mu};
84 }
85 _cv.notify_all();
86}

References FORCE_FINISHING.

Referenced by onert::exec::ThreadPool::~ThreadPool().


The documentation for this class was generated from the following files: