Просмотр исходного кода

Merge pull request #8 from yangjie11/yangjie

modify readme ,modify the  format of samples,modify the  name of threads
yqiu 7 лет назад
Родитель
Сommit
c3317d6be4
9 измененных файлов с 24 добавлено и 19 удалено
  1. 1 1
      README.md
  2. 2 1
      dynmem_sample.c
  3. 0 1
      event_sample.c
  4. 2 4
      interrupt_sample.c
  5. 9 2
      msgq_sample.c
  6. 0 2
      priority_inversion.c
  7. 4 3
      semaphore_sample.c
  8. 4 3
      thread_sample.c
  9. 2 2
      timeslice_sample.c

+ 1 - 1
README.md

@@ -16,7 +16,7 @@
 | memp_sample.c  | 内存池的使用 |
 | msgq_sample.c | 消息队列的使用 |
 | mutex_sample.c  | 互斥量的使用 |
-| priority_inversion.c | 优先级翻转特性 |
+| priority_inversion.c | 互斥量解决优先级翻转问题 |
 | producer_consumer.c | 生产者消费者模型 |
 | scheduler_hook.c | 调度器钩子的使用 |
 | semaphore_sample.c | 信号量的使用|

+ 2 - 1
dynmem_sample.c

@@ -50,7 +50,7 @@ void thread1_entry(void *parameter)
 
 int dynmem_sample(void)
 {
-    rt_thread_t tid;
+    rt_thread_t tid = RT_NULL;
 
     /* 创建线程1 */
     tid = rt_thread_create("thread1",
@@ -63,5 +63,6 @@ int dynmem_sample(void)
 
     return 0;
 }
+
 /* 导出到 msh 命令列表中 */
 MSH_CMD_EXPORT(dynmem_sample, dynmem sample);

+ 0 - 1
event_sample.c

@@ -56,7 +56,6 @@ static void thread1_recv_event(void *param)
     rt_kprintf("thread1 leave.\n");
 }
 
-
 ALIGN(RT_ALIGN_SIZE)
 static char thread2_stack[1024];
 static struct rt_thread thread2;

+ 2 - 4
interrupt_sample.c

@@ -37,20 +37,18 @@ void thread_entry(void *parameter)
     }
 }
 
-/* 用户应用程序入口 */
 int interrupt_sample(void)
 {
     rt_thread_t thread;
 
-    /* 创建t1线程 */
+    /* 创建thread1线程 */
     thread = rt_thread_create("thread1", thread_entry, (void *)10,
                               THREAD_STACK_SIZE,
                               THREAD_PRIORITY, THREAD_TIMESLICE);
     if (thread != RT_NULL)
         rt_thread_startup(thread);
 
-
-    /* 创建t2线程 */
+    /* 创建thread2线程 */
     thread = rt_thread_create("thread2", thread_entry, (void *)20,
                               THREAD_STACK_SIZE,
                               THREAD_PRIORITY, THREAD_TIMESLICE);

+ 9 - 2
msgq_sample.c

@@ -16,6 +16,9 @@
  */
 #include <rtthread.h>
 
+#define THREAD_PRIORITY      25
+#define THREAD_TIMESLICE     5
+
 /* 消息队列控制块 */
 static struct rt_messagequeue mq;
 /* 消息队列中用到的放置消息的内存池 */
@@ -24,6 +27,7 @@ static rt_uint8_t msg_pool[2048];
 ALIGN(RT_ALIGN_SIZE)
 static char thread1_stack[1024];
 static struct rt_thread thread1;
+
 /* 线程1入口函数 */
 static void thread1_entry(void *parameter)
 {
@@ -52,6 +56,7 @@ static void thread1_entry(void *parameter)
 ALIGN(RT_ALIGN_SIZE)
 static char thread2_stack[1024];
 static struct rt_thread thread2;
+
 /* 线程2入口 */
 static void thread2_entry(void *parameter)
 {
@@ -121,7 +126,8 @@ int msgq_sample(void)
                    thread1_entry,
                    RT_NULL,
                    &thread1_stack[0],
-                   sizeof(thread1_stack), 25, 5);
+                   sizeof(thread1_stack), 
+                   THREAD_PRIORITY, THREAD_TIMESLICE);
     rt_thread_startup(&thread1);
 
     rt_thread_init(&thread2,
@@ -129,7 +135,8 @@ int msgq_sample(void)
                    thread2_entry,
                    RT_NULL,
                    &thread2_stack[0],
-                   sizeof(thread2_stack), 25, 5);
+                   sizeof(thread2_stack), 
+                   THREAD_PRIORITY, THREAD_TIMESLICE);
     rt_thread_startup(&thread2);
 
     return 0;

