Synchronous barrier message processing method and electronic device
By detecting and removing synchronization barrier messages that are not processed in time, the problem of message queue blocking in the Android operating system is solved, improving user experience and saving resources.
Patent Information
- Application Number
- CN202310841504.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-10
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2043-07-10
AI Technical Summary
The message queue in the Android operating system is not processed in time due to the negligence of programmers, resulting in the synchronization barrier message being unable to be processed in time, resulting in the message queue being blocked and affecting the user experience.
By detecting whether the first message in the message queue is a synchronization barrier message and not being processed within the preset time it stays, the synchronization barrier message is removed from the message queue to avoid blocking and provide a call stack for synchronization barrier messages. Information for developers to locate issues.
It effectively avoids blocking of message queues, prevents the occurrence of freezing screens, improves user experience, and saves log resources.
Smart Images

Figure CN117724861B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software, and in particular to a synchronization barrier message processing method and electronic device. Background Art
[0002] The messages in the message queue of the Android operating system include synchronous messages, asynchronous messages, and synchronous barrier messages; among them, the synchronous barrier message at the front of the message queue will intercept the processing of the subsequent synchronous messages until the synchronous barrier message is processed. The subsequent synchronous messages can be processed. However, due to the negligence of programmers, synchronous barrier messages are often submitted to the message queue without being paired for processing, resulting in the subsequent synchronous messages not being processed in time and the message queue being blocked. For example, view-related operations are generally triggered by synchronous messages. If the synchronous messages cannot be processed in time, the screen will freeze and freeze, affecting the user experience. Summary of the invention
[0003] The embodiments of the present application provide a synchronous barrier message processing method and an electronic device for avoiding blocking of a message queue.
[0004] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[0005] In a first aspect, a synchronization barrier message processing method is provided, comprising: determining whether the first message in a message queue is a synchronization barrier message; if the first message is a synchronization barrier message, and the duration for which the synchronization barrier message stays in the message queue is greater than a first preset duration, removing the synchronization barrier message from the message queue.
[0006] The synchronization barrier message processing method provided in the embodiment of the present application, if the first message in the message queue is a synchronization barrier message, and the synchronization barrier message has not been processed within the first preset time since the synchronization barrier message was added to the message queue, then the synchronization barrier message is removed from the message queue, so that the synchronization message can continue to be processed, avoiding blocking of the message queue.
[0007] In a possible implementation, the synchronization barrier message includes call stack information of the synchronization barrier message, and the call stack information is used to indicate the stack information of each function on the synchronization barrier message generation path; the method also includes: sending the call stack information of the synchronization barrier message to the server to output the call stack information of the synchronization barrier message.
[0008] The server can output the call stack information of the synchronization barrier message to the log file, which is convenient for developers to determine which function in which application caused the synchronization barrier message not to be processed in time according to the generation path of the synchronization barrier message, so as to solve the problem. For the synchronization barrier message that stays in the message queue for no longer than the first preset time, since the above problem does not occur, it is not necessary to send the call stack information of the synchronization barrier message to the server, so as to save log resources.
[0009] In one possible implementation, determining whether the first message in a message queue is a synchronization barrier message includes: a handle subthread of a target thread periodically submitting a preset synchronization message to the message queue of the target thread; if the preset synchronization message stays in the message queue for a period longer than a second preset period, determining whether the first message in the message queue is a synchronization barrier message, and the target thread is a main thread or a user interface (UI) thread.
[0010] This implementation is relatively simple in function and consumes relatively little resources. When the handle sub-thread is created by the UI thread, only the synchronization barrier message from the UI thread can be detected. When the handle sub-thread is created by the main thread, the synchronization barrier messages from all threads can be detected.
[0011] In a possible implementation, the method is executed when traversing each message in the message queue, or when a message is enqueued.
[0012] That is, this method can be implemented in the next function of the message queue (MessageQueue) class for traversing each message in the message queue, and can also be implemented in the enqueueMessage function of the MessageQueue class for enqueuing a message.
[0013] In a possible implementation, it also includes: holding a synchronization lock; if the traversal preparation variable is false, and the view tree that needs to be refreshed is ready to be traversed, submitting a synchronization barrier message to the message queue and setting the traversal preparation variable to true, the traversal preparation variable is used to indicate whether the view tree that needs to be refreshed is ready to be traversed; if the traversal preparation variable is true, and the traversal of the view tree that needs to be refreshed is canceled or the traversal of the view tree that needs to be refreshed is executed, removing the synchronization barrier message from the message queue and setting the traversal preparation variable to false.
[0014] This implementation can fundamentally avoid the problem that the synchronization barrier message is not removed in time due to the child thread refreshing the view, and there is no need to wait for the first preset time to remove the synchronization barrier message, thereby avoiding the phenomenon of stuck and frozen screen.
[0015] In a second aspect, an electronic device is provided, comprising a processor and a memory, wherein instructions are stored in the memory, and when the processor executes the instructions, the method described in the first aspect and any embodiment thereof is executed.
[0016] According to a third aspect, a computer-readable storage medium is provided, comprising instructions, which, when executed on an electronic device, enable the electronic device to execute the method according to the first aspect and any embodiment thereof.
[0017] In a fourth aspect, a computer program product comprising instructions is provided. When the instructions are executed on the electronic device, the electronic device executes the method as described in the first aspect and any embodiment thereof.
[0018] In a fifth aspect, a chip system is provided, the chip system including a processor for supporting an electronic device to implement the functions involved in the first aspect. In one possible design, the device also includes an interface circuit, which can be used to receive signals from other devices (such as a memory) or send signals to other devices (such as a communication interface). The chip system may include a chip and may also include other discrete devices.
[0019] The technical effects of the second to fifth aspects refer to the technical effects of the first aspect and any of its embodiments, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 A schematic diagram of adding a synchronous message, an asynchronous message and a synchronous barrier message to a message queue provided in an embodiment of the present application;
[0021] Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0022] Figure 3 A schematic diagram of a software architecture for operation of an electronic device provided in an embodiment of the present application;
[0023] Figure 4 A flowchart of a first synchronization barrier message processing method provided in an embodiment of the present application;
[0024] Figure 5 A flowchart of a second synchronization barrier message processing method provided in an embodiment of the present application;
[0025] Figure 6 A flowchart of a third synchronization barrier message processing method provided in an embodiment of the present application;
[0026] Figure 7 A flowchart of a fourth synchronization barrier message processing method provided in an embodiment of the present application;
[0027] Figure 8 A flowchart of a fifth synchronization barrier message processing method provided in an embodiment of the present application;
[0028] Fig. 9 A schematic diagram of the structure of a chip system provided in an embodiment of the present application. DETAILED DESCRIPTION
[0029] First, some concepts involved in this application are described.
[0030] The terms "first", "second", etc. involved in the embodiments of the present application are only used to distinguish features of the same type and cannot be understood as indicating relative importance, quantity, order, etc.
[0031] The terms "exemplary" or "for example" and the like in the embodiments of the present application are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of the terms "exemplary" or "for example" is intended to present the related concepts in a specific way.
[0032] The terms "coupling" and "connection" involved in the embodiments of the present application should be understood in a broad sense. For example, they may refer to a direct physical connection, or an indirect connection achieved through electronic devices, such as a connection achieved through resistors, inductors, capacitors or other electronic devices.
[0033] Synchronous lock: In order to ensure that only one thread executes code at the same time and not allow multiple threads to execute the same code at the same time, you can use synchronized(this) to declare a synchronized code block, such as synchronized(this){code}. This ensures atomic operation of the code when it is executed.
[0034] The message queue, also known as the event loop, is used to maintain messages generated by a thread of an application (such as the main thread). Figure 1As shown, the application is idle most of the time. When the handler thread detects that an event related to the thread occurs, such as a user clicking a button, refreshing the interface, etc., a message will be submitted (posted) to the message queue. The looper calls the next function of the message queue (MessageQueue) class to traverse each message in the message queue, and continuously queries the message queue to see if there are any pending messages. If there are, the message is processed and removed from the message queue. If not, it is blocked in the next function, waiting for new messages to be queued. The message queue has no pending messages, including: there are no messages in the message queue, the first message in the message queue is a synchronous barrier message, etc. The message queue has pending messages, including: the first message in the message queue is a synchronous message or an asynchronous message.
[0035] like Figure 1 As shown, the messages in the message queue include synchronous messages, asynchronous messages, and synchronous barrier messages. For synchronous messages, they are inserted into the message queue according to the message submission time, and the new messages are queued according to the principle that the first submitted messages are in the front position. Then, when being processed, the messages in the front position in the message queue will be processed first. For example, suppose that the message queue includes messages A and C in order: if the time of message A is earlier than the time of new message B, and the time of new message B is earlier than the time of message C, then the new message B is inserted between messages A and C. If the time of new message B is earlier than the time of message A, then the new message B is inserted before message A, and if the time of message C is earlier than the time of new message B, then the new message B is inserted after message C. If a message submitted later requires priority processing, the message is an asynchronous message. The synchronous barrier message is used in conjunction with the asynchronous message. When an asynchronous message needs to be submitted to the message queue, the handler thread will first submit the synchronous barrier message through the postSyncBarrier function of the MessageQueue class, which is used to submit the synchronous barrier message, and then submit the asynchronous message. After the asynchronous message is processed, the removeSyncBarrier function of the MessageQueue class, which is used to remove the synchronous barrier message, is used to remove the synchronous barrier message.
[0036] When the looper finds that the first message in the message queue is a synchronous barrier message, it will ignore the subsequent synchronous messages until an asynchronous message is found, and then prioritize the asynchronous message until the synchronous barrier message is removed from the message queue, and then process the synchronous message in the order of enqueue. If the asynchronous message is never found, it will be blocked to wait for the asynchronous message. In the Android operating system, the ViewRootImpl class is responsible for view rendering, which is implemented through asynchronous messages, so that view rendering is processed first to avoid jamming. Correspondingly, it involves submitting synchronous barrier messages to the message queue and removing synchronous barrier messages from the message queue.
[0037] Therefore, when the asynchronous message is not submitted in time, or a large number of synchronous barrier messages are submitted, or the control logic is wrong and the synchronous barrier message is not removed correctly, a large number of synchronous messages in the message queue cannot be processed, and the application will experience random application not responding (ANR), freezing and other phenomena.
[0038] To this end, the embodiment of the present application provides a barrier message processing method and electronic device. If the first message in the message queue is a synchronous barrier message, and the synchronous barrier message has not been processed within the first preset time since it was added to the message queue, the synchronous barrier message is removed from the message queue so that the synchronous message can continue to be processed, thereby avoiding blocking of the message queue. This method can be implemented in the next function of the message queue (MessageQueue) class for traversing each message in the message queue, or in the enqueueMessage function of the MessageQueue class for enqueuing a message, or in the handle subthread of the main thread or UI thread.
[0039] The embodiment of the present application provides an electronic device, which can be mobile or fixed. The electronic device can be deployed on land (for example, indoors or outdoors, handheld or vehicle-mounted, etc.), on the water (for example, ships, etc.), or in the air (for example, airplanes, balloons, and satellites, etc.). The electronic device can be called user equipment (UE), access terminal, terminal unit, subscriber unit, terminal station, mobile station (MS), mobile station, terminal agent or terminal device, etc. For example, the electronic device can be a mobile phone, a tablet computer, a laptop computer, a smart bracelet, a smart watch, a headset, a smart speaker, a virtual reality (VR) device, an augmented reality (AR) device, a terminal in industrial control, a terminal in self-driving, a terminal in remote medical, a terminal in smart grid, a terminal in transportation safety, a terminal in smart city, a terminal in smart home, etc. The embodiment of the present application does not limit the specific type and structure of the electronic device. A possible structure of the electronic device is described below.
[0040] Taking a mobile phone as an example, Figure 2 1 shows a possible structure of the electronic device 101. The electronic device 101 may include a processor 210, an external memory interface 220, an internal memory 221, a universal serial bus (USB) interface 230, a power management module 240, a battery 241, a wireless charging coil 242, an antenna 1, an antenna 2, a mobile communication module 250, a wireless communication module 260, an audio module 270, a speaker 270A, a receiver 270B, a microphone 270C, an earphone interface 270D, a sensor module 280, a button 290, a motor 291, an indicator 292, a camera 293, a display screen 294, and a subscriber identification module (SIM) card interface 295.
[0041] Among them, the sensor module 280 may include a pressure sensor, a gyroscope sensor, an air pressure sensor, a magnetic sensor, an acceleration sensor, a distance sensor, a proximity light sensor, a fingerprint sensor, a temperature sensor, a touch sensor, an ambient light sensor, a bone conduction sensor, and the like.
[0042] It is understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 101. In other embodiments of the present application, the electronic device 101 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0043] The processor 210 may include one or more processing units, for example, the processor 210 may be a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), a system on chip (SoC), a central processing unit (CPU), an application processor (AP), a network processor (NP), a digital signal processor (DSP), a microcontroller unit (MCU), a programmable logic device (PLD), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a video codec, a baseband processor, and a neural network processor (NPU). Among them, different processing units may be independent devices or integrated into one or more processors. For example, the processor 210 may be an application processor AP. Alternatively, the above-mentioned processor 210 may be integrated in a system on chip (SoC). Alternatively, the above-mentioned processor 210 may be integrated in an integrated circuit (IC) chip. The processor 210 may include an analog front end (AFE) and a micro-controller unit (MCU) in an IC chip.
[0044] The controller may be the nerve center and command center of the electronic device 101. The controller may generate an operation control signal according to the instruction operation code and the timing signal to complete the control of fetching and executing instructions.
[0045] The processor 210 may also be provided with a memory for storing computer instructions and data. In some embodiments, the memory in the processor 210 is a cache memory. The memory may store computer instructions or data that the processor 210 has just used or circulated. If the processor 210 needs to use the computer instructions or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 210, and thus improves the efficiency of the system.
[0046] In some embodiments, the processor 210 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a USB interface, etc.
[0047] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present application is only a schematic illustration and does not constitute a structural limitation on the electronic device 101. In other embodiments of the present application, the electronic device 101 may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.
[0048] The wireless communication function of the electronic device 101 can be implemented through the antenna 1, the antenna 2, the mobile communication module 250, the wireless communication module 260, the modem processor, the baseband processor, and the like.
[0049] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 101 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.
[0050] The mobile communication module 250 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to the electronic device 101. The wireless communication module 260 can provide solutions for wireless communications including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication technology (NFC), infrared technology (IR), etc., applied to the electronic device 101. In some embodiments, the antenna 1 of the electronic device 101 is coupled to the mobile communication module 250, and the antenna 2 is coupled to the wireless communication module 260, so that the electronic device 101 can communicate with the network and other devices through wireless communication technology.
[0051] The external memory interface 220 can be used to connect an external memory card, such as a micro SanDisk (Micro SD) card, to expand the storage capacity of the electronic device 101. The external memory card communicates with the processor 210 through the external memory interface 220 to implement a data storage function, such as storing music, video and other files in the external memory card.
[0052] The internal memory 221 may be used to store computer executable program codes, which include computer instructions. The processor 210 executes various functional applications and data processing of the electronic device 101 by running the computer instructions stored in the internal memory 221. In addition, the internal memory 221 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0053] The memory involved in the embodiments of the present application may be a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memories. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), and direct RAM (DR RAM). It should be noted that the memory of the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0054] The electronic device 101 can implement audio functions such as music playing and recording through the audio module 270, the speaker 270A, the receiver 270B, the microphone 270C, the headphone interface 270D and the application processor.
[0055] The audio module 270 is used to convert digital audio information into analog audio signal output, and is also used to convert analog audio input into digital audio signal. In some embodiments, the audio module 270 can be arranged in the processor 210, or some functional modules of the audio module 270 can be arranged in the processor 210. The speaker 270A, also known as the "speaker", is used to convert the audio electrical signal into a sound signal. The receiver 270B, also known as the "earpiece", is used to convert the audio electrical signal into a sound signal. The microphone 270C, also known as the "microphone", is used to convert the sound signal into an electrical signal. The electronic device 101 can be provided with at least one microphone 270C. The headphone interface 270D is used to connect a wired headset. The headphone interface 270D can be a USB interface 230, or it can be a 3.5mm open mobile terminal platform (OMTP) standard interface, or a cellular telecommunications industry association of the USA (CTIA) standard interface.
[0056] The button 290 includes a power button, a volume button, etc. The button 290 may be a mechanical button. It may also be a touch button. The electronic device 101 may receive a button input and generate a key signal input related to the user settings and function control of the electronic device 101. The motor 291 may generate a vibration prompt. The motor 291 may be used for an incoming call vibration prompt or for touch vibration feedback. The indicator 292 may be an indicator light, which may be used to indicate the charging status, power changes, messages, missed calls, notifications, etc. The SIM card interface 295 is used to connect a SIM card. The SIM card may be inserted into or removed from the SIM card interface 295 to achieve contact and separation with the electronic device 101. The electronic device 101 may support 1 or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 295 may support a Nano SIM card, a Micro SIM card, a SIM card, etc. In some embodiments, the electronic device 101 uses an embedded SIM (eSIM) card. The eSIM card can be embedded in the electronic device 101 and cannot be separated from the electronic device 101.
[0057] The electronic device 101 can realize the shooting function through the ISP, the camera 293, the video codec, the GPU, the display screen 294, and the application processor. The ISP is used to process the data fed back by the camera 293. In some embodiments, the ISP can be set in the camera 293. The camera 293 is used to capture a static image or a video. In some embodiments, the electronic device 101 may include 1 or N cameras 293, where N is a positive integer greater than 1.
[0058] The electronic device 101 can realize the display function through a GPU, a display screen 294, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 294 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 210 may include one or more GPUs, which execute computer instructions to generate or change display information.
[0059] The display screen 294 is used to display images, videos, etc. The display screen 294 includes a display panel. In some embodiments, the electronic device 101 may include one or more display screens 294. In other embodiments, the touch screen in the display screen 294 may be a folding screen.
[0060] The battery 241 may include at least two batteries, and the at least two batteries may be connected in series, in parallel, or the like to supply power to the load.
[0061] The power management module 240 is used to receive charging input from a charger. The charger may be a wireless charger, such as a wireless charging base, other electronic devices 101 with reverse wireless charging function, etc. The power management module 240 may receive wireless charging input via a wireless charging coil 242 of the electronic device. The charger may also be a wired charger, for example, the power management module 240 may receive charging input from a wired charger via the USB interface 230.
[0062] The processor 210 executes the charging control method provided in the embodiment of the present application by executing the program and computer instructions stored in the internal memory 221. The program run by the processor 210 can be based on an operating system, such as Android. Apple (iOS) Windows wait.
[0063] like Figure 3 As shown, the program running on the processor 210 is based on Android For example, the programs run by the processor 210 are layered according to their functions, and may include an application layer, a framework layer, an Android runtime (Android runtime) and a system library, a hardware abstraction layer, and a kernel layer.
[0064] The application layer can include a series of application packages. For example, an application package can include applications such as camera, gallery, etc.
[0065] The framework layer provides application programming interfaces (APIs) and programming frameworks for applications in the application layer. The framework layer includes some predefined functions, such as window managers, view systems, resource managers, etc. Among them, the window manager is used to manage window programs. The window manager can obtain the size of the display screen, determine whether there is a status bar, lock the screen, capture the screen, etc. The view system includes visual controls, such as controls for displaying text, controls for displaying pictures, etc. The view system can be used to build applications. The display interface can be composed of one or more views. For example, a display interface including a text notification icon can include a view for displaying text and a view for displaying pictures.
[0066] The Android runtime includes a core library and a virtual machine. The Android runtime is responsible for scheduling and management of the Android system. The core library consists of two parts: one part is the function that the Java language needs to call, and the other part is the Android core library. The application layer and the framework layer run in the virtual machine. The virtual machine is used to perform object life cycle management, stack management, thread management, security and exception management, and garbage collection. The system library can include multiple functional modules. For example, the surface manager, media libraries, 3D graphics processing library, 2D graphics engine, etc. Among them, the surface manager is used to manage the display subsystem and provide 2D and 3D layer fusion for multiple applications. The media library supports a variety of commonly used audio and video format playback and recording, as well as static image files. The media library can support a variety of audio and video encoding formats, such as moving pictures experts group 4 (MPEG4), joint photographic experts group (JPG), etc. The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, synthesis and layer processing. The 2D graphics engine is a drawing engine for 2D drawing.
[0067] The hardware abstraction layer includes a drawing rendering service (SurfaceFlinger), which periodically outputs a drawing signal - a vertical synchronization (Vsync) signal, thereby periodically refreshing the view tree in the UI (a tree data structure formed by multiple views).
[0068] The kernel layer includes the operating system (OS) kernel and the hardware drivers used to drive hardware resources. The operating system kernel is the core of the operating system of the electronic device. It is the first layer of software expansion based on hardware. It provides the most basic functions of the operating system and is the basis for the operation of the operating system. It is responsible for managing the system's processes, memory, device drivers, files, and network systems, and determines the performance and stability of the system. The kernel also includes critical data structures, such as the message queues and synchronization barrier message processing methods involved in this application, which are implemented in the kernel. Hardware drivers include display drivers, audio drivers, sensor drivers, etc.
[0069] like Figure 4 As shown, the synchronization barrier message processing method provided in the embodiment of the present application includes:
[0070] S101. Determine whether the first message in the message queue is a synchronization barrier message.
[0071] The structure of the Message class includes at least the following information: target pointer, message identifier, timestamp, and next pointer. The target pointer of the message is used to point to the handler thread that submits the message. When the message is a synchronous barrier message, the target pointer of the message is null. When the message is a synchronous message or an asynchronous message, the target pointer of the message is not null. The message identifier is used to uniquely identify the message. The timestamp is used to indicate the submission time of the message. The next pointer defaults to null and is used to point to the next message in the message queue (described in detail later).
[0072] Optionally, you can overload the postSyncBarrier function and add a parameter, the call stack information, to the structure of the Message class when initializing the synchronization barrier message. The call stack information is used to indicate the stack information of each function on the path where the synchronization barrier message is generated. For example, if function 1 calls function 2 and function 2 generates a synchronization barrier message, the call stack information includes the stack information of function 1 and the stack information of function 2.
[0073] When the synchronization barrier message is not processed in time, the developer can learn the generation path of the synchronization barrier message through the call stack information of the synchronization barrier message, so as to find the source of the problem, that is, determine which function in which application caused the problem, and help the developer solve the problem. The call stack information of the synchronization barrier message can be sent to the server, and the call stack information of the synchronization barrier message can be sent to the server through a child thread to avoid blocking the main thread. The server provides a cross-platform interface for calling the underlying implementation for the upper-level application. The server can output the call stack information of the synchronization barrier message to the log file, which is convenient for the developer to determine which function in which application caused the synchronization barrier message not to be processed in time according to the generation path of the synchronization barrier message, so as to solve the problem. For the synchronization barrier message that stays in the message queue for no more than the first preset time, since the above-mentioned problem does not occur, it is not necessary to send the call stack information of the synchronization barrier message to the server to save log resources.
[0074] The messages in the message queue are arranged in a linked list. The pointer of the message queue (that is, the pointer of the linked list) points to the first message in the message queue. The next pointer of each message points to the next message. The next pointer of the last message in the queue is null. Starting from the pointer of the message queue, you can traverse each message in the message queue by searching for the next pointer of the message in turn.
[0075] When a new message is queued, the new message is inserted into the message queue according to the message submission time. For example, suppose the message queue includes message A and message C in order, and the linked list formed by these two messages is A->C. If the time of message A is earlier than the time of new message B, and the time of new message B is earlier than the time of message C, then the new message B is inserted between message A and message C. In other words, point the next pointer of message A to message B, and point the next pointer of message B to message C. If the time of new message B is earlier than the time of message A, insert the new message B before message A. In other words, point the pointer of the message queue to message B, and point the next pointer of message B to message A. If the time of message C is earlier than the time of new message B, insert the new message B after message C. In other words, point the next pointer of message C to message B.
[0076] When a message is dequeued, it is usually dequeued from the head of the queue. By pointing the pointer of the message queue to the message pointed to by the next pointer of the first message, the first message can be removed from the queue. For example, suppose the message queue includes message A and message B in order, and the linked list formed by these two messages is A->B. When dequeuing, just point the pointer of the message queue to message B.
[0077] Since the target pointer of a synchronous barrier message is null, and the target pointer of a synchronous message or an asynchronous message is not null, by judging whether the target pointer of the message is null, it is possible to judge whether the message is a synchronous barrier message.
[0078] S102: If the first message in the message queue is a synchronization barrier message, and the duration for which the synchronization barrier message stays in the message queue is greater than a first preset duration, remove the synchronization barrier message from the message queue.
[0079] Since the timestamp included in the message structure can indicate the time when the synchronization barrier message is submitted, the length of time that the synchronization barrier message stays in the message queue can be obtained by subtracting the time when the synchronization barrier message is submitted from the current time.
[0080] If the duration of the synchronization barrier message staying in the message queue is greater than a first preset duration (e.g., 3 seconds, 5 seconds, etc.), that is, the synchronization barrier message is not processed within the first preset duration, the synchronization barrier message is removed from the message queue (or the synchronization barrier message is dequeued). If the duration of the synchronization barrier message staying in the message queue is less than or equal to the first preset duration, step S101 is re-executed.
[0081] Specifically, if the first message in the message queue is a synchronization barrier message, the synchronization barrier message can be removed by pointing the pointer of the message queue to the message pointed to by the next pointer of the synchronization barrier message.
[0082] The synchronization barrier message processing method provided in the embodiment of the present application, if the first message in the message queue is a synchronization barrier message, and the synchronization barrier message has not been processed within the first preset time length since it was added to the message queue, then the synchronization barrier message is removed from the message queue, so that the synchronization message can continue to be processed, avoiding blocking of the message queue.
[0083] The above synchronization barrier message processing method is explained below in conjunction with specific functions.
[0084] In a possible implementation, Figure 5 As shown, the following functions can be implemented in the next function of the MessageQueue class called by the looper: determine whether the first message in the message queue is a synchronization barrier message; if the first message is a synchronization barrier message, and if the synchronization barrier message stays in the message queue for a period longer than a first preset period, remove the synchronization barrier message from the message queue. The specific method is as follows:
[0085] S201. After entering the next function, first hold the synchronization lock to prevent multiple threads from executing the same code at the same time and causing conflicts.
[0086] Regarding the synchronization lock, please refer to the previous description and will not be repeated here.
[0087] S202: Determine whether the first message in the message queue is a synchronization barrier message.
[0088] Specifically, whether the message is a synchronization barrier message is determined according to whether the target pointer of the first message is null. If the target pointer of the message is not null, it is determined that the message is not a synchronization barrier message. If the target pointer of the message is null, it can be determined that the message is a synchronization barrier message, and step S203 is executed.
[0089] S203, obtain the duration of the synchronization barrier message staying in the message queue, and determine whether the duration of the synchronization barrier message staying in the message queue is greater than the first preset duration. If the duration of the synchronization barrier message staying in the message queue is greater than the first preset duration, execute steps S204-S206, otherwise execute step S202.
[0090] As mentioned above, since the timestamp included in the message structure can indicate the time when the synchronization barrier message is submitted, the duration of the synchronization barrier message being added to the message queue can be obtained by subtracting the time when the synchronization barrier message is submitted from the current time.
[0091] S204: If the duration for which the synchronization barrier message stays in the message queue is greater than a first preset duration, remove the synchronization barrier message from the message queue.
[0092] Regarding how to remove the synchronization barrier message from the message queue, please refer to the description in step S102 and will not be repeated here.
[0093] S205. Submit the call stack information of the synchronization barrier message to the server.
[0094] The server can output the call stack information of the synchronization barrier message to a log file, so that developers can determine which function in which application caused the synchronization barrier message not to be processed in time according to the generation path of the synchronization barrier message, so as to solve the problem.
[0095] S206: Return the pending message to the looper, or block the looper to wait for a new message to be queued.
[0096] If there are pending messages in the message queue (except synchronization barrier messages), the pending messages can be returned to the looper. If no pending messages are found, the nativePollOnce function can be called to block in the next function and wait for new messages to be queued.
[0097] This implementation can handle exceptions for synchronization barrier messages from any source and can cover the entire life cycle of an application program, thus having a wide range of applications.
[0098] In another possible implementation, Figure 6 As shown, the following functions can be implemented in the enqueueMessage function of the MessageQueue class called by the handler thread for enqueuing a message: determine whether the first message in the message queue is a synchronization barrier message, and if the synchronization barrier message stays in the message queue for a time period greater than a first preset time period, remove the synchronization barrier message from the message queue. The specific method is as follows:
[0099] S301. After entering the enqueueMessage function, first hold the synchronization lock to prevent multiple threads from operating the message queue at the same time and causing conflicts.
[0100] Regarding the synchronization lock, please refer to the previous description and will not be repeated here.
[0101] Steps S302 to S305 refer to steps S202 to S205 and are not described in detail here.
[0102] S306: Determine whether the new message is a vertical synchronization (Vsync) message. If the new message is a vertical synchronization (Vsync) message, insert the new message into the head of the message queue, otherwise execute step S307.
[0103] The vertical synchronization (Vsync) message is the message corresponding to the drawing signal periodically output by the drawing rendering service (SurfaceFlinger) - the vertical synchronization (Vsync) signal. Since the vertical synchronization (Vsync) signal is used to periodically refresh the display screen, it has the highest priority. The vertical synchronization (Vsync) message is inserted into the head of the message queue to ensure that it is processed first.
[0104] S307. If the message queue is empty, or the submission time of the new message is 0 (ie, the highest priority), or the submission time of the new message is less than the submission time of the first message in the message queue, the new message is inserted into the head of the message queue, otherwise execute step S308.
[0105] S308: If the processing of the message queue is blocked, and the first message is a synchronous barrier message, and the new message is an asynchronous message, the wake-up flag is set to valid.
[0106] The wake-up flag is used to indicate whether to wake up the looper to process the messages in the message queue, that is, whether to wake up the looper to dequeue.
[0107] S309: Insert the new message into the message queue according to the submission time (ie, timestamp) of the new message.
[0108] Specific reference Figure 1 The relevant description will not be repeated here.
[0109] S310: If there is an asynchronous message in the message queue, set the wake-up flag to invalid.
[0110] S311. Determine the state of the wake-up flag. If the wake-up flag is valid, wake up the looper to process the message in the message queue.
[0111] The looper is woken up by the nativeWake function to process the messages in the message queue.
[0112] This implementation can also handle exceptions for synchronization barrier messages from any source, and can cover the entire life cycle of an application, with a wide range of applications.
[0113] In another possible implementation, Figure 7 As shown, the system can create a child thread, and the child thread periodically submits preset synchronization messages to the message queue of the main thread or UI thread in a watchdog-like manner. If the preset synchronization message stays in the message queue for longer than the second preset time (that is, the preset synchronization message is not processed within the second preset time), it indicates that the message queue is blocked. At this time, determine whether the first message in the message queue is a synchronization barrier message (that is, determine whether the message queue is blocked because the first message in the message queue is a synchronization barrier message). If the first message in the message queue is a synchronization barrier message, and if the synchronization barrier message stays in the message queue for longer than the first preset time, then remove the synchronization barrier message from the message queue. The specific method is as follows:
[0114] S401, the system creates a child thread to detect whether the message queue of the main thread or the UI thread is blocked.
[0115] S402: The child thread periodically (for example, 2 seconds) submits a preset synchronization message to the message queue of the main thread or the UI thread.
[0116] S403: Determine whether the duration for which the preset synchronization message stays in the message queue is less than or equal to a second preset duration.
[0117] If the duration of the preset synchronization message staying in the message queue is less than or equal to the second preset duration (ie, the preset synchronization message is processed within the second preset duration), the subline is executed again from step S402. Otherwise, step S404 is executed.
[0118] S404: If the preset synchronization message stays in the message queue for a period longer than a second preset period, the child thread determines whether the first message in the message queue is a synchronization barrier message.
[0119] If the first message in the message queue is a synchronization barrier message, execute steps S405-S407, otherwise execute step S407.
[0120] If the preset synchronization message stays in the message queue for longer than the second preset time, that is, the preset synchronization message is not processed within the second preset time, it indicates that the message queue is blocked. At this time, it can be determined whether the message queue is blocked due to the synchronization barrier message in the message queue based on whether the first message in the message queue is a synchronization barrier message. If the first message in the message queue is a synchronization barrier message, the message queue will be blocked.
[0121] Other possible reasons for the message queue to be blocked include: the looper is blocked when processing a synchronous message in the message queue, the looper's processing performance is too low, etc.
[0122] S405. If the first message in the message queue is a synchronization barrier message, the sub-line obtains the length of time the synchronization barrier message stays in the message queue. If the length of time the synchronization barrier message stays in the message queue is greater than the first preset length of time, the synchronization barrier message is removed from the message queue.
[0123] Regarding obtaining the duration of the synchronization barrier message staying in the message queue, refer to the description of step S102, which will not be repeated here. The first preset duration is greater than the second preset duration, for example, the first preset duration is 3 seconds and the second preset duration is 2 seconds.
[0124] S406: The child thread submits the call stack information of the synchronization barrier message to the server.
[0125] This step refers to step S205 and will not be described in detail here.
[0126] S407: The child thread removes the preset synchronization message and starts again from step S402.
[0127] So that the preset synchronization message can be resubmitted in the next round and the timing can be restarted.
[0128] This implementation is relatively simple in function and consumes relatively little resources. When the child thread is created by the UI thread, only the synchronization barrier message from the UI thread can be detected. When the child thread is created by the main thread, the synchronization barrier messages from all threads can be detected.
[0129] In addition, as mentioned above, in the Android operating system, the ViewRootImpl class is responsible for view refresh, which is implemented through asynchronous messages. Every time the main thread or UI thread refreshes the view, it will eventually call the scheduleTraversals function, unscheduleTraversals function, and doTraversal function in the ViewRootImpl class to implement view refresh related functions, and the view is refreshed according to the frequency of the Vsync signal. Among them, the scheduleTraversals function is used to prepare for traversal of the view tree that needs to be refreshed, the unscheduleTraversals function is used to cancel the traversal of the view tree that needs to be refreshed, and the doTraversal function is used to execute the traversal of the view tree that needs to be refreshed.
[0130] In addition, the scheduleTraversals function, unscheduleTraversals function, and doTraversal function will all judge and operate the traversal preparation (mTraversalScheduled) variable. The mTraversalScheduled variable is used to indicate whether the view tree that needs to be refreshed is ready to be traversed, which can ensure that the view tree that needs to be refreshed is only traversed once before the next Vsync signal arrives. For example, Figure 8 As shown in A, in the scheduleTraversals function, the mTraversalScheduled variable is checked for true or false. If the mTraversalScheduled variable is false, the postSyncBarrier function of the MessageQueue class is called to submit the synchronization barrier message and the mTraversalScheduled variable is set to true, indicating that the view tree that needs to be refreshed is ready to be traversed. Then, when each Vsync signal arrives, the view only needs to be refreshed once until the next Vsync signal arrives. Figure 8 As shown in B, in the unscheduleTraversals function and the doTraversal function, the truth of the mTraversalScheduled variable will be judged. If the mTraversalScheduled variable is true, the removeSyncBarrier function of the MessageQueue class is called to remove the synchronization barrier message, and the mTraversalScheduled variable is set to false to indicate that the view has been refreshed. In this way, the scheduleTraversals function can prepare for the next Vsync signal to traverse the view tree that needs to be refreshed.
[0131] However, in the code written by the programmer, there may be irregular operations of refreshing the view by the child thread instead of the main thread or UI thread, which conflicts with the operation of refreshing the view by the main thread or UI thread. The reason is that the operations of the scheduleTraversals function, unscheduleTraversals function, and doTraversal function on the mTraversalScheduled variable in the ViewRootImpl class are not atomic, which may cause the main thread or UI thread to call the scheduleTraversals function and submit a synchronization barrier message, and the child thread also calls the scheduleTraversals function and submits a synchronization barrier message; the main thread or UI thread calls the unscheduleTraversals function or the doTraversal function and removes a synchronization barrier message, but the child thread does not call the unscheduleTraversals function or the doTraversal function to remove a synchronization barrier message. In other words, two synchronization barrier messages (represented by the identifier token) are submitted to the message queue, but only one synchronization barrier message is removed, so that there is always a synchronization barrier message in the message queue that will not be removed, so the message queue is blocked.
[0132] Therefore, in the embodiment of the present application, the judgment (true or false) and operation (set to true or false) of the mTraversalScheduled variable in the scheduleTraversals function, the unscheduleTraversals function, and the doTraversal function, as well as the submission of the synchronization barrier message and the removal of the synchronization barrier message, can be extracted into a lock function with an atomic operation function, and an additional parameter is passed to the lock function, which is used to indicate that the lock function is called by the scheduleTraversals function, the unscheduleTraversals function, or the doTraversal function. If the lock function returns true, it indicates that the operation is successful. If the lock function returns false, it indicates that the operation failed.
[0133] like Figure 8As shown in C, the lock holding function is called by the scheduleTraversals function, unscheduleTraversals function, and doTraversal function to implement atomic operations on the mTraversalScheduled variable, to prevent the child thread from calling the scheduleTraversals function again and submitting a synchronization barrier message when the main thread or UI thread has already called the scheduleTraversals function and submitted a synchronization barrier message. Other functions in the scheduleTraversals function, unscheduleTraversals function, and doTraversal function remain unchanged, for example, the scheduleTraversals function continues to request the Vsync signal, the unscheduleTraversals function continues to stop view drawing, and the doTraversal function continues to execute view drawing.
[0134] Specifically, Figure 8 As shown in C, the lock function implements the following functions: after entering the lock function, first hold the synchronization lock. If the mTraversalScheduled variable is false, and the additional parameter indicates that the lock function is called by the scheduleTraversals function (i.e., preparing to traverse the view tree that needs to be refreshed), then call the postSyncBarrier function of the MessageQueue class to submit the synchronization barrier message to the message queue, and set the mTraversalScheduled variable to true, and the return value of the lock function is true. If the mTraversalScheduled variable is true, and the additional parameter indicates that the lock function is called by the unscheduleTraversals function (i.e., cancel the traversal of the view tree that needs to be refreshed) or by the doTraversal function (i.e., execute the traversal of the view tree that needs to be refreshed), then call the removeSyncBarrier function of the MessageQueue class to remove the synchronization barrier message from the message queue, and set the mTraversalScheduled variable to false, and the return value of the lock function is true. Otherwise the return value of the lock holding function is false.
[0135] This implementation can fundamentally avoid the problem that the synchronization barrier message is not removed in time due to the child thread refreshing the view, and there is no need to wait for the first preset time to remove the synchronization barrier message, thereby avoiding the phenomenon of stuck and frozen screen.
[0136] like Fig. 9As shown, the embodiment of the present application also provides a chip system. The chip system 90 includes at least one processor 901 and at least one interface circuit 902. The at least one processor 901 and the at least one interface circuit 902 can be interconnected through a line. The processor 901 is used to support the electronic device to implement each step in the above method embodiment, such as Figure 4-Figure 8 In the method shown, at least one interface circuit 702 may be used to receive signals from other devices (eg, memory) or to send signals to other devices (eg, communication interface). The chip system may include a chip and may also include other discrete devices.
[0137] The present application also provides a computer-readable storage medium, which includes instructions. When the instructions are executed on the electronic device, the electronic device executes each step in the method embodiment, such as executing Figure 4-Figure 8 The method shown.
[0138] The present application also provides a computer program product including instructions. When the instructions are executed on the electronic device, the electronic device executes each step in the method embodiment, such as executing Figure 4-Figure 8 The method shown.
[0139] Regarding the technical effects of the chip system, computer-readable storage medium, and computer program product, refer to the technical effects of the previous method embodiments.
[0140] It should be understood that in the various embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0141] Those of ordinary skill in the art will appreciate that the modules and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0142] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and modules described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0143] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.
[0144] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical modules, that is, they may be located in one device or distributed on multiple devices. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present embodiment.
[0145] In addition, each functional module in each embodiment of the present application may be integrated into one device, or each module may exist physically separately, or two or more modules may be integrated into one device.
[0146] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using a software program, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or may contain one or more servers, data centers and other data storage devices that can be integrated with the medium. The available medium may be a magnetic medium (eg, a floppy disk, a hard disk, a magnetic tape), an optical medium (eg, a DVD), or a semiconductor medium (eg, a solid state disk (SSD)).
[0147] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A synchronization barrier message processing method, characterized in that: include: Determine whether the first message in the message queue is a synchronization barrier message, wherein the synchronization barrier message includes call stack information of the synchronization barrier message, and the call stack information is used to indicate the stack information of each function on the generation path of the synchronization barrier message; If the first message is a synchronization barrier message, and the time the synchronization barrier message stays in the message queue is greater than the first preset time, the synchronization barrier message is removed from the message queue, and the call stack information of the synchronization barrier message is sent to the server, and the server outputs the call stack information of the synchronization barrier message.
2. The method according to claim 1, characterized in that The step of determining whether the first message in the message queue is a synchronization barrier message includes: The child thread periodically submits a preset synchronization message to the message queue of the main thread or the user interface thread. If the preset synchronization message stays in the message queue for longer than a second preset time, it determines whether the first message in the message queue is a synchronization barrier message.
3. The method according to claim 1, characterized in that The method is executed when traversing each message in the message queue, or when a message is enqueued.
4. The method according to claim 1, characterized in that: Also includes: Hold the sync lock; If the traversal preparation variable is false, and the view tree that needs to be refreshed is ready to be traversed, a synchronization barrier message is submitted to the message queue, and the traversal preparation variable is set to true, where the traversal preparation variable is used to indicate whether the view tree that needs to be refreshed is ready to be traversed; If the traversal preparation variable is true, and the traversal of the view tree that needs to be refreshed is canceled or the traversal of the view tree that needs to be refreshed is executed, the synchronization barrier message is removed from the message queue and the traversal preparation variable is set to false.
5. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores instructions, and when the processor executes the instructions, the method according to any one of claims 1 to 4 is executed.
6. A computer-readable storage medium, characterized in that: The method comprises instructions, which, when executed on an electronic device, cause the electronic device to execute the method according to any one of claims 1 to 4.
