Files CCore/inc/task/TaskCore.h CCore/src/task/TaskCore.cpp
ForcedTimeout can be used to simulate a timeout. It can be useful to interrupt a blocking call.
class ForcedTimeout : NoCopy
{
....
public:
ForcedTimeout();
~ForcedTimeout() { clear(); }
bool clear();
// functions
Function<void (void)> function_abort();
Function<void (void)> function_abort_int();
Function<void (void)> function_abort_any();
};
Lets consider an example.
ForcedTimeout fto; Ticker ticker(fto.function_abort_int()); ticker.start(3_sec); Sem sem; sem.take(100_sec); Printf(Con,"done #;\n",fto.clear());
In this example we create the ForcedTimeout object. Then we take one of abort functions, provided by the object, and create and start a Ticker. The ticker will call this function after the 3 seconds delay. Abort function interrupts any call of a timed blocking synchronization object method. In the example we are using the Sem. Without ForcedTimeout the call sem.take(100_sec) would block the execution for 100 seconds. Abort function is interrupting the wait after 3 seconds.
Three variants of the abort function are called from different execution contexts: function_abort() from a task context, function_abort_int() from the interrupt context and function_abort_any() from everywhere.
Only timed variants of blocking methods can be interrupted.
An abort function sets the forced timeout state for the task. If this state is on, the task cannot be timed blocked, and any timed block is released.
The method clear() can be used to clear the forced timeout state from the task. The return value is this state, before is is cleared.
ForcedTimeout destructor clears the forced timeout state.