Skip to content

Commit 9b44535

Browse files
committed
add RT_DEBUG_SCHEDULER_AVAILABLE check
1 parent e665bac commit 9b44535

File tree

6 files changed

+96
-18
lines changed

6 files changed

+96
-18
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ 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+
if (timeout != 0)
62+
{
63+
RT_DEBUG_SCHEDULER_AVAILABLE;
64+
}
65+
6066
result = RT_EOK;
6167
thread = rt_thread_self();
6268

components/drivers/ipc/dataqueue.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ 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+
if (timeout != 0)
103+
{
104+
RT_DEBUG_SCHEDULER_AVAILABLE;
105+
}
106+
101107
result = RT_EOK;
102108
thread = rt_thread_self();
103109

@@ -112,9 +118,6 @@ rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
112118
goto __exit;
113119
}
114120

115-
/* current context checking */
116-
RT_DEBUG_NOT_IN_INTERRUPT;
117-
118121
/* reset thread error number */
119122
thread->error = RT_EOK;
120123

@@ -217,6 +220,12 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
217220
RT_ASSERT(data_ptr != RT_NULL);
218221
RT_ASSERT(size != RT_NULL);
219222

223+
/* current context checking */
224+
if (timeout != 0)
225+
{
226+
RT_DEBUG_SCHEDULER_AVAILABLE;
227+
}
228+
220229
result = RT_EOK;
221230
thread = rt_thread_self();
222231

@@ -230,9 +239,6 @@ rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
230239
goto __exit;
231240
}
232241

233-
/* current context checking */
234-
RT_DEBUG_NOT_IN_INTERRUPT;
235-
236242
/* reset thread error number */
237243
thread->error = RT_EOK;
238244

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;
133133

134134
tick = rt_tick_from_millisecond(msec);
135135

include/rtdebug.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,31 @@ 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 \
120+
do \
121+
{ \
122+
rt_base_t level; \
123+
level = rt_hw_interrupt_disable(); \
124+
if (rt_critical_level() != 0) \
125+
{ \
126+
rt_kprintf("Function[%s]: scheduler is not available\n", \
127+
__FUNCTION__); \
128+
RT_ASSERT(0) \
129+
} \
130+
RT_DEBUG_IN_THREAD_CONTEXT; \
131+
rt_hw_interrupt_enable(level); \
132+
} \
133+
while (0)
113134
#else
114135
#define RT_DEBUG_NOT_IN_INTERRUPT
115136
#define RT_DEBUG_IN_THREAD_CONTEXT
137+
#define RT_DEBUG_SCHEDULER_AVAILABLE
116138
#endif
117139

118140
#else /* RT_DEBUG */
@@ -121,6 +143,7 @@ while (0)
121143
#define RT_DEBUG_LOG(type, message)
122144
#define RT_DEBUG_NOT_IN_INTERRUPT
123145
#define RT_DEBUG_IN_THREAD_CONTEXT
146+
#define RT_DEBUG_SCHEDULER_AVAILABLE
124147

125148
#endif /* RT_DEBUG */
126149

src/ipc.c

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,11 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
484484
/* parameter check */
485485
RT_ASSERT(sem != RT_NULL);
486486
RT_ASSERT(rt_object_get_type(&sem->parent.parent) == RT_Object_Class_Semaphore);
487+
/* current context checking */
488+
if (time != 0)
489+
{
490+
RT_DEBUG_SCHEDULER_AVAILABLE;
491+
}
487492

488493
RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(sem->parent.parent)));
489494

