Skip to content

Commit 7243f87

Browse files
committed
fixup: add compatible codes for thread structure
1 parent b7e5770 commit 7243f87

File tree

15 files changed

+97
-58
lines changed

15 files changed

+97
-58
lines changed

components/drivers/audio/audio_pipe.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ static void _rt_pipe_resume_writer(struct rt_audio_pipe *pipe)
2121
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_WR);
2222

2323
/* get suspended thread */
24-
thread = rt_list_entry(pipe->suspended_write_list.next,
25-
struct rt_thread,
26-
tlist);
24+
thread = RT_THREAD_LIST_NODE_ENTRY(pipe->suspended_write_list.next);
2725

2826
/* resume the write thread */
2927
rt_thread_resume(thread);
@@ -73,7 +71,7 @@ static rt_ssize_t rt_pipe_read(rt_device_t dev,
7371
rt_thread_suspend(thread);
7472
/* waiting on suspended read list */
7573
rt_list_insert_before(&(pipe->suspended_read_list),
76-
&(thread->tlist));
74+
&RT_THREAD_LIST_NODE(thread));
7775
rt_hw_interrupt_enable(level);
7876

7977
rt_schedule();
@@ -103,9 +101,7 @@ static void _rt_pipe_resume_reader(struct rt_audio_pipe *pipe)
103101
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
104102

105103
/* get suspended thread */
106-
thread = rt_list_entry(pipe->suspended_read_list.next,
107-
struct rt_thread,
108-
tlist);
104+
thread = RT_THREAD_LIST_NODE_ENTRY(pipe->suspended_read_list.next);
109105

110106
/* resume the read thread */
111107
rt_thread_resume(thread);
@@ -161,7 +157,7 @@ static rt_ssize_t rt_pipe_write(rt_device_t dev,
161157
rt_thread_suspend(thread);
162158
/* waiting on suspended read list */
163159
rt_list_insert_before(&(pipe->suspended_write_list),
164-
&(thread->tlist));
160+
&RT_THREAD_LIST_NODE(thread));
165161
rt_hw_interrupt_enable(level);
166162

167163
rt_schedule();

components/finsh/cmd.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,22 @@ long list_thread(void)
217217

218218
#ifdef RT_USING_SMP
219219
/* no synchronization applied since it's only for debug */
220-
if (thread->sched_thread_ctx.oncpu != RT_CPU_DETACHED)
220+
if (SCHED_CTX(thread).oncpu != RT_CPU_DETACHED)
221221
rt_kprintf("%-*.*s %3d %3d %4d ", maxlen, RT_NAME_MAX,
222-
thread->parent.name, thread->sched_thread_ctx.oncpu,
223-
thread->sched_thread_ctx.bind_cpu,
224-
thread->sched_thread_ctx.sched_thread_priv.current_priority);
222+
thread->parent.name, SCHED_CTX(thread).oncpu,
223+
SCHED_CTX(thread).bind_cpu,
224+
SCHED_PRIV(thread).current_priority);
225225
else
226226
rt_kprintf("%-*.*s N/A %3d %4d ", maxlen, RT_NAME_MAX,
227227
thread->parent.name,
228-
thread->sched_thread_ctx.bind_cpu,
229-
(thread->sched_thread_ctx.sched_thread_priv).current_priority);
228+
SCHED_CTX(thread).bind_cpu,
229+
SCHED_PRIV(thread).current_priority);
230230

