Skip to content

Commit 67fdcbb

Browse files
committed
fixup: synchronization of rt_thread signal
1 parent 1f9c8cc commit 67fdcbb

10 files changed

Lines changed: 161 additions & 97 deletions

File tree

components/libc/compilers/common/include/sys/time.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ int8_t rt_tz_is_dst(void);
4646

4747
struct itimerspec;
4848

49-
#ifndef _TIMEVAL_DEFINED
49+
#if defined(RT_USING_SMART) && !defined(_TIMEVAL_DEFINED)
5050
#define _TIMEVAL_DEFINED
5151
struct timeval
5252
{
5353
time_t tv_sec; /* seconds */
5454
suseconds_t tv_usec; /* and microseconds */
5555
};
56-
#endif /* _TIMEVAL_DEFINED */
56+
#endif /* defined(RT_USING_SMART) && !defined(_TIMEVAL_DEFINED) */
5757

5858
#if defined(_GNU_SOURCE) && (defined(__x86_64__) || defined(__i386__) || defined(RT_USING_SMART))
5959
/* linux x86 platform gcc use! */

examples/utest/testcases/kernel/mutex_tc.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
* 2021-09.01 luckyzjq the first version
99
* 2023-09-15 xqyjlj change stack size in cpu64
1010
*/
11+
#define __RTT_IPC_SOURCE__
1112

1213
#include <rtthread.h>
1314
#include <stdlib.h>
1415
#include "utest.h"
1516

1617
#ifdef ARCH_CPU_64BIT
17-
#define THREAD_STACKSIZE 4096
18+
#define THREAD_STACKSIZE 8192
1819
#else
19-
#define THREAD_STACKSIZE 1024
20+
#define THREAD_STACKSIZE 4096
2021
#endif
2122

2223
static struct rt_mutex static_mutex;
@@ -241,7 +242,7 @@ static void static_thread1_entry(void *param)
241242

242243
/* thread3 hode mutex thread2 take mutex */
243244
/* check thread2 and thread3 priority */
244-
if (tid2->current_priority != tid3->current_priority)
245+
if (SCHED_PRIV(tid2).current_priority != SCHED_PRIV(tid3).current_priority)
245246
{
246247
uassert_true(RT_FALSE);
247248
}
@@ -550,7 +551,7 @@ static void dynamic_thread1_entry(void *param)
550551

551552
/* thread3 hode mutex thread2 take mutex */
552553
/* check thread2 and thread3 priority */
553-
if (tid2->current_priority != tid3->current_priority)
554+
if (SCHED_PRIV(tid2).current_priority != SCHED_PRIV(tid3).current_priority)
554555
{
555556
uassert_true(RT_FALSE);
556557
}

examples/utest/testcases/kernel/signal_tc.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
#include <rtthread.h>
2323
#include "utest.h"
2424

25-
int recive_sig = 0;
25+
static volatile int recive_sig = 0;
26+
static struct rt_semaphore _received_signal;
2627

2728
void sig_handle_default(int signo)
2829
{
@@ -125,12 +126,15 @@ void rt_signal_wait_thread(void *parm)
125126
(void)sigaddset(&selectset, SIGUSR1);
126127

127128
/* case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. */
128-
if (rt_signal_wait(&selectset, &recive_si, RT_TICK_PER_SECOND) != RT_EOK)
129+
if (rt_signal_wait((void *)&selectset, &recive_si, RT_TICK_PER_SECOND) != RT_EOK)
129130
{
130131
return;
131132
}
132133

133134
recive_sig = recive_si.si_signo;
135+
136+
LOG_I("received signal %d", recive_sig);
137+
rt_sem_release(&_received_signal);
134138
}
135139

136140
static void rt_signal_wait_test(void)
@@ -147,7 +151,7 @@ static void rt_signal_wait_test(void)
147151
rt_thread_mdelay(1);
148152
/* case 5:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: kill, should received. */
149153
uassert_int_equal(rt_thread_kill(t1, SIGUSR1), RT_EOK);
150-
rt_thread_mdelay(1);
154+
rt_sem_take(&_received_signal, RT_WAITING_FOREVER);
151155
uassert_int_equal(recive_sig, SIGUSR1);
152156

