CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
mptask.cxh
1/// @brief Multiphase task with internal state machine
2///
3/// @defgroup mptask MultiphaseTask
4/// @ingroup tq_task
5/// @{
6///
7/// MultiphaseTask (MPTask) extends ComplexTask for operations that proceed through multiple
8/// distinct phases. Instead of implementing a single run() method, you add phase functions
9/// that execute sequentially based on return values.
10///
11/// **Key features:**
12/// - Phase functions are called in order from an array
13/// - Each phase returns a result that determines what happens next
14/// - Separate fail phases can be added for error handling
15/// - Can be greedy (execute all phases without yielding) or cooperative (yield between phases)
16///
17/// **Common workflow:**
18/// 1. Add normal phases with `mptaskAddPhases()`
19/// 2. Optionally add fail phases with `mptaskAddFailPhases()`
20/// 3. Override `finish()` if you need custom cleanup or want to change the final result
21///
22/// **Example:**
23/// @code
24/// static MPTPhaseFunc myPhases[] = {
25/// (MPTPhaseFunc)phase1,
26/// (MPTPhaseFunc)phase2,
27/// (MPTPhaseFunc)phase3,
28/// };
29///
30/// static MPTPhaseFunc myFailPhases[] = {
31/// (MPTPhaseFunc)cleanup,
32/// };
33///
34/// void mytaskInit(MyTask *self) {
35/// mptaskAddPhases(self, myPhases);
36/// mptaskAddFailPhases(self, myFailPhases);
37/// }
38/// @endcode
39#include "complextask.cxh"
40
41/// Flags controlling MultiphaseTask behavior
42enum MultiphaseTaskFlagsEnum {
43 MPTASK_Greedy = 0x10 ///< Execute all phases immediately without yielding to other tasks
44};
45
46/// Phase function signature for multiphase tasks.
47/// @param self Pointer to the MultiphaseTask (cast to your derived type)
48/// @param tq Task queue the phase is running on
49/// @param worker Worker thread executing this phase
50/// @param tcon Task control structure for output parameters
51/// @return Task result code (TASK_Result_Success, TASK_Result_Failure, etc.)
52typedef uint32 (*MPTPhaseFunc)(void *self, TaskQueue* tq, TQWorker* worker, TaskControl *tcon);
53saDeclare(MPTPhaseFunc);
54
55/// mptaskAddPhases(MultiphaseTask* self, MPTPhaseFunc parr[])
56///
57/// Adds normal execution phases from a static array.
58/// The array size is automatically calculated.
59#define mptaskAddPhases(self, parr) mptask_addPhases(self, sizeof(parr) / sizeof((parr)[0]), (parr), false)
60
61/// mptaskAddFailPhases(MultiphaseTask* self, MPTPhaseFunc parr[])
62///
63/// Adds failure handling phases from a static array.
64/// These phases only run if a normal phase returns TASK_Result_Failure.
65#define mptaskAddFailPhases(self, parr) mptask_addPhases(self, sizeof(parr) / sizeof((parr)[0]), (parr), true)
66
67/// Multiphase task with internal state machine for sequential execution phases.
68[methodprefix mptask] abstract class MultiphaseTask extends ComplexTask {
69 [noinit] sarray:MPTPhaseFunc phases; ///< Normal execution phases
70 [noinit] sarray:MPTPhaseFunc failphases; ///< Failure handling phases
71 uint32 _phase; ///< Current phase index
72 bool _fail; ///< True if currently executing fail phases
73
74 /// Called after all phases complete.
75 /// Can be overridden to perform additional cleanup or change the final result.
76 /// @param result Result code from the last phase
77 /// @param tcon Task control structure
78 /// @return Final result code for the task
79 uint32 finish(uint32 result, TaskControl *tcon);
80
81 /// Internal method to add phases from array.
82 /// Use mptaskAddPhases() or mptaskAddFailPhases() macros instead.
83 /// @param num Number of phases in array
84 /// @param parr Array of phase functions
85 /// @param fail If true, these are fail phases
86 unbound void _addPhases(int32 num, MPTPhaseFunc parr[], bool fail);
87
88 override run;
89 init();
90}
91/// @}