231231
#else
232232
/* no synchronization applied since it's only for debug */
233-
rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->parent.name, (thread->sched_thread_ctx.sched_thread_priv).current_priority);
233+
rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->parent.name, SCHED_PRIV(thread).current_priority);
234234
#endif /*RT_USING_SMP*/
235-
stat = (thread->sched_thread_ctx.stat & RT_THREAD_STAT_MASK);
235+
stat = (SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK);
236236
if (stat == RT_THREAD_READY) rt_kprintf(" ready ");
237237
else if ((stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK) rt_kprintf(" suspend");
238238
else if (stat == RT_THREAD_INIT) rt_kprintf(" init ");
@@ -258,7 +258,7 @@ long list_thread(void)
258258
thread->stack_size,
259259
(thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100
260260
/ thread->stack_size,
261-
(thread->sched_thread_ctx.sched_thread_priv).remaining_tick,
261+
SCHED_PRIV(thread).remaining_tick,
262262
rt_strerror(thread->error),
263263
thread);
264264
#endif

components/libc/posix/libdl/dlmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void dlmodule_destroy_subthread(struct rt_dlmodule *module, rt_thread_t thread)
203203
rt_enter_critical();
204204

205205
/* remove thread from thread_list (ready or defunct thread list) */
206-
rt_list_remove(&(thread->tlist));
206+
rt_list_remove(&RT_THREAD_LIST_NODE(thread));
207207

208208
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_CLOSE &&
209209
(thread->thread_timer.parent.type == (RT_Object_Class_Static | RT_Object_Class_Timer)))

components/libc/posix/pthreads/pthread_cond.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
285285
rt_thread_suspend(thread);
286286

287287
/* Only support FIFO */
288-
rt_list_insert_before(&(sem->parent.suspend_thread), &(thread->tlist));
288+
rt_list_insert_before(&(sem->parent.suspend_thread), &RT_THREAD_LIST_NODE(thread));
289289

290290
/**
291291
rt_ipc_list_suspend(&(sem->parent.suspend_thread),

components/vbus/prio_queue.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void rt_prio_queue_detach(struct rt_prio_queue *que)
8989
rt_base_t level = rt_hw_interrupt_disable();
9090

9191
/* get next suspend thread */
92-
thread = rt_list_entry(que->suspended_pop_list.next, struct rt_thread, tlist);
92+
thread = RT_THREAD_LIST_NODE_ENTRY(que->suspended_pop_list.next);
9393
/* set error code to -RT_ERROR */
9494
thread->error = -RT_ERROR;
9595

