diff --git a/docs/about-go-task.md b/docs/about-go-task.md index 4d3f12b9..fb9f1048 100644 --- a/docs/about-go-task.md +++ b/docs/about-go-task.md @@ -62,7 +62,7 @@ int main(void) class WFTaskFactory { /* Create 'Go' task with running time limit in seconds plus nanoseconds. - * If time exceeded, state WFT_STATE_ABORTED will be got in callback. */ + * If time exceeded, state WFT_STATE_SYS_ERROR and error ETIMEDOUT will be got in callback. */ template static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds, const std::string& queue_name, @@ -70,7 +70,7 @@ class WFTaskFactory }; ~~~ 相比创建普通的go task,create_timedgo_task函数需要多传两个参数,seconds和nanoseconds。 -如果func的运行时间到达seconds+nanosconds时限,task直接callback,且state为WFT_STATE_ABORTED。 +如果func的运行时间到达seconds+nanosconds时限,task直接callback,且state为WFT_STATE_SYS_ERROR,error为ETIMEDOUT。 注意,框架无法中断用户执行中的任务。func依然会继续执行到结束,但不会再次callback。另外,nanoseconds取值区间在\[0,10亿)。 另外,当我们给go task加上了运行时间限制,callback的时机可能会先于func函数的结束,任务所在series可能也会先于func结束。 如果我们在func里访问series,可能就是一个错误了。例如: @@ -99,7 +99,7 @@ int main() { ... } - else // state == WFT_STATE_ABORTED. // 超过运行时间限制 + else // state == WFT_STATE_SYS_ERROR && error == ETIMEDOUT // 超过运行时间限制 { ... } @@ -120,7 +120,7 @@ int main() { int result = (int)task->user_data; } - else // state == WFT_STATE_ABORTED. // 超过运行时间限制 + else // state == WFT_STATE_SYS_ERROR && error == ETIMEDOUT // 超过运行时间限制 { ... } diff --git a/docs/en/about-go-task.md b/docs/en/about-go-task.md index 22c7d5e0..b87496da 100644 --- a/docs/en/about-go-task.md +++ b/docs/en/about-go-task.md @@ -57,14 +57,14 @@ You may create a go task with running time limit by calling WFTaskFactory::creat class WFTaskFactory { /* Create 'Go' task with running time limit in seconds plus nanoseconds. - * If time exceeded, state WFT_STATE_ABORTED will be got in callback. */ + * If time exceeded, state WFT_STATE_SYS_ERROR and error ETIMEDOUT will be got in callback. */ template static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds, const std::string& queue_name, FUNC&& func, ARGS&&... args); }; ~~~ -Compared with creating a normal go task, the ``create_timedgo_task`` function needs to pass two more parameters, seconds and nanoseconds. If the running time of ``func`` reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_ABORTED. +Compared with creating a normal go task, the ``create_timedgo_task`` function needs to pass two more parameters, seconds and nanoseconds. If the running time of ``func`` reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_SYS_ERROR and the error is ETIMEDOUT. Note that the framework cannot interrupt the user's ongoing task. ``func`` will still continue to execute to the end, but will not callback again. In addition, the value range of nanoseconds is [0,1 billion). diff --git a/docs/en/tutorial-08-matrix_multiply.md b/docs/en/tutorial-08-matrix_multiply.md index 9fb3519b..81f1383e 100644 --- a/docs/en/tutorial-08-matrix_multiply.md +++ b/docs/en/tutorial-08-matrix_multiply.md @@ -172,7 +172,7 @@ Obviously, our framework can not interrupt a computing task because it's a user ... }; ~~~ -This create_thread_task function needs to pass two more parameters, seconds and nanoseconds. If the running time of func reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_ABORTED. But the task routine will continue to run till the end. +This create_thread_task function needs to pass two more parameters, seconds and nanoseconds. If the running time of func reaches the seconds+nanosconds time limit, the task callback directly, and the state is WFT_STATE_SYS_ERROR and the error is ETIMEDOUT. But the task routine will continue to run till the end. # Symmetry of the algorithm and the protocol diff --git a/docs/tutorial-08-matrix_multiply.md b/docs/tutorial-08-matrix_multiply.md index ed2fbae4..dc16209f 100644 --- a/docs/tutorial-08-matrix_multiply.md +++ b/docs/tutorial-08-matrix_multiply.md @@ -164,7 +164,7 @@ public: }; ~~~ 参数seconds和nanoseconds构成了运行时限。在这里,nanoseconds的取值范围在\[0,1000000000)。 -当任务无法在运行时限内结束,会直接回到callback,并且任务的状态为WFT_STATE_ABORTED。 +当任务无法在运行时限内结束,会直接回到callback,并且任务的状态为WFT_STATE_SYS_ERROR且错误码为ETIMEDOUT。 还是用matrix_multiply的例子,我们可以这样写: ~~~cpp void callback(MMTask *task) // MMtask = WFThreadTask @@ -172,12 +172,12 @@ void callback(MMTask *task) // MMtask = WFThreadTask MMInput *input = task->get_input(); MMOutput *output = task->get_output(); - if (task->get_state() == WFT_STATE_ABORTED) + if (task->get_state() == WFT_STATE_SYS_ERROR && task->get_error() == ETIMEDOUT) { printf("Run out of time.\n"); return; } - + assert(task->get_state() == WFT_STATE_SUCCESS) if (output->error) @@ -210,13 +210,13 @@ int main() ... } ~~~ -上面的示例,限制了任务运行时间不超过1毫秒,否则,以WFT_STATE_ABORTD的状态返回。 +上面的示例,限制了任务运行时间不超过1毫秒,否则,以WFT_STATE_SYS_ERROR的状态返回。 再次提醒,我们并不会中断用户的实际运行函数。当任务超时并callback,计算函数还会一直运行直到结束。 如果用户希望函数不再继续执行,需要在代码中自行加入检查点来实现这样的功能。可以在INPUT里加入flag,例如: ~~~cpp void callback(MMTask *task) // MMtask = WFThreadTask { - if (task->get_state() == WFT_STATE_ABORTED) + if (task->get_state() == WFT_STATE_SYS_ERROR && task->get_error() == ETIMEDOUT) { task->get_input()->flag = true; printf("Run out of time.\n"); diff --git a/src/factory/WFTaskFactory.cc b/src/factory/WFTaskFactory.cc index 0caaa7bf..fcb75b60 100644 --- a/src/factory/WFTaskFactory.cc +++ b/src/factory/WFTaskFactory.cc @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -665,8 +666,8 @@ void __WFTimedGoTask::timer_callback(WFTimerTask *timer) if (--task->ref == 3) { - task->state = WFT_STATE_ABORTED; - task->error = 0; + task->state = WFT_STATE_SYS_ERROR; + task->error = ETIMEDOUT; task->subtask_done(); } diff --git a/src/factory/WFTaskFactory.h b/src/factory/WFTaskFactory.h index 0902d4b6..43370f5e 100644 --- a/src/factory/WFTaskFactory.h +++ b/src/factory/WFTaskFactory.h @@ -313,7 +313,8 @@ public: FUNC&& func, ARGS&&... args); /* Create 'Go' task with running time limit in seconds plus nanoseconds. - * If time exceeded, state WFT_STATE_ABORTED will be got in callback. */ + * If time exceeded, state WFT_STATE_SYS_ERROR and error ETIMEDOUT + * will be got in callback. */ template static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds, const std::string& queue_name, diff --git a/src/factory/WFTaskFactory.inl b/src/factory/WFTaskFactory.inl index e5e71586..369fc17a 100644 --- a/src/factory/WFTaskFactory.inl +++ b/src/factory/WFTaskFactory.inl @@ -822,8 +822,8 @@ void __WFTimedThreadTask::timer_callback(WFTimerTask *timer) if (--task->ref == 3) { - task->state = WFT_STATE_ABORTED; - task->error = 0; + task->state = WFT_STATE_SYS_ERROR; + task->error = ETIMEDOUT; task->subtask_done(); }