Skip to content

Commit 4412c9e

Browse files
committed
cleanup and bug fix
The mutex can only be released by the owner and it is available in initial state. Fix the profile code and cleanup the unnecessary Mutex_Control.
1 parent aca51c8 commit 4412c9e

File tree

1 file changed

+35
-110
lines changed

1 file changed

+35
-110
lines changed

bsp/stm32f10x/applications/test_schedule.c

+35-110
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include "systick_count.h"
55
#include "test_schedule.h"
66

7-
unsigned int loop_num=0;
8-
9-
10-
117
void test_schedule_init(void)
128
{
139

@@ -17,42 +13,42 @@ void test_schedule_init(void)
1713
if (IPC_Control == RT_NULL)
1814
{
1915
rt_kprintf("create IPC_Control failed\n");
20-
}
21-
22-
Mutex_Control = rt_sem_create("Mutex_Control", 0, RT_TEST_IPC_TYPE);
23-
if (Mutex_Control == RT_NULL)
24-
{
25-
rt_kprintf("create Mutex_Control failed\n");
16+
return;
2617
}
2718

2819
sem_test = rt_sem_create("sem_test", 0, RT_TEST_IPC_TYPE);
2920
if (sem_test == RT_NULL)
3021
{
3122
rt_kprintf("create sem_test failed\n");
23+
return;
3224
}
3325

3426
mutex_test = rt_mutex_create("mutex_test", RT_TEST_IPC_TYPE);
3527
if (mutex_test == RT_NULL)
3628
{
3729
rt_kprintf("create mutex_test failed\n");
30+
return;
3831
}
3932

4033
event_test = rt_event_create("event_test", RT_TEST_IPC_TYPE);
4134
if (event_test == RT_NULL)
4235
{
4336
rt_kprintf("create event_test failed\n");
37+
return;
4438
}
4539

4640
mailbox_test = rt_mb_create("mailbox_test", RT_TEST_IPC_SIZE, RT_TEST_IPC_TYPE);
4741
if (mailbox_test == RT_NULL)
4842
{
4943
rt_kprintf("create mailbox_test failed\n");
44+
return;
5045
}
5146

5247
mq_test = rt_mq_create("mq_test",RT_TEST_SINGLE_IPC_SIZE,RT_TEST_IPC_SIZE, RT_TEST_IPC_TYPE);
5348
if (mq_test == RT_NULL)
5449
{
5550
rt_kprintf("create mq_test failed\n");
51+
return;
5652
}
5753

5854
// //add for Thread_IPC
@@ -65,8 +61,6 @@ void test_schedule_init(void)
6561
// rt_thread_startup(thread_IPC_TCB);
6662
#endif
6763

68-
69-
7064
#ifdef RT_TEST_NEED_THREAD_A
7165
//add for Thread_A
7266

@@ -102,7 +96,6 @@ void test_schedule_init(void)
10296

10397

10498
#ifdef RT_TEST_DUMMY_THREAD
105-
10699
{
107100
int index;
108101
int priority=5;
@@ -183,42 +176,30 @@ void thread_A_entry(void* parameter)
183176
}
184177
rt_kprintf("sem test Done\n");
185178
rt_thread_delay(100);//等待删除退出的线程
186-
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187-
//for mutex
188-
rt_sem_init(Mutex_Control, "Mutex_Control",0, RT_TEST_IPC_TYPE);
189-
rt_sem_release(Mutex_Control);
190179
rt_kprintf("\n");
191180

192181
//for Mutex
193-
/* 首先创建2个mutex线程 */
194-
mutex_Tx_TCB = rt_thread_create("mutex_Tx_TCB",
195-
thread_mutex_TX_entry, RT_NULL,
196-
512, THREAD_IPC_RX_PRIORITY, THREAD_IPC_TX_TIME_SLICE);
197-
mutex_Rx_TCB = rt_thread_create("mutex_Rx_TCB",
182+
mutex_Rx_TCB = rt_thread_create("mRx",
198183
thread_mutex_RX_entry, RT_NULL,
199-
512, THREAD_IPC_TX_PRIORITY, THREAD_IPC_RX_TIME_SLICE);
200-
//对于mutex ,先启动Tx,占用资源
201-
if(mutex_Tx_TCB != RT_NULL)
202-
{
203-
rt_thread_startup(mutex_Tx_TCB);
204-
}
184+
512, THREAD_IPC_RX_PRIORITY, THREAD_IPC_RX_TIME_SLICE);
185+
186+
rt_mutex_take(mutex_test, RT_WAITING_FOREVER);
205187

206188
if(mutex_Rx_TCB != RT_NULL)
207189
{
208190
rt_thread_startup(mutex_Rx_TCB);
209191
}
210192

211-
{
212-
rt_sem_take(IPC_Control, RT_WAITING_FOREVER);
213-
rt_sem_take(IPC_Control, RT_WAITING_FOREVER);
214-
}
193+
start_count();
194+
rt_mutex_release(mutex_test);
195+
rt_kprintf("mutex test Done\n");
215196

197+
rt_sem_take(IPC_Control, RT_WAITING_FOREVER);
216198
rt_kprintf("mutex test Done\n");
217199
rt_thread_delay(100);//等待删除退出的线程
200+
218201
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219202
//for event
220-
rt_sem_init(Mutex_Control, "Mutex_Control",0, RT_TEST_IPC_TYPE);
221-
rt_sem_release(Mutex_Control);
222203
rt_kprintf("\n");
223204

224205
/* 首先创建2个event线程 */
@@ -229,18 +210,14 @@ void thread_A_entry(void* parameter)
229210
thread_event_RX_entry, RT_NULL,
230211
512, THREAD_IPC_RX_PRIORITY, THREAD_IPC_RX_TIME_SLICE);
231212

