CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
taskrequiresgate.cxh
1/// @brief Gate-based event synchronization for tasks
2///
3/// @defgroup taskrequires_gate Task Gates
4/// @ingroup taskrequires
5/// @{
6///
7/// Gates provide a lightweight synchronization primitive for tasks waiting on one-time events.
8/// Similar to task dependencies, but more flexible and efficient when multiple tasks need to
9/// wait for the same external event.
10///
11/// **Gate states:**
12/// - **Closed** (initial) - Tasks depending on this gate will wait
13/// - **Open** - Gate has been opened, all waiting tasks are released, future tasks pass through
14/// - **Sealed** - Gate is permanently closed, any task depending on it will fail immediately
15///
16/// **Use cases:**
17/// - Waiting for initialization to complete before allowing tasks to proceed
18/// - Coordinating shutdown across multiple tasks
19/// - External event notification (network connection, file ready, etc.)
20///
21/// Gates track progress timestamps which prevent monitoring from reporting stuck tasks.
22/// Call progress() periodically while work is ongoing but the gate isn't ready to open.
23#include "taskrequires.cxh"
24
25#include <cx/thread/mutex.h>
26#include <cx/taskqueue/task/complextask.cxh>
27
28/// One-time event gate for task synchronization.
29class TRGate {
30 string name; ///< Gate name for logging and debugging
31 atomic:uint32 state; ///< Current gate state (closed/open/sealed)
32
33 /// Open the gate, releasing all waiting tasks.
34 /// Future tasks depending on this gate will not wait.
35 /// @return true if gate was opened (was previously closed)
36 bool open();
37
38 /// Seal the gate permanently.
39 /// Any task depending on a sealed gate will fail immediately.
40 /// @return true if gate was sealed
41 bool seal();
42
43 /// Update progress timestamp without opening gate.
44 /// Prevents monitoring from reporting tasks as stalled.
45 void progress();
46
47 /// Register a task to be advanced when gate opens.
48 /// @param task Task to register
49 /// @return true if registration was successful
50 bool registerTask(ComplexTask *task);
51
52 Mutex _wlmtx;
53 sarray:object:ComplexTask _waitlist;
54 int64 lastprogress; ///< Last time progress() was called
55
56 /// Create a new gate.
57 /// @param name Gate name for logging and debugging
58 /// @return New TRGate instance in closed state
59 factory create(strref name);
60}
61
62/// Requirement that waits for a gate to open.
63class TaskRequiresGate extends TaskRequires {
64 object:TRGate gate; ///< Gate to wait for
65
66 /// Create a gate requirement.
67 /// @param gate Gate that must be opened before task can run
68 /// @return New TaskRequiresGate instance
69 factory create([in] TRGate *gate);
70}
71
72/// @}