Skip to content

Commit c00f5b9

Browse files
committed
fix unexpected wakeup when waiting mutex
1 parent b924ee3 commit c00f5b9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Diff for: examples/utest/testcases/kernel/mutex_pi_tc.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static void test_mutex_pi_recursive_prio_update(void)
233233
rt_thread_startup(t_hi_prio);
234234

235235
rt_thread_mdelay(1500);
236-
236+
237237
for (int i = 0; i < 4; i++)
238238
{
239239
uassert_true(RT_SCHED_PRIV(t[i]).current_priority == 3);
@@ -258,7 +258,7 @@ static void test_mutex_waiter_to_wakeup_entry(void *para)
258258
{
259259
rt_thread_mdelay(1000);
260260
rt_err_t err = rt_mutex_take(&m[3], RT_WAITING_FOREVER);
261-
uassert_true(err == -RT_ETIMEOUT);
261+
uassert_true(err == -RT_EAGAIN);
262262
}
263263

264264
static void wakeup_func(void *para)
@@ -285,7 +285,7 @@ static void test_mutex_pi_wakeup_mutex_waiter(void)
285285
rt_timer_init(&wakeup_timer, "wakeup_timer", wakeup_func, RT_NULL, rt_tick_from_millisecond(2000), RT_TIMER_FLAG_ONE_SHOT);
286286
rt_timer_start(&wakeup_timer);
287287
rt_thread_mdelay(1500);
288-
288+
289289
for (int i = 0; i < 4; i++)
290290
{
291291
uassert_true(RT_SCHED_PRIV(t[i]).current_priority == 3);
@@ -304,6 +304,7 @@ static void test_mutex_pi_wakeup_mutex_waiter(void)
304304
{
305305
rt_mutex_detach(&m[i]);
306306
}
307+
rt_timer_detach(&wakeup_timer);
307308
}
308309

309310
static rt_err_t utest_tc_init(void)

Diff for: src/ipc.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1448,13 +1448,13 @@ static rt_err_t _rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout, int suspend
14481448

14491449
rt_spin_lock(&(mutex->spinlock));
14501450

1451-
if (thread->error == RT_EOK)
1451+
if (mutex->owner == thread)
14521452
{
14531453
/**
14541454
* get mutex successfully
14551455
* Note: assert to avoid an unexpected resume
14561456
*/
1457-
RT_ASSERT(mutex->owner == thread);
1457+
RT_ASSERT(thread->error == RT_EOK);
14581458
}
14591459
else
14601460
{
@@ -1466,6 +1466,11 @@ static rt_err_t _rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout, int suspend
14661466
/* get value first before calling to other APIs */
14671467
ret = thread->error;
14681468

1469+
if (ret == RT_EOK)
1470+
{
1471+
ret = -RT_AGAIN;
1472+
}
1473+
14691474
rt_sched_lock(&slvl);
14701475

14711476
/**

0 commit comments

Comments
 (0)