搜索
bottom↓
回复: 12

[MsgOS]空闲任务源码

[复制链接]

出0入0汤圆

发表于 2015-8-2 21:09:05 | 显示全部楼层 |阅读模式
本帖最后由 科技猎人 于 2015-8-2 21:10 编辑

下面是MsgOS里空闲任务的源码,感兴趣的朋友可以猜猜里面的"玄机"

void  thread_idle_entry(void *parameter)
{
   uint32  idle_t=0;
    while(1)
   {
      idle_t++;     
   }     
}

相关帖子
http://www.amobbs.com/forum.php? ... &extra=page%3D1
http://www.amobbs.com/forum.php? ... =5621706&extra=
http://www.amobbs.com/forum.php? ... =5603557&extra=

出0入0汤圆

发表于 2015-8-2 21:33:09 | 显示全部楼层
为什么while(1)呢。什么玄机?

出0入76汤圆

发表于 2015-8-2 21:43:45 | 显示全部楼层
想知道,你的这个MsgOS有抢占式的,还是合作式的? 如果是合作式的, 不知道你这个任务如何被才能挂起(或打断)。。

出0入0汤圆

发表于 2015-8-2 22:02:41 | 显示全部楼层
有什么玄机呀??
idle_t本来可以用来统计cpu使用率,但是又放在线程的堆载里面.....

出0入0汤圆

 楼主| 发表于 2015-8-2 22:05:08 | 显示全部楼层
