收藏官网首页
查看: 18718|回复: 0

【一起来玩RTOS系列】之RT-Thread 线程的挂起和恢复

321

主题

1054

帖子

4513

积分

论坛元老

Rank: 8Rank: 8

积分
4513
QQ
跳转到指定楼层
楼主
发表于 2017-11-25 12:40:13 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
汉枫LPB120模块
线程挂起

当线程调用rt_thread_delay,调用线程将主动挂起,当调用rt_sem_take,rt_mb_recv等函数时,资源不可使用也将导致调用线程挂起。处于挂起状态的线程,如果其等待的资源超时(超过其设定的等待时间),那么该线程将不再等待这些资源,并返回到就绪状态;或者,当其它线程释放掉该线程所等待的资源时,该线程也会返回到就绪状态。
线程挂起使用下面的函数接口:
  1. rt_err_t rt_thread_suspend (rt_thread_t thread);
复制代码
线程安全:安全
中断例程:可调用
函数参数
thread  线程句柄。
函数返回
如果这个线程的状态并不是就绪状态,将返回-RT_ERROR,否则返回RT_EOK
注:通常不应该使用这个函数来挂起线程本身,如果确实需要采用rt_thread_suspend函数挂起当前任务,需要在调用rt_thread_suspend()函数后立刻调用rt_schedule()函数进行手动的线程上下文切换。

线程恢复

线程恢复就是让挂起的线程重新进入就绪状态,如果被恢复线程在所有就绪态线程中,位于最高优先级链表的第一位,那么系统将进行线程上下文的切换。线程恢复使用下面的函数接口:
  1.     rt_err_t rt_thread_resume (rt_thread_t thread);
复制代码
线程安全:安全
中断例程:可调用
函数参数
thread  要恢复的线程句柄。
函数返回
如果恢复的线程状态并不是RT_THREAD_SUSPEND状态,将返回-RT_ERROR;否则返回RT_EOK。

