Skip to content

Commit 8771ad8

Browse files
committed
PTP: Add state tracking for Tasks
Tasks can be in one of these states: - QUEUED - ASSIGNED - COMPLETED
1 parent a894655 commit 8771ad8

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎src/node_threadpool.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@ void Worker::Join(void) {
3939
void Worker::_Run(void* data) {
4040
TaskQueue* queue = static_cast<TaskQueue*>(data);
4141
while (std::unique_ptr<Task> task = queue->BlockingPop()) {
42+
task->UpdateState(Task::ASSIGNED);
4243
task->Run();
44+
task->UpdateState(Task::COMPLETED);
4345
}
4446
}
4547

48+
/**************
49+
* Task
50+
***************/
51+
52+
void Task::UpdateState(enum State state) {
53+
state_ = state;
54+
}
55+
4656
/**************
4757
* LibuvExecutor
4858
***************/
@@ -139,8 +149,10 @@ bool TaskQueue::Push(std::unique_ptr<Task> task) {
139149
return false;
140150
}
141151

152+
task->UpdateState(Task::QUEUED);
142153
queue_.push(std::move(task));
143154
tasks_available_.Signal(scoped_lock);
155+
144156
return true;
145157
}
146158

‎src/node_threadpool.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,28 @@ class TaskDetails {
7373
// - User work from the N-API
7474
class Task {
7575
public:
76+
enum State {
77+
QUEUED
78+
, ASSIGNED
79+
, COMPLETED
80+
};
81+
7682
Task() {}
7783
// Invoked after Run().
7884
virtual ~Task() {}
7985

8086
// Invoked on some thread in the Threadpool.
8187
virtual void Run() = 0;
8288

89+
// Different Threadpool components should update this as the Task travels around.
90+
void UpdateState(enum State state);
91+
8392
// Run() can access details.
8493
// Should be set in subclass constructor.
8594
TaskDetails details_;
86-
private:
95+
96+
protected:
97+
enum State state_;
8798
};
8899

89100
// Shim that we plug into the libuv "pluggable TP" interface.

0 commit comments

Comments
 (0)