WO2020147859A1 - 分散式重锁降级 - Google Patents

分散式重锁降级 Download PDF

Info

Publication number
WO2020147859A1
WO2020147859A1 PCT/CN2020/073102 CN2020073102W WO2020147859A1 WO 2020147859 A1 WO2020147859 A1 WO 2020147859A1 CN 2020073102 W CN2020073102 W CN 2020073102W WO 2020147859 A1 WO2020147859 A1 WO 2020147859A1
Authority
WO
WIPO (PCT)
Prior art keywords
lock
operation method
thread
application
threads
Prior art date
Application number
PCT/CN2020/073102
Other languages
English (en)
French (fr)
Inventor
杨启彬
秦巍
张斌
赵俊民
朱金鹏
Original Assignee
华为技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 华为技术有限公司 filed Critical 华为技术有限公司
Publication of WO2020147859A1 publication Critical patent/WO2020147859A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/45Exploiting coarse grain parallelism in compilation, i.e. parallelism between groups of instructions
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/524Deadlock detection or avoidance

Definitions

  • the present invention relates to the computer field, in particular to a lock operation method and an electronic device running the lock operation method.
  • Java language is widely used.
  • the Android operating system commonly used in mobile terminals
  • application software that can run on the Android operating system or ios operating system
  • rich client applications The (rich client application) medium can use the Java language for development, and then make the software run in the Java runtime environment.
  • Java supports parallel processing (parallel processing), also known as concurrency, that is, there are multiple programs or multiple threads at the same time in a period of time. Some of them may need to access the same resource, such as the same object, There was competition.
  • Java provides an object-based lock mechanism. Only one thread can hold the lock at the same time, and only when the thread releases the lock, other threads may hold the lock again.
  • the object locks and runs the program code contained in the object.
  • One of the locks used under fierce competition is called a fatlock, which will cause more system calls and thus generate greater overhead. And once the object has a relock, it will not be cancelled by the thread to obtain and release the lock.
  • the ART (android runtime) virtual machine developed by Google provides a lock downgrade mechanism attached to garbage collection (GC).
  • GC will pause the runtime, that is, the thread created by the application is paused.
  • the method of centralized lock downgrading during GC cannot solve the unnecessary system burden caused by relocking between two GCs.
  • the suspension of threads during GC brings users a stuck experience (according to the scope of GC cleanup The difference, a single GC can take several hundred milliseconds). Therefore, suspending the virtual machine is undesirable from the perspective of ensuring a smooth user experience, and the GC with a too long interval is inefficient from the perspective of lock degradation.
  • the embodiment of the present application provides a decentralized lock downgrade mechanism, wherein the lock downgrade timing is independent of the GC, and if the current lock-holding thread detects that the object is no longer in contention when releasing the relock, it will downgrade the relock in time. It takes nanoseconds to downgrade the lock once, and users cannot perceive it. Therefore, this decentralized lock downgrade mechanism ensures that the user's fluency experience will not be affected by lock downgrade, and on the other hand, avoid unnecessary relocking overhead in time.
  • a lock operation method runs on an electronic device. There are multiple application threads running on the electronic device. The multiple application threads include a first application thread.
  • the lock operation method includes degrading the lock under the condition that all or part of the multiple application threads are not suspended.
  • the lock includes a lock word, and the lock word defines whether the lock is a heavy lock, a light lock, or no lock.
  • the lock is a relock.
  • the lock operation method includes downgrading the lock to light lock or no lock without suspending any of the multiple application threads.
  • the lock operation method includes degrading the lock to light lock or no lock without suspending other application threads among the multiple application threads under the condition that the first application thread is suspended.
  • the lock operation method includes downgrading the lock to a light lock or no lock without suspending the first application thread, but suspending a thread with a lower priority among multiple application threads.
  • downgrading the lock includes downgrading the lock to light lock and downgrading to no lock.
  • the lock downgrade is performed without other threads waiting for the lock.
  • the lock downgrade is performed under the condition that no other thread has been blocked on the lock or the java method wait or sleep is called.
  • the lock downgrade is performed in the MonitorExit module.
  • an electronic device including a memory, a processor, and a computer program stored in the memory and capable of running on the processor, wherein the processor executes the computer program to enable the electronic device to implement The lock operation method of any implementation form of the first aspect.
  • a computer storage medium including computer instructions, which when the computer instructions run on an electronic device, cause the electronic device to execute the lock operation method according to any implementation form of the first aspect.
  • a computer program product which when the computer program product runs on a computer, causes the computer to execute the lock operation method according to any implementation form of the first aspect.
  • FIG. 1 shows a schematic structural diagram of an electronic device 100 provided by an embodiment of the present application.
  • FIG. 2 shows a block diagram of the software structure of the electronic device 100 according to an embodiment of the present invention.
  • Figure 3 shows a lock structure provided by an embodiment of the present application.
  • Figure 4 shows the light lock and heavy lock mechanisms provided by the embodiment of the present application.
  • Figures 5A to 5F show user experience scenarios.
  • Fig. 6 shows a lock operation method provided by an embodiment of the present application.
  • Figure 7 shows a lock operation method provided by an embodiment of the present application.
  • Figure 8 shows a lock operation method provided by an embodiment of the present application.
  • 9A and 9B show the lock operation method provided by the embodiment of the present application.
  • Fig. 10 shows a lock operation method provided by an embodiment of the present application.
  • Fig. 11 shows a lock operation method provided by an embodiment of the present application.
  • Figure 12 shows a lock operation method provided by an embodiment of the present application.
  • Figure 13 shows a lock operation method provided by an embodiment of the present application.
  • Figure 14 shows a lock operation method provided by an embodiment of the present application.
  • Figure 15 shows a lock operation method provided by an embodiment of the present application.
  • Figure 16 shows a lock operation method provided by an embodiment of the present application.
  • Figure 17 shows a lock operation method provided by an embodiment of the present application.
  • Figure 18 shows a lock operation method provided by an embodiment of the present application.
  • Figure 19 shows a lock operation method provided by an embodiment of the present application.
  • OS Operating system
  • Runtime also known as runtime environment or runtime system, is an environment in which semi-compiled or fully compiled runtime codes are run on the target machine.
  • the runtime referred to in this application is the Java runtime.
  • a process is a running activity of an application on a certain data set, and is the basic unit of the operating system (such as the Android system) for resource allocation and scheduling.
  • Each process occupies an address space, and the application program runs on the operating system in the form of one or more processes to achieve corresponding functions.
  • a thread is an entity of a process. It is a basic unit that is smaller than a process and can run independently. A thread can share all resources owned by the process with other threads belonging to the same process. A thread can create and cancel another thread, and multiple threads in the same process can execute concurrently.
  • Application threads are threads generated during the running of an application. These threads are hung on a list by the runtime system after they are created. Application thread is also called user thread.
  • Synchronized In order to ensure the concurrency of multiple threads, mutual exclusion methods are adopted for objects in the running of java programs, so that the same code segment can be run by only one thread at the same time to avoid running chaos.
  • the means of mutual exclusion is usually to add the synchronized keyword.
  • Synchronized at the Java language level will be converted to lower-level monitor enter and monitor exit when executed, where monitor enter is placed at the head of the protected code, and monitor exit is placed at the end of the protected code . This means that if a thread wants to run a program to which the synchronized keyword is added, it must run monitor enter to enter the object lock and monitor exit to exit the object lock.
  • Each FatLock has its own monitor ID, which is the aforementioned MonitorId. All monitors will be placed in the same area of the software structure of the electronic device.
  • the electronic device is equipped with a java virtual machine for running java programs, and the monitor can be stored in a specific area of the java virtual machine .
  • Lock is a mechanism to ensure that only one thread accesses a method or variable at a time.
  • a thread accesses an object or a piece of code, it must acquire the lock to which the object belongs, otherwise the thread will wait (or block) until the lock is released and successfully acquire the released lock. That is to say, only one thread can acquire the lock at most.
  • thread A tries to acquire the object lock held by thread B, thread A must wait or block until thread B releases the lock, then thread A can acquire the lock To access the corresponding code snippet.
  • Lock deflation is to change heavy locks into light locks or no locks.
  • lock downgrading is also referred to as downgrading for the sake of smooth contextual expression.
  • Block usually refers to a situation in which multiple threads execute a shared code segment, because a thread acquires a lock and other threads are waiting for the lock to be released and cannot enter the code segment.
  • Garbage collection is the garbage collection module at runtime, which performs garbage collection on objects that are not used in the current system to reduce the burden of system memory.
  • ART's GC trigger conditions include: memory allocation failure; explicit call to the System.gc() function; trimming of the heap at runtime, etc.
  • An application is an application software that enables a computing device to perform tasks for a user of the computing device.
  • system_server It is a process of running services in the operating system. It is the provider of basic services and the basic requirements for system operation. system_server is The naming in other operating systems may have other naming, but if the nature is the process of running services in the operating system and the provider of basic services, they are all within the scope of the term system_server in this application.
  • the lock operation method provided in the embodiment of the present application runs on an electronic device.
  • the lock operation device provided in the embodiment of the present application is arranged in an electronic device.
  • electronic devices can correspond to smart phones, portable phones, game consoles, televisions, display units, head-up display units for vehicles, notebook computers, laptop computers, tablet personal computers (PC), personal Media player (PMP), personal digital assistant (PDA), set-top box, etc.
  • the electronic device may also be implemented as a communication terminal having a communication module, and the communication terminal may communicate with an external electronic device such as a server, or perform operations by interworking with the external electronic device.
  • an electronic device capable of running computer instructions can run the lock operation method provided in the embodiment of the present application and is provided with the lock operation device provided in the embodiment of the present application.
  • FIG. 1 shows a schematic structural diagram of an electronic device 100 provided by an embodiment of the present application.
  • the electronic device 100 may include a processor 110, where the processor 110 may include a memory.
  • the electronic device 100 may also optionally include an internal memory 121, an external memory interface 120, a display screen 194, a touch sensor 180K, a USB interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, and mobile communications Module 150, wireless communication module 160, audio module 170, speaker 170A, receiver 170B, microphone 170C, earphone interface 170D, sensor module 180, buttons 190, motor 191, indicator 192, camera 193, SIM card interface 195 and so on.
  • the sensor module 180 can include pressure sensor 180A, gyroscope sensor 180B, air pressure sensor 180C, magnetic sensor 180D, acceleration sensor 180E, distance sensor 180F, proximity light sensor 180G, fingerprint sensor 180H, temperature sensor 180J, ambient light sensor 180L, and bone Conduction sensor 180M etc.
  • the structure illustrated in the embodiment of the present invention does not constitute a specific limitation on the electronic device 100.
  • the electronic device 100 may include more or fewer components than shown, or combine some components, or split some components, or arrange different components.
  • the illustrated components can be implemented in hardware, software, or a combination of software and hardware.
  • the processor 110 may include one or more processing units.
  • the processor 110 may include an application processor (AP), a modem processor, a graphics processing unit (GPU), and an image signal processor. (image signal processor, ISP), controller, memory, video codec, digital signal processor (digital signal processor, DSP), baseband processor, and/or neural network processor (Neural-network Processing Unit, NPU) Wait.
  • AP application processor
  • modem processor modem processor
  • GPU graphics processing unit
  • image signal processor image signal processor
  • ISP image signal processor
  • controller memory
  • video codec digital signal processor
  • DSP digital signal processor
  • baseband processor baseband processor
  • neural network processor Neural-network Processing Unit, NPU
  • different processing units may be independent devices, or may be integrated in one or more processors.
  • the controller may be the nerve center and command center of the electronic device 100.
  • the controller can generate operation control signals according to the instruction operation code and timing signals, and complete the control of fetching and executing instructions.
  • the processor 110 may also be provided with a memory for storing instructions and data.
  • the memory in the processor 110 is a cache memory.
  • the memory may store instructions or data that the processor 110 has just used or recycled. If the processor 110 needs to use the instruction or data again, it can be directly called from the memory. The repeated access is avoided, and the waiting time of the processor 110 is reduced, thereby improving the efficiency of the system.
  • the processor 110 may include one or more interfaces.
  • Interfaces may include integrated circuit (inter-integrated circuit, I2C) interface, integrated circuit built-in audio (inter-integrated circuit, sound, I2S) interface, pulse code modulation (pulse code modulation (PCM) interface, universal asynchronous transceiver (universal asynchronous) receiver/transmitter (UART) interface, mobile industry processor interface (mobile industry interface, MIPI), general-purpose input/output (GPIO) interface, subscriber identity module (subscriber identity module, SIM) interface, and And/or universal serial bus (USB) interface, etc.
  • I2C integrated circuit
  • I2S integrated circuit built-in audio
  • PCM pulse code modulation
  • UART universal asynchronous transceiver
  • MIPI mobile industry processor interface
  • GPIO general-purpose input/output
  • SIM subscriber identity module
  • USB universal serial bus
  • the I2C interface is a bidirectional synchronous serial bus, including a serial data line (serial data line, SDA) and a serial clock line (derail clock line, SCL).
  • the processor 110 may include multiple sets of I2C buses.
  • the processor 110 may be coupled to the touch sensor 180K, charger, flash, camera 193, etc., respectively through different I2C bus interfaces.
  • the processor 110 may couple the touch sensor 180K through the I2C interface, so that the processor 110 and the touch sensor 180K communicate through the I2C bus interface, and realize the touch function of the electronic device 100.
  • the I2S interface can be used for audio communication.
  • the processor 110 may include multiple sets of I2S buses.
  • the processor 110 may be coupled to the audio module 170 through an I2S bus to implement communication between the processor 110 and the audio module 170.
  • the audio module 170 can transfer audio signals to the wireless communication module 160 through the I2S interface, so as to realize the function of answering the call through the Bluetooth headset.
  • the PCM interface can also be used for audio communication, sampling, quantizing and encoding analog signals.
  • the audio module 170 and the wireless communication module 160 may be coupled through a PCM bus interface.
  • the audio module 170 may also transmit audio signals to the wireless communication module 160 through the PCM interface, so as to realize the function of answering calls through the Bluetooth headset. Both the I2S interface and the PCM interface can be used for audio communication.
  • UART interface is a universal serial data bus used for asynchronous communication.
  • the bus may be a bidirectional communication bus. It converts the data to be transmitted between serial communication and parallel communication.
  • the UART interface is generally used to connect the processor 110 and the wireless communication module 160.
  • the processor 110 communicates with the Bluetooth module in the wireless communication module 160 through the UART interface to implement the Bluetooth function.
  • the audio module 170 may transmit audio signals to the wireless communication module 160 through a UART interface, so as to realize the function of playing music through a Bluetooth headset.
  • the MIPI interface can be used to connect the processor 110 to peripheral devices such as the display screen 194 and the camera 193.
  • MIPI interface includes camera serial interface (camera serial interface, CSI), display serial interface (display serial interface, DSI) and so on.
  • the processor 110 and the camera 193 communicate through a CSI interface to implement the shooting function of the electronic device 100.
  • the processor 110 and the display screen 194 communicate through the DSI interface to realize the display function of the electronic device 100.
  • the GPIO interface can be configured via software.
  • the GPIO interface can be configured as a control signal or a data signal.
  • the GPIO interface may be used to connect the processor 110 with the camera 193, the display screen 194, the wireless communication module 160, the audio module 170, the sensor module 180, and so on.
  • GPIO interface can also be configured as I2C interface, I2S interface, UART interface, MIPI interface, etc.
  • the USB interface 130 is an interface that conforms to the USB standard, and may specifically be a Mini USB interface, a Micro USB interface, a USB Type C interface, and so on.
  • the USB interface can be used to connect a charger to charge the electronic device 100, and can also be used to transfer data between the electronic device 100 and peripheral devices. It can also be used to connect headphones and play audio through the headphones. This interface can also be used to connect other electronic devices, such as AR devices.
  • the interface connection relationship between the modules illustrated in the embodiment of the present invention is merely illustrative, and does not constitute a structural limitation of the electronic device 100.
  • the electronic device 100 may also use different interface connection methods in the foregoing embodiments, or a combination of multiple interface connection methods.
  • the charging management module 140 is used to receive charging input from the charger.
  • the wireless communication function of the electronic device 100 can be implemented by the antenna module 1, the antenna module 2 mobile communication module 150, the wireless communication module 160, the modem processor, and the baseband processor.
  • Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals.
  • Each antenna in the electronic device 100 may be used to cover a single or multiple communication frequency bands.
  • the mobile communication module 150 may provide a wireless communication solution including 2G/3G/4G/5G and the like applied to the electronic device 100.
  • at least part of the functional modules of the mobile communication module 150 may be provided in the processor 110.
  • at least part of the functional modules of the mobile communication module 150 and at least part of the modules of the processor 110 may be provided in the same device.
  • the modem processor may include a modulator and a demodulator.
  • the modulator is used to modulate the low-frequency baseband signal to be transmitted into a high-frequency signal.
  • the demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. Then the demodulator transmits the demodulated low-frequency baseband signal to the baseband processor for processing.
  • the wireless communication module 160 can provide applications on the electronic device 100 including wireless local area networks (WLAN), Bluetooth (bluetooth, BT), global navigation satellite system (GNSS), frequency modulation (frequency modulation, FM), near field communication (NFC), infrared technology (infrared, IR) and other wireless communication solutions.
  • WLAN wireless local area networks
  • Bluetooth blue, BT
  • GNSS global navigation satellite system
  • FM frequency modulation
  • NFC near field communication
  • infrared technology infrared, IR
  • the electronic device 100 realizes a display function through a GPU, a display screen 194, and an application processor.
  • the GPU is a microprocessor for image processing, connecting the display screen 194 and the application processor.
  • the GPU is used to perform mathematical and geometric calculations and used for graphics rendering.
  • the processor 110 may include one or more GPUs that execute program instructions to generate or change display information.
  • the display screen 194 is used to display images, videos, etc.
  • the display screen 194 includes a display panel.
  • the display panel can use LCD (liquid crystal), OLED (organic light-emitting diode), active matrix organic light-emitting diode or active matrix organic light-emitting diode (active-matrix organic light) emitting diode, AMOLED), flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, quantum dot light emitting diode (QLED), etc.
  • the electronic device 100 may include 1 or N display screens, and N is a positive integer greater than 1.
  • the external memory interface 120 may be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100.
  • the external memory card communicates with the processor 110 through the external memory interface 120 to realize the data storage function. For example, save music, video and other files in an external memory card.
  • the internal memory 121 may be used to store computer executable program code, where the executable program code includes instructions.
  • the processor 110 executes various functional applications and data processing of the electronic device 100 by running instructions stored in the internal memory 121.
  • the memory 121 may include a program storage area and a data storage area. Among them, the storage program area may store an operating system, at least one function required application programs (such as sound playback function, image playback function, etc.) and so on.
  • the data storage area can store data (such as audio data, phone book, etc.) created during the use of the electronic device 100.
  • the memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, a universal flash storage (UFS), and the like.
  • UFS universal flash storage
  • Touch sensor 180K also known as "touch panel”.
  • the touch sensor 180K may be disposed on the display screen 194, and the touch screen is composed of the touch sensor 180K and the display screen 194, which is also called a “touch screen”.
  • the touch sensor 180K is used to detect touch operations acting on or near it.
  • the touch sensor can pass the detected touch operation to the application processor to determine the type of touch event.
  • the visual output related to the touch operation can be provided through the display screen 194.
  • the touch sensor 180K may also be disposed on the surface of the electronic device 100, which is different from the position of the display screen 194.
  • the button 190 includes a power button, a volume button, and so on.
  • the keys can be mechanical keys. It can also be a touch button.
  • the electronic device 100 may receive key input, and generate key signal input related to user settings and function control of the electronic device 100.
  • the software system of the electronic device 100 may adopt a layered architecture, an event-driven architecture, a microkernel architecture, a microservice architecture, or a cloud architecture.
  • the embodiment of the present invention adopts a layered architecture
  • the system is taken as an example to illustrate the software structure of the electronic device 100.
  • FIG. 2 shows a block diagram of the software structure of the electronic device 100 according to an embodiment of the present invention.
  • the layered architecture divides the software into several layers, and each layer has a clear role and division of labor. Communication between layers through software interface.
  • the The system is divided into four layers, from top to bottom are the application layer, the application framework layer, and the Android runtime ( runtime) and system libraries, as well as the kernel layer.
  • the operating system (OS) of the electronic device 100 includes an application framework layer, an Android runtime and a system library.
  • the application layer may include a series of application packages.
  • the application package may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message, etc.
  • the application framework layer provides application programming interfaces (application programming interface, API) and programming frameworks for applications in the application layer.
  • the application framework layer includes some predefined functions.
  • the application framework layer can include a window manager, a content provider, a view system, a phone manager, a resource manager, and a notification manager.
  • 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, intercept the screen, etc.
  • Content providers are used to store and retrieve data and make it accessible to applications.
  • the data may include videos, images, audio, calls made and received, browsing history and bookmarks, phonebooks, etc.
  • the view system includes visual controls, such as controls that display text and controls that display pictures.
  • the view system can be used to build applications.
  • the display interface can be composed of one or more views.
  • a display interface that includes an SMS notification icon may include a view that displays text and a view that displays pictures.
  • the phone manager is used to provide the communication function of the electronic device 100. For example, the management of the call state (including connection, hang up, etc.).
  • the resource manager provides various resources for the application, such as localized strings, icons, pictures, layout files, video files, and so on.
  • the notification manager enables applications to display notification information in the status bar, which can be used to convey notification-type messages, and can disappear after a short stay without user interaction.
  • the notification manager is used to notify the completion of downloading, message reminders, etc.
  • the notification manager can also be a notification that appears in the status bar at the top of the system in the form of a chart or scroll bar text, such as a notification of an application running in the background, or a notification that appears on the screen in the form of a dialog window.
  • the text message is displayed in the status bar, a prompt sound is emitted, the electronic device vibrates, and the indicator light flashes.
  • Runtime includes core libraries and virtual machines. The runtime is responsible for the scheduling and management of the Android system.
  • the core library contains two parts: one part is the function function that Java language needs to call, and the other part is the core library of Android.
  • the application layer and the application framework layer run in the virtual machine.
  • the virtual machine executes the java files of the application layer and the application framework layer into binary files.
  • the virtual machine is used to perform functions such as object lifecycle management, stack management, thread management, security and exception management, and garbage collection.
  • the system library can include multiple functional modules. For example: surface manager (surface manager), media library (Media library), 3D graphics processing library (for example: OpenGL ES), 2D graphics engine (for example: SGL), etc.
  • surface manager surface manager
  • media library Media library
  • 3D graphics processing library for example: OpenGL ES
  • 2D graphics engine for example: SGL
  • the surface manager is used to manage the display subsystem and provides a combination of 2D and 3D layers for multiple applications.
  • the media library supports a variety of commonly used audio, video format playback and recording, and still image files.
  • the media library can support multiple audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, 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.
  • the kernel layer is the layer between hardware and software.
  • the kernel layer contains at least the display driver, camera driver, audio driver, and sensor driver.
  • Figure 3 shows a lock structure provided by an embodiment of the present application.
  • a java object will include multiple areas, for example, including object header (Header) and instance data (Instance Data). Header usually includes the runtime data of the object itself, such as hash code and UnLock, ThinLock, FatLock classification, etc. In this embodiment, 32 bits are reserved as a lockword area in the header. However, the lockword area is not dedicated, and other information such as hash codes may also be stored. Therefore, when reading and using the lockword information, the status bit of the lockword should be confirmed first. In other embodiments, the area of the lockword can be set to be dedicated, so when the lockword is read, there is no need to query whether the content inside is a lock.
  • the types of locks include unlock (unlock, or UnLock, etc., case-insensitive), light lock (thinlock, or ThinLock, case-insensitive), and heavy lock (fatlock, or FatLock, case-insensitive) write).
  • unlock unlock, or UnLock, etc., case-insensitive
  • light lock thinlock, or ThinLock, case-insensitive
  • heavy lock fatlock, or FatLock, case-insensitive
  • the first row is any 32-bit lockword, where the first two bits from the left represent the lock status (lock status).
  • the second row is the unlocked state. It can be seen that in no lock, the lock state is represented by 00, and the remaining bits are all 0. In the figure, the sum of the remaining bits is 0, which means that the remaining bits are all 0.
  • the first row is any 32-bit lockword, where the first two bits from the left represent the lock status (lock status).
  • the second row is the state of light lock. It can be seen that in the light lock, the lock state is represented by 00, and the next two bits are free, which are reserved bits. In other implementation forms, it can also be used for hash marks, etc.
  • the remaining bits are divided into the number of reentrants (lock count) and the thread ID (thread id).
  • the number of reentrants represents the number of times that a particular thread continues to enter the lock without releasing the lock.
  • the thread ID records the thread ID that currently holds the light lock. In this embodiment, 12 bits are divided into the number of reentrants, and 16 bits are used for the thread identification. In other embodiments, other numbers of digits can also be divided into reentrant times.
  • the first row is any 32-bit lockword, and the first two bits from the left represent the lock state.
  • the second row is the relocked state. It can be seen that in the relock, the lock status is represented by 01, and the next two bits are free, which are reserved bits. In other implementation forms, it can also be used for hash marks, etc. The remaining bits are allocated to the monitor ID (MonitorId). Each FatLock has its own monitor.
  • Figure 4 shows the light lock and heavy lock mechanisms provided by the embodiment of the present application.
  • the lock of an object cannot be held by more than one thread at the same time, that is, after a thread obtains the object lock, other threads cannot obtain the lock before it releases the lock.
  • T2 waits for the lock is explained based on whether the current state of the lock held by T1 is light lock or heavy lock.
  • T2 when a thread T2 is blocked on the lock, T2 will transfer the cpu ownership by calling sched_yield, and the system will add the thread to the end of the queue. If there is no thread with a higher priority than it, the thread will resume immediately Execute, otherwise let other threads occupy the cpu to continue execution. T2 will detect the lock state when it regains the cpu. If T1 has released the lock at this time, T2 will obtain the lock. At this time, only context switching is involved, and the overhead is small. Context switching refers to Save the status of registers, signals, etc. when going to the queue, so that the process can be executed normally when switching back.
  • the number of times that sched_yield is called by T2 during the light lock held by T1 is counted. If it exceeds a certain threshold, such as 50 times, it indicates that the contention caused by the lock is relatively serious, so T2 will lock the light lock. Change to relock, but the holder of relock is still T1.
  • the lock is a heavy lock
  • T2 accesses the lock
  • the kernel executes a complex scheduling algorithm.
  • T1 releases the lock, it will wake up the blocked thread T2 through the futex system call.
  • the whole process involves not only context switching, but also expensive overheads such as thread state changes and kernel scheduling.
  • the operation of generating and destroying Monitor also has overhead.
  • a known method is to uniformly degrade the relock when allocating memory (Allocate Memory) and GC. For example, when allocating memory and GC, the memory will be trimmed (trim), including relocking and downgrading. Before performing lock downgrade, all threads created by the application program will be suspended, and after the downgrade, execution of threads created by the application program will be resumed. This strategy is easy to implement, but the drawback is that it needs to be done when the thread created by the application is suspended while the virtual machine is running.
  • the pause time will be increased and the user experience will be damaged. Moreover, the time when the GC occurs is difficult to predict, so it is difficult to remind the user that the jam is about to occur before the GC occurs.
  • the user's bad experience may be caused by the pause during GC. For example, as shown in FIG. 5A, the user is using an electronic device to talk on the phone with a callee of higher social status. After the call ends, the user clicks to hang up, as shown in Figure 5B. Then the user puts the mobile phone in the pocket, as shown in Figure 5C.
  • the extension of the centralized lock downgrade to the GC depends on the number of relocks accumulated during the GC. If there are many threads running before the GC, such as during the boot process, the lock downgrade will take longer. The delay of lock degradation to GC can reach about 200ms.
  • Fig. 6 shows a lock operation method provided by an embodiment of the present application.
  • An object A is arranged in the electronic device, for example, a piece of code, and the thread T1 created by the application program is holding the lock of the object A.
  • object locks can be referred to as object locks for short.
  • the synchronized keyword can be used to modify the object for mutual exclusion, for example, in the form of synchronized(obj), where obj represents the object.
  • the Java Compiler inserts SyncEnter and SyncExit instructions at the beginning and end of the protected code segment.
  • the java virtual machine or other operating mechanism similar to the java virtual machine that converts the java language into a language understandable by the operating system will have corresponding functions like MonitorEnter and MonitorExit as the underlying implementation for the SyncEnter and SyncExit instructions.
  • MonitorEnter and MonitorExit as the underlying implementation for the SyncEnter and SyncExit instructions.
  • the lock operation method provided in this embodiment runs in the MonitorExit module. In the process of executing the lock operation method provided in this embodiment, it is not necessary to suspend all threads created by the application as in the GC.
  • step 601 the lock type is checked. If it is judged to be a relock, step 602 is executed, where it is judged that the object corresponding to the current lock is not used for wait, Java Methods such as sleep are not in a conflict state (that is, other threads are ready to block on the lock), and the lock is downgraded in step 603.
  • step 601 the current state of the object lock can be determined through the structure of the lockword. If the lockword has a Fatlock structure as shown in Figure 3, that is, the two digits of the lock are 01 and the last 28 digits are the monitor ID, then it is judged as a relock.
  • lock downgrading includes downgrading to light lock and no lock.
  • Figure 7 shows a lock operation method provided by an embodiment of the present application.
  • An object A is arranged in the electronic device, for example, a piece of code
  • the thread T1 created by the application program is holding the lock of the object A, and the state of the lock is relock.
  • T2 accesses the object, and then enters the MonitorEnter module.
  • the lock operation method provided in this embodiment runs in the MonitorEnter module.
  • step 701 it is determined that the lock state of the current object is relocked.
  • the specific measure can be to read the lockword, and judge whether it is relocking through the first two digits of the lockword and its structure. The meaning is to first judge whether it is relocking. In the case of relocking, the program after the last colon is allowed to be executed.
  • a spinlock is added for protection, so that threads other than T2 can no longer enter the code block protected by the spin lock temporarily.
  • the code block includes two blocks, one is the code segment protected by lock downgrade, and the other is The code segment in MonitorEnter that is ready to enter the conflict waiting.
  • the state of the lock is checked again, for example, the lockword is checked through the lw.GetState() function to obtain the lock state.
  • step 703 If the check result in step 703 is that the lock has been degraded, then the spin lock is cancelled in step 704, and the lock state is re-read in step 705, which may be UnLock or ThinLock.
  • step 706 the relock conflict flag is increased by one.
  • the relock conflict flag indicates: when the current thread T1 holds the relock and is about to downgrade, if it is found that other threads T2 have entered the code segment protected by the spin lock in the Monitorenter before T1 and are ready to relock here
  • T2 will set the conflict flag, and T1 will not downgrade if it detects it.
  • step 707 the protection of the spin lock is cancelled.
  • step 708 T2 enters the blocking state. Until T2 obtains the object lock, it will decrement the relock conflict flag by 1 in step 709.
  • step 702 of adding spin lock protection and the steps 704 and 707 of canceling the spin lock protection are optional.
  • FIG. 8 shows a lock operation method provided by an embodiment of the present application.
  • the lock operation method of FIG. 8 and the lock operation method of FIG. 7 act on the same object A.
  • the lock operation method in Figure 8 runs in the MonitorExit module.
  • T1 checks whether there are other threads waiting for the object lock. For example, check whether there is a thread that calls java functions such as wait and sleep for the relock held by T1, and there is no relock conflict flag. If not, go to step 802.
  • step 801 it is also checked whether the lockword of the current object lock is written as a hash code. If it is not a hash code and there is no waiting thread, step 802 is entered.
  • step 802 spin lock protection is added, so that when T1 executes the next steps in the lock operation method, no other thread can execute the code segment protected by the spin lock.
  • step 803 it is checked whether the relock conflict flag is 0. 0 means that no other threads have entered the MonitorEnter module of the current object when T1 is about to release the lock and are ready to block waiting. If it is not 0, it means that other threads have entered the MonitorEnter before the downgrade of T1 and are ready to block and wait on the relock. If the result of the check in step 803 is that the relock conflict flag is not 0, then the spin lock protection is cancelled in step 804, and the lock operation method in this embodiment is the result of the flow. That is, the relock is not downgraded.
  • step 803 If the result of the check in step 803 is that the relock conflict flag is 0, then in step 805, the relock is downgraded to no lock. The method is, for example, to zero the lockword. And in step 806, the spin lock is cancelled.
  • the step 802 of adding a spin lock and the steps 804 and 806 of canceling the spin lock are optional.
  • the exemplary code of this embodiment is:
  • FIGS. 9A and 9B show the lock operation method provided by the embodiment of the present application.
  • the lock operation method of FIGS. 9A and 9B and the lock operation method of FIG. 7 act on the same object A.
  • the lock operation method of Fig. 9A and Fig. 9B runs in the MonitorExit module. Thread T1 needs to release the lock after running the code of object A, so it goes to the MonitorExit module.
  • Thread T1 needs to release the lock after running the code of object A, so it goes to the MonitorExit module.
  • Thread T1 needs to release the lock after running the code of object A, so it goes to the MonitorExit module.
  • FIG. 9A it is checked whether the holder ID of the current object lock is the thread running the lock operation method. If the holder ID of the current object lock is not the thread running the lock operation method, an error has occurred, and the process of the method is ended by the return function.
  • step 902 If the holder ID of the current object lock is the thread running the lock operation method, it is checked in step 902 whether the number of reentrants is zero.
  • the number of reentries records the total number of times that thread T1 enters the MonitorEnter module of the object without releasing the current object lock.
  • the general operating rule of the lock mechanism in a Java program is that every time a MonitorExit is taken, the number of reentrants will be reduced by 1, until it is reduced to 0, the thread holding the lock is allowed to release the lock. If it is checked that the number of reentries is 0 in step 902, then in step 903, the lock holder ID is set to zero.
  • step 904 it is checked whether there is a thread waiting for the current object lock, and at least one of the waiting threads is awakened under the condition that there is a thread waiting. If it is checked in step 902 that the number of reentrants is not 0, then in step 905 the number of reentrants is reduced by one. After step 904 or step 905, go to step 906. In step 906, it is determined whether any thread has blocked on the lock or called the wait, sleep Java method. If it exists, the process of the lock operation method is ended, that is, the lock operation method is ended without degrading the relock.
  • Figure 9B shows step 907 and subsequent steps. In step 907, it is determined whether the lock holder ID is 0.
  • step 907 If it is determined in step 907 that the lock holder ID is not 0, then in step 908, spin lock protection is added. In step 909, under the condition that the relock conflict flag is 0, the relock is downgraded to light lock. In step 910, the protection of the spin lock is cancelled. If it is determined in step 907 that the lock holder identifier is 0, then in step 911, spin lock protection is added. In step 912, the relock is downgraded to no lock under the condition that the relock conflict flag is 0. In step 913, the protection of the spin lock is cancelled. After step 910 or step 913, the process of the lock operation method ends. Steps 908 and 911 of adding spin lock and steps 910 and 913 of canceling spin lock are optional.
  • the example code of the lock operation method in this embodiment is:
  • the lock operation method provided by the embodiments of FIGS. 6, 8 and 9A-B of this application enables the relock without contention to be dynamically downgraded in the process of thread running code, and the time for each downgrade is milliseconds, so The large block of time for degrading the centralized lock is scattered with the action of the thread, and will not cause the electronic equipment to freeze, thereby maintaining a smooth user experience. For example, compare an electronic device running the lock operation method provided in the embodiment of the application with an electronic device running the Google art system without the lock operation method.
  • the compiler After using the synchronized keyword to modify the object obj, the compiler will insert SyncEnter and SyncExit at the beginning and end of the code of the object, and SyncEnter and SyncExit correspond to the underlying MonitorExit function and MonitorExit function implementation respectively. As shown in the following code:
  • the execution time of the thread from Statement1 to Statment2 depends on the execution time of the lock and the execution time of the code sequence protected by the lock.
  • the main comparison is the number of lock degradations and the number of relocks, instead of comparing the execution time, because the compiler is different and the system is different. Etc. will cause differences in the execution time of the code sequence.
  • Fig. 10 shows a lock operation method provided by an embodiment of the present application.
  • MonitorEnter When a thread enters MonitorEnter for the first time before other threads, it gets the lock and changes the lock from the initial state (UnLock) to ThinLock;
  • LockCount++ if LockCount has exceeded the maximum value that can be represented at this time, the lock is upgraded from ThinLock to FatLock,
  • the thread sched_yield waits a certain number of times; if the wait exceeds a certain number of times, the lock is upgraded from ThinLock to FatLock;
  • Fig. 11 shows a lock operation method provided by an embodiment of the present application.
  • FIGS. 12 to 19 show that this application can perform lock downgrade without suspending all application threads.
  • Figure 12 shows a lock operation method provided by an embodiment of the present application.
  • the lock downgrade is performed under the condition that all application threads do not need to be suspended, and the downgrade is no lock or light lock.
  • Figure 13 shows a lock operation method provided by an embodiment of the present application.
  • Fig. 13 is an exemplary MonitorEnter scenario of the lock operation method shown in Fig. 12.
  • the system process is started for 15 seconds, 45 seconds, and accumulated 5 minutes at startup, and tests are performed on X (not adopted) and Y (adopted) respectively.
  • Figure 14 shows a lock operation method provided by an embodiment of the present application.
  • the lock downgrade is performed under the condition that all application threads do not need to be suspended, and downgrade to lock-free.
  • FIG. 15 shows a lock operation method provided by an embodiment of the present application.
  • FIG. 15 shows an exemplary MonitorEnter scenario of the lock operation method of FIG. 14.
  • Figure 16 shows a lock operation method provided by an embodiment of the present application.
  • the lock downgrade is performed under the condition that only the owner thread, that is, the thread currently holding the lock, is suspended. Application threads other than the Owner thread do not need to be suspended.
  • FIG. 17 shows a lock operation method provided by an embodiment of the present application.
  • FIG. 17 shows an exemplary MonitorEnter scenario of the lock operation method of FIG. 16.
  • Figure 18 shows a lock operation method provided by an embodiment of the present application.
  • the lock downgrade is performed under the condition that the owner thread is not suspended, but some application threads in other application threads are suspended.
  • Owner thread refers to the application thread currently holding the lock.
  • FIG. 19 shows a lock operation method provided by an embodiment of the present application.
  • FIG. 19 shows an exemplary MonitorEnter scenario of the lock operation method of FIG. 18.
  • the embodiment of the present application also provides an electronic device, including a memory, a processor, and a computer program stored on the memory and capable of running on the processor.
  • the feature is that the processor executes the computer program to make the electronic device implement the application.
  • the lock operation method provided by any embodiment.
  • the embodiments of the present application also provide a computer storage medium, including computer instructions, which when the computer instructions run on the electronic device, cause the electronic device to execute the lock operation method provided in any embodiment of the present application.
  • the embodiment of the present application also provides a computer program product, which when the computer program product runs on a computer, causes the computer to execute the lock operation method provided in any embodiment of the present application.
  • the computer program product may include one or more computer instructions.
  • the computer may be a general-purpose computer, a dedicated computer, a computer network, or other programmable devices.
  • 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, computer, server, or data center.
  • the computer-readable storage medium may be any available medium that can be accessed by a computer or a data storage device including one or more available medium integrated servers, data centers, and the like.
  • the usable medium may be a magnetic medium (for example, a floppy disk, a hard disk, and a magnetic disk), an optical medium (for example, a DVD), or a semiconductor medium (for example, a solid state disk (SSD)).
  • the disclosed system, device, and method may be implemented in other ways.
  • the device embodiments described above are only schematic.
  • the division of the unit is only a logical function division, and there may be other divisions in actual implementation, for example, multiple units or components may be combined or Can be integrated into another system, or some features can be ignored, or not implemented.
  • the displayed or discussed mutual coupling or direct coupling or communication connection may be indirect coupling or communication connection through some interfaces, devices or units, and may be in electrical, mechanical, or other forms.
  • the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
  • each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units may be integrated into one unit.
  • the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium.
  • the technical solution of the present application essentially or part of the contribution to the existing technology or part of the technical solution can be embodied in the form of a software product
  • the computer software product is stored in a storage medium, including Several instructions are used to enable a computer device (which may be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in the embodiments of the present application.
  • the foregoing storage media include various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory, a random access memory, a magnetic disk, or an optical disk.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Computing Systems (AREA)
  • Stored Programmes (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

本申请的实施例提供了一种锁操作方法,锁操作方法运行在电子设备上,电子设备上运行有多个应用线程,多个应用线程包括第一应用线程,第一应用线程持有锁,锁操作方法包括在将多个应用线程全部不暂停或部分不暂停的条件下将锁降级。

Description

分散式重锁降级
本申请要求在2019年1月20日提交中国国家知识产权局、申请号为201910050640.6、发明名称为“分散式重锁降级”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
技术领域
本发明涉及计算机领域,尤其涉及一种锁操作方法和运行该锁操作方法的电子设备。
背景技术
Java语言应用广泛,例如在采用J2EE的网络服务中,在移动终端中通常采用的安卓(android)操作系统中,在可运行在安卓操作系统或ios操作系统上的应用软件中,在富客户应用(rich client application)中等都可以使用Java语言进行开发,进而使得软件在Java运行环境中运行。
Java一个特征是支持并行运行(parallel processing),也称为并发,即在一个时段内同时有多个程序或多个线程,它们中的一些可能要去访问相同的资源,例如同一个对象,从而产生了竞争。为了保证有序的运行,Java提供了基于对象的锁(lock)机制,同一时间内只有一个线程能够持有锁,而只有当该线程释放该锁后,别的线程才有可能再持有该对象的锁并且运行该对象包含的程序代码。其中一种用于竞争较为激烈情况下的锁被称为重锁(fatlock),其会导致较多的系统调用,从而产生较大的开销。并且对象一旦具有了重锁,是不会通过线程拿(obtain)锁和释放(release)锁取消的。换言之,如果没有特殊干预,这些对象将一直背负重锁,随着重锁对象数目增多以及在其生命周期中访问其的次数累积,伴随的系统调用给系统带来较大负担。而其中一些对象在具有重锁后可能已不再处于竞争状态,重锁对其不再必要。可以看出,需要一种锁降级(deflation)机制,将不必要存在的重锁降级,以降低系统负担,提高运行效率。
目前,谷歌公司开发的ART(android runtime)虚拟机提供一种在垃圾回收(garbage collection,GC)中附带的锁降级机制。GC时会暂停运行时,即暂停由应用程序创建的线程。GC时集中锁降级的方式一方面不能解决两次GC之间重锁带来的不必要的系统负担问题,另一方面,GC时对线程的暂停给用户带来卡顿体验(根据GC清理范围的不同,单次GC耗时可达到几百毫秒)。因此暂停虚拟机是从保证用户流畅体验角度所不希望的,而过长间隔的GC从锁降级角度来讲又是低效的。
发明内容
本申请实施例提供一种分散式锁降级机制,其中,锁降级时机独立于GC,当前持锁线程在释放重锁时如果检测到对象该锁不再处于竞争中,就及时将重锁降级。一次锁降级耗时为纳秒级,用户感知不到。从而通过这种分散式锁降级机制一方面保证了不因锁降级而影响用 户的流畅性体验,一方面及时规避不必要的重锁开销。
为了达到上述目的,本申请实施例采用如下技术方案:
第一方面,提供了一种锁操作方法,所述锁操作方法运行在电子设备上,电子设备上运行有多个应用线程,该多个应用线程包括第一应用线程,第一应用线程持有锁,锁操作方法包括在将所述多个应用线程全部不暂停或部分不暂停的条件下将锁降级。在一些可能的实施方式中,锁包括锁字,锁字定义了锁是重锁、轻锁还是无锁。
在一种可能的实施形式中,锁是重锁。
在一种可能的实施形式中,锁操作方法包括在不将多个应用线程中任一个暂停的条件下,将锁降级为轻锁或者无锁。
在一种可能的实施形式中,锁操作方法包括在暂停第一应用线程的条件下,不暂停多个应用线程中其他应用线程的条件下将锁降级为轻锁或者无锁。
在一种可能的实施形式中,锁操作方法包括在不暂停第一应用线程,暂停多个应用线程中优先级较低的线程的条件下,将锁降级为轻锁或者无锁。
在一种可能的实施形式中,将锁降级包括将锁降级为轻锁和降级为无锁。
在一种可能的实施形式中,在无其他线程在等候锁的条件下执行锁降级。
在一种可能的实施形式中,在没有其他线程已经在所述锁上被阻塞或者调用了java方法wait或sleep的条件下执行锁降级。
在一种可能的实施形式中,在MonitorExit模块中执行锁降级。
根据本申请第二方面,提供了一种电子设备,包括存储器,处理器以及存储在存储器上并可在处理器上运行的计算机程序,其特征在于,处理器执行计算机程序时使得电子设备实现根据第一方面任一实施形式的锁操作方法。
根据本申请第三方面,提供了一种计算机存储介质,包括计算机指令,当计算机指令在电子设备上运行时,使得电子设备执行根据第一方面任一实施形式的锁操作方法。
根据本申请第四方面,提供了一种计算机程序产品,当计算机程序产品在计算机上运行时,使得计算机执行根据第一方面任一实施形式的锁操作方法。
附图说明
图1示出了本申请实施例提供的电子设备100的结构示意图。
图2示出了本发明实施例的电子设备100的软件结构框图。
图3示出了本申请实施例提供锁结构。
图4示出了本申请实施例提供轻锁和重锁机制。
图5A至5F示出了用户体验场景。
图6示出了本申请实施例提供的锁操作方法。
图7示出了本申请实施例提供的锁操作方法。
图8示出了本申请实施例提供的锁操作方法。
图9A和图9B示出了本申请实施例提供的锁操作方法。
图10示出了本申请实施例提供的锁操作方法。
图11示出了本申请实施例提供的锁操作方法。
图12示出了本申请实施例提供的锁操作方法。
图13示出了本申请实施例提供的锁操作方法。
图14示出了本申请实施例提供的锁操作方法。
图15示出了本申请实施例提供的锁操作方法。
图16示出了本申请实施例提供的锁操作方法。
图17示出了本申请实施例提供的锁操作方法。
图18示出了本申请实施例提供的锁操作方法。
图19示出了本申请实施例提供的锁操作方法。
具体实施方式
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行描述。其中,在本申请实施例的描述中,除非另有说明,“/”表示或的意思,例如,A/B可以表示A或B;本文中的“和/或”仅仅是一种描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。另外,在本申请实施例的描述中,“多个”是指两个或多于两个。
以下,术语“第一”、“第二”等仅用于描述目的,而不能理解为指示或暗示相对重要性或者隐含指明所指示的技术特征的数量。由此,限定有“第一”、“第二”等的特征可以明示或者隐含地包括一个或者更多个该特征。在本申请实施例的描述中,除非另有说明,“多个”的含义是两个或两个以上。
为清楚阐述本申请实施例提供的一种Java重锁降级方法,首先将介绍一些后续实施例中可能会出现的概念。
操作系统(operating system,OS),是管理计算机硬件和软件资源的系统软件,为计算机程序提供公共服务。
运行时(runtime),又称运行环境(runtime environment)或者运行时系统(runtime system),是把半编译或全编译的运行码在目标机器上运行的环境。本申请所称的运行时是Java运行时。
进程(process),是应用程序关于某数据集合上的一次运行活动,是操作系统(例如安卓系统)进行资源分配和调度的基本单位。每一个进程都会占用一块地址空间,应用程序以一个或多个进程的形式运行在操作系统上,实现相应的功能。
线程(thread),是进程的一个实体,它是比进程更小的能独立运行的基本单位。线程可与同属一个进程的其他的线程共享该进程所拥有的全部资源。一个线程可以创建和撤销另一个线程,同一个进程中的多个线程之间可以并发执行。
应用线程(application thread),是应用程序在运行中产生的线程,这些线程在创建后由运行时系统挂在一个列表(list)上。应用线程也称用户线程。
同步(synchronized),在java程序运行中,为了保证多线程并发而对对象采用互斥手段,使得同一代码段在同一时间里只被一个线程运行,以避免运行混乱。互斥的手段通常是添加synchronized关键字。Java语言层面的synchronized在被执行时会转换为更底层的监视器进入(monitor enter)和监视器退出(monitor exit),其中monitor enter布置在被保护代码头部, monitor exit布置在被保护代码尾部。这意味着,线程想要运行被添加了synchronized关键字的程序,就必须运行monitor enter以进入对象锁和运行monitor exit以退出对象锁。每个重锁(FatLock)会有自己的monitor标识,即前面提及的MonitorId。所有的monitor会被放在电子设备软件结构的同一个区域中,在一些实施形式中,电子设备中设有用于运行java程序的java虚拟机,则monitor可以存放在java虚拟机的一块特定区域中。
锁(lock),是保证同一时间内只有一个线程访问方法或变量的一种机制。在Java语言中,当一个线程访问一个对象、一段代码时,必须获取该对象所属的锁,否则该线程将等待(或阻塞),直到该锁被释放,并且成功获得该被释放的锁。也就是说,最多只有一个线程能够获得该锁,当线程A尝试去获得线程B持有的对象锁时,线程A必须等待或者阻塞,直到线程B释放这个锁后,线程A才能够获取该锁以访问相应的代码段。
轻锁(ThinLock),线程首次获得此锁时会将其状态由UnLock设置为ThinLock,此后该锁冲突不激烈,或者该线程有重入且其重入次数未超过轻锁LockWord中表示的最大值时此锁会一直保持轻锁状态。当该线程释放此ThinLock时,会将其状态设置为UnLock。
重锁(FatLock),当竞争比较激烈(竞争线程等待时调用sched_yield超过一定次数)、线程自重入次数超过该ThinLock表示的最大值或者调用了wait,sleep等Java方法时,该锁会由ThinLock升级为FatLock。
锁降级(deflation),是将重锁变为轻锁或者无锁。在本申请中也会为了上下文表述顺畅而将锁降级称为降级。
阻塞(block),通常指多线程执行共享代码段时,由于某个线程拿到锁,而其他线程在等待该锁释放而无法进入此代码段的情景。
垃圾回收(garbage collection,GC),是运行时中的garbage collection模块,对当前系统中不使用的对象进行垃圾回收,减少系统内存负担。ART的GC触发条件中包括:内存分配失败;显式调用System.gc()函数;对运行时中的堆(heap)做剪裁(trim)等。
应用程序(application,APP),是可以使得计算设备为计算设备的用户执行任务的应用软件。
system_server:是操作系统中运行服务的一个进程,是基本服务的提供者和系统运行的基本需求。system_server是
Figure PCTCN2020073102-appb-000001
中的命名,其他操作系统中可能有其他命名,但是如果性质是操作系统中运行服务的进程,是基本服务的提供者,则都在本申请的术语system_server的范围中。
本申请实施例提供的锁操作方法运行在电子设备上。本申请实施例提供的锁操作装置布置在电子设备中。对应于市场上的产品,电子设备可以对应于智能电话、便携式电话、游戏机、电视、显示单元、用于车辆的抬头显示单元、笔记本计算机、膝上型计算机、平板个人计算机(PC)、个人媒体播放器(PMP)器、个人数字助理(PDA),机顶盒等等。电子设备也可以实施为具有通信模块的通信终端,通信终端可以与诸如服务器等的外部电子设备通信,或者通过与外部电子设备交互工作来执行操作。只要是具有处理器和存储器,能够运行计算机指令的电子设备都可以运行本申请实施例提供的锁操作方法以及设有本申请实施例提供的锁操作装置。
为了便于理解本申请实施例提供的应用锁操作方法和应用锁操作装置,首先借助图1和图2简要介绍电子设备的示例性结构和电子设备的软件结构的示例性结构。
图1示出了本申请实施例提供的电子设备100的结构示意图。
电子设备100可以包括处理器110,其中处理器110中可以包括存储器。电子设备100还可以选地包括内部存储器121、外部存储器接口120,显示屏194、触摸传感器180K,USB接口130,充电管理模块140,电源管理模块141,电池142,天线1,天线2,移动通信模块150,无线通信模块160,音频模块170,扬声器170A,受话器170B,麦克风170C,耳机接口170D,传感器模块180,按键190,马达191,指示器192,摄像头193,以及SIM卡接口195等。其中传感器模块180可以包括压力传感器180A,陀螺仪传感器180B,气压传感器180C,磁传感器180D,加速度传感器180E,距离传感器180F,接近光传感器180G,指纹传感器180H,温度传感器180J,环境光传感器180L,骨传导传感器180M等。
可以理解的是,本发明实施例示意的结构并不构成对电子设备100的具体限定。在本申请另一些实施例中,电子设备100可以包括比图示更多或更少的部件,或者组合某些部件,或者拆分某些部件,或者不同的部件布置。图示的部件可以以硬件,软件或软件和硬件的组合实现。
处理器110可以包括一个或多个处理单元,例如:处理器110可以包括应用处理器(application processor,AP),调制解调处理器,图形处理器(graphics processing unit,GPU),图像信号处理器(image signal processor,ISP),控制器,存储器,视频编解码器,数字信号处理器(digital signal processor,DSP),基带处理器,和/或神经网络处理器(Neural-network Processing Unit,NPU)等。其中,不同的处理单元可以是独立的器件,也可以集成在一个或多个处理器中。
其中,控制器可以是电子设备100的神经中枢和指挥中心。控制器可以根据指令操作码和时序信号,产生操作控制信号,完成取指令和执行指令的控制。
处理器110中还可以设置存储器,用于存储指令和数据。在一些实施例中,处理器110中的存储器为高速缓冲存储器。该存储器可以保存处理器110刚用过或循环使用的指令或数据。如果处理器110需要再次使用该指令或数据,可从所述存储器中直接调用。避免了重复存取,减少了处理器110的等待时间,因而提高了系统的效率。
在一些实施例中,处理器110可以包括一个或多个接口。接口可以包括集成电路(inter-integrated circuit,I2C)接口,集成电路内置音频(inter-integrated circuit sound,I2S)接口,脉冲编码调制(pulse code modulation,PCM)接口,通用异步收发传输器(universal asynchronous receiver/transmitter,UART)接口,移动产业处理器接口(mobile industry processor interface,MIPI),通用输入输出(general-purpose input/output,GPIO)接口,用户标识模块(subscriber identity module,SIM)接口,和/或通用串行总线(universal serial bus,USB)接口等。
I2C接口是一种双向同步串行总线,包括一根串行数据线(serial data line,SDA)和一根串行时钟线(derail clock line,SCL)。在一些实施例中,处理器110可以包含多组I2C总线。处理器110可以通过不同的I2C总线接口分别耦合触摸传感器180K,充电器,闪光灯,摄像头193等。例如:处理器110可以通过I2C接口耦合触摸传感器180K,使处理器110与触摸传感器180K通过I2C总线接口通信,实现电子设备100的触摸功能。
I2S接口可以用于音频通信。在一些实施例中,处理器110可以包含多组I2S总线。处理器110可以通过I2S总线与音频模块170耦合,实现处理器110与音频模块170之间的通信。 在一些实施例中,音频模块170可以通过I2S接口向无线通信模块160传递音频信号,实现通过蓝牙耳机接听电话的功能。
PCM接口也可以用于音频通信,将模拟信号抽样,量化和编码。在一些实施例中,音频模块170与无线通信模块160可以通过PCM总线接口耦合。在一些实施例中,音频模块170也可以通过PCM接口向无线通信模块160传递音频信号,实现通过蓝牙耳机接听电话的功能。所述I2S接口和所述PCM接口都可以用于音频通信。
UART接口是一种通用串行数据总线,用于异步通信。该总线可以为双向通信总线。它将要传输的数据在串行通信与并行通信之间转换。在一些实施例中,UART接口通常被用于连接处理器110与无线通信模块160。例如:处理器110通过UART接口与无线通信模块160中的蓝牙模块通信,实现蓝牙功能。在一些实施例中,音频模块170可以通过UART接口向无线通信模块160传递音频信号,实现通过蓝牙耳机播放音乐的功能。
MIPI接口可以被用于连接处理器110与显示屏194,摄像头193等外围器件。MIPI接口包括摄像头串行接口(camera serial interface,CSI),显示屏串行接口(display serial interface,DSI)等。在一些实施例中,处理器110和摄像头193通过CSI接口通信,实现电子设备100的拍摄功能。处理器110和显示屏194通过DSI接口通信,实现电子设备100的显示功能。
GPIO接口可以通过软件配置。GPIO接口可以被配置为控制信号,也可被配置为数据信号。在一些实施例中,GPIO接口可以用于连接处理器110与摄像头193,显示屏194,无线通信模块160,音频模块170,传感器模块180等。GPIO接口还可以被配置为I2C接口,I2S接口,UART接口,MIPI接口等。
USB接口130是符合USB标准规范的接口,具体可以是Mini USB接口,Micro USB接口,USB Type C接口等。USB接口可以用于连接充电器为电子设备100充电,也可以用于电子设备100与外围设备之间传输数据。也可以用于连接耳机,通过耳机播放音频。该接口还可以用于连接其他电子设备,例如AR设备等。
可以理解的是,本发明实施例示意的各模块间的接口连接关系,只是示意性说明,并不构成对电子设备100的结构限定。在本申请另一些实施例中,电子设备100也可以采用上述实施例中不同的接口连接方式,或多种接口连接方式的组合。
充电管理模块140用于从充电器接收充电输入。
电子设备100的无线通信功能可以通过天线模块1,天线模块2移动通信模块150,无线通信模块160,调制解调处理器以及基带处理器等实现。
天线1和天线2用于发射和接收电磁波信号。电子设备100中的每个天线可用于覆盖单个或多个通信频带。
移动通信模块150可以提供应用在电子设备100上的包括2G/3G/4G/5G等无线通信的解决方案。在一些实施例中,移动通信模块150的至少部分功能模块可以被设置于处理器110中。在一些实施例中,移动通信模块150的至少部分功能模块可以与处理器110的至少部分模块被设置在同一个器件中。
调制解调处理器可以包括调制器和解调器。其中,调制器用于将待发送的低频基带信号调制成中高频信号。解调器用于将接收的电磁波信号解调为低频基带信号。随后解调器将解调得到的低频基带信号传送至基带处理器处理。
无线通信模块160可以提供应用在电子设备100上的包括无线局域网(wireless local area  networks,WLAN),蓝牙(bluetooth,BT),全球导航卫星系统(global navigation satellite system,GNSS),调频(frequency modulation,FM),近距离无线通信技术(near field communication,NFC),红外技术(infrared,IR)等无线通信的解决方案。
电子设备100通过GPU,显示屏194,以及应用处理器等实现显示功能。GPU为图像处理的微处理器,连接显示屏194和应用处理器。GPU用于执行数学和几何计算,用于图形渲染。处理器110可包括一个或多个GPU,其执行程序指令以生成或改变显示信息。
显示屏194用于显示图像,视频等。显示屏194包括显示面板。显示面板可以采用LCD(liquid crystal display,液晶显示屏),OLED(organic light-emitting diode,有机发光二极管),有源矩阵有机发光二极体或主动矩阵有机发光二极体(active-matrix organic light emitting diode的,AMOLED),柔性发光二极管(flex light-emitting diode,FLED),Miniled,MicroLed,Micro-oLed,量子点发光二极管(quantum dot light emitting diodes,QLED)等。在一些实施例中,电子设备100可以包括1个或N个显示屏,N为大于1的正整数。
外部存储器接口120可以用于连接外部存储卡,例如Micro SD卡,实现扩展电子设备100的存储能力。外部存储卡通过外部存储器接口120与处理器110通信,实现数据存储功能。例如将音乐,视频等文件保存在外部存储卡中。
内部存储器121可以用于存储计算机可执行程序代码,所述可执行程序代码包括指令。处理器110通过运行存储在内部存储器121的指令,从而执行电子设备100的各种功能应用以及数据处理。存储器121可以包括存储程序区和存储数据区。其中,存储程序区可存储操作系统,至少一个功能所需的应用程序(比如声音播放功能,图像播放功能等)等。存储数据区可存储电子设备100使用过程中所创建的数据(比如音频数据,电话本等)等。此外,存储器121可以包括高速随机存取存储器,还可以包括非易失性存储器,例如至少一个磁盘存储器件,闪存器件,通用闪存存储器(universal flash storage,UFS)等。
触摸传感器180K,也称“触控面板”。触摸传感器180K可以设置于显示屏194,由触摸传感器180K与显示屏194组成触摸屏,也称“触控屏”。触摸传感器180K用于检测作用于其上或附近的触摸操作。触摸传感器可以将检测到的触摸操作传递给应用处理器,以确定触摸事件类型。可以通过显示屏194提供与触摸操作相关的视觉输出。在另一些实施例中,触摸传感器180K也可以设置于电子设备100的表面,与显示屏194所处的位置不同。
按键190包括开机键,音量键等。按键可以是机械按键。也可以是触摸式按键。电子设备100可以接收按键输入,产生与电子设备100的用户设置以及功能控制有关的键信号输入。
电子设备100的软件系统可以采用分层架构,事件驱动架构,微核架构,微服务架构,或云架构。本发明实施例以分层架构的
Figure PCTCN2020073102-appb-000002
系统为例,示例性说明电子设备100的软件结构。
图2示出了本发明实施例的电子设备100的软件结构框图。
分层架构将软件分成若干个层,每一层都有清晰的角色和分工。层与层之间通过软件接口通信。在一些实施例中,将
Figure PCTCN2020073102-appb-000003
系统分为四层,从上至下分别为应用程序层,应用程序框架层,安卓运行时(
Figure PCTCN2020073102-appb-000004
runtime)和系统库,以及内核层。电子设备100的操作系统(operating system,OS)包括应用程序框架层,安卓运行时和系统库。
应用程序层可以包括一系列应用程序包。
如图2所示,应用程序包可以包括相机,图库,日历,通话,地图,导航,WLAN,蓝牙, 音乐,视频,短信息等应用程序。
应用程序框架层为应用程序层的应用程序提供应用编程接口(application programming interface,API)和编程框架。应用程序框架层包括一些预先定义的函数。
如图2所示,应用程序框架层可以包括窗口管理器,内容提供器,视图系统,电话管理器,资源管理器,通知管理器等。
窗口管理器用于管理窗口程序。窗口管理器可以获取显示屏大小,判断是否有状态栏,锁定屏幕,截取屏幕等。
内容提供器用来存放和获取数据,并使这些数据可以被应用程序访问。所述数据可以包括视频,图像,音频,拨打和接听的电话,浏览历史和书签,电话簿等。
视图系统包括可视控件,例如显示文字的控件,显示图片的控件等。视图系统可用于构建应用程序。显示界面可以由一个或多个视图组成的。例如,包括短信通知图标的显示界面,可以包括显示文字的视图以及显示图片的视图。
电话管理器用于提供电子设备100的通信功能。例如通话状态的管理(包括接通,挂断等)。
资源管理器为应用程序提供各种资源,比如本地化字符串,图标,图片,布局文件,视频文件等等。
通知管理器使应用程序可以在状态栏中显示通知信息,可以用于传达告知类型的消息,可以短暂停留后自动消失,无需用户交互。比如通知管理器被用于告知下载完成,消息提醒等。通知管理器还可以是以图表或者滚动条文本形式出现在系统顶部状态栏的通知,例如后台运行的应用程序的通知,还可以是以对话窗口形式出现在屏幕上的通知。例如在状态栏提示文本信息,发出提示音,电子设备振动,指示灯闪烁等。
Figure PCTCN2020073102-appb-000005
Runtime包括核心库和虚拟机。
Figure PCTCN2020073102-appb-000006
runtime负责安卓系统的调度和管理。
核心库包含两部分:一部分是java语言需要调用的功能函数,另一部分是安卓的核心库。
应用程序层和应用程序框架层运行在虚拟机中。虚拟机将应用程序层和应用程序框架层的java文件执行为二进制文件。虚拟机用于执行对象生命周期的管理,堆栈管理,线程管理,安全和异常的管理,以及垃圾回收等功能。
系统库可以包括多个功能模块。例如:表面管理器(surface manager),媒体库(Media Libraries),三维图形处理库(例如:OpenGL ES),2D图形引擎(例如:SGL)等。
表面管理器用于对显示子系统进行管理,并且为多个应用程序提供了2D和3D图层的融合。
媒体库支持多种常用的音频,视频格式回放和录制,以及静态图像文件等。媒体库可以支持多种音视频编码格式,例如:MPEG4,H.264,MP3,AAC,AMR,JPG,PNG等。
三维图形处理库用于实现三维图形绘图,图像渲染,合成,和图层处理等。
2D图形引擎是2D绘图的绘图引擎。
内核层是硬件和软件之间的层。内核层至少包含显示驱动,摄像头驱动,音频驱动,传感器驱动。
图3示出了本申请实施例提供的锁结构。一个java object会包括多个区域,例如包括对象头(Header)和实例数据(Instance Data)。Header通常包括对象自身的运行时数据,例如哈希码和UnLock,ThinLock,FatLock分类等。在本实施例中,在Header中留出32位(bit)作为锁字(lockword)区域。但是该lockword的区域不是专用的,也可能保存哈希码等其他信 息,所以在读取和使用lockword信息的时候,应该先确认lockword的状态位。在另一些实施例中,可以将lockword的区域设置为专用的,则读取lockword时候无需再查询里面的内容是不是锁。
锁的类型包括无锁(unlock,也可写作UnLock等,不区分大小写)、轻锁(thinlock,也可写作ThinLock,不区分大小写)和重锁(fatlock,也可写作FatLock,不区分大小写)。类型是通过lockword的结构来体现的。
如图3所示,在无锁中,第一排是任意32bit的lockword,其中左起头两位代表锁状态(lock status)。第二排是无锁的状态。可以看到在无锁中,锁状态用00表示,剩余位都是0,在图中为了简洁写了剩余位的和为0,代表了剩余位都是0。
在轻锁中,第一排是任意32bit的lockword,其中左起头两位代表锁状态(lock status)。第二排是轻锁的状态。可以看到在轻锁中,锁状态用00表示,接下来两位空闲,是保留位,在另一些实施形式中也可以用于哈希标志等。剩余位划分成了重入次数(lock count)和线程标识(thread id)。重入次数代表特定线程在未释放锁的条件下持续进入该锁的次数。线程标识记录的是当前持有该轻锁的线程ID。在该实施例中,分了12位给重入次数,16位给线程标识。在另一些实施例中,也可以分其他数目的位数给重入次数。
在重锁中,第一排是任意32bit的lockword,其中左起头两位代表锁状态。第二排是重锁的状态。可以看到在重锁中,锁状态用01表示,接下来两位空闲,是保留位,在另一些实施形式中也可以用于哈希标志等。剩余位分配给了监视器标识(MonitorId)。每个FatLock有自己的监视器。
图4示出了本申请实施例提供轻锁和重锁机制。一个对象的锁不能同时被一个以上的线程持有,即一个线程拿到对象锁之后,其他线程在它释放该锁之前无法获得该锁。如图4所示,根据T1持有锁的当前状态是轻锁还是重锁来解释T2如何等候该锁。
a)该锁的状态是轻锁
此时当一个线程T2阻塞在该锁上时,T2会通过调用sched_yield来出让cpu占有权,系统会将该线程加到队尾,若没有比它优先级更高的线程,则此线程马上恢复执行,否则让其他线程占有cpu继续执行,T2重新得到cpu时会检测锁状态,若此时T1已经释放锁,T2获得此锁,此时只涉及上下文切换,开销很小,上下文切换指的是去排队时保存寄存器、信号等的状态,目的是切换回来的时候还能够正常执行流程。在该实施例中会计数在T1持有轻锁期间sched_yield被T2调用的次数,如果超过特定阈值,例如50次,说明该锁引起的竞争(contention)比较严重,因此会使得T2将该轻锁改成重锁,但重锁的持有者还是T1。
b)该锁的状态是重锁
如果该锁是重锁,则T2访问该锁时,会通过futex系统调用进入内核并睡眠,内核执行复杂的调度算法,当T1释放锁时又会通过futex系统调用唤醒被block的线程T2。整个过程不仅有上下文切换,还涉及到线程状态的改变、内核调度等昂贵的开销。此外,对于FatLock,其产生、销毁Monitor的操作也有开销。
如果能将生成的重锁在竞争不大的情况下降级为轻锁或无锁,可以节省系统开销,使得被保护的代码段执行速度加快,进而提高电子设备的使用流畅度。已知的一种方法是在分配内存(Allocate Memory)、GC时统一将重锁降级。在例如在分配内存、GC时会针对内存做裁剪(trim),也包括重锁降级。执行锁降级前会将所有由应用程序创建的线程暂停,降级之 后再恢复由应用程序创建的线程执行。此策略容易实现,但是缺陷是需要在暂停虚拟机运行中的由应用程序创建的线程时做,若此时积累了大量需要降级的重锁,则增大了停顿时间,破坏用户体验。而且GC发生的时间是难以预计的,因此难以在GC发生之前提醒用户卡顿即将发生。如图5A至5E所示,可能由于GC时的停顿而导致用户的不良体验。例如,如图5A所示,用户正在使用电子设备与一位社会地位较高的通话对象通电话。通话结束后,用户点击挂机,如图5B所示。接着用户将手机放入口袋,如图5C所示。用户以为挂机成功,于是用激烈的语言表达抒发了情绪,如图5D所示。可是点挂机的同时GC发生,包括通话应用线程在内的所有由应用程序创建的线程被暂停,挂机操作并没有被执行,如图5E所示。用户放到口袋中后GC结束,通话应用线程恢复,所以用户的激烈语言表达被电子设备采集,并且被通话对象听到,从而当用户发现后产生了对电子设备不良的使用体验。从该案例可以看出,如果能够通过不在GC时集中锁降级而缩短时间,会有助于避免用户对电子设备的不良体验。集中锁降级对GC的延长视GC时积累的重锁多少而定,如果GC前运行的线程多,例如在开机过程中,则锁降级耗时较久。锁降级对GC的延时可达到200ms左右。
图6示出了本申请实施例提供的锁操作方法。在电子设备中布置有对象A,例如是一段代码,由应用程序创建的线程T1正持有该对象A的锁。后文中为了表述清楚而可以将对象的锁简称为对象锁。如上文中提及过的,java语言的程序在运行中,为了互斥而可以用synchronized关键字修饰对象,形式例如为synchronized(obj),其中obj即代表对象。针对synchronized语法,java编译器(Java Compiler)会在其保护的代码段的开始和结束分别插入SyncEnter and SyncExit指令。java虚拟机或其他类似于java虚拟机的、将java语言转换为操作系统可懂的语言的运行机构针对SyncEnter and SyncExit指令会有相应的类似MonitorEnter和MonitorExit函数作为底层实现。线程在释放锁时会进入MonitorExit模块。本实施例提供的锁操作方法正是运行在MonitorExit模块中。在执行本实施例提供的锁操作方法的过程中不需要如GC中那样将所有由应用程序创建的线程暂停。
在不暂停所有由应用程序创建的线程的条件下,在步骤601中,检查锁的类型,如果判断为重锁,则执行步骤602,其中判断当前锁对应的对象(Object)没有用于wait、sleep等Java Method,也没有处于冲突状态(即其他线程已经准备在该锁上阻塞),则在步骤603中将锁降级。
步骤601中,可以通过lockword的结构来判断对象锁当前的状态。如果lockword具有如图3中重锁(Fatlock)的结构,即锁头两位是01,后28位是监视器标识,则将其判断为重锁。
在步骤603中,锁降级降级。锁降级包括降级为轻锁和无锁。
图7示出了本申请实施例提供的锁操作方法。电子设备中布置有对象A,例如是一段代码,由应用程序创建的线程T1正持有该对象A的锁,并且该锁的状态是重锁。T2来访问该对象,于是进入MonitorEnter模块,该实施例提供的锁操作方法运行在MonitorEnter模块中。在步骤701中,判断当前对象的锁状态重锁。具体措施是可以是读取lockword,通过其lockword头两位标识以及结构来判断是不是重锁,其含义是先判断是不是重锁,是重锁的情况下允许执行最后一个冒号后面的程序。在步骤702中,加自旋锁(spinlock)进行保护,使得T2以外的其他线程暂时不能再进入自旋锁保护的代码块,代码块包括两块,一块是锁降级保护的代码段,一块是MonitorEnter中准备进入冲突等待的代码段。在步骤703中,再次检查锁的 状态,例如通过lw.GetState()函数来检查lockword从而得知锁状态。
如果在步骤703中检查结果是锁已经被降级,则在步骤704中将自旋锁取消,并且在步骤705中重新读取锁状态,这里可能是UnLock,也可能是ThinLock。
如果在步骤703中检查结果是锁未被降级,则在步骤706中将重锁冲突标志加1。重锁冲突标志表示:对当前线程T1持有该重锁且准备降级时,若此时发现有其他线程T2已经先于T1进入了Monitorenter中由自旋锁保护的代码段并准备在此重锁上冲突等待时,T2会设置该冲突标志,此时T1检测到就不做降级。在步骤707中取消自旋锁的保护。在步骤708中T2进入阻塞状态。一直到T2拿到该对象锁,它才会在步骤709中将重锁冲突标志减1。
在该实施例中,添加自旋锁保护的步骤702,以及取消自旋锁保护的步骤704和707是可选的。
该实施例的示例代码为:
Figure PCTCN2020073102-appb-000007
图8示出了本申请实施例提供的锁操作方法。图8的锁操作方法和图7的锁操作方法作用于同一对象A。图8的锁操作方法在MonitorExit模块中运行。线程T1在释放锁时,于是走到了MonitorExit模块中。在步骤801中,T1检查是否有其他线程在等候该对象锁。例如检查有没有对于T1持有的重锁调用了wait、sleep等java函数的线程,以及没有重锁冲突标志。若没有则进入步骤802。在另一些实施形式中,在步骤801中还检查当前对象锁的lockword是不是被写成了哈希码。如果不是哈希码也没有等候线程则进入步骤802。在步骤802中,添加自旋锁保护,使得在T1执行本锁操作方法中接下来的步骤时,不会有其他线程能够执行由该自旋锁保护的代码段。在步骤803中,检查重锁冲突标志是不是为0。为0意味着没有其他线程在T1准备释放锁时进入了当前对象的MonitorEnter模块且准备阻塞等待。不为0则意味着有其他线程在先于T1降级进入了MonitorEnter中且准备在该重锁上阻塞等待。如果在步骤803中检查的结果是重锁冲突标志不为0,则在步骤804中取消自旋锁保护,该实施例中的锁操作方法流程结果。也就是没有将重锁降级。如果在步骤803中检查的结果是重锁冲突标志为0,则在步骤805中,将重锁降级为无锁。方法例如是将lockword置零。并且在步骤806中将自旋锁取消。在该实施例中,添加自旋锁的步骤802以及取消自旋锁的步骤804 和806是可选的。该实施例的示例性代码为:
Figure PCTCN2020073102-appb-000008
图9A和图9B示出了本申请实施例提供的锁操作方法。图9A和图9B的锁操作方法和图7的锁操作方法作用于同一对象A。图9A和图9B的锁操作方法在MonitorExit模块中运行。线程T1运行了对象A的代码后要释放锁,于是走到了MonitorExit模块中。首先看图9A,在步骤901中,检查当前对象锁的持有者标识是不是运行该锁操作方法的线程。如果当前对象锁的持有者标识不是运行该锁操作方法的线程,说明出错了,通过return函数结束该方法的流程。如果当前对象锁的持有者标识是运行该锁操作方法的线程,在步骤902中检查重入次数是否为0。重入次数记录的是线程T1在未释放当前对象锁的条件下进入该对象的MonitorEnter模块的总次数。Java程序中锁机制的一般运行规则是每走一次MonitorExit,重入次数会减1,一直到减到0,才允许持有锁的线程释放该锁。如果在步骤902中检查到重入次数为0,则在步骤903中将锁的持有者标识置零。在可选的步骤904中检查是否有线程在等候当前对象锁,并且在有线程等候的条件下唤醒等候的线程中的至少一个。如果在步骤902中检查到重入次数不为0,则在步骤905中将重入次数减1。在步骤904或步骤905之后走到步骤906,在步骤906中判断是否有线程已经在该锁上阻塞或者调用了wait,sleep Java方法。若存在则结束该锁操作方法的流程,即不将重锁降级就结束该锁操作方法。图9B示出了步骤907以及后续步骤。在步骤907中,判断锁持有者标识是否是0。如果在步骤907中判断锁持有者标识不是0,则在步骤908中加自旋锁保护。在步骤909中,在重锁冲突标志为0的条件下将重锁降级为轻锁。在步骤910中取消自旋锁的保护。如果在步骤907中判断锁持有者标识是0,则在步骤911中加自旋锁保护。在步骤912中,在重锁冲突标志为0的条件下将重锁降级为无锁。在步骤913中取消自旋锁的保护。在步骤910或步骤913后结束该锁操作方法的流程。添加自旋锁的步骤908和911以及取消自旋锁的步骤910和913是可选的。该实施例中的锁操作方法的示例代码为:
Figure PCTCN2020073102-appb-000009
Figure PCTCN2020073102-appb-000010
Figure PCTCN2020073102-appb-000011
本申请图6、图8和图9A-B的实施例提供的锁操作方法使得能够在线程运行代码的流程中,动态地将没有竞争的重锁降级,每次降级的时间为毫秒级,于是将集中锁降级的大块时间随线程的动作而进行了分散,不会造成电子设备的卡顿,从而维护了用户顺畅的使用体验。例如,对运行有本申请实施例提供的锁操作方法的电子设备与运行有不带该锁操作方法的谷歌art系统的电子设备进行比较。在采用synchronized关键字对对象obj进行修饰后,编译器会将SyncEnter和SyncExit插入到对象的代码首尾,而SyncEnter和SyncExit分别对应底层的MonitorExit函数和MonitorExit函数实现。如下面的代码所示:
原始Java代码:
Statement1;//语句1
synchronized(obj){//用synchronized关键字对对象obj进行修饰
CodeSquence;//代码序列
}
Statement2;//语句2
经过编译synchronized变成对MonitorEnter和MonitorExit的调用:
Statement1;
MonitorEnter(obj){
CodeSquence;
}
MonitorExit(obj)
Statement2;
线程从Statement1到Statment2的执行时间,取决于锁的执行时间和被锁保护的代码序列的执行时间。在比较带有和不带有本申请实施例提供的锁操作方法的电子设备的性能时,主要比较锁降级的次数和重锁的数量,而不主要比较执行时间,因为编译器不同和系统差异等会造成代码序列执行时间的差异。
图10示出了本申请实施例提供的锁操作方法。
当一个线程先于其他线程首次进入MonitorEnter时,便得到了锁,并将锁由初始状态(UnLock)改为ThinLock;
当此线程Reentry再次进入时,LockCount++;若此时LockCount已经超过能够表示的最大值,则将此锁由ThinLock升级为FatLock,
若此时其他线程进入且锁状态为ThinLock,则该线程sched_yield一定次数等待;若等待超过一定次数,则将此锁由ThinLock升级为FatLock;
若为哈希码,则进行升级或保存哈希标志(HashFlag);
在MonitorEnter FatLock场景中,在SpinLock保护下,重新读取LockWord并检测状态,若此时没有其他Thread Deflate,则设置Contention Flag,执行正常的Lock流程;lock函数是拿到锁或者等待。若已经被其他Thread Deflate,则重新走UnLock或ThinLock状态;退出SpinLock保护。
图11示出了本申请实施例提供的锁操作方法。
当线程进入且锁为UnLock状态,则出错,抛异常;
当线程进入且锁为ThinLock状态,则将LockCount--,若LockCount已经为0,则将锁置为UnLock状态;
当线程进入且锁为为哈希码,则进行升级或保存哈希码;
在MonitorExit->Unlock FatLock场景中;检测当前锁Wait Number为空,即没有用于wait、sleep等Java Method的线程,也没有处于冲突;统计Deflate Number,若达到一定次数;在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate的场景存在;若不存在,就执行FatLock到UnLock或ThinLock转换,降级(Deflate)完毕;退出SpinLock保护。
图12至图19的多个实施例示出了本申请可在不将应用线程全部暂停的条件下进行锁降级。
图12示出了本申请实施例提供的锁操作方法。在图12示出的锁操作方法中,在所有应用线程都不用暂停的条件下进行锁降级,降级无锁或轻锁。
在MonitorExit->Unlock FatLock场景中:
1)检测此重锁Owner是当前线程;
2)根据重入次数是否为0来设置owner_id,并在当有线程等候该锁时唤醒等候的线程;
3)检测当前锁NumWaiters为空,即没有用于wait、sleep等Method,也没有已经阻塞到该锁的线程;
4)若此时owner_id为0,即最后一次释放。在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate准备进入MonitorEnter的FatLock冲突场景(fat_lock_count_值),若不存在,就执行FatLock to UnLock转换,Deflate完毕;退出SpinLock保护;
5)若此时owner_id不为0,即还有重入。当重入次数未超过轻锁表示范围时,在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate准备进入MonitorEnter的FatLock冲突场景(fat_lock_count_值),若不存在,就执行FatLock to ThinLock转换,Deflate完毕;退出SpinLock保护。
图12对应的代码例如如下:
Figure PCTCN2020073102-appb-000012
Figure PCTCN2020073102-appb-000013
Figure PCTCN2020073102-appb-000014
图13示出了本申请实施例提供的锁操作方法。图13是图12所示锁操作方法的示例性MonitorEnter场景。
在MonitorEnter FatLock场景中:
1)在SpinLock保护下,重新读取LockWord并检测状态,若此时没有其他Thread将该重锁Deflated,则将ContentionFlag(fat_lock_count_)加一,执行正常的Lock阻塞流程,直到Owner线程释放锁且当前线程获得该锁,然后再将ContentionFlag(fat_lock_count_)减一;
2)若已经被其他Thread Deflated,则重新读取LockWord状态;
3)退出SpinLock保护。
图13对应的代码例如如下:
Figure PCTCN2020073102-appb-000015
对于图12和13所示的实施例,开机时系统进程启动到15秒、45秒和累计5分钟后,分别针对X(未采用)和Y(采用)测试。
1)15秒:
电子设备 轻锁数量 重锁数量 重锁降级次数
X 574224 231660 0
Y 700025 15699 2183
2)45秒:
电子设备 轻锁数量 重锁数量 重锁降级次数
X 691128 285566 0
Y 870103 18499 2850
3)累计5分钟:
电子设备 轻锁数量 重锁数量 重锁降级次数
X 792162 322958 0
Y 1040661 19956 3173
从测试结果可以得出如下结论:
1)Y相比较X产生的重锁减少三十万次以上,减少大量系统开销;
2)从轻重锁的比例上可见Y及时将重锁降级且缓解系统压力;
3)X中如在更晚的时段上发生集中降级,会涉及到暂停、恢复运行时,可能导致严重卡顿。
图14示出了本申请实施例提供的锁操作方法。在图14示出的锁操作方法中在所有应用线程都不用暂停的条件下进行锁降级,且降级为无锁。
在MonitorExit->Unlock FatLock场景中:
1)检测此重锁Owner是当前线程;
2)根据重入次数是否为0来设置owner_id,并在当有线程等候该锁时唤醒等候的线程;
3)检测当前锁NumWaiters为空,即没有用于wait、sleep等Method,也没有已经阻塞到该锁的线程;
4)若此时owner_id为0,即最后一次释放,并且当前锁不做为哈希码。在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate准备进入MonitorEnter的FatLock冲突场景(fat_lock_count_值),若不存在,就执行FatLock to UnLock转换,Deflate完毕;退出SpinLock保护。
图14对应的代码如下:
Figure PCTCN2020073102-appb-000016
Figure PCTCN2020073102-appb-000017
图15示出了本申请实施例提供的锁操作方法。图15示出了图14的锁操作方法的示例性MonitorEnter场景。
在MonitorEnter FatLock场景中:
1)在SpinLock保护下,重新读取LockWord并检测状态,若此时没有其他Thread将该重锁Deflated,则将冲突标志(fat_lock_count_)加一,执行正常的Lock阻塞流程,直到Owner线程释放锁且当前线程获得该锁,然后再将冲突标志(fat_lock_count_)减一;
2)若已经被其他Thread Deflated,则重新读取LockWord状态;
3)退出SpinLock保护。
图15对应的代码如下:
Figure PCTCN2020073102-appb-000018
Figure PCTCN2020073102-appb-000019
图16示出了本申请实施例提供的锁操作方法。在图16示出的锁操作方法中,在仅暂停owner线程,即当前持锁的线程的条件下进行锁降级。Owner线程以外的其他应用线程不必暂停。
在MonitorExit->Unlock FatLock场景中:
1)检测此重锁Owner是当前线程;
2)根据重入次数是否为0来设置owner_id,并在当有线程等候该锁时唤醒等候的线程;
3)检测当前锁NumWaiters为空,即没有用于wait、sleep等Method,也没有已经阻塞到该锁的线程;
4)若此时owner_id为0,即最后一次释放。在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate准备进入MonitorEnter的FatLock冲突场景(fat_lock_count_值),若不存在,先将自己暂停,睡眠一会(如0.1毫秒),然后执行FatLock to UnLock转换,Deflate完毕;退出SpinLock保护;
5)若此时owner_id不为0,即还有重入。当重入次数未超过轻锁表示范围时,在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate准备进入MonitorEnter的FatLock冲突场景(fat_lock_count_值),若不存在,先将自己暂停,睡眠一会(如0.1毫秒),然后执行FatLock to ThinLock转换,Deflate完毕;退出SpinLock保护。
图16对应的代码如下:
Figure PCTCN2020073102-appb-000020
Figure PCTCN2020073102-appb-000021
Figure PCTCN2020073102-appb-000022
图17示出了本申请实施例提供的锁操作方法。图17示出了图16的锁操作方法的示例性MonitorEnter场景。
在MonitorEnter FatLock场景中:
1)在SpinLock保护下,重新读取LockWord并检测状态,若此时没有其他Thread将该重锁Deflated,则将ContentionFlag(fat_lock_count_)加一,执行正常的Lock阻塞流程,直到Owner线程释放锁且当前线程获得该锁,然后再将ContentionFlag(fat_lock_count_)减一;
2)若已经被其他Thread Deflated,则重新读取LockWord状态;
3)退出SpinLock保护。
图17对应的代码如下:
Figure PCTCN2020073102-appb-000023
图18示出了本申请实施例提供的锁操作方法。在图18示出的锁操作方法中,在不暂停owner线程,而暂停其他应用线程中部分应用线程的条件下进行锁降级。Owner线程指的是当前持有锁的应用线程。
在MonitorExit->Unlock FatLock场景中:
1)检测此重锁Owner是当前线程;
2)根据重入次数是否为0来设置owner_id,并在当有线程等候该锁时唤醒等候的线程;
3)检测当前锁NumWaiters为空,即没有用于wait、sleep等Method,也没有已经阻塞到该锁的线程;
4)若此时owner_id为0,即最后一次释放。在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate准备进入MonitorEnter的FatLock冲突场景(fat_lock_count_值),若不存在,先将其他线程中优先级比较低的线程暂停(可能是1个也可能是多个),然后执行FatLock to UnLock转换,Deflate完毕;将之前被暂停的线程恢复执行,退出SpinLock保护;
5)若此时owner_id不为0,即还有重入。当重入次数未超过轻锁表示范围时,在SpinLock保护下,检测是否有其他Thread先于当前Thread Deflate准备进入MonitorEnter的FatLock冲突场景(fat_lock_count_值),若不存在,先将其他线程中优先级比较低的线程暂停(可能是1个也可能是多个),然后执行FatLock to ThinLock转换,Deflate完毕;将之前被暂停的线程恢复执行,退出SpinLock保护。
图18对应的代码如下:
Figure PCTCN2020073102-appb-000024
Figure PCTCN2020073102-appb-000025
图19示出了本申请实施例提供的锁操作方法。图19示出了图18的锁操作方法的示例性MonitorEnter场景。
在MonitorEnter FatLock场景中:
1)在SpinLock保护下,重新读取LockWord并检测状态,若此时没有其他Thread将该重锁Deflated,则将ContentionFlag(fat_lock_count_)加一,执行正常的Lock阻塞流程,直到Owner线程释放锁且当前线程获得该锁,然后再将ContentionFlag(fat_lock_count_)减一;
2)若已经被其他Thread Deflated,则重新读取LockWord状态;
3)退出SpinLock保护。
图19对应的代码如下:
Figure PCTCN2020073102-appb-000026
本申请的实施例还提供了一种电子设备,包括存储器,处理器以及存储在存储器上并可在处理器上运行的计算机程序,其特征在于,处理器执行计算机程序时使得电子设备实现本申请任一实施例提供的锁操作方法。
本申请的实施例还提供了一种计算机存储介质,包括计算机指令,当计算机指令在电子设备上运行时,使得电子设备执行本申请任一实施例提供的锁操作方法。
本申请的实施例还提供了一种计算机程序产品,当计算机程序产品在计算机上运行时,使得计算机执行本申请任一实施例提供的锁操作方法。
上述实施例中,可以全部或部分地通过软件、硬件、固件或者其任意组合来实现。当使用软件实现时,可以全部或部分地以计算机程序产品的形式实现。所述计算机程序产品可以包括一个或多个计算机指令。在计算机上加载和执行所述计算机程序指令时,全部或部分地产生按照本申请实施例所述的流程或功能。所述计算机可以是通用计算机、专用计算机、计算机网络或者其他可编程装置。所述计算机指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一个计算机可读存储介质传输,例如,所述计算机指令可以从一个网站站点、计算机、服务器或数据中心通过有线(例如同轴电缆、光纤、数字用户(DSL))或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、服务器或数据中心进行传输。所述计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含一个或多个可用介质集成的服务器、数据中心等数据存储设备。所述可用介质可以是磁性介质(例如,软盘、硬盘、磁盘)、光介质(例如,DVD)、或者半导体介质(例如固态硬盘Solid State Disk(SSD))等。
本领域普通技术人员可以意识到,结合本文中所公开的实施例描述的各示例的单元及算法步骤,能够以电子硬件、或者计算机软件和电子硬件的结合来实现。这些功能究竟以硬件还是软件方式来执行,取决于技术方案的特定应用和设计约束条件。专业技术人员可以对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本申请的范围。
所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,上述描述的系统、装置 和单元的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。
在本申请所提供的几个实施例中,应该理解到,所揭露的系统、装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个系统,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。
所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。
另外,在本申请各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。
所述功能如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本申请各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器、随机存取存储器、磁碟或者光盘等各种可以存储程序代码的介质。
以上所述,仅为本申请的具体实施方式,但本申请的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本申请揭露的技术范围内,可轻易想到变化或替换,都应涵盖在本申请的保护范围之内。因此,本申请的保护范围应所述以权利要求的保护范围为准。

