interrupt_sample.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. */
  10. /* 程序清单:关闭中断进行全局变量的访问 */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #define THREAD_PRIORITY 20
  14. #define THREAD_STACK_SIZE 512
  15. #define THREAD_TIMESLICE 5
  16. /* 同时访问的全局变量 */
  17. static rt_uint32_t cnt;
  18. void thread_entry(void *parameter)
  19. {
  20. rt_uint32_t no;
  21. rt_uint32_t level;
  22. no = (rt_uint32_t) parameter;
  23. while (1)
  24. {
  25. /* 关闭中断 */
  26. level = rt_hw_interrupt_disable();
  27. cnt += no;
  28. /* 恢复中断 */
  29. rt_hw_interrupt_enable(level);
  30. rt_kprintf("protect thread[%d]'s counter is %d\n", no, cnt);
  31. rt_thread_mdelay(no * 10);
  32. }
  33. }
  34. /* 用户应用程序入口 */
  35. int interrupt_sample(void)
  36. {
  37. rt_thread_t thread;
  38. /* 创建t1线程 */
  39. thread = rt_thread_create("thread1", thread_entry, (void *)10,
  40. THREAD_STACK_SIZE,
  41. THREAD_PRIORITY, THREAD_TIMESLICE);
  42. if (thread != RT_NULL)
  43. rt_thread_startup(thread);
  44. /* 创建t2线程 */
  45. thread = rt_thread_create("thread2", thread_entry, (void *)20,
  46. THREAD_STACK_SIZE,
  47. THREAD_PRIORITY, THREAD_TIMESLICE);
  48. if (thread != RT_NULL)
  49. rt_thread_startup(thread);
  50. return 0;
  51. }
  52. /* 导出到 msh 命令列表中 */
  53. MSH_CMD_EXPORT(interrupt_sample, interrupt sample);