mutex_example.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2021, Meco Jianting Man <jiantingman@foxmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-09-30 Meco Man first version
  9. */
  10. #include <ucos_ii.h>
  11. #define TASK_SIZE 256
  12. #define TASK1_PRIO 5
  13. #define TASK2_PRIO 6
  14. static OS_STK task1_stack [TASK_SIZE];
  15. static OS_STK task2_stack [TASK_SIZE];
  16. static OS_EVENT *pMutex;
  17. static void task1(void *p_arg)
  18. {
  19. INT8U err;
  20. while(1)
  21. {
  22. OSMutexPend(pMutex,0,&err);
  23. rt_kprintf("task1\n");
  24. OSMutexPost(pMutex);
  25. OSTimeDly(1000);
  26. }
  27. }
  28. static void task2(void *p_arg)
  29. {
  30. INT8U err;
  31. while(1)
  32. {
  33. OSMutexPend(pMutex,0,&err);
  34. rt_kprintf("task2\n");
  35. OSMutexPost(pMutex);
  36. OSTimeDly(1000);
  37. }
  38. }
  39. void mutex_example (void)
  40. {
  41. INT8U err;
  42. pMutex = OSMutexCreate(0,&err); /*第一个参数在兼容层中没有意义,可以随意填写*/
  43. #if OS_STK_GROWTH == 0u
  44. OSTaskCreateExt(task1,0,&task1_stack[0],TASK_PRIO,0,&task1_stack[TASK_SIZE-1],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  45. #else
  46. OSTaskCreateExt(task1,0,&task1_stack[TASK_SIZE-1],TASK1_PRIO,0,&task1_stack[0],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  47. #endif
  48. #if OS_STK_GROWTH == 0u
  49. OSTaskCreateExt(task2,0,&task2_stack[0],TASK_PRIO,0,&task2_stack[TASK_SIZE-1],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  50. #else
  51. OSTaskCreateExt(task2,0,&task2_stack[TASK_SIZE-1],TASK2_PRIO,0,&task2_stack[0],TASK_SIZE,0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);
  52. #endif
  53. }