Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cancelable timer task, update thread/go task's state when timeout. #1389

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/about-go-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ 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<class FUNC, class... ARGS>
static WFGoTask *create_timedgo_task(time_t seconds, long nanoseconds,
const std::string& queue_name,
FUNC&& func, ARGS&&... args);
};
~~~
相比创建普通的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,可能就是一个错误了。例如:
Expand Down Expand Up @@ -99,7 +99,7 @@ int main()
{
...
}
else // state == WFT_STATE_ABORTED. // 超过运行时间限制
else // state == WFT_STATE_SYS_ERROR && error == ETIMEDOUT // 超过运行时间限制
{
...
}
Expand All @@ -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 // 超过运行时间限制
{
...
}
Expand Down
4 changes: 2 additions & 2 deletions docs/en/about-go-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<class FUNC, class... ARGS>
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).


Expand Down
2 changes: 1 addition & 1 deletion docs/en/tutorial-08-matrix_multiply.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions docs/tutorial-08-matrix_multiply.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,20 @@ 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<MMInput, MMOutput>
{
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)
Expand Down Expand Up @@ -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<MMInput, MMOutput>
{
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");
Expand Down
Loading