232-
233-
234-
235-
236-
if(event_Tx_TCB != RT_NULL)
213+
if(event_Rx_TCB != RT_NULL)
237214
{
238-
rt_thread_startup(event_Tx_TCB);
215+
rt_thread_startup(event_Rx_TCB);
239216
}
240217

241-
if(event_Rx_TCB != RT_NULL)
218+
if(event_Tx_TCB != RT_NULL)
242219
{
243-
rt_thread_startup(event_Rx_TCB);
220+
rt_thread_startup(event_Tx_TCB);
244221
}
245222

246223
{
@@ -283,8 +260,6 @@ void thread_A_entry(void* parameter)
283260

284261
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285262

286-
rt_sem_init(Mutex_Control, "Mutex_Control",0, RT_TEST_IPC_TYPE);
287-
rt_sem_release(Mutex_Control);
288263
rt_kprintf("\n");
289264

290265
/* 首先创建2个mailbox线程 */
@@ -295,16 +270,14 @@ void thread_A_entry(void* parameter)
295270
thread_mb_RX_entry, RT_NULL,
296271
512, THREAD_IPC_RX_PRIORITY, THREAD_IPC_RX_TIME_SLICE);
297272

298-
rt_kprintf("\n");
299-
300-
if(mb_Tx_TCB != RT_NULL)
273+
if(mb_Rx_TCB != RT_NULL)
301274
{
302-
rt_thread_startup(mb_Tx_TCB);
275+
rt_thread_startup(mb_Rx_TCB);
303276
}
304277

305-
if(mb_Rx_TCB != RT_NULL)
278+
if(mb_Tx_TCB != RT_NULL)
306279
{
307-
rt_thread_startup(mb_Rx_TCB);
280+
rt_thread_startup(mb_Tx_TCB);
308281
}
309282