@@ -514,9 +519,6 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
514519
}
515520
else
516521
{
517-
/* current context checking */
518-
RT_DEBUG_IN_THREAD_CONTEXT;
519-
520522
/* semaphore is unavailable, push to suspend list */
521523
/* get current thread */
522524
thread = rt_thread_self();
@@ -914,6 +916,12 @@ rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
914916
/* this function must not be used in interrupt even if time = 0 */
915917
RT_DEBUG_IN_THREAD_CONTEXT;
916918

919+
/* current context checking */
920+
if (time != 0)
921+
{
922+
RT_DEBUG_SCHEDULER_AVAILABLE;
923+
}
924+
917925
/* parameter check */
918926
RT_ASSERT(mutex != RT_NULL);
919927
RT_ASSERT(rt_object_get_type(&mutex->parent.parent) == RT_Object_Class_Mutex);
@@ -1566,12 +1574,18 @@ rt_err_t rt_event_recv(rt_event_t event,
15661574
register rt_ubase_t level;
15671575
register rt_base_t status;
15681576

1569-
RT_DEBUG_IN_THREAD_CONTEXT;
1570-
15711577
/* parameter check */
15721578
RT_ASSERT(event != RT_NULL);
15731579
RT_ASSERT(rt_object_get_type(&event->parent.parent) == RT_Object_Class_Event);
15741580

1581+
/* current context checking */
1582+
RT_DEBUG_IN_THREAD_CONTEXT;
1583+
1584+
if (timeout != 0)
1585+
{
1586+
RT_DEBUG_SCHEDULER_AVAILABLE;
1587+
}
1588+
15751589
if (set == 0)
15761590
return -RT_ERROR;
15771591

@@ -1993,6 +2007,12 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
19932007
RT_ASSERT(mb != RT_NULL);
19942008
RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
19952009

2010+
/* current context checking */
2011+
if (timeout != 0)
2012+
{
2013+
RT_DEBUG_SCHEDULER_AVAILABLE;
2014+
}
2015+
19962016
/* initialize delta tick */
19972017
tick_delta = 0;
19982018
/* get current thread */
@@ -2025,7 +2045,6 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb,
20252045
return -RT_EFULL;
20262046
}
20272047

2028-
RT_DEBUG_IN_THREAD_CONTEXT;
20292048
/* suspend current thread */
20302049
_ipc_list_suspend(&(mb->suspend_sender_thread),
20312050
thread,
@@ -2236,6 +2255,12 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
22362255
RT_ASSERT(mb != RT_NULL);
22372256
RT_ASSERT(rt_object_get_type(&mb->parent.parent) == RT_Object_Class_MailBox);
22382257

2258+
/* current context checking */
2259+
if (timeout != 0)
2260+
{
2261+
RT_DEBUG_SCHEDULER_AVAILABLE;
2262+
}
2263+
22392264
/* initialize delta tick */
22402265
tick_delta = 0;
22412266
/* get current thread */
@@ -2271,7 +2296,6 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_ubase_t *value, rt_int32_t timeout)
22712296
return -RT_ETIMEOUT;
22722297
}
22732298

2274-
RT_DEBUG_IN_THREAD_CONTEXT;
22752299
/* suspend current thread */
22762300
_ipc_list_suspend(&(mb->parent.suspend_thread),
22772301
thread,
@@ -2744,6 +2768,12 @@ rt_err_t rt_mq_send_wait(rt_mq_t mq,
27442768
RT_ASSERT(buffer != RT_NULL);
27452769
RT_ASSERT(size != 0);
27462770

2771+
/* current context checking */
2772+
if (timeout != 0)
2773+
{
2774+
RT_DEBUG_SCHEDULER_AVAILABLE;
2775+
}
2776+
27472777
/* greater than one message size */
27482778
if (size > mq->msg_size)
27492779
return -RT_ERROR;
@@ -2784,7 +2814,6 @@ rt_err_t rt_mq_send_wait(rt_mq_t mq,
27842814
return -RT_EFULL;
27852815
}
27862816

2787-
RT_DEBUG_IN_THREAD_CONTEXT;
27882817
/* suspend current thread */
27892818
_ipc_list_suspend(&(mq->suspend_sender_thread),
27902819
thread,
@@ -3054,6 +3083,12 @@ rt_err_t rt_mq_recv(rt_mq_t mq,
30543083
RT_ASSERT(buffer != RT_NULL);
30553084
RT_ASSERT(size != 0);
30563085

3086+
/* current context checking */
3087+
if (timeout != 0)
3088+
{
3089+
RT_DEBUG_SCHEDULER_AVAILABLE;
3090+
}
3091+
30573092
/* initialize delta tick */
30583093
tick_delta = 0;
30593094
/* get current thread */
@@ -3074,8 +3109,6 @@ rt_err_t rt_mq_recv(rt_mq_t mq,
30743109
/* message queue is empty */
30753110
while (mq->entry == 0)
30763111
{
3077-
RT_DEBUG_IN_THREAD_CONTEXT;
3078-
30793112
/* reset error number in thread */
30803113
thread->error = RT_EOK;
30813114

0 commit comments

Comments
 (0)