Skip to content

Commit 0892e41

Browse files
committed
Improve mutex for efficiency
This commit leverages existing scheduler timeout infrastructure instead of manual polling, reducing CPU usage while maintaining correctness and improving integration with the CPU scheduling mechanisms. In addition, it enhances ace condition handling by clearing delay values when granting ownership or signaling conditions.
1 parent ff94b81 commit 0892e41

File tree

2 files changed

+250
-129
lines changed

2 files changed

+250
-129
lines changed

include/sys/mutex.h

Lines changed: 112 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,56 +33,149 @@ typedef struct {
3333
uint32_t magic; /* Magic number for validation */
3434
} mutex_t;
3535

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+
*/
3741
int32_t mo_mutex_init(mutex_t *m);
3842

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+
*/
4050
int32_t mo_mutex_destroy(mutex_t *m);
4151

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+
*/
4361
int32_t mo_mutex_lock(mutex_t *m);
4462

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+
*/
4670
int32_t mo_mutex_trylock(mutex_t *m);
4771

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+
*/
4980
int32_t mo_mutex_timedlock(mutex_t *m, uint32_t ticks);
5081

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+
*/
5290
int32_t mo_mutex_unlock(mutex_t *m);
5391

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+
*/
5597
bool mo_mutex_owned_by_current(mutex_t *m);
5698

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+
*/
58104
int32_t mo_mutex_waiting_count(mutex_t *m);
59105

60106
/* 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.
63112
*/
64113
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 */
67117
} cond_t;
68118

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+
*/
70124
int32_t mo_cond_init(cond_t *c);
71125

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+
*/
73133
int32_t mo_cond_destroy(cond_t *c);
74134

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+
*/
76144
int32_t mo_cond_wait(cond_t *c, mutex_t *m);
77145

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+
*/
79155
int32_t mo_cond_timedwait(cond_t *c, mutex_t *m, uint32_t ticks);
80156

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+
*/
82164
int32_t mo_cond_signal(cond_t *c);
83165

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+
*/
85174
int32_t mo_cond_broadcast(cond_t *c);
86175

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+
*/
88181
int32_t mo_cond_waiting_count(cond_t *c);

0 commit comments

Comments
 (0)