# Event - [Basic Concepts](#section122115620816) - [Working Principles](#section94611116593) - [Event Control Block](#section1161415384467) - [Working Principles](#section187761153144617) - [Development Guidelines](#section44744471891) - [Available APIs](#section172373513919) - [How to Develop](#section1118215161013) - [Development Example](#section19986143311020) - [Example Description](#section128221510145718) - [Sample Code](#section71507479577) - [Verification](#section16570171645813) ## Basic Concepts 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 implement the following types of synchronization: - One-to-many synchronization: A task waits for the triggering of multiple events. A task is woken up by one or multiple events. - Many-to-many synchronization: Multiple tasks wait for the triggering of multiple events. The events provided by the OpenHarmony LiteOS-A event module have the following features: - A task triggers or waits for an event by creating an event control block. - Events are independent of each other. The internal implementation is a 32-bit unsigned integer, and each bit indicates an event type. The 25th bit is unavailable. Therefore, a maximum of 31 event types are supported. - Events are used only for synchronization between tasks, but not for data transmission. - Writing the same event type to the event control block for multiple times is equivalent to writing the event type only once before the event control block is cleared. - Multiple tasks can read and write the same event. - The event read/write timeout mechanism is supported. ## Working Principles ### Event Control Block ``` /** * 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; ``` ### Working Principles **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: - **LOS\_WAITMODE\_AND**: Event reading is successful only when all the events corresponding to **eventMask** occur. Otherwise, the task will be blocked, or an error code will be returned. - **LOS\_WAITMODE\_OR**: Event reading is successful when any of the events corresponding to **eventMask** occur. Otherwise, the task will be blocked, or an error code will be returned. - **LOS\_WAITMODE\_CLR**: This mode must be used with **LOS\_WAITMODE\_AND** or **LOS\_WAITMODE\_OR** \(LOS\_WAITMODE\_AND | LOS\_WAITMODE\_CLR or LOS\_WAITMODE\_OR | LOS\_WAITMODE\_CLR\). In this mode, if **LOS\_WAITMODE\_AND** or **LOS\_WAITMODE\_OR** is successful, the corresponding event type bit in the event control block will be automatically cleared. **Clearing event**: 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 ![](figure/event-working-mechanism-21.png "event-working-mechanism-21") ## Development Guidelines ### Available APIs The following table describes APIs available for the OpenHarmony LiteOS-A event module. **Table 1** Event module APIs