foxpro2005 发表于 2015-8-2 21:43
想知道,你的这个MsgOS有抢占式的,还是合作式的? 如果是合作式的, 不知道你这个任务如何被才能挂起(或 ...

抢占式的

出0入0汤圆

 楼主| 发表于 2015-8-2 22:06:45 | 显示全部楼层
gwnpeter 发表于 2015-8-2 22:02
有什么玄机呀??
idle_t本来可以用来统计cpu使用率,但是又放在线程的堆载里面..... ...

问题就在这里

出0入0汤圆

发表于 2015-8-2 23:54:07 | 显示全部楼层
抢占式的,才需要空闲线程。
协作式的,不需要空闲线程。
如此而已。

出0入0汤圆

 楼主| 发表于 2015-8-3 09:07:06 | 显示全部楼层
对比一下其他RTOS系统的空闲任务,下面是ucosiii的空闲任务
void  OS_IdleTask (void  *p_arg)
{
    CPU_SR_ALLOC();



    p_arg = p_arg;                                          /* Prevent compiler warning for not using 'p_arg'         */

    while (DEF_ON) {
        CPU_CRITICAL_ENTER();
        OSIdleTaskCtr++;
#if OS_CFG_STAT_TASK_EN > 0u
        OSStatTaskCtr++;
#endif
        CPU_CRITICAL_EXIT();

        OSIdleTaskHook();                                   /* Call user definable HOOK                               */
    }
}

出0入0汤圆

 楼主| 发表于 2015-8-3 09:11:27 | 显示全部楼层
rt_thread里的空闲任务
static void rt_thread_idle_entry(void *parameter)
{
    while (1)
    {
        #ifdef RT_USING_HOOK
        if (rt_thread_idle_hook != RT_NULL)
            rt_thread_idle_hook();
        #endif

        rt_thread_idle_excute();
    }
}

出0入0汤圆

 楼主| 发表于 2015-8-3 09:19:37 | 显示全部楼层
freeOS里的空闲任务
/*
* -----------------------------------------------------------
* The Idle task.
* ----------------------------------------------------------
*
* The portTASK_FUNCTION() macro is used to allow port/compiler specific
* language extensions.  The equivalent prototype for this function is:
*
* void prvIdleTask( void *pvParameters );
*
*/
static portTASK_FUNCTION( prvIdleTask, pvParameters )
{
        /* Stop warnings. */
        ( void ) pvParameters;

        for( ;; )
        {
                /* See if any tasks have been deleted. */
                prvCheckTasksWaitingTermination();

                #if ( configUSE_PREEMPTION == 0 )
                {
                        /* If we are not using preemption we keep forcing a task switch to
                        see if any other task has become available.  If we are using
                        preemption we don't need to do this as any task becoming available
                        will automatically get the processor anyway. */
                        taskYIELD();
                }
                #endif /* configUSE_PREEMPTION */

                #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
                {
                        /* When using preemption tasks of equal priority will be
                        timesliced.  If a task that is sharing the idle priority is ready
                        to run then the idle task should yield before the end of the
                        timeslice.

                        A critical region is not required here as we are just reading from
                        the list, and an occasional incorrect value will not matter.  If
                        the ready list at the idle priority contains more than one task
                        then a task other than the idle task is ready to execute. */
                        if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
                        {
                                taskYIELD();
                        }
                        else
                        {
                                mtCOVERAGE_TEST_MARKER();
                        }
                }
                #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */

                #if ( configUSE_IDLE_HOOK == 1 )
                {
                        extern void vApplicationIdleHook( void );

                        /* Call the user defined function from within the idle task.  This
                        allows the application designer to add background functionality
                        without the overhead of a separate task.
                        NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
                        CALL A FUNCTION THAT MIGHT BLOCK. */
                        vApplicationIdleHook();
                }
                #endif /* configUSE_IDLE_HOOK */

                /* This conditional compilation should use inequality to 0, not equality
                to 1.  This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
                user defined low power mode        implementations require
                configUSE_TICKLESS_IDLE to be set to a value other than 1. */
                #if ( configUSE_TICKLESS_IDLE != 0 )
                {
                TickType_t xExpectedIdleTime;

                        /* It is not desirable to suspend then resume the scheduler on
                        each iteration of the idle task.  Therefore, a preliminary
                        test of the expected idle time is performed without the
                        scheduler suspended.  The result here is not necessarily
                        valid. */
                        xExpectedIdleTime = prvGetExpectedIdleTime();

                        if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
                        {
                                vTaskSuspendAll();
                                {
                                        /* Now the scheduler is suspended, the expected idle
                                        time can be sampled again, and this time its value can
                                        be used. */
                                        configASSERT( xNextTaskUnblockTime >= xTickCount );
                                        xExpectedIdleTime = prvGetExpectedIdleTime();

                                        if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
                                        {
                                                traceLOW_POWER_IDLE_BEGIN();
                                                portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
                                                traceLOW_POWER_IDLE_END();
                                        }
                                        else
                                        {
                                                mtCOVERAGE_TEST_MARKER();
                                        }
                                }
                                ( void ) xTaskResumeAll();
                        }
                        else
                        {
                                mtCOVERAGE_TEST_MARKER();
                        }
                }
                #endif /* configUSE_TICKLESS_IDLE */
        }
}

出0入0汤圆

发表于 2015-8-3 14:22:15 | 显示全部楼层
看不出有什么玄机

出0入0汤圆

发表于 2015-8-4 13:48:22 | 显示全部楼层
1.如果cpu支持省电模式,并且支持systick唤醒,那么该任务可以允许cpu进入低功耗,更爽。
2.自加的这个量,可以用来测试cpu使用率。
3.如果系统使用了看门狗,个人认为可以在此喂狗。你的系统设计idle任务优先级最低,所以任何一个比他高的任务死掉后,喂狗都会无效导致重启。
4.想不出来了

出0入0汤圆

 楼主| 发表于 2015-8-4 20:08:45 | 显示全部楼层
avr-arm 发表于 2015-8-4 13:48
1.如果cpu支持省电模式,并且支持systick唤醒,那么该任务可以允许cpu进入低功耗,更爽。
2.自加的这个量, ...

在我的系统里,喂狗,休眠,运行灯控制等都是在切入或切出空闲任务的钩子函数里实现的。空闲任务只做一件事就是++,但这个++的变量是局部变量,外部不能直接访问。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-30 00:43

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

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