Skip to content

Commit 71560ba

Browse files
polarvidxqyjlj
andauthored
🎯 Sync smart & scheduler codes (#8537)
Signed-off-by: Shell <[email protected]> Co-authored-by: xqyjlj <[email protected]>
1 parent 6fe69d7 commit 71560ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+6107
-2218
lines changed

bsp/qemu-vexpress-a9/applications/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
#include <stdint.h>
1212
#include <stdio.h>
13-
#include <stdlib.h>
13+
#include <rtthread.h>
1414

1515
int main(void)
1616
{
17-
printf("Hello RT-Thread!\n");
17+
rt_kprintf("Hello RT-Thread!\n");
1818

1919
return 0;
2020
}

bsp/qemu-vexpress-a9/drivers/secondary_cpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void rt_hw_secondary_cpu_up(void)
4747
*plat_boot_reg-- = (void *)(size_t)-1;
4848
*plat_boot_reg = (void *)entry;
4949
rt_hw_dsb();
50-
rt_hw_ipi_send(0, 1 << 1);
50+
rt_hw_ipi_send(0, RT_CPU_MASK ^ (1 << rt_hw_cpu_id()));
5151
}
5252

5353
/* Interface */

components/dfs/dfs_v2/filesystems/romfs/dfs_romfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ static int dfs_romfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32
352352

353353
d->d_namlen = rt_strlen(name);
354354
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
355-
rt_strncpy(d->d_name, name, DFS_PATH_MAX);
355+
rt_strncpy(d->d_name, name, DIRENT_NAME_MAX);
356356

357357
/* move to next position */
358358
++ file->fpos;

components/dfs/dfs_v2/src/dfs_pcache.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,10 @@ static int dfs_page_insert(struct dfs_page *page)
822822
rt_list_insert_before(&aspace->list_inactive, &page->space_node);
823823
aspace->pages_count ++;
824824

825-
RT_ASSERT(_dfs_page_insert(aspace, page) == 0);
825+
if (_dfs_page_insert(aspace, page))
826+
{
827+
RT_ASSERT(0);
828+
}
826829

827830
if (aspace->pages_count > RT_PAGECACHE_ASPACE_COUNT)
828831
{

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/drivers/include/ipc/completion.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
#include <rtconfig.h>
1414

1515
/**
16-
* Completion
16+
* Completion - A tiny IPC implementation for resource-constrained scenarios
17+
*
18+
* It's an IPC using one CPU word with the encoding:
19+
*
20+
* BIT | MAX-1 ----------------- 1 | 0 |
21+
* CONTENT | suspended_thread & ~1 | completed flag |
1722
*/
1823

1924
struct rt_completion
2025
{
21-
rt_uint32_t flag;
22-
23-
/* suspended list */
24-
rt_list_t suspended_list;
25-
struct rt_spinlock spinlock;
26+
/* suspended thread, and completed flag */
27+
rt_base_t susp_thread_n_flag;
2628
};
2729

30+
#define RT_COMPLETION_INIT(comp) {0}
31+
2832
void rt_completion_init(struct rt_completion *completion);
2933
rt_err_t rt_completion_wait(struct rt_completion *completion,
3034
rt_int32_t timeout);

components/drivers/ipc/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ if not GetDepend('RT_USING_HEAP'):
88
SrcRemove(src, 'dataqueue.c')
99
SrcRemove(src, 'pipe.c')
1010

11-
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_DEVICE_IPC'], CPPPATH = CPPPATH)
11+
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_DEVICE_IPC'], CPPPATH = CPPPATH, LOCAL_CPPDEFINES=['__RT_IPC_SOURCE__'])
1212

1313
Return('group')

components/drivers/ipc/completion.c

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,24 @@
88
* 2012-09-30 Bernard first version.
99
* 2021-08-18 chenyingchun add comments
1010
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
11+
* 2024-01-25 Shell reduce resource usage in completion for better synchronization
12+
* and smaller footprint.
1113
*/
1214

15+
#define DBG_TAG "drivers.ipc"
16+
#define DBG_LVL DBG_INFO
17+
#include <rtdbg.h>
18+
1319
#include <rthw.h>
1420
#include <rtdevice.h>
1521

1622
#define RT_COMPLETED 1
1723
#define RT_UNCOMPLETED 0
24+
#define RT_COMPLETION_FLAG(comp) ((comp)->susp_thread_n_flag & 1)
25+
#define RT_COMPLETION_THREAD(comp) ((rt_thread_t)((comp)->susp_thread_n_flag & ~1))
26+
#define RT_COMPLETION_NEW_STAT(thread, flag) (((flag) & 1) | (((rt_base_t)thread) & ~1))
27+
28+
static struct rt_spinlock _completion_lock = RT_SPINLOCK_INIT;
1829

1930
/**
2031
* @brief This function will initialize a completion object.
@@ -23,14 +34,9 @@
2334
*/
2435
void rt_completion_init(struct rt_completion *completion)
2536
{
26-
rt_base_t level;
2737
RT_ASSERT(completion != RT_NULL);
2838

29-
rt_spin_lock_init(&(completion->spinlock));
30-
level = rt_spin_lock_irqsave(&(completion->spinlock));
31-
completion->flag = RT_UNCOMPLETED;
32-
rt_list_init(&completion->suspended_list);
33-
rt_spin_unlock_irqrestore(&(completion->spinlock), level);
39+
completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_UNCOMPLETED);
3440
}
3541
RTM_EXPORT(rt_completion_init);
3642

