CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
taskresource.cxh
1/// @brief Abstract base class for shared resources
2///
3/// @defgroup taskresource Task Resources
4/// @ingroup taskqueue
5/// @{
6///
7/// TaskResource is the abstract base for all shared resources that tasks can acquire
8/// for exclusive access. Resources enable serialized access patterns without blocking
9/// worker threads.
10///
11/// **Resource lifecycle:**
12/// 1. Task calls ctaskRequireResource() to add a resource requirement
13/// 2. Queue calls registerTask() to register the task with the resource
14/// 3. Queue calls canAcquire() to check if acquisition is possible
15/// 4. Queue calls tryAcquire() when resource might be available (MUST NOT block)
16/// 5. Task runs with exclusive resource access
17/// 6. Queue calls release() when task completes
18///
19/// **Concrete implementations:**
20/// - TRMutex - Simple mutex with approximate FIFO ordering
21/// - TRFifo - Strict FIFO ordering, efficient for many waiters
22/// - TRLifo - Stack ordering (most recent first)
23///
24/// Resources handle their own wakeup mechanisms, typically by advancing waiting tasks
25/// out of the defer queue when the resource becomes available.
26
27// Pre-declare the task
28class ComplexTask;
29
30/// Abstract base for shared resources with exclusive access.
31abstract class TaskResource {
32 /// Register task with resource for later notification.
33 /// Reserves a slot in FIFO/LIFO queues. Registration is consumed by successful acquisition.
34 /// @param task Task to register
35 /// @return true if registration was successful
36 [abstract] bool registerTask(ComplexTask *task);
37
38 /// Check if task can attempt to acquire the resource now.
39 /// @param task Task checking acquisition possibility
40 /// @return true if tryAcquire() should be called
41 [abstract] bool canAcquire(ComplexTask *task);
42
43 /// Try to acquire resource exclusively for task. MUST NOT block.
44 /// State tracking (owned flag) is maintained by caller (TaskRequiresResource).
45 /// @param task Task acquiring resource
46 /// @return true if resource was acquired
47 [abstract] bool tryAcquire(ComplexTask *task);
48
49 /// Release resource previously acquired by task.
50 /// State tracking is maintained by caller.
51 /// @param task Task releasing resource
52 [abstract] void release(ComplexTask *task);
53}
54
55/// @}