In many operating systems, there exists a type of API function interface called: HOOK functions.
For example, HOOK functions can be found in desktop operating systems like Windows, as well as in real-time operating systems such as µC/OS and FreeRTOS.
Let’s discuss HOOK functions in the context of µC/OS, including what they are and their uses.
What are Hook Functions?
Hook functions, also known as HOOK functions, are functions that are called internally by the operating system.
If HOOKs are enabled, the operating system will call the corresponding hook function (the one you wrote) at specific events.
For instance, in an RTOS, when a Task is deleted, the corresponding App_TaskIdleHook function will be called. (This “App_TaskIdleHook” function needs to be defined by you.)
Most online resources about HOOKs use Windows as an example to describe the function and purpose of hook functions, such as Baidu Baike:
Hook functions are part of the Windows message processing mechanism. By setting a “hook”, applications can filter all messages and events at the system level, accessing messages that are normally inaccessible. The essence of a hook is a program that processes system messages, which is attached to the system through system calls.
Why is it called a “Hook”?
There are many interpretations online, for example: it is “hooked” by the operating system. Interested readers can look it up online.
Uses and Implementation of Hook Functions
Real-time operating systems like µC/OS and FreeRTOS all have Hook functions that can be enabled through configuration files.
Let’s discuss the uses and implementation of HOOK functions with µC/OS as an example:
1. Configuring HOOK
Many RTOS have one (or more) configuration files to configure (cut down) the system.
For instance, in µC/OS, there is the os_cfg.h system configuration file. It contains enable switches; enable the necessary features and disable those that are not needed to achieve system trimming.
/* ---------------------- MISCELLANEOUS ----------------------- */#define OS_APP_HOOKS_EN 1 /* Application-defined hooks are called from the uC/OS-II hooks */#define OS_ARG_CHK_EN 0 /* Enable (1) or Disable (0) argument checking */#define OS_CPU_HOOKS_EN 1 /* uC/OS-II hooks are found in the processor port files */#define OS_DEBUG_EN 1 /* Enable(1) debug variables */
#define OS_APP_HOOKS_EN 1#define OS_CPU_HOOKS_EN 1
0: Disable;
1: Enable;
Likewise, FreeRTOS has similar configurations:
We simply enable the HOOK that we need.
2. Using TaskIdleHook
TaskIdleHook, or Task Idle Hook Function, is called when the task is idle.
This hook function exists in many operating systems and is called when all other tasks are suspended.
For example, the CPU utilization we often mention is calculated during idle tasks. Taking µC/OS as an example:
void OS_TaskIdle (void *p_arg){#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0u;#endif p_arg = p_arg; /* Prevent compiler warning for not using 'p_arg' */ for (;;) { OS_ENTER_CRITICAL(); OSIdleCtr++; OS_EXIT_CRITICAL(); OSTaskIdleHook(); /* Call user definable HOOK */ }}
System Idle Hook Function OSTaskIdleHook:
#if OS_CPU_HOOKS_EN > 0uvoid OSTaskIdleHook (void){#if OS_APP_HOOKS_EN > 0u App_TaskIdleHook();#endif}#endif
Application Idle Hook Function App_TaskIdleHook:
If enabled, this function will be executed, which we need to implement ourselves. For example: if the idle count exceeds 50, we print a message:
void App_TaskIdleHook (void){ UserIdleCtr++; // Count idle occurrences if(50 < UserIdleCtr) { UserIdleCtr = 0; printf("SYS_IDLE"); }}
END
→ Follow to stay updated ←
Leave a Comment
Your email address will not be published. Required fields are marked *