下面在机智云gokit开发板上演示线程的创建和恢复过程,程序中创建了2个不同优先级的线程,高优先级线程启动后立即阻塞,让出处理器,低优先级的线程启动后挂起自身等待高优先级线程的唤醒,通过串口可以查看打印信息。
  1. /**
  2.   ****************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ****************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2017 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ****************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "STM32f1xx_hal.h"
  41. #include "usart.h"
  42. #include "gpio.h"

  43. /* USER CODE BEGIN Includes */
  44. #include "rtthread.h"
  45. /* USER CODE END Includes */

  46. /* Private variables ---------------------------------------------------------*/

  47. /* USER CODE BEGIN PV */
  48. /* Private variables ---------------------------------------------------------*/

  49. /* USER CODE END PV */

  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);

  52. /* USER CODE BEGIN PFP */
  53. /* Private function prototypes -----------------------------------------------*/

  54. /* USER CODE END PFP */

  55. /* USER CODE BEGIN 0 */
  56. //重映射串口1到rt_kprintf
  57. void rt_hw_console_output(const char *str)
  58. {
  59.     /* empty console output */
  60.         char aa='\r';
  61.           rt_enter_critical();

  62.                 while(*str!='\0')
  63.                 {
  64.                         if(*str=='\n')
  65.                         {
  66.                                 HAL_UART_Transmit(&huart1, (uint8_t *)&aa, 1, 10);
  67.                         }
  68.                                 HAL_UART_Transmit(&huart1, (uint8_t *)(str++), 1, 10);
  69.                 }
  70.                
  71.                 rt_exit_critical();
  72. }


  73. /*
  74. * 程序清单:线程的挂起和恢复
  75. * 这个例子中将创建两个动态线程(tid1和tid2),
  76. * 低优先级线程t1将挂起自身
  77. * 高优先级线程t2将在一定时刻后唤醒低优先级线程。
  78. */

  79. /* 指向线程控制块的指针 */
  80. static rt_thread_t tid1 = RT_NULL;
  81. static rt_thread_t tid2 = RT_NULL;

  82. /* 线程1入口 */
  83. static void thread1_entry(void* parameter)
  84. {
  85.         /* 低优先级线程1开始运行 */
  86.         rt_kprintf("thread1 startup\n");
  87.         /* 挂起自身 */
  88.         rt_kprintf("thread1 suspend thread self\n");
  89.         rt_thread_suspend(tid1);
  90.         /* 主动执行线程调度 */
  91.         rt_schedule();
  92.         /* 当线程1被唤醒时 */
  93.         rt_kprintf("thread1 resumed\n");
  94. }

  95. /* 线程2入口 */
  96. static void thread2_entry(void* parameter)
  97. {
  98.         /* 延时1000个OS Tick */
  99.         rt_thread_delay(1000);
  100.         /* 唤醒线程1 */
  101.         rt_thread_resume(tid1);
  102.         /* 延时1000个OS Tick */
  103.         rt_thread_delay(1000);
  104.         /* 线程2自动退出 */
  105. }

  106. /* USER CODE END 0 */

  107. int main(void)
  108. {

  109.   /* USER CODE BEGIN 1 */

  110.   /* USER CODE END 1 */

  111.   /* MCU Configuration----------------------------------------------------------*/

  112. //  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  113. //  HAL_Init();

  114. //  /* USER CODE BEGIN Init */

  115. //  /* USER CODE END Init */

  116. //  /* Configure the system clock */
  117. //  SystemClock_Config();

  118. //  /* USER CODE BEGIN SysInit */

  119. //  /* USER CODE END SysInit */

  120. //  /* Initialize all configured peripherals */
  121. //  MX_GPIO_Init();
  122. //  MX_USART1_UART_Init();

  123.   /* USER CODE BEGIN 2 */
  124.        
  125.    /* 创建线程1 */
  126.     tid1 = rt_thread_create("t1",
  127.                                                         thread1_entry,
  128.                                                         RT_NULL,
  129.                                                         512,
  130.                                                         2,
  131.                                                         20);
  132.         if (tid1 != RT_NULL)
  133.                 rt_thread_startup(tid1);
  134.        
  135.         /* 创建线程2 */
  136.         tid2 = rt_thread_create("t2",
  137.                                                         thread2_entry,
  138.                                                         RT_NULL,
  139.                                                         512,
  140.                                                         1,
  141.                                                         20);

  142.         if (tid2 != RT_NULL)
  143.                 rt_thread_startup(tid2);
  144.        
  145.         printf("\r\n机智云  只为智能硬件而生\r\n");
  146.         printf("Gizwits Smart Cloud for Smart Products\r\n");
  147.         printf("链接|增值|开放|中立|安全|自有|自由|生态\r\n");
  148.         printf("www.gizwits.com\r\n");
  149.         printf("\r\nGokit RT-Thread Demo\r\n\r\n");
  150.        
  151.         return 0;
  152.        
  153.        


  154.   /* USER CODE END 2 */

  155.   /* Infinite loop */
  156.   /* USER CODE BEGIN WHILE */
  157. //  while (1)
  158. //  {
  159.   /* USER CODE END WHILE */

  160.   /* USER CODE BEGIN 3 */
  161. //               
  162. //  }
  163.   /* USER CODE END 3 */

  164. }

  165. /** System Clock Configuration
  166. */
  167. void SystemClock_Config(void)
  168. {

  169.   RCC_OscInitTypeDef RCC_OscInitStruct;
  170.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  171.     /**Initializes the CPU, AHB and APB busses clocks
  172.     */
  173.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  174.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  175.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  176.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  177.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  178.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  179.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  180.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  181.   {
  182.     _Error_Handler(__FILE__, __LINE__);
  183.   }

  184.     /**Initializes the CPU, AHB and APB busses clocks
  185.     */
  186.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  187.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  188.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  189.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  190.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  191.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  192.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  193.   {
  194.     _Error_Handler(__FILE__, __LINE__);
  195.   }

  196.     /**Configure the Systick interrupt time
  197.     */
  198.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  199.     /**Configure the Systick
  200.     */
  201.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  202.   /* SysTick_IRQn interrupt configuration */
  203.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  204. }

  205. /* USER CODE BEGIN 4 */

  206. /* USER CODE END 4 */

  207. /**
  208.   * @brief  This function is executed in case of error occurrence.
  209.   * @param  None
  210.   * @retval None
  211.   */
  212. void _Error_Handler(char * file, int line)
  213. {
  214.   /* USER CODE BEGIN Error_Handler_Debug */
  215.   /* User can add his own implementation to report the HAL error return state */
  216.   while(1)
  217.   {
  218.   }
  219.   /* USER CODE END Error_Handler_Debug */
  220. }

  221. #ifdef USE_FULL_ASSERT

  222. /**
  223.    * @brief Reports the name of the source file and the source line number
  224.    * where the assert_param error has occurred.
  225.    * @param file: pointer to the source file name
  226.    * @param line: assert_param error line source number
  227.    * @retval None
  228.    */
  229. void assert_failed(uint8_t* file, uint32_t line)
  230. {
  231.   /* USER CODE BEGIN 6 */
  232.   /* User can add his own implementation to report the file name and line number,
  233.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  234.   /* USER CODE END 6 */

  235. }

  236. #endif

  237. /**
  238.   * @}
  239.   */

  240. /**
  241.   * @}
  242. */

  243. /**************** (C) COPYRIGHT STMicroelectronics ****END OF FILE***/
复制代码

通过串口看到打印信息与预期一致:



源码工程下载:
Gokit_RT-Thread.zip (8.7 MB, 下载次数: 1, 售价: 1 金钱)



1、机智云QQ群:G1群:104975951 G2群:491509598 G3群:287087942
机智云爱好者-APP开发群: 599735135
QQ群目前非常活跃,欢迎大家参与进来,交流,讨论,答疑,解惑~~
2、机智云微信公众号: 机智云 gizwits、 机智云智能宠物屋go-iot
关注机智云Gizwits官方公众号随时掌握最新资讯和活动信息
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表