@@ -33,56 +33,149 @@ typedef struct {
33
33
uint32_t magic ; /* Magic number for validation */
34
34
} mutex_t ;
35
35
36
- /* Initialize a mutex to an unlocked state */
36
+ /* Initialize a mutex to an unlocked state.
37
+ * @m: Pointer to mutex structure (must be non-NULL)
38
+ *
39
+ * Returns ERR_OK on success, ERR_FAIL on failure
40
+ */
37
41
int32_t mo_mutex_init (mutex_t * m );
38
42
39
- /* Destroy a mutex (fails if tasks are waiting) */
43
+ /* Destroy a mutex and free its resources.
44
+ * Fails if any tasks are currently waiting on the mutex.
45
+ * @m: Pointer to mutex structure (NULL is no-op)
46
+ *
47
+ * Returns ERR_OK on success, ERR_TASK_BUSY if tasks waiting, ERR_FAIL if
48
+ * invalid
49
+ */
40
50
int32_t mo_mutex_destroy (mutex_t * m );
41
51
42
- /* Acquire mutex lock (blocking) */
52
+ /* Acquire mutex lock, blocking if necessary.
53
+ * If the mutex is free, acquires it immediately. If owned by another task,
54
+ * the calling task is placed in the wait queue and blocked until the mutex
55
+ * becomes available. Non-recursive - returns error if caller already owns
56
+ * mutex.
57
+ * @m: Pointer to mutex structure (must be valid)
58
+ *
59
+ * Returns ERR_OK on success, ERR_TASK_BUSY if already owned by caller
60
+ */
43
61
int32_t mo_mutex_lock (mutex_t * m );
44
62
45
- /* Attempt to acquire mutex lock without blocking */
63
+ /* Attempt to acquire mutex lock without blocking.
64
+ * Returns immediately whether the lock was acquired or not.
65
+ * @m: Pointer to mutex structure (must be valid)
66
+ *
67
+ * Returns ERR_OK if acquired, ERR_TASK_BUSY if unavailable, ERR_FAIL if
68
+ * invalid
69
+ */
46
70
int32_t mo_mutex_trylock (mutex_t * m );
47
71
48
- /* Attempt to acquire mutex lock with timeout */
72
+ /* Attempt to acquire mutex lock with timeout.
73
+ * Blocks for up to 'ticks' scheduler ticks waiting for the mutex.
74
+ * @m: Pointer to mutex structure (must be valid)
75
+ * @ticks: Maximum time to wait in scheduler ticks (0 = trylock behavior)
76
+ *
77
+ * Returns ERR_OK if acquired, ERR_TIMEOUT if timed out, ERR_TASK_BUSY if
78
+ * recursive
79
+ */
49
80
int32_t mo_mutex_timedlock (mutex_t * m , uint32_t ticks );
50
81
51
- /* Release mutex lock */
82
+ /* Release mutex lock.
83
+ * If tasks are waiting, ownership is transferred to the next task in FIFO
84
+ * order. The released task is marked ready but may not run immediately
85
+ * depending on scheduler priority.
86
+ * @m: Pointer to mutex structure (must be valid)
87
+ *
88
+ * Returns ERR_OK on success, ERR_NOT_OWNER if caller doesn't own mutex
89
+ */
52
90
int32_t mo_mutex_unlock (mutex_t * m );
53
91
54
- /* Check if current task owns the mutex */
92
+ /* Check if the current task owns the specified mutex.
93
+ * @m: Pointer to mutex structure
94
+ *
95
+ * Returns true if current task owns mutex, false otherwise
96
+ */
55
97
bool mo_mutex_owned_by_current (mutex_t * m );
56
98
57
- /* Get the number of tasks waiting on the mutex */
99
+ /* Get the number of tasks currently waiting on the mutex.
100
+ * @m: Pointer to mutex structure
101
+ *
102
+ * Returns Number of waiting tasks, or -1 if mutex is invalid
103
+ */
58
104
int32_t mo_mutex_waiting_count (mutex_t * m );
59
105
60
106
/* Condition Variable Control Block
61
- * A condition variable allows tasks to wait for an arbitrary condition to
62
- * become true while a mutex is held.
107
+ *
108
+ * A condition variable allows tasks to wait for arbitrary conditions to become
109
+ * true. Tasks must hold an associated mutex when calling wait operations.
110
+ * The mutex is atomically released during the wait and re-acquired before
111
+ * the wait operation returns.
63
112
*/
64
113
typedef struct {
65
- list_t * waiters ; /* list of 'tcb_t *' blocked on this condition */
66
- uint32_t magic ; /* Magic number for validation */
114
+ list_t
115
+ * waiters ; /* List of 'tcb_t *' blocked on this condition (FIFO order) */
116
+ uint32_t magic ; /* Magic number for validation and corruption detection */
67
117
} cond_t ;
68
118
69
- /* Initialize a condition variable */
119
+ /* Initialize a condition variable.
120
+ * @c: Pointer to condition variable structure (must be non-NULL)
121
+ *
122
+ * Returns ERR_OK on success, ERR_FAIL on failure
123
+ */
70
124
int32_t mo_cond_init (cond_t * c );
71
125
72
- /* Destroy a condition variable (fails if tasks are waiting) */
126
+ /* Destroy a condition variable and free its resources.
127
+ * Fails if any tasks are currently waiting on the condition variable.
128
+ * @c: Pointer to condition variable structure (NULL is no-op)
129
+ *
130
+ * Returns ERR_OK on success, ERR_TASK_BUSY if tasks waiting, ERR_FAIL if
131
+ * invalid
132
+ */
73
133
int32_t mo_cond_destroy (cond_t * c );
74
134
75
- /* Wait on condition variable (atomically releases mutex) */
135
+ /* Wait on condition variable (atomically releases mutex).
136
+ * The calling task must own the specified mutex. The mutex is atomically
137
+ * released and the task blocks until another task signals the condition.
138
+ * Upon waking, the mutex is re-acquired before returning.
139
+ * @c: Pointer to condition variable structure (must be valid)
140
+ * @m: Pointer to mutex structure that caller must own
141
+ *
142
+ * Returns ERR_OK on success, ERR_NOT_OWNER if caller doesn't own mutex
143
+ */
76
144
int32_t mo_cond_wait (cond_t * c , mutex_t * m );
77
145
78
- /* Wait on condition variable with timeout */
146
+ /* Wait on condition variable with timeout.
147
+ * Like mo_cond_wait(), but limits wait time to specified number of ticks.
148
+ * @c: Pointer to condition variable structure (must be valid)
149
+ * @m: Pointer to mutex structure that caller must own
150
+ * @ticks: Maximum time to wait in scheduler ticks (0 = immediate timeout)
151
+ *
152
+ * Returns ERR_OK if signaled, ERR_TIMEOUT if timed out, ERR_NOT_OWNER if not
153
+ * owner
154
+ */
79
155
int32_t mo_cond_timedwait (cond_t * c , mutex_t * m , uint32_t ticks );
80
156
81
- /* Signal one waiting task */
157
+ /* Signal one waiting task.
158
+ * Wakes up the oldest task waiting on the condition variable (FIFO order).
159
+ * The signaled task will attempt to re-acquire the associated mutex.
160
+ * @c: Pointer to condition variable structure (must be valid)
161
+ *
162
+ * Returns ERR_OK on success, ERR_FAIL if invalid
163
+ */
82
164
int32_t mo_cond_signal (cond_t * c );
83
165
84
- /* Signal all waiting tasks */
166
+ /* Signal all waiting tasks.
167
+ * Wakes up all tasks waiting on the condition variable. All tasks will
168
+ * attempt to re-acquire their associated mutexes, but only one will succeed
169
+ * immediately (others will block on the mutex).
170
+ * @c: Pointer to condition variable structure (must be valid)
171
+ *
172
+ * Returns ERR_OK on success, ERR_FAIL if invalid
173
+ */
85
174
int32_t mo_cond_broadcast (cond_t * c );
86
175
87
- /* Get the number of tasks waiting on the condition variable */
176
+ /* Get the number of tasks currently waiting on the condition variable.
177
+ * @c: Pointer to condition variable structure
178
+ *
179
+ * Returns Number of waiting tasks, or -1 if condition variable is invalid
180
+ */
88
181
int32_t mo_cond_waiting_count (cond_t * c );
0 commit comments