+ 0 - 2
priority_inversion.c

@@ -27,7 +27,6 @@ static rt_thread_t tid2 = RT_NULL;
 static rt_thread_t tid3 = RT_NULL;
 static rt_mutex_t mutex = RT_NULL;
 
-
 #define THREAD_PRIORITY       10
 #define THREAD_STACK_SIZE     512
 #define THREAD_TIMESLICE      5
@@ -67,7 +66,6 @@ static void thread2_entry(void *parameter)
     /* 先让低优先级线程运行 */
     rt_thread_mdelay(50);
 
-
     /*
      * 试图持有互斥锁,此时 thread3 持有,应把 thread3 的优先级提升
      * 到 thread2 相同的优先级

+ 4 - 3
semaphore_sample.c

@@ -41,7 +41,7 @@ static void rt_thread1_entry(void *parameter)
         /* count每计数10次,就释放一次信号量 */
          if(0 == (count % 10))
         {
-            rt_kprintf("t1 release a dynamic semaphore.\n" ); 
+            rt_kprintf("thread1 release a dynamic semaphore.\n" ); 
             rt_sem_release(dynamic_sem);            
         }
     }
@@ -60,14 +60,14 @@ static void rt_thread2_entry(void *parameter)
         result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER);
         if (result != RT_EOK)
         {        
-            rt_kprintf("t2 take a dynamic semaphore, failed.\n");
+            rt_kprintf("thread2 take a dynamic semaphore, failed.\n");
             rt_sem_delete(dynamic_sem);
             return;
         }
         else
         {      
             number++;             
-            rt_kprintf("t2 take a dynamic semaphore. number = %d\n" ,number);                        
+            rt_kprintf("thread2 take a dynamic semaphore. number = %d\n" ,number);                        
         }
     }   
 }
@@ -107,6 +107,7 @@ int semaphore_sample()
 
     return 0;
 }
+
 /* 导出到 msh 命令列表中 */
 MSH_CMD_EXPORT(semaphore_sample, semaphore sample);
 

+ 4 - 3
thread_sample.c

@@ -9,10 +9,10 @@
  */ 
 
 /*
- * 程序清单:创建/删除、初始化线程
+ * 程序清单:创建、初始化/脱离线程
  *
  * 这个例子会创建两个线程,一个动态线程,一个静态线程。
- * 一个线程在运行完毕后自动被系统删除,另一个线程一直打印计数。
+ * 静态线程在运行完毕后自动被系统脱离,动态线程一直打印计数。
  */
 #include <rtthread.h>
 
@@ -38,6 +38,7 @@ static void thread1_entry(void *parameter)
 ALIGN(RT_ALIGN_SIZE)
 static char thread2_stack[1024];
 static struct rt_thread thread2;
+
 /* 线程2入口 */
 static void thread2_entry(void *param)
 {
@@ -54,7 +55,7 @@ static void thread2_entry(void *param)
     (线程控制块和线程栈依然在idle线程中释放) */
 }
 
-/* 删除线程示例的初始化 */
+/* 线程示例 */
 int thread_sample(void)
 {
     /* 创建线程1,名称是thread1,入口是thread1_entry*/

+ 2 - 2
timeslice_sample.c

@@ -43,7 +43,7 @@ static void thread_entry(void* parameter)
 
 int timeslice_sample(void)
 {
-    rt_thread_t tid;
+    rt_thread_t tid = RT_NULL;
     /* 创建线程1 */
     tid = rt_thread_create("thread1", 
                             thread_entry, (void*)1, 
@@ -52,7 +52,6 @@ int timeslice_sample(void)
     if (tid != RT_NULL) 
         rt_thread_startup(tid);
 
-
     /* 创建线程2 */
     tid = rt_thread_create("thread2", 
                             thread_entry, (void*)2,
@@ -60,6 +59,7 @@ int timeslice_sample(void)
                             THREAD_PRIORITY, THREAD_TIMESLICE-5);
     if (tid != RT_NULL) 
         rt_thread_startup(tid);
+        
     return 0;
 }