@@ -64,11 +70,11 @@ rt_err_t rt_completion_wait(struct rt_completion *completion,
6470
result = RT_EOK;
6571
thread = rt_thread_self();
6672

67-
level = rt_spin_lock_irqsave(&(completion->spinlock));
68-
if (completion->flag != RT_COMPLETED)
73+
level = rt_spin_lock_irqsave(&_completion_lock);
74+
if (RT_COMPLETION_FLAG(completion) != RT_COMPLETED)
6975
{
7076
/* only one thread can suspend on complete */
71-
RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
77+
RT_ASSERT(RT_COMPLETION_THREAD(completion) == RT_NULL);
7278

7379
if (timeout == 0)
7480
{
@@ -81,40 +87,43 @@ rt_err_t rt_completion_wait(struct rt_completion *completion,
8187
thread->error = RT_EOK;
8288

8389
/* suspend thread */
84-
rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
85-
/* add to suspended list */
86-
rt_list_insert_before(&(completion->suspended_list),
87-
&(thread->tlist));
88-
89-
/* current context checking */
90-
RT_DEBUG_NOT_IN_INTERRUPT;
91-
92-
/* start timer */
93-
if (timeout > 0)
90+
result = rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
91+
if (result == RT_EOK)
9492
{
95-
/* reset the timeout of thread timer and start it */
96-
rt_timer_control(&(thread->thread_timer),
97-
RT_TIMER_CTRL_SET_TIME,
98-
&timeout);
99-
rt_timer_start(&(thread->thread_timer));
93+
/* add to suspended thread */
94+
completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(thread, RT_UNCOMPLETED);
95+
96+
/* current context checking */
97+
RT_DEBUG_NOT_IN_INTERRUPT;
98+
99+
/* start timer */
100+
if (timeout > 0)
101+
{
102+
/* reset the timeout of thread timer and start it */
103+
rt_timer_control(&(thread->thread_timer),
104+
RT_TIMER_CTRL_SET_TIME,
105+
&timeout);
106+
rt_timer_start(&(thread->thread_timer));
107+
}
108+
/* enable interrupt */
109+
rt_spin_unlock_irqrestore(&_completion_lock, level);
110+
111+
/* do schedule */
112+
rt_schedule();
113+
114+
/* thread is waked up */
115+
result = thread->error;
116+
117+
level = rt_spin_lock_irqsave(&_completion_lock);
100118
}
101-
/* enable interrupt */
102-
rt_spin_unlock_irqrestore(&(completion->spinlock), level);
103-
104-
/* do schedule */
105-
rt_schedule();
106-
107-
/* thread is waked up */
108-
result = thread->error;
109-
110-
level = rt_spin_lock_irqsave(&(completion->spinlock));
111119
}
112120
}
113-
/* clean completed flag */
114-
completion->flag = RT_UNCOMPLETED;
121+
122+
/* clean completed flag & remove susp_thread on the case of waking by timeout */
123+
completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_UNCOMPLETED);
115124

116125
__exit:
117-
rt_spin_unlock_irqrestore(&(completion->spinlock), level);
126+
rt_spin_unlock_irqrestore(&_completion_lock, level);
118127

119128
return result;
120129
}
@@ -128,35 +137,33 @@ RTM_EXPORT(rt_completion_wait);
128137
void rt_completion_done(struct rt_completion *completion)
129138
{
130139
rt_base_t level;
140+
rt_err_t error;
141+
rt_thread_t suspend_thread;
131142
RT_ASSERT(completion != RT_NULL);
132143

133-
if (completion->flag == RT_COMPLETED)
144+
level = rt_spin_lock_irqsave(&_completion_lock);
145+
if (RT_COMPLETION_FLAG(completion) == RT_COMPLETED)
146+
{
147+
rt_spin_unlock_irqrestore(&_completion_lock, level);
134148
return;
149+
}
135150

136-
level = rt_spin_lock_irqsave(&(completion->spinlock));
137-
completion->flag = RT_COMPLETED;
138-
139-
if (!rt_list_isempty(&(completion->suspended_list)))
151+
suspend_thread = RT_COMPLETION_THREAD(completion);
152+
if (suspend_thread)
140153
{
141154
/* there is one thread in suspended list */
142-
struct rt_thread *thread;
143-
144-
/* get thread entry */
145-
thread = rt_list_entry(completion->suspended_list.next,
146-
struct rt_thread,
147-
tlist);
148155

149156
/* resume it */
150-
rt_thread_resume(thread);
151-
rt_spin_unlock_irqrestore(&(completion->spinlock), level);
152-
153-
/* perform a schedule */
154-
rt_schedule();
155-
}
156-
else
157-
{
158-
rt_spin_unlock_irqrestore(&(completion->spinlock), level);
157+
error = rt_thread_resume(suspend_thread);
158+
if (error)
159+
{
160+
LOG_D("%s: failed to resume thread", __func__);
161+
}
159162
}
163+
164+
completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_COMPLETED);
165+
166+
rt_spin_unlock_irqrestore(&_completion_lock, level);
160167
}
161168
RTM_EXPORT(rt_completion_done);
162169

0 commit comments

Comments
 (0)