Skip to content

Commit 580194f

Browse files
authored
Merge pull request #4740 from Guozhanxin/scheduler_check
2 parents cd929f0 + 500d26c commit 580194f

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

components/dfs/filesystems/devfs/devfs.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,34 @@ int dfs_device_fs_open(struct dfs_fd *file)
144144
{
145145
count ++;
146146
}
147+
rt_exit_critical();
147148

148149
root_dirent = (struct device_dirent *)rt_malloc(sizeof(struct device_dirent) +
149150
count * sizeof(rt_device_t));
150151
if (root_dirent != RT_NULL)
151152
{
153+
/* lock scheduler */
154+
rt_enter_critical();
155+
152156
root_dirent->devices = (rt_device_t *)(root_dirent + 1);
153157
root_dirent->read_index = 0;
154158
root_dirent->device_count = count;
155159
count = 0;
156160
/* get all device node */
157161
for (node = information->object_list.next; node != &(information->object_list); node = node->next)
158162
{
163+
/* avoid memory write through */
164+
if (count == root_dirent->device_count)
165+
{
166+
rt_kprintf("warning: There are newly added devices that are not displayed!");
167+
break;
168+
}
159169
object = rt_list_entry(node, struct rt_object, list);
160170
root_dirent->devices[count] = (rt_device_t)object;
161171
count ++;
162172
}
173+
rt_exit_critical();
163174
}
164-
rt_exit_critical();
165175

166176
/* set data */
167177
file->data = root_dirent;

components/drivers/ipc/completion.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ rt_err_t rt_completion_wait(struct rt_completion *completion,
5757
rt_thread_t thread;
5858
RT_ASSERT(completion != RT_NULL);
5959

60+
/* current context checking */
61+
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
62+
6063
result = RT_EOK;
6164
thread = rt_thread_self();
6265

@@ -82,9 +85,6 @@ rt_err_t rt_completion_wait(struct rt_completion *completion,
8285
rt_list_insert_before(&(completion->suspended_list),
8386
&(thread->tlist));
8487

85-
/* current context checking */
86-
RT_DEBUG_NOT_IN_INTERRUPT;
87-
8888
/* start timer */
8989
if (timeout > 0)
9090
{

components/drivers/ipc/dataqueue.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
9898
RT_ASSERT(queue != RT_NULL);
9999
RT_ASSERT(queue->magic == DATAQUEUE_MAGIC);
100100

101+
/* current context checking */
102+
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
103+
101104
result = RT_EOK;
102105
thread = rt_thread_self();
103106

@@ -112,9 +115,6 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
112115
goto __exit;
113116
}
114117

115-
/* current context checking */
116-
RT_DEBUG_NOT_IN_INTERRUPT;
117-
118118
/* reset thread error number */
119119
thread->error = RT_EOK;
120120

@@ -217,6 +217,9 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
217217
RT_ASSERT(data_ptr != RT_NULL);
218218
RT_ASSERT(size != RT_NULL);
219219

220+
/* current context checking */
221+
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
222+
220223
result = RT_EOK;
221224
thread = rt_thread_self();
222225

@@ -230,9 +233,6 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
230233
goto __exit;
231234
}
232235

233-
/* current context checking */
234-
RT_DEBUG_NOT_IN_INTERRUPT;
235-
236236
/* reset thread error number */
237237
thread->error = RT_EOK;
238238

components/drivers/ipc/waitqueue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int msec)
129129
rt_base_t level;
130130

131131
/* current context checking */
132-
RT_DEBUG_NOT_IN_INTERRUPT;
132+
RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
133133

134134
tick = rt_tick_from_millisecond(msec);
135135

include/rtdebug.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,34 @@ do \
110110
rt_hw_interrupt_enable(level); \
111111
} \
112112
while (0)
113+
114+
/* "scheduler available" means:
115+
* 1) the scheduler has been started.
116+
* 2) not in interrupt context.
117+
* 3) scheduler is not locked.
118+
*/
119+
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check) \
120+
do \
121+
{ \
122+
if (need_check) \
123+
{ \
124+
rt_base_t level; \
125+
level = rt_hw_interrupt_disable(); \
126+
if (rt_critical_level() != 0) \
127+
{ \
128+
rt_kprintf("Function[%s]: scheduler is not available\n", \
129+
__FUNCTION__); \
130+
RT_ASSERT(0) \
131+
} \
132+
RT_DEBUG_IN_THREAD_CONTEXT; \
133+
rt_hw_interrupt_enable(level); \
134+
} \
135+
} \
136+
while (0)
113137
#else
114138
#define RT_DEBUG_NOT_IN_INTERRUPT
115139
#define RT_DEBUG_IN_THREAD_CONTEXT
140+
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
116141
#endif
117142

