搜索
bottom↓
回复: 2

RT-Thread 中线程栈溢出检查的疑惑

[复制链接]

出0入0汤圆

发表于 2019-6-10 19:16:43 | 显示全部楼层 |阅读模式
本帖最后由 MurphyZhao 于 2019-6-10 19:19 编辑

    RT-Thread 中线程栈溢出检查在 void rt_schedule(void) 函数中,
但是,为什么仅检查了 to_thread,而不是检查 from_thread

    如果在 from_thread 栈溢出破坏了 to_thread 线程栈,检查 to_thread 栈溢出的时候并不会检查到异常,
但,切换到 to_thread 线程运行的时候,出现了错误,这个时候,定位到的错误在 to_thread,但确实是 from_thread 线程栈溢出导致的,这种情况是不是检查 from_thread 线程栈更加合理?

检查栈溢出的函数如下:

  1. _rt_scheduler_stack_check(to_thread);
复制代码


检查线程栈溢出的代码如下:


  1.             if (to_thread != rt_current_thread)
  2.             {
  3.                 /* if the destination thread is not the same as current thread */
  4.                 rt_current_priority = (rt_uint8_t)highest_ready_priority;
  5.                 from_thread         = rt_current_thread;
  6.                 rt_current_thread   = to_thread;

  7.                 RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));

  8.                 if (need_insert_from_thread)
  9.                 {
  10.                     rt_schedule_insert_thread(from_thread);
  11.                 }

  12.                 rt_schedule_remove_thread(to_thread);
  13.                 to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);

  14.                 /* switch to new thread */
  15.                 RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
  16.                         ("[%d]switch to priority#%d "
  17.                          "thread:%.*s(sp:0x%08x), "
  18.                          "from thread:%.*s(sp: 0x%08x)\n",
  19.                          rt_interrupt_nest, highest_ready_priority,
  20.                          RT_NAME_MAX, to_thread->name, to_thread->sp,
  21.                          RT_NAME_MAX, from_thread->name, from_thread->sp));

  22. #ifdef RT_USING_OVERFLOW_CHECK
  23.                 _rt_scheduler_stack_check(to_thread);
  24. #endif

  25.                 if (rt_interrupt_nest == 0)
  26.                 {
  27.                     extern void rt_thread_handle_sig(rt_bool_t clean_state);

  28.                     rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
  29.                             (rt_ubase_t)&to_thread->sp);

  30.                     /* enable interrupt */
  31.                     rt_hw_interrupt_enable(level);

  32. #ifdef RT_USING_SIGNALS
  33.                     /* check signal status */
  34.                     rt_thread_handle_sig(RT_TRUE);
  35. #endif
  36.                     goto __exit;
  37.                 }
  38.                 else
  39.                 {
  40.                     RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));

  41.                     rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
  42.                             (rt_ubase_t)&to_thread->sp);
  43.                 }
  44.             }
复制代码

出0入0汤圆

 楼主| 发表于 2019-6-11 18:21:42 | 显示全部楼层
本帖最后由 MurphyZhao 于 2019-6-12 08:35 编辑

特意去查了下 FreeRTOS 的源码,发现 FreeRTOS 会对 from_thread 进行堆栈溢出检查,还检查了两次(不知道分别做什么),代码如下:

  1.                 /* Check for stack overflow, if configured. */
  2.                 taskFIRST_CHECK_FOR_STACK_OVERFLOW();
  3.                 taskSECOND_CHECK_FOR_STACK_OVERFLOW();

  4.                 /* Select a new task to run using either the generic C or port
  5.                 optimised asm code. */
  6.                 taskSELECT_HIGHEST_PRIORITY_TASK();
  7.                 traceTASK_SWITCHED_IN();

  8.                 #if ( configUSE_NEWLIB_REENTRANT == 1 )
  9.                 {
  10.                         /* Switch Newlib's _impure_ptr variable to point to the _reent
  11.                         structure specific to this task. */
  12.                         _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
  13.                 }
  14.                 #endif /* configUSE_NEWLIB_REENTRANT */
  15.         }
  16.         _next_thread = (void *)pxCurrentTCB;
复制代码

出0入198汤圆

发表于 2019-6-11 18:23:08 | 显示全部楼层
难道是为了让系统多跑一会
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-20 08:25

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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