application.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-04-16 first version
  13. */
  14. #include <rtthread.h>
  15. #define THREAD_STACK_SIZE 1024
  16. #if 0
  17. struct rt_semaphore sem1, sem2;
  18. static struct rt_thread thread1;
  19. ALIGN(4)
  20. static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
  21. static struct rt_thread thread2;
  22. ALIGN(4)
  23. static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
  24. static void thread1_entry(void* parameter)
  25. {
  26. while (1)
  27. {
  28. rt_sem_release(&sem2);
  29. rt_kprintf("thread1..: %s\n", rt_thread_self()->name);
  30. rt_sem_take(&sem1, RT_WAITING_FOREVER);
  31. rt_kprintf("get semaphore: %s!\n", rt_thread_self()->name);
  32. }
  33. }
  34. static void thread2_entry(void* parameter)
  35. {
  36. while (1)
  37. {
  38. rt_sem_take(&sem2, RT_WAITING_FOREVER);
  39. rt_kprintf("thread2--->: %s\n", rt_thread_self()->name);
  40. rt_sem_release(&sem1);
  41. }
  42. }
  43. /* user application */
  44. int rt_application_init()
  45. {
  46. rt_err_t result;
  47. rt_sem_init(&sem1, "s1", 0, RT_IPC_FLAG_FIFO);
  48. rt_sem_init(&sem2, "s2", 0, RT_IPC_FLAG_FIFO);
  49. result = rt_thread_init(&thread1, "t1", thread1_entry, RT_NULL,
  50. &thread1_stack[0], sizeof(thread1_stack), 10, 10);
  51. if (result == RT_EOK)
  52. rt_thread_startup(&thread1);
  53. result = rt_thread_init(&thread2, "t2", thread2_entry, RT_NULL,
  54. &thread2_stack[0], sizeof(thread2_stack), 18, 10);
  55. if (result == RT_EOK)
  56. rt_thread_startup(&thread2);
  57. return 0;
  58. }
  59. #else
  60. static struct rt_thread thread1;
  61. ALIGN(4)
  62. static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
  63. rt_timer_t ttimer;
  64. static void thread1_entry(void* parameter)
  65. {
  66. rt_uint32_t count = 0;
  67. while (1)
  68. {
  69. rt_kprintf("%s: count = %d\n", rt_thread_self()->name, count ++);
  70. rt_thread_delay(10);
  71. }
  72. }
  73. /* user application */
  74. int rt_application_init()
  75. {
  76. rt_err_t result;
  77. result = rt_thread_init(&thread1, "t1", thread1_entry, RT_NULL,
  78. &thread1_stack[0], sizeof(thread1_stack), 10, 10);
  79. ttimer = &(thread1.thread_timer);
  80. if (result == RT_EOK)
  81. rt_thread_startup(&thread1);
  82. return 0;
  83. }
  84. #endif