MiniGUI 定时器分析

2023-12-11 06:32
文章标签 分析 定时器 minigui

本文主要是介绍MiniGUI 定时器分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MiniGUI 定时器分析 (注:该MiniGUI库版本为1.6.10 非LITE版本)

MiniGUI几个定时器相关的函数如下:

BOOL GUIAPI ResetTimerEx(HWND hWnd, int id, unsigned int speed, TIMERPROC timer_proc);

BOOL GUIAPI SetTimerEx(HWND hWnd, int id, unsigned int speed, TIMERPROC timer_proc);

#define SetTimer(hwnd, id, speed) SetTimerEx(hwnd, id, speed, NULL)

#define ResetTimer(hwnd, id, speed) ResetTimerEx(hwnd, id, speed, (TIMERPROC)0xFFFFFFFF)

hWnd为创建定时器时传入的窗口句柄。

id 为定时器id号

speed 为定时器时间间隔10ms 为单位。

timer_proc 为定时器回调函数

MiniGUI定时器内部运行原理

src/kernel/init.c 文件下InitGUI函数是在MiniGUI程序初始化的时候被调用的。

int GUIAPI InitGUI (int args, const char *agr[])

{

。。。

  SystemThreads()

。。。

}

InitGUI函数中调用SystemThreads函数

SystemThreads函数中创建了DesktopMain线程。

pthread_create (&__mg_desktop, NULL, DesktopMain, &wait);函数

void* DesktopMain (void* data)

{

    MSG Msg;

。。。

    while (GetMessage(&Msg, HWND_DESKTOP)) {

        int iRet = 0;

        iRet = DesktopWinProc (HWND_DESKTOP, Msg.message, Msg.wParam, Msg.lParam);

。。。      

    }

    return NULL;

}

可以看到在这里MiniGUI创建了一个桌面线程用于处理桌面线程消息。

int DesktopWinProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)

{

case MSG_TIMER:      // per 0.01s

        {

            static UINT uCounter = 0;

            DispatchTimerMessage (1);

            if (__mg_timer_counter % 10 != 0)

                break;

            uCounter += 100;

         }

        break;

}

在这里可以看到桌面窗口回调函数对MSG_TIMER消息的处理。调用了DispatchTimerMessage (1);函数。

timerstr该结构体数组里面最大可以放DEF_NR_TIMERS这么多个定时器,轮询每个定时器检查时间是否超时,如超时则设置定时器时间到标志,让PeekMessageEx函数做处理。

void DispatchTimerMessage (unsigned int inter)

{

    int i;

    TIMER_LOCK ();

    for (i=0; i<DEF_NR_TIMERS; i++) {

        if (timerstr[i] && timerstr[i]->msg_queue) {

            timerstr[i]->count += inter;

            if (timerstr[i]->count >= timerstr[i]->speed) {

                if (timerstr[i]->tick_count == 0)

                    timerstr[i]->tick_count = __mg_timer_counter;

                SetMsgQueueTimerFlag (timerstr[i]->msg_queue, i);

                

                timerstr[i]->count -= timerstr[i]->speed;

            }

        }

    }

    TIMER_UNLOCK ();

}

SetMsgQueueTimerFlag 函数中调用了POST_MSGQ (pMsgQueue);

static inline void

SetMsgQueueTimerFlag (PMSGQUEUE pMsgQueue, int slot)

{

    pMsgQueue->TimerMask |= (0x01 << slot);

    POST_MSGQ (pMsgQueue);  //该宏的作用是将窗口消息循环由阻塞状态唤醒。

}

此时创建该定时器的窗口过程中的消息循环PeekMessageEx会被唤醒。

BOOL PeekMessageEx (PMSG pMsg, HWND hWnd, int iMsgFilterMin, int iMsgFilterMax, 

                          BOOL bWait, UINT uRemoveMsg)

{

。。。

      if ((timer = __mg_get_timer (slot))) {

            unsigned int tick_count = timer->tick_count;

            timer->tick_count = 0;

            pMsgQueue->TimerMask &= ~(0x01 << slot);

            if (timer->proc) { //如果该定时器定义回调函数

                BOOL ret_timer_proc;

                UNLOCK_MSGQ (pMsgQueue);

                ret_timer_proc = timer->proc (timer->hWnd,  timer->id, tick_count);

                LOCK_MSGQ (pMsgQueue);

                if (!ret_timer_proc) {

                    __mg_remove_timer (timer, slot);

                }

            }

            else { 

//如果该函数回调函数指针为空则转成消息放入消息队列等着DispatchMessage函数处理

                pMsg->message = MSG_TIMER;

                pMsg->hwnd = timer->hWnd;

                pMsg->wParam = timer->id;

                pMsg->lParam = tick_count;

                SET_PADD (NULL);

                UNLOCK_MSGQ (pMsgQueue);

                return TRUE;

            }

        }

。。。

}

SystemThreads函数中调用__mg_timer_init函数,该函数又启动了 TimerEntry 线程。

int __mg_timer_init (void)

{

    sem_t wait;

    sem_init (&wait, 0, 0);

    pthread_create (&__mg_timer, NULL, TimerEntry, &wait);

    sem_wait (&wait);

    sem_destroy (&wait);

    return 0;

}

TimerEntry 线程调用了_os_timer_loop 时间循环函数

static void* TimerEntry (void* data)

{

    if (!InitTimer ()) {

        fprintf (stderr, "TIMER: Init Timer failure, exit!\n");

        return NULL;

    }

    sem_post ((sem_t*)data);

    _os_timer_loop ();

    return NULL;

}

时间循环函数如下

static inline void _os_timer_loop (void)

{

    while (1) {

        __mg_os_time_delay (10);  //延时10ms

        __mg_timer_action (NULL);      //每10ms调用一次

    }

}

每10ms AlertDesktopTimerEvent 函数被调用

static void __mg_timer_action (void *data)

{

    __mg_timer_counter ++;

    AlertDesktopTimerEvent ();

}

每10ms 由AlertDesktopTimerEvent 给桌面消息循环设置时间到标志,如该循环阻塞,则唤醒该循环。

AlertDesktopTimerEvent (void)

{

    __mg_dsk_msg_queue->TimerMask = 1;

    POST_MSGQ(__mg_dsk_msg_queue);

}


这篇关于MiniGUI 定时器分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/479763

相关文章

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Springboot如何配置Scheduler定时器

《Springboot如何配置Scheduler定时器》:本文主要介绍Springboot如何配置Scheduler定时器问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Springboot配置Scheduler定时器1.在启动类上添加 @EnableSchedulin

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景