310283
{
@@ -392,71 +365,33 @@ void thread_sem_TX_entry(void* parameter)
392365
void thread_sem_RX_entry(void* parameter)
393366
{
394367
unsigned int loop_num = IPC_REPEAT_TIME;
395-
//while(1)
396-
{
397-
{
398-
while(loop_num --)
399-
{
400-
rt_sem_take(sem_test, RT_WAITING_FOREVER);
401-
stop_count();
402-
calc_count();
403-
}
404-
}
405-
406-
rt_sem_release(IPC_Control);//跑完放出control信号量
407-
//rt_thread_suspend(rt_thread_self());
408-
}
409-
}
410-
411-
void thread_mutex_TX_entry(void* parameter)
412-
{
413-
unsigned int loop_num = IPC_REPEAT_TIME;
414-
415-
//need to take the mutex first to ensure that Rx Thread can not get it
416-
rt_mutex_take(mutex_test, RT_WAITING_FOREVER);
417368

418369
while(loop_num --)
419370
{
420-
//wait receive is done
421-
rt_sem_take(Mutex_Control, RT_WAITING_FOREVER);
422-
423-
start_count();
424-
rt_mutex_release(mutex_test);
371+
rt_sem_take(sem_test, RT_WAITING_FOREVER);
372+
stop_count();
373+
calc_count();
425374
}
426375

427376
rt_sem_release(IPC_Control);//跑完放出control信号量
428377
}
429378

430379
void thread_mutex_RX_entry(void* parameter)
431380
{
432-
unsigned int loop_num = IPC_REPEAT_TIME;
433-
434-
while(loop_num --)
435-
{
436-
rt_mutex_take(mutex_test, RT_WAITING_FOREVER);
437-
stop_count();
438-
calc_count();
439-
440-
//we should start schedule to Thread TX
441-
//list_thread();
442-
rt_sem_release(Mutex_Control);
443-
}
381+
rt_mutex_take(mutex_test, RT_WAITING_FOREVER);
382+
stop_count();
383+
calc_count();
444384

445385
rt_sem_release(IPC_Control);//跑完放出control信号量
446386
}
447387

448388

449-
450389
void thread_event_TX_entry(void* parameter)
451390
{
452391
unsigned int loop_num = IPC_REPEAT_TIME;
453392

454393
while(loop_num --)
455394
{
456-
//等待接收完成
457-
rt_sem_take(Mutex_Control, RT_WAITING_FOREVER);
458-
459-
460395
start_count();
461396
rt_event_send(event_test, IPC_EVENT);
462397
}
@@ -472,15 +407,12 @@ void thread_event_RX_entry(void* parameter)
472407
while(loop_num --)
473408
{
474409

475-
if (rt_event_recv(event_test, IPC_EVENT,RT_EVENT_FLAG_OR \
476-
| RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER, &e) == RT_EOK)
410+
if (rt_event_recv(event_test, IPC_EVENT,
411+
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
412+
RT_WAITING_FOREVER, &e) == RT_EOK)
477413
{
478414
stop_count();
479415
calc_count();
480-
//rt_kprintf("thread1: AND recv event 0x%x\n", e);
481-
482-
rt_sem_release(Mutex_Control);
483-
484416
}
485417
}
486418

@@ -493,8 +425,6 @@ void thread_mb_TX_entry(void* parameter)
493425

494426
while(loop_num --)
495427
{
496-
rt_sem_take(Mutex_Control, RT_WAITING_FOREVER);
497-
498428
start_count();
499429
rt_mb_send(mailbox_test,(rt_uint32_t)&mb_str[0]);
500430
}
@@ -509,22 +439,17 @@ void thread_mb_RX_entry(void* parameter)
509439

510440
while(loop_num --)
511441
{
512-
if ( rt_mb_recv(mailbox_test, (rt_uint32_t*)&str, RT_WAITING_FOREVER)== RT_EOK)
442+
if (rt_mb_recv(mailbox_test,
443+
(rt_uint32_t*)&str,
444+
RT_WAITING_FOREVER) == RT_EOK)
513445
{
514446
stop_count();
515447
calc_count();
516-
//rt_kprintf("thread1: get a mail, the content:%s\n", str);
517-
518-
rt_sem_release(Mutex_Control);
519448
}
520449
}
521450

522451
rt_sem_release(IPC_Control);//跑完放出control信号量
523452
}
524453

525-
526-
527-
528-
529454
#endif
530455

0 commit comments

Comments
 (0)