118143
#else /* RT_DEBUG */
@@ -121,6 +146,7 @@ while (0)
121146
#define RT_DEBUG_LOG(type, message)
122147
#define RT_DEBUG_NOT_IN_INTERRUPT
123148
#define RT_DEBUG_IN_THREAD_CONTEXT
149+
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
124150

125151
#endif /* RT_DEBUG */
126152

src/ipc.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
485485
RT_ASSERT(sem != RT_NULL);
486486
RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
487487

488+
/* current context checking */
489+
RT_DEBUG_SCHEDULER_AVAILABLE(time != 0);
490+
488491
RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));
489492

490493
/* disable interrupt */
@@ -514,9 +517,6 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
514517
}
515518
else
516519
{
517-
/* current context checking */
518-
RT_DEBUG_IN_THREAD_CONTEXT;
519-
520520
/* semaphore is unavailable, push to suspend list */
521521
/* get current thread */
522522
thread = rt_thread_self();
@@ -912,7 +912,8 @@ rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
912912
struct rt_thread *thread;
913913

914914
/* this function must not be used in interrupt even if time = 0 */
915-
RT_DEBUG_IN_THREAD_CONTEXT;
915+
/* current context checking */
916+
RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
916917

917918
/* parameter check */
918919
RT_ASSERT(mutex != RT_NULL);
@@ -1566,12 +1567,13 @@ rt_err_t rt_event_recv(rt_event_t event,
15661567
register rt_ubase_t level;
15671568
register rt_base_t status;
15681569

1569-
RT_DEBUG_IN_THREAD_CONTEXT;
1570-
15711570
/* parameter check */
15721571
RT_ASSERT(event != RT_NULL);
15731572
RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
15741573

1574+
/* current context checking */
1575+
RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
1576+
15751577
if (set == 0)
15761578
return -RT_ERROR;
15771579

@@ -1993,6 +1995,9 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
19931995
RT_ASSERT(mb != RT_NULL);
19941996
RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
19951997

1998+
/* current context checking */
1999+
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2000+
19962001
/* initialize delta tick */
19972002
tick_delta = 0;
19982003
/* get current thread */
@@ -2025,7 +2030,6 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
20252030
return -RT_EFULL;
20262031
}
20272032

2028-
RT_DEBUG_IN_THREAD_CONTEXT;
20292033
/* suspend current thread */
20302034
_ipc_list_suspend(&(mb->suspend_sender_thread),
20312035
thread,
@@ -2236,6 +2240,9 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
22362240
RT_ASSERT(mb != RT_NULL);
22372241
RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
22382242

2243+
/* current context checking */
2244+
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2245+
22392246
/* initialize delta tick */
22402247
tick_delta = 0;
22412248
/* get current thread */
@@ -2271,7 +2278,6 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
22712278
return -RT_ETIMEOUT;
22722279
}
22732280

2274-
RT_DEBUG_IN_THREAD_CONTEXT;
22752281
/* suspend current thread */
22762282
_ipc_list_suspend(&(mb->parent.suspend_thread),
22772283
thread,
@@ -2744,6 +2750,9 @@ rt_err_t rt_mq_send_wait(rt_mq_t mq,
27442750
RT_ASSERT(buffer != RT_NULL);
27452751
RT_ASSERT(size != 0);
27462752

2753+
/* current context checking */
2754+
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
2755+
27472756
/* greater than one message size */
27482757
if (size > mq->msg_size)
27492758
return -RT_ERROR;
@@ -2784,7 +2793,6 @@ rt_err_t rt_mq_send_wait(rt_mq_t mq,
27842793
return -RT_EFULL;
27852794
}
27862795

2787-
RT_DEBUG_IN_THREAD_CONTEXT;
27882796
/* suspend current thread */
27892797
_ipc_list_suspend(&(mq->suspend_sender_thread),
27902798
thread,
@@ -3054,6 +3062,9 @@ rt_err_t rt_mq_recv(rt_mq_t mq,
30543062
RT_ASSERT(buffer != RT_NULL);
30553063
RT_ASSERT(size != 0);
30563064

3065+
/* current context checking */
3066+
RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
3067+
30573068
/* initialize delta tick */
30583069
tick_delta = 0;
30593070
/* get current thread */
@@ -3074,8 +3085,6 @@ rt_err_t rt_mq_recv(rt_mq_t mq,
30743085
/* message queue is empty */
30753086
while (mq->entry == 0)
30763087
{
3077-
RT_DEBUG_IN_THREAD_CONTEXT;
3078-
30793088
/* reset error number in thread */
30803089
thread->error = RT_EOK;
30813090

0 commit comments

Comments
 (0)