153157
return;
@@ -167,19 +171,23 @@ static void rt_signal_wait_test2(void)
167171
/* case 6:rt_signal_wait, two thread, thread1: install and unmask, then wait 1s; thread2: sleep 2s then kill, should can't received. */
168172
rt_thread_mdelay(2000);
169173
uassert_int_equal(rt_thread_kill(t1, SIGUSR1), RT_EOK);
170-
rt_thread_mdelay(1);
174+
uassert_int_not_equal(
175+
rt_sem_take(&_received_signal, 1),
176+
RT_EOK);
171177
uassert_int_not_equal(recive_sig, SIGUSR1);
172178

173179
return;
174180
}
175181

176182
static rt_err_t utest_tc_init(void)
177183
{
184+
rt_sem_init(&_received_signal, "utest", 0, RT_IPC_FLAG_PRIO);
178185
return RT_EOK;
179186
}
180187

181188
static rt_err_t utest_tc_cleanup(void)
182189
{
190+
rt_sem_detach(&_received_signal);
183191
return RT_EOK;
184192
}
185193

examples/utest/testcases/kernel/thread_tc.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* 2021-10.11 mazhiyuan add idle, yield, suspend, control, priority, delay_until
1010
*/
1111

12+
#define __RTT_IPC_SOURCE__ /* include internal API for utest */
13+
1214
#include <rtthread.h>
1315
#include <stdlib.h>
1416
#include "utest.h"
@@ -56,7 +58,7 @@ static void test_dynamic_thread(void)
5658
thread1_entry,
5759
(void *)1,
5860
THREAD_STACK_SIZE,
59-
__current_thread->current_priority + 1,
61+
UTEST_THR_PRIORITY + 1,
6062
THREAD_TIMESLICE - 5);
6163
if (tid1 == RT_NULL)
6264
{
@@ -105,7 +107,7 @@ static void test_static_thread(void)
105107
(void *)2,
106108
&thread2_stack[0],
107109
sizeof(thread2_stack),
108-
__current_thread->current_priority + 1,
110+
UTEST_THR_PRIORITY + 1,
109111
THREAD_TIMESLICE);
110112
if (ret_init != RT_EOK)
111113
{
@@ -139,10 +141,11 @@ static void test_static_thread(void)
139141

140142
static void thread3_entry(void *parameter)
141143
{
142-
rt_tick_t tick;
144+
rt_tick_t tick, latency_tick;
143145
tick = rt_tick_get();
144146
rt_thread_delay(15);
145-
if (rt_tick_get() - tick > 16)
147+
latency_tick = rt_tick_get() - tick;
148+
if (latency_tick > 16 || latency_tick < 15)
146149
{
147150
tid3_finish_flag = 1;
148151
tid3_delay_pass_flag = 0;
@@ -160,7 +163,7 @@ static void test_thread_delay(void)
160163
thread3_entry,
161164
RT_NULL,
162165
THREAD_STACK_SIZE,
163-
__current_thread->current_priority - 1,
166+
UTEST_THR_PRIORITY - 1,
164167
THREAD_TIMESLICE);
165168
if (tid3 == RT_NULL)
166169
{
@@ -210,7 +213,7 @@ static void test_idle_hook(void)
210213
thread4_entry,
211214
RT_NULL,
212215
THREAD_STACK_SIZE,
213-
__current_thread->current_priority - 1,
216+
UTEST_THR_PRIORITY - 1,
214217
THREAD_TIMESLICE);
215218
if (tid4 == RT_NULL)
216219
{
@@ -264,7 +267,7 @@ static void test_thread_yield(void)
264267
thread5_entry,
265268
RT_NULL,
266269
THREAD_STACK_SIZE,
267-
__current_thread->current_priority - 1,
270+
UTEST_THR_PRIORITY - 1,
268271
THREAD_TIMESLICE);
269272
if (tid5 == RT_NULL)
270273
{
@@ -283,7 +286,7 @@ static void test_thread_yield(void)
283286
thread6_entry,
284287
RT_NULL,
285288
THREAD_STACK_SIZE,
286-
__current_thread->current_priority - 1,
289+
UTEST_THR_PRIORITY - 1,
287290
THREAD_TIMESLICE);
288291
if (tid6 == RT_NULL)
289292
{
@@ -319,12 +322,13 @@ static void test_thread_control(void)
319322
{
320323
rt_err_t ret_control = -RT_ERROR;
321324
rt_err_t rst_delete = -RT_ERROR;
325+
rt_sched_lock_level_t slvl;
322326

323327
tid7 = rt_thread_create("thread7",
324328
thread7_entry,
325329
RT_NULL,
326330
THREAD_STACK_SIZE,
327-
__current_thread->current_priority + 1,
331+
UTEST_THR_PRIORITY + 1,
328332
THREAD_TIMESLICE);
329333
if (tid7 == RT_NULL)
330334
{
@@ -342,12 +346,17 @@ static void test_thread_control(void)
342346
}
343347
rt_thread_mdelay(200);
344348
rt_thread_control(tid7, RT_THREAD_CTRL_CHANGE_PRIORITY, &change_priority);
345-
if (tid7->current_priority != change_priority)
349+
350+
rt_sched_lock(&slvl);
351+
if (rt_sched_thread_get_curr_prio(tid7) != change_priority)
346352
{
347353
LOG_E("rt_thread_control failed!");
348354
uassert_false(1);
355+
rt_sched_unlock(slvl);
349356
goto __exit;
350357
}
358+
rt_sched_unlock(slvl);
359+
351360
rst_delete = rt_thread_control(tid7, RT_THREAD_CTRL_CLOSE, RT_NULL);
352361
if (rst_delete != RT_EOK)
353362
{
@@ -380,7 +389,7 @@ static void test_thread_priority(void)
380389
thread8_entry,
381390
RT_NULL,
382391
THREAD_STACK_SIZE,
383-
__current_thread->current_priority - 1,
392+
UTEST_THR_PRIORITY - 1,
384393
THREAD_TIMESLICE);
385394
if (tid8 == RT_NULL)
386395
{
@@ -495,7 +504,7 @@ void test_timeslice(void)
495504
timeslice_cntB2 = 0;
496505

497506
tidA = rt_thread_create("timeslice", test_timeslice_threadA_entry, RT_NULL,
498-
2048, __current_thread->current_priority + 1, 10);
507+
2048, UTEST_THR_PRIORITY + 1, 10);
499508
if (!tidA)
500509
{
501510
LOG_E("rt_thread_create failed!");
@@ -512,7 +521,7 @@ void test_timeslice(void)
512521
}
513522

514523
tidB1 = rt_thread_create("timeslice", test_timeslice_threadB1_entry, RT_NULL,
515-
2048, __current_thread->current_priority + 2, 2);
524+
2048, UTEST_THR_PRIORITY + 2, 2);
516525
if (!tidB1)
517526
{
518527
LOG_E("rt_thread_create failed!");
@@ -529,7 +538,7 @@ void test_timeslice(void)
529538
}
530539

531540
tidB2 = rt_thread_create("timeslice", test_timeslice_threadB2_entry, RT_NULL,
532-
2048, __current_thread->current_priority + 2, 2);
541+
2048, UTEST_THR_PRIORITY + 2, 2);
533542
if (!tidB2)
534543
{
535544
LOG_E("rt_thread_create failed!");
@@ -655,7 +664,7 @@ void test_thread_yield_nosmp(void)
655664
// thread9_entry,
656665
// RT_NULL,
657666
// THREAD_STACK_SIZE,
658-
// __current_thread->current_priority + 1,
667+
// UTEST_THR_PRIORITY + 1,
659668
// THREAD_TIMESLICE);
660669
// if (tid == RT_NULL)
661670
// {
@@ -695,7 +704,7 @@ void test_thread_yield_nosmp(void)
695704
static rt_err_t utest_tc_init(void)
696705
{
697706
__current_thread = rt_thread_self();
698-
change_priority = __current_thread->current_priority + 5;
707+
change_priority = UTEST_THR_PRIORITY + 5;
699708
tid3_delay_pass_flag = 0;
700709
tid3_finish_flag = 0;
701710
tid4_finish_flag = 0;

src/ipc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ static rt_bool_t _check_and_update_prio(rt_thread_t thread, rt_mutex_t mutex)
892892
return do_sched;
893893
}
894894

895-
static rt_bool_t _mutex_before_delete_detach(rt_mutex_t mutex)
895+
static void _mutex_before_delete_detach(rt_mutex_t mutex)
896896
{
897897
rt_ubase_t level;
898898
rt_sched_lock_level_t slvl;

src/scheduler_comm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ rt_err_t rt_sched_tick_increase(void)
160160
}
161161
else
162162
{
163-
SCHED_CTX(thread).stat |= RT_THREAD_STAT_YIELD;
163+
rt_sched_thread_yield(thread);
164164

165165
/* request a rescheduling even though we are probably in an ISR */
166166
rt_sched_unlock_n_resched(slvl);
@@ -282,7 +282,7 @@ rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t pr
282282
SCHED_CTX(thread).stat = RT_THREAD_INIT;
283283

284284
/* insert thread to schedule queue again */
285-
rt_sched_remove_thread(thread);
285+
rt_sched_insert_thread(thread);
286286
}
287287
else
288288
{

src/scheduler_mp.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ void rt_scheduler_ipi_handler(int vector, void *param)
566566
rt_schedule();
567567
}
568568

569+
/**
570+
* @brief Lock the system scheduler
571+
*
572+
* @param plvl pointer to the object where lock level stores to
573+
*
574+
* @return rt_err_t RT_EOK
575+
*/
569576
rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl)
570577
{
571578
rt_base_t level;
@@ -578,6 +585,14 @@ rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl)
578585
return RT_EOK;
579586
}
580587

588+
/**
589+
* @brief Unlock the system scheduler
590+
* @note this will not cause the scheduler to do a reschedule
591+
*
592+
* @param level the lock level of previous call to rt_sched_lock()
593+
*
594+
* @return rt_err_t RT_EOK
595+
*/
581596
rt_err_t rt_sched_unlock(rt_sched_lock_level_t level)
582597
{
583598
SCHEDULER_UNLOCK(level);
@@ -701,7 +716,7 @@ static rt_thread_t _prepare_context_switch_locked(int cpu_id,
701716
static void _sched_thread_preprocess_signal(struct rt_thread *current_thread)
702717
{
703718
/* should process signal? */
704-
if (rt_sched_is_thread_suspended(current_thread))
719+
if (rt_sched_thread_is_suspended(current_thread))
705720
{
706721
/* if current_thread signal is in pending */
707722
if ((SCHED_CTX(current_thread).stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL_PENDING)
@@ -740,21 +755,20 @@ static void _sched_thread_process_signal(struct rt_thread *current_thread)
740755
/* lock is released above */
741756
}
742757

743-
#define SCHED_THREAD_PREPROCESS_SIGNAL(curthr)
744-
do
745-
{
746-
SCHEDULER_CONTEXT_LOCK(current_thread);
747-
_sched_thread_preprocess_signal(curthr);
748-
SCHEDULER_CONTEXT_UNLOCK(current_thread);
749-
} while (0)
758+
#define SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, curthr) \
759+
do \
760+
{ \
761+
SCHEDULER_CONTEXT_LOCK(pcpu); \
762+
_sched_thread_preprocess_signal(curthr); \
763+
SCHEDULER_CONTEXT_UNLOCK(pcpu); \
764+
} while (0)
750765
#define SCHED_THREAD_PREPROCESS_SIGNAL_LOCKED(curthr) \
751766
_sched_thread_preprocess_signal(curthr)
752767
#define SCHED_THREAD_PROCESS_SIGNAL(curthr) _sched_thread_process_signal(curthr)
753768

769+
#else /* ! RT_USING_SIGNALS */
754770

755-
#else
756-
757-
#define SCHED_THREAD_PREPROCESS_SIGNAL(curthr)
771+
#define SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, curthr)
758772
#define SCHED_THREAD_PREPROCESS_SIGNAL_LOCKED(curthr)
759773
#define SCHED_THREAD_PROCESS_SIGNAL(curthr)
760774
#endif /* RT_USING_SIGNALS */
@@ -873,7 +887,7 @@ void rt_schedule(void)
873887
SCHEDULER_ENTER_CRITICAL(current_thread);
874888

875889
/* prepare current_thread for processing if signals existed */
876-
SCHED_THREAD_PREPROCESS_SIGNAL(current_thread);
890+
SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, current_thread);
877891

878892
/* whether caller had locked the local scheduler already */
879893
if (SCHED_CTX(current_thread).critical_lock_nest > 1)
@@ -948,7 +962,7 @@ void rt_scheduler_do_irq_switch(void *context)
948962
/* forbid any recursive entries of schedule() */
949963
SCHEDULER_ENTER_CRITICAL(current_thread);
950964

951-
SCHED_THREAD_PREPROCESS_SIGNAL(current_thread);
965+
SCHED_THREAD_PREPROCESS_SIGNAL(pcpu, current_thread);
952966

953967
/* any pending scheduling existed? */
954968
if (pcpu->irq_switch_flag == 0)

0 commit comments

Comments
 (0)