同步操作将从 OpenHarmony/docs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
An event is a mechanism for communication between tasks. It can be used to synchronize tasks.
In multi-task environment, synchronization is required between tasks. Events can be used for synchronization in the following cases:
The event mechanism provided by the OpenHarmony LiteOS-A event module has the following features:
/**
* Event control block data structure
*/
typedef struct tagEvent {
UINT32 uwEventID; /* Event set, which is a collection of events processed (written and cleared). */
LOS_DL_LIST stEventList; /* List of tasks waiting for specific events */
} EVENT_CB_S, *PEVENT_CB_S;
Initializing an event: An event control block is created to maintain a collection of processed events and a linked list of tasks waiting for specific events.
Writing an event: When a specified event is written to the event control block, the event control block updates the event set, traverses the task linked list, and determines whether to wake up related task based on the task conditions.
Reading an event: If the read event already exists, it is returned synchronously. In other cases, the return time is determined based on the timeout period and event triggering status. If the wait event condition is met before the timeout period expires, the blocked task will be directly woken up. Otherwise, the blocked task will be woken up only after the timeout period has expired.
The input parameters eventMask and mode determine whether the condition for reading an event is met. eventMask indicates the mask of the event. mode indicates the handling mode, which can be any of the following:
Clearing events: Clear the event set of the event control block based on the specified mask. If the mask is 0, the event set will be cleared. If the mask is 0xffff, no event will be cleared, and the event set remains unchanged.
Destroying an event: Destroy the specified event control block.
Figure 1 Event working mechanism for small systems
The following table describes APIs available for the OpenHarmony LiteOS-A event module.
Table 1 Event module APIs
The typical event development process is as follows:
NOTE:
- When an event is read or written, the 25th bit of the event is reserved and cannot be set.
- Repeated writes of the same event are treated as one write.
In this example, run the Example_TaskEntry task to create the Example_Event task, run the Example_Event task to read an event to trigger task switching, and run the Example_TaskEntry task to write an event. You can understand the task switching during event operations based on the sequence in which logs are recorded.
The sample code is as follows:
#include "los_event.h"
#include "los_task.h"
#include "securec.h"
/* Task ID*/
UINT32 g_testTaskId;
/* Event control structure*/
EVENT_CB_S g_exampleEvent;
/* Type of the wait event*/
#define EVENT_WAIT 0x00000001
/* Example task entry function*/
VOID Example_Event(VOID)
{
UINT32 event;
/* Set a timeout period for event reading to 100 ticks. If the specified event is not read within 100 ticks, the read operation times out and the task is woken up.*/
printf("Example_Event wait event 0x%x \n", EVENT_WAIT);
event = LOS_EventRead(&g_exampleEvent, EVENT_WAIT, LOS_WAITMODE_AND, 100);
if (event == EVENT_WAIT) {
printf("Example_Event,read event :0x%x\n", event);
} else {
printf("Example_Event,read event timeout\n");
}
}
UINT32 Example_EventEntry(VOID)
{
UINT32 ret;
TSK_INIT_PARAM_S task1;
/* Initialize the event.*/
ret = LOS_EventInit(&g_exampleEvent);
if (ret != LOS_OK) {
printf("init event failed .\n");
return -1;
}
/* Create a task.*/
(VOID)memset_s(&task1, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
task1.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_Event;
task1.pcName = "EventTsk1";
task1.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
task1.usTaskPrio = 5;
ret = LOS_TaskCreate(&g_testTaskId, &task1);
if (ret != LOS_OK) {
printf("task create failed.\n");
return LOS_NOK;
}
/* Write the task wait event (g_testTaskId). */
printf("Example_TaskEntry write event.\n");
ret = LOS_EventWrite(&g_exampleEvent, EVENT_WAIT);
if (ret != LOS_OK) {
printf("event write failed.\n");
return LOS_NOK;
}
/* Clear the flag.*/
printf("EventMask:%d\n", g_exampleEvent.uwEventID);
LOS_EventClear(&g_exampleEvent, ~g_exampleEvent.uwEventID);
printf("EventMask:%d\n", g_exampleEvent.uwEventID);
/* Delete the task.*/
ret = LOS_TaskDelete(g_testTaskId);
if (ret != LOS_OK) {
printf("task delete failed.\n");
return LOS_NOK;
}
return LOS_OK;
}
The development is successful if the return result is as follows:
Example_Event wait event 0x1
Example_TaskEntry write event.
Example_Event,read event :0x1
EventMask:1
EventMask:0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。