Claims (12)

  1. 一种锁操作方法,所述锁操作方法运行在电子设备上,所述电子设备上运行有多个应用线程,所述多个应用线程包括第一应用线程,所述第一应用线程持有锁,所述锁操作方法包括在将所述多个应用线程全部不暂停或部分不暂停的条件下将所述锁降级。
  2. 根据权利要求1所述的锁操作方法,其特征在于,所述锁是重锁。
  3. 根据权利要求1或2所述的锁操作方法,其特征在于,所述锁操作方法包括在不将所述多个应用线程中任一个暂停的条件下,将所述锁降级为轻锁或者无锁。
  4. 根据权利要求1或2所述的锁操作方法,其特征在于,所述锁操作方法包括在暂停所述第一应用线程的条件下,不暂停所述多个应用线程中其他应用线程的条件下将所述锁降级为轻锁或者无锁。
  5. 根据权利要求1或2所述的锁操作方法,其特征在于,所述锁操作方法包括在不暂停所述第一应用线程,暂停所述多个应用线程中优先级较低的线程的条件下,将所述锁降级为轻锁或者无锁。
  6. 根据权利要求1或2所述的锁操作方法,其特征在于,将所述锁降级包括将所述锁转降级为轻锁和降级为无锁。
  7. 根据权利要求1或2所述的锁操作方法,其特征在于,在无其他线程在等候所述锁的条件下执行所述锁降级。
  8. 根据权利要求1或2所述的锁操作方法,其特征在于,在没有其他线程已经在所述锁上被阻塞或者调用了java方法wait或sleep的条件下执行所述锁降级。
  9. 根据上述权利要求中任一项所述的锁操作方法,其特征在于,在MonitorExit模块中执行锁降级。
  10. 一种电子设备,包括存储器,处理器以及存储在存储器上并可在处理器上运行的计算机程序,其特征在于,处理器执行计算机程序时使得电子设备实现根据权利要求1至9中任一项所述的锁操作方法。
  11. 一种计算机存储介质,包括计算机指令,当计算机指令在电子设备上运行时,使得电子设备执行根据权利要求1至9中任一项所述的锁操作方法。
  12. 一种计算机程序产品,当计算机程序产品在计算机上运行时,使得计算机执行根据权利要求1至9中任一项所述的锁操作方法。