@@ -160,9 +160,7 @@ rt_err_t rt_prio_queue_push(struct rt_prio_queue *que,
160160
rt_thread_t thread;
161161

162162
/* get thread entry */
163-
thread = rt_list_entry(que->suspended_pop_list.next,
164-
struct rt_thread,
165-
tlist);
163+
thread = RT_THREAD_LIST_NODE_ENTRY(que->suspended_pop_list.next);
166164
/* resume it */
167165
rt_thread_resume(thread);
168166
rt_hw_interrupt_enable(level);
@@ -207,7 +205,7 @@ rt_err_t rt_prio_queue_pop(struct rt_prio_queue *que,
207205
thread->error = RT_EOK;
208206
rt_thread_suspend(thread);
209207

210-
rt_list_insert_before(&(que->suspended_pop_list), &(thread->tlist));
208+
rt_list_insert_before(&(que->suspended_pop_list), &RT_THREAD_LIST_NODE(thread));
211209

212210
if (timeout > 0)
213211
{

components/vbus/vbus.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ rt_err_t rt_vbus_post(rt_uint8_t id,
336336
rt_enter_critical();
337337
rt_thread_suspend(thread);
338338

339-
rt_list_insert_after(&_chn_suspended_threads[id], &thread->tlist);
339+
rt_list_insert_after(&_chn_suspended_threads[id], &RT_THREAD_LIST_NODE(thread));
340340
if (timeout > 0)
341341
{
342342
rt_timer_control(&(thread->thread_timer),
@@ -443,9 +443,7 @@ static void rt_vbus_notify_chn(unsigned char chnr, rt_err_t err)
443443
{
444444
rt_thread_t thread;
445445

446-
thread = rt_list_entry(_chn_suspended_threads[chnr].next,
447-
struct rt_thread,
448-
tlist);
446+
thread = RT_THREAD_LIST_NODE_ENTRY(_chn_suspended_threads[chnr].next);
449447
thread->error = err;
450448
rt_thread_resume(thread);
451449
}
@@ -855,9 +853,7 @@ static int _chn0_actor(unsigned char *dp, size_t dsize)
855853
{
856854
rt_thread_t thread;
857855

858-
thread = rt_list_entry(_chn_suspended_threads[chnr].next,
859-
struct rt_thread,
860-
tlist);
856+
thread = RT_THREAD_LIST_NODE_ENTRY(_chn_suspended_threads[chnr].next);
861857
rt_thread_resume(thread);
862858
}
863859
rt_exit_critical();

components/vbus/watermark_queue.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ void rt_wm_que_dump(struct rt_watermark_queue *wg)
4343
{
4444
rt_thread_t thread;
4545

46-
thread = rt_list_entry(wg->suspended_threads.next,
47-
struct rt_thread,
48-
tlist);
46+
thread = RT_THREAD_LIST_NODE_ENTRY(wg->suspended_threads.next);
4947
rt_kprintf(" %.*s", RT_NAME_MAX, thread->parent.name);
5048
}
5149
rt_kprintf("\n");

components/vbus/watermark_queue.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ rt_inline rt_err_t rt_wm_que_inc(struct rt_watermark_queue *wg,
6464
thread = rt_thread_self();
6565
thread->error = RT_EOK;
6666
rt_thread_suspend(thread);
67-
rt_list_insert_after(&wg->suspended_threads, &thread->tlist);
67+
rt_list_insert_after(&wg->suspended_threads, &RT_THREAD_LIST_NODE(thread));
6868
if (timeout > 0)
6969
{
7070
rt_timer_control(&(thread->thread_timer),
@@ -116,9 +116,7 @@ rt_inline void rt_wm_que_dec(struct rt_watermark_queue *wg)
116116
{
117117
rt_thread_t thread;
118118

119-
thread = rt_list_entry(wg->suspended_threads.next,
120-
struct rt_thread,
121-
tlist);
119+
thread = RT_THREAD_LIST_NODE_ENTRY(wg->suspended_threads.next);
122120
rt_thread_resume(thread);
123121
need_sched = 1;
124122
}

include/rtdef.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -720,21 +720,20 @@ struct rt_cpu_usage_stats
720720
};
721721
typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t;
722722

723+
#define _SCHEDULER_CONTEXT(fileds) fileds
724+
723725
/**
724726
* CPUs definitions
725727
*
726728
*/
727-
#warning TODO Add align cache line, to improve percpu performance?
728-
#warning TODO Add a new sched_cpu_ctx object for better protection
729-
#warning TODO Add scheduler early?
730729
struct rt_cpu
731730
{
732731
/**
733732
* protected by:
734733
* - other cores: accessing from other coress is undefined behaviour
735734
* - local core: rt_enter_critical()/rt_exit_critical()
736735
*/
737-
struct {
736+
_SCHEDULER_CONTEXT(
738737
struct rt_thread *current_thread;
739738

740739
rt_uint8_t irq_switch_flag:1;
@@ -751,7 +750,7 @@ struct rt_cpu
751750
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
752751

753752
rt_atomic_t tick; /**< Passing tickes on this core */
754-
};
753+
);
755754

756755
struct rt_thread *idle_thread;
757756
rt_atomic_t irq_nest;
@@ -867,7 +866,7 @@ struct rt_thread
867866
rt_atomic_t cpus_lock_nest; /**< cpus lock count */
868867
#endif
869868

870-
struct rt_sched_thread_ctx sched_thread_ctx;
869+
RT_SCHED_THREAD_CTX;
871870
struct rt_timer thread_timer; /**< built-in thread timer */
872871
rt_thread_cleanup_t cleanup; /**< cleanup function when thread exit */
873872

include/rtsched.h

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct rt_thread;
1818

1919
typedef rt_uint8_t rt_sched_thread_status_t;
2020

21+
#ifdef RT_USING_SCHED_THREAD_CTX
22+
2123
/**
2224
* Scheduler private status binding on thread. Caller should never accessing
2325
* these members.
@@ -60,24 +62,61 @@ struct rt_sched_thread_ctx
6062
struct rt_sched_thread_priv sched_thread_priv; /**< private context of scheduler */
6163
};
6264

63-
/**
64-
* NOTE: user should NEVER use these APIs directly. See rt_thread_.* or IPC
65-
* methods instead.
66-
*/
67-
#if defined(__RTTHREAD_SOURCE__) || defined(__RTT_IPC_SOURCE__)
65+
#define RT_SCHED_THREAD_CTX struct rt_sched_thread_ctx sched_thread_ctx
6866

6967
#define SCHED_PRIV(thread) ((thread)->sched_thread_ctx.sched_thread_priv)
7068
#define SCHED_CTX(thread) ((thread)->sched_thread_ctx)
7169

7270
/**
73-
* Convert a list node in container thread->sched_thread_ctx->thread_list_node
71+
* Convert a list node in container SCHED_CTX(thread)->thread_list_node
7472
* to a thread pointer.
7573
*/
7674
#define RT_THREAD_LIST_NODE_ENTRY(node) \
7775
rt_container_of( \
7876
rt_list_entry((node), struct rt_sched_thread_ctx, thread_list_node), \
7977
struct rt_thread, sched_thread_ctx)
80-
#define RT_THREAD_LIST_NODE(thread) ((thread)->sched_thread_ctx.thread_list_node)
78+
#define RT_THREAD_LIST_NODE(thread) (SCHED_CTX(thread).thread_list_node)
79+
80+
#else /* !defined(RT_USING_SCHED_THREAD_CTX) */
81+
82+
#if RT_THREAD_PRIORITY_MAX > 32
83+
#define _RT_SCHED_THREAD_CTX_PRIO_EXT \
84+
rt_uint8_t number; /**< priority low number */ \
85+
rt_uint8_t high_mask; /**< priority high mask */
86+
87+
#else /* ! RT_THREAD_PRIORITY_MAX > 32 */
88+
89+
#define _RT_SCHED_THREAD_CTX_PRIO_EXT
90+
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
91+
92+
#define RT_SCHED_THREAD_CTX \
93+
rt_list_t tlist; /**< node in thread list */ \
94+
rt_uint8_t stat; /**< thread status */ \
95+
rt_uint8_t sched_flag_locked:1; \
96+
/**< calling thread have the scheduler locked */ \
97+
rt_uint8_t sched_flag_ttmr_set:1; /**< thread timer is start */ \
98+
rt_tick_t init_tick; /**< thread's initialized tick */ \
99+
rt_tick_t remaining_tick; /**< remaining tick */ \
100+
rt_uint8_t current_priority; /**< current priority */ \
101+
rt_uint8_t init_priority; /**< initialized priority */ \
102+
_RT_SCHED_THREAD_CTX_PRIO_EXT; \
103+
rt_uint32_t number_mask; /**< priority number mask */
104+
105+
#define SCHED_PRIV(thread) (*thread)
106+
#define SCHED_CTX(thread) (*thread)
107+
108+
/**
109+
* Convert a list node in container SCHED_CTX(thread)->thread_list_node
110+
* to a thread pointer.
111+
*/
112+
#define RT_THREAD_LIST_NODE_ENTRY(node) rt_list_entry((node), struct rt_thread, tlist)
113+
#define RT_THREAD_LIST_NODE(thread) (SCHED_CTX(thread).tlist)
114+
115+
#endif /* RT_USING_SCHED_THREAD_CTX */
116+
117+
/**
118+
* System Scheduler Locking
119+
*/
81120

82121
typedef rt_ubase_t rt_sched_lock_level_t;
83122

@@ -97,6 +136,12 @@ rt_bool_t rt_sched_is_locked(void);
97136
#define RT_SCHED_DEBUG_IS_UNLOCKED
98137
#endif /* RT_USING_SMP */
99138

139+
/**
140+
* NOTE: user should NEVER use these APIs directly. See rt_thread_.* or IPC
141+
* methods instead.
142+
*/
143+
#if defined(__RTTHREAD_SOURCE__) || defined(__RTT_IPC_SOURCE__)
144+
100145
/* thread initialization and startup routine */
101146
void rt_sched_thread_init_ctx(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority);
102147
void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority);

0 commit comments

Comments
 (0)