AliOS things Asynchronous Event Framework Yloop

Source: Internet
Author: User

This article is reproduced, the original address: 78852247

Yloop Overview

Yloop is an asynchronous event framework for alios things. Yloop for reference, LIBUV and embedded industry common event loop, considering the use of complexity, performance, and footprint, the implementation of a suitable for the MCU of the incident scheduling mechanism.

Yloop context

Each Yloop instance (aos_loop_t) is bound to a specific task context, AliOS things the context of the program portal Application_Start is bound to the system's primary Yloop instance, which is also known as the primary task. Tasks other than the main task can also create their own yloop instances.

Yloop Scheduling

Yloop implements the unified scheduling management for Io,timer,callback,event:

    • IO: The most common is the socket, or it can be alios things VFS managed Device
    • Timer: A common timer
    • Callback: a specific execution function
    • Event: Includes system events, user-defined events

When Aos_loop_run is called, the current task waits for each of these types of events to occur.

Yloop Implementation principle

Yloop uses the Select interface of the protocol stack to realize the dispatch of IO and timer. AliOS things comes with a protocol stack that exposes a special EVENTFD interface, Yloop uses this interface to correlate VFS ' device files and EVENTFD, realizing a unified scheduling of events across the entire system.

The use of Yloop starts with Hello World

Hello World Example
There is a piece of code in it:

Two things were done in Application_Start:

    • Call Aos_post_delayed_action to create a 1 second timer (Yloop inside only oneshot timer)
    • Call Aos_loop_run into event loop

After 1 seconds, the timer triggers, app_delayed_action is called, and app_delayed_action inside

    • Call log Print
    • Create a 5-second timer again, and implement regular execution app_delayed_action

Note here that the program does not need Aos_loop_init () to create the Yloop instance, because the primary Yloop instance is automatically created by default.

and socket combination

Take the framework/connectivity/mqtt/mqtt_client.c of Mqtt as an example:

After establishing a socket connection with the server, call AOS_POLL_READ_FD () to add the MQTT socket to the Yloop listener. When there is data on the server, the CB_RECV callback is called and the data is processed. In this way, MQTT does not need a separate task to handle the socket, thus saving memory usage. At the same time, because all processing is performed on the main task, complex mutex operations are not required.

Handling of system events

AliOS things defines a series of system events that can be processed by Aos_register_event_filter () registering event listener functions, such as WiFi events.

#define EV_USER 0x1000
The event ID after Ev_user can be used for user-defined events.

Yloop Callback

The Yloop callback is used for cross-task processing. Take the following pseudo-code as an example:

Suppose UART_RECV_DATA_CB is the callback at which the IO device receives data, and the actual processing do_uart_io_in_main_task is placed in the main task context by Aos_schedule_call after receiving the data. In this way, the logical processing of data do_uart_io_in_main_task does not need to consider concurrency, but to do complex mutex operations.

Precautions

The Yloop API (INCLUDE/AOS/YLOOP.H) must be executed in the context of the task to which the Yloop instance is bound, in addition to the following APIs:

    • Aos_schedule_call
    • Aos_loop_schedule_call
    • Aos_loop_schedule_work
    • Aos_cancel_work
    • Aos_post_event
Summary

Yloop as Alios things's event framework, and VFS, the protocol stack depth combination, in achieving better footprint, can better adapt to the footprint requires only a main task of the system, can also be applied to the processing of high concurrency requirements of the system.

AliOS things Asynchronous Event Framework Yloop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.