PCT/CN2020/073102 2019-01-20 2020-01-20 分散式重锁降级 WO2020147859A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201910050640.6 2019-01-20
CN201910050640.6A CN111459462B (zh) 2019-01-20 2019-01-20 分散式重锁降级

Publications (1)

Publication Number Publication Date
WO2020147859A1 true WO2020147859A1 (zh) 2020-07-23

Family

ID=71614132

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/073102 WO2020147859A1 (zh) 2019-01-20 2020-01-20 分散式重锁降级

Country Status (2)

Country Link
CN (1) CN111459462B (zh)
WO (1) WO2020147859A1 (zh)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114296862A (zh) * 2021-09-29 2022-04-08 苏州浪潮智能科技有限公司 一种基于JVM Markword的Synchronized锁降级方法、设备及介质

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2001188685A (ja) * 1999-12-27 2001-07-10 Internatl Business Mach Corp <Ibm> オブジェクトのロック管理方法及び装置
US7886300B1 (en) * 2006-09-26 2011-02-08 Oracle America, Inc. Formerly Known As Sun Microsystems, Inc. Mechanism for implementing thread synchronization in a priority-correct, low-memory safe manner
CN103246552A (zh) * 2012-02-14 2013-08-14 腾讯科技(深圳)有限公司 防止线程出现阻塞的方法和装置

Family Cites Families (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6247025B1 (en) * 1997-07-17 2001-06-12 International Business Machines Corporation Locking and unlocking mechanism for controlling concurrent access to objects
JP3737638B2 (ja) * 1998-08-31 2006-01-18 インターナショナル・ビジネス・マシーンズ・コーポレーション オブジェクトのロック管理方法及び装置、並びにオブジェクトのロック解除方法及び装置
US6542891B1 (en) * 1999-01-29 2003-04-01 International Business Machines Corporation Safe strength reduction for Java synchronized procedures
US6546443B1 (en) * 1999-12-15 2003-04-08 Microsoft Corporation Concurrency-safe reader-writer lock with time out support
US6735760B1 (en) * 2000-11-08 2004-05-11 Sun Microsystems, Inc. Relaxed lock protocol
US7823150B2 (en) * 2005-01-25 2010-10-26 International Business Machines Corporation Computer-implemented method, system and program product for establishing multiple read-only locks on a shared data object
US7747996B1 (en) * 2006-05-25 2010-06-29 Oracle America, Inc. Method of mixed lock-free and locking synchronization

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2001188685A (ja) * 1999-12-27 2001-07-10 Internatl Business Mach Corp <Ibm> オブジェクトのロック管理方法及び装置
US7886300B1 (en) * 2006-09-26 2011-02-08 Oracle America, Inc. Formerly Known As Sun Microsystems, Inc. Mechanism for implementing thread synchronization in a priority-correct, low-memory safe manner
CN103246552A (zh) * 2012-02-14 2013-08-14 腾讯科技(深圳)有限公司 防止线程出现阻塞的方法和装置

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
ANONYMOUS: "About JVM Thin Lock, Fat Lock, SPIN Lock and Tasuki Lock of JVM", CSDN, 9 June 2011 (2011-06-09), pages 1 - 4, XP055719663 *

Also Published As

Publication number Publication date
CN111459462A (zh) 2020-07-28
CN111459462B (zh) 2023-05-09

Similar Documents

Publication Publication Date Title
KR102148948B1 (ko) 전자 장치의 멀티 태스킹 방법 및 그 전자 장치
WO2021083378A1 (zh) 一种加速应用程序启动的方法及电子设备
WO2021057643A1 (zh) 一种多线程同步方法及电子设备
US11385935B2 (en) Task scheduling method and electronic device for implementing same
EP4145286A1 (en) Memory management method and electronic device
WO2022199509A1 (zh) 应用执行绘制操作的方法及电子设备
WO2021190354A1 (zh) 一种采集追踪trace调用链的方法和电子设备
US20150128079A1 (en) Method for executing function in response to touch input and electronic device implementing the same
AU2015350680A1 (en) Power control method and apparatus for reducing power consumption
WO2021223539A1 (zh) 射频资源分配方法及装置
CN115629884B (zh) 一种线程调度方法、电子设备及存储介质
WO2019128588A1 (zh) 进程处理方法和装置、电子设备、计算机可读存储介质
WO2021185352A1 (zh) 一种版本升级方法及相关装置
WO2023051355A1 (zh) 权限检查的方法和电子设备
WO2022078105A1 (zh) 内存管理方法、电子设备以及计算机可读存储介质
CN115292199B (zh) 一种显存泄露的处理方法及相关装置
WO2023202429A1 (zh) 垃圾回收的方法及电子设备
WO2019128571A1 (zh) 资源管理方法、装置、移动终端及计算机可读存储介质
CN115016631B (zh) 进程调度方法和终端设备
WO2020147859A1 (zh) 分散式重锁降级
WO2021169379A1 (zh) 权限复用方法、基于权限复用的资源访问方法及相关设备
CN111381996B (zh) 内存异常处理方法及装置
WO2023024900A1 (zh) 安全启动校验的方法及电子设备
US10545562B2 (en) Electronic device and method for operating the same
WO2021238376A1 (zh) 功能包的加载方法、装置、服务器和电子设备

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20741277

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20741277

Country of ref document: EP

Kind code of ref document: A1