WO2021057643A1 - Multi-thread synchronization method and electronic device - Google Patents

Multi-thread synchronization method and electronic device Download PDF

Info

Publication number
WO2021057643A1
WO2021057643A1 PCT/CN2020/116437 CN2020116437W WO2021057643A1 WO 2021057643 A1 WO2021057643 A1 WO 2021057643A1 CN 2020116437 W CN2020116437 W CN 2020116437W WO 2021057643 A1 WO2021057643 A1 WO 2021057643A1
Authority
WO
WIPO (PCT)
Prior art keywords
thread
lock
target
field
holding
Prior art date
Application number
PCT/CN2020/116437
Other languages
French (fr)
Chinese (zh)
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 华为技术有限公司
Priority to EP20869498.4A priority Critical patent/EP4020210A4/en
Priority to US17/763,490 priority patent/US20220350602A1/en
Publication of WO2021057643A1 publication Critical patent/WO2021057643A1/en

Links

Images

Classifications

    • 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/30Arrangements for executing machine instructions, e.g. instruction decode
    • G06F9/30003Arrangements for executing specific machine instructions
    • G06F9/30076Arrangements for executing specific machine instructions to perform miscellaneous control operations, e.g. NOP
    • G06F9/3009Thread control 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/30Arrangements for executing machine instructions, e.g. instruction decode
    • G06F9/38Concurrent instruction execution, e.g. pipeline or look ahead
    • G06F9/3854Instruction completion, e.g. retiring, committing or graduating
    • G06F9/3858Result writeback, i.e. updating the architectural state or memory
    • G06F9/38585Result writeback, i.e. updating the architectural state or memory with result invalidation, e.g. nullification
    • 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
    • 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/526Mutual exclusion algorithms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/50Indexing scheme relating to G06F9/50
    • G06F2209/5018Thread allocation

Definitions

  • This application relates to the field of terminal technology, and in particular to a multi-thread synchronization method and electronic equipment.
  • the Android operating system is usually used in electronic devices such as mobile phones and tablet computers to provide a unified interface and a friendly interactive interface for users to use electronic devices, and can support multi-threaded operation.
  • shared resources such as shared memory, shared code, shared data, etc.
  • the shared resources are protected by locks. Take lock 1 to protect shared resource 1 as an example. In multithreading, only the thread holding lock 1 can access shared resource 1. Once a thread acquires lock 1, other threads can no longer hold lock 1, that is, lock 1 can only be held by one thread at the same time. Have. Only when the thread holding lock 1 releases lock 1, other threads get the opportunity to hold lock 1.
  • the embodiments of the present application provide a multi-thread synchronization method and an electronic device, which help reduce the length of time that a lock-holding thread accesses a shared resource, thereby reducing the probability of the electronic device being stuck and improving the user experience.
  • the first aspect is a multi-thread synchronization method according to an embodiment of this application, which specifically includes: a first thread requests to acquire a target lock.
  • the first thread requests to acquire the target lock, including:
  • Step 1 The first thread acquires the lock state variable of the target lock.
  • the lock state variable includes the lock-holding thread identification field and the blocked thread number field.
  • the lock-holding thread identification field is used to indicate the lock-holding thread of the target lock
  • the blocked thread number field is used To indicate the number of threads that are blocked for the target lock;
  • Step 2 The first thread checks the lock thread identification field
  • Step 3 If the first thread checks that the lock-holding thread identification field is a valid thread and is not the first thread, the first thread checks the blocked thread number field;
  • Step 4 If the first thread checks that the number of blocked threads is less than the first threshold, the first thread performs spin waiting;
  • Step 5 When the number of spin waits for the first thread reaches the second threshold, if the check that the lock-holding thread identification field is a valid thread and not the first thread, the first thread performs an operation of incrementing the number field of blocked threads and hangs. Start to enter the blocking state.
  • the lock state variable of the target lock includes the lock thread identification field and the blocked thread number field
  • the blocked thread number field is used to indicate the number of threads in the blocked state for the target lock
  • the thread field is modified so that when the number of spin waits for the first thread reaches the second threshold, if the lock-holding thread identification field is checked as a valid thread other than the first thread, the first thread does not need to suspend the lock-holding thread , Without allocating montior objects, it is possible to add 1 to the blocked thread number field, which helps to reduce the length of time that the lock-holding thread accesses shared resources, thereby reducing the probability of electronic device jams and improving user experience.
  • step three it also includes:
  • Step 6 If the first thread checks that the blocked thread number field is greater than or equal to the first threshold, the first thread performs an operation of adding 1 to the blocked thread number field and suspends and enters the blocking state.
  • the lock state variable also includes a repeated lock count field, and the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock.
  • the lock state variable can record the number of locks held by the lock-holding thread for the target lock, which helps reduce the possibility of exceptions when the lock-holding thread releases the lock.
  • step two it also includes:
  • Step 7 If the first thread checks that the lock-holding thread identification field is the first thread, the first thread performs an operation of adding 1 to the repeated lock-holding times field. Since the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock, and the first thread adds 1 to the repeated lock count field, there is no need to allocate a monitor object, which helps to improve the running performance of the first thread. Efficiency, further reducing the probability of electronic equipment jams, thereby improving user experience.
  • step two it also includes:
  • Step 8 If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks field to the initial value.
  • the method further includes: the first thread requests to release the target lock.
  • the first thread requests to release the target lock, including:
  • Step 9 The first thread acquires the lock state variable of the target lock:
  • Step 10 The first thread checks the lock thread identification field
  • Step 11 If the first thread checks the lock-holding thread identification field as the first thread, the first thread checks the repeated lock-holding times field;
  • Step 12 If the first thread checks that the repeated lock count field is not 0, the first thread performs an operation to subtract 1 from the repeated lock count field.
  • step twelve it also includes:
  • Step 13 If the first thread checks that the repeated lock count field is 0, the first thread sets the lock thread identification field as an invalid thread
  • Step 14 The first thread checks the blocked thread count field
  • Step 15 If the first thread checks that the blocked thread count field is not 0, the first thread wakes up at least one thread that is blocked for the target lock.
  • the first thread After the first thread releases control of the target lock, it can also wake up at least one thread that is blocked for the target lock, thereby increasing the possibility that the thread locks the target and improving the efficiency of thread operation.
  • the method further includes: the first thread requests to suspend the lock on the target.
  • the first thread's request to suspend the lock on the target includes:
  • Step 16 The first thread acquires the lock state variable of the target lock
  • Step 17 The first thread checks the lock thread identification field
  • Step 18 If the first thread checks that the lock-holding thread identification field is the first thread, the first thread saves the repeated lock-holding times field as the context of the first thread for the target lock;
  • Step 19 The first thread adds the thread identification ID of the first thread to the thread waiting list corresponding to the ID of the target lock;
  • Step 20 The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field to be an invalid thread, and suspends and enters a blocking state.
  • the first thread can add the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock, it is convenient for the first thread to be awakened by other threads or systems.
  • step 20 it also includes:
  • Step 21 After the first thread is awakened, acquire the lock state variable of the target lock
  • Step 22 The first thread checks the lock thread identification field
  • Step 23 If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks according to the context of the first thread for the target lock area;
  • Step 24 The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
  • the first thread can be awakened in the following manner to acquire the lock state variable of the target lock:
  • the first thread After the first thread is awakened by the second thread, it acquires the lock state variable of the target lock.
  • the second thread wakes up the second thread when the thread ID of the first thread is stored in the thread waiting list corresponding to the ID of the target lock. Threaded; the second thread belongs to the same process as the first thread; or,
  • the first thread is awakened by the system when the suspended duration exceeds the third threshold, and acquires the lock state variable of the target lock.
  • the blocked thread number field includes a first value and a high-order flag of the number of blocked threads.
  • the first value is the low-order value of the number of threads in a blocked state for the target lock
  • the high-order flag of the number of blocked threads is used to indicate Whether the second value corresponding to the ID of the target lock is stored in the high list of the number of blocked threads, and the second value is the high value of the number of threads in the blocked state for the target lock. This helps to avoid overflow when the number of threads that are blocked for the target lock is large.
  • the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks.
  • the third value is the low value of the number of locks held by the lock thread for the target lock, and the number of repeated locks is high.
  • the identifier is used to indicate whether the high-order list of the number of repeated locks stores a fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread.
  • the fourth value is the high-order value of the number of times the lock-holding thread holds the lock for the target lock. value. This helps to avoid overflow when the lock-holding thread holds more locks for the target lock.
  • the first thread can suspend and enter the blocking state based on the following methods:
  • the first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock. This helps simplify the implementation.
  • the second aspect is an electronic device according to an embodiment of this application, which specifically includes a processor and a memory, where program instructions are stored; the processor calls the program instructions stored in the memory for the first thread to request to acquire the target lock;
  • the first thread requests to acquire the target lock, including:
  • Step 1 The first thread acquires the lock state variable of the target lock.
  • the lock state variable includes the lock-holding thread identification field and the blocked thread number field.
  • the lock-holding thread identification field is used to indicate the lock-holding thread of the target lock
  • the blocked thread number field is used To indicate the number of threads that are blocked for the target lock;
  • Step 2 The first thread checks the lock thread identification field:
  • Step 3 If the first thread checks that the lock-holding thread identification field is a valid thread and is not the first thread, the first thread checks the blocked thread number field;
  • Step 4 If the first thread checks that the number of blocked threads is less than the first threshold, the first thread performs spin waiting;
  • Step 5 When the number of spin waits for the first thread reaches the second threshold, if the check that the lock-holding thread identification field is a valid thread and not the first thread, the first thread performs an operation of incrementing the number field of blocked threads and hangs. Start to enter the blocking state.
  • step three it also includes:
  • Step 6 If the first thread checks that the blocked thread number field is greater than or equal to the first threshold, the first thread performs an operation of adding 1 to the blocked thread number field and suspends and enters the blocking state.
  • the lock state variable of the target lock also includes a repeated lock count field, and the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock.
  • step two it also includes:
  • Step 7 If the first thread checks that the lock-holding thread identification field is the first thread, the first thread performs an operation of adding 1 to the repeated lock-holding times field.
  • step two it also includes:
  • Step 8 If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks field to the initial value.
  • the processor calls the program instructions, which are also used by the first thread to request the release of the target lock:
  • the first thread requests to release the target lock, including:
  • Step 9 The first thread acquires the lock state variable of the target lock:
  • Step 10 The first thread checks the lock thread identification field
  • Step 11 If the first thread checks the lock-holding thread identification field as the first thread, the first thread checks the repeated lock-holding times field;
  • Step 12 If the first thread checks that the repeated lock count field is not 0, the first thread performs an operation to subtract 1 from the repeated lock count field.
  • step twelve it also includes:
  • Step 13 If the first thread checks that the repeated lock count field is 0, the first thread sets the lock thread identification field as an invalid thread
  • Step 14 The first thread checks the blocked thread count field
  • Step 15 If the first thread checks that the blocked thread count field is not 0, the first thread wakes up at least one thread that is blocked for the target lock.
  • the processor calls the program instructions, which are also used by the first thread to request the suspension of the lock on the target:
  • the first thread's request to suspend the lock on the target includes:
  • Step 16 The first thread acquires the lock state variable of the target lock
  • Step 17 The first thread checks the lock thread identification field
  • Step 18 If the first thread checks that the lock-holding thread identification field is the first thread, save the repeated lock-holding times field as the context of the first thread for the target lock;
  • Step 19 The first thread adds the thread identification ID of the first thread to the thread waiting list corresponding to the ID of the target lock;
  • Step 20 The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field to be an invalid thread, and suspends and enters a blocking state.
  • step 20 it also includes:
  • Step 21 After the first thread is awakened, acquire the lock state variable of the target lock
  • Step 22 The first thread checks the lock thread identification field
  • Step 23 If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks according to the context of the first thread for the target lock area;
  • Step 24 The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
  • the first thread can be awakened based on the following methods, and then acquire the lock state variable of the target lock:
  • the first thread When the first thread is awakened by the second thread, it acquires the lock state variable of the target lock.
  • the second thread wakes up the second thread when the thread ID of the first thread is stored in the thread waiting list corresponding to the ID of the target lock. Threaded; the second thread belongs to the same process as the first thread; or,
  • the first thread is awakened by the system when the suspended duration exceeds the third threshold, and acquires the lock state variable of the target lock.
  • the blocked thread number field includes a first value and a high-order flag of the number of blocked threads.
  • the first value is the low-order value of the number of threads in a blocked state for the target lock
  • the high-order flag of the number of blocked threads is used to indicate Whether the second value corresponding to the ID of the target lock is stored in the high list of the number of blocked threads, and the second value is the high value of the number of threads in the blocked state for the target lock.
  • the field of the number of repeated locks includes a third value and a high flag for the number of repeated locks.
  • the third value is the low value of the number of locks held by the lock thread for the target lock, and the number of repeated locks is high.
  • the identifier is used to indicate whether the high-order list of the number of repeated locks stores the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread.
  • the fourth value is the high-order number of the lock-holding thread for the target lock. value.
  • the first thread can suspend and enter the blocking state based on the following methods:
  • the first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock.
  • an electronic device provided by an embodiment of the present application includes a device for executing the foregoing first aspect and any possible design method involved in the first aspect of the embodiment of the present application.
  • a chip provided by an embodiment of the present application includes a processor and an interface, configured to call and run the program instructions stored in the memory from the memory, and execute the above-mentioned first aspect and the first aspect of the embodiment of the present application. Any possible design method involved in the aspect.
  • a computer-readable storage medium stores program instructions.
  • the program instructions run on an electronic device, the electronic device executes the above-mentioned On the one hand and any possible design method involved in the first aspect.
  • a computer program product of an embodiment of the present application when the computer program product runs on an electronic device, causes the electronic device to execute and implement the above-mentioned first aspect and any of the first aspects of the embodiments of the present application.
  • a possible design method when the computer program product runs on an electronic device, causes the electronic device to execute and implement the above-mentioned first aspect and any of the first aspects of the embodiments of the present application.
  • FIG. 1 is a schematic diagram of the format of a lock state variable according to an embodiment of the application
  • FIG. 2 is a schematic diagram of the hardware structure of an electronic device according to an embodiment of the application.
  • FIG. 3 is a schematic diagram of the software structure of an electronic device according to an embodiment of the application.
  • FIG. 4 is a schematic diagram of a storage format of the high-order value of the number of blocked threads according to an embodiment of the application
  • FIG. 5 is a schematic diagram of a storage format of the high-order value of the number of repeated locks according to an embodiment of the application.
  • FIG. 6 is a schematic diagram of the format of another lock state variable according to an embodiment of the application.
  • FIG. 7 is a schematic diagram of a storage format of a hash value according to an embodiment of the application.
  • FIG. 8 is a schematic flowchart of a multi-thread synchronization method according to an embodiment of the application.
  • FIG. 9 is a schematic flowchart of a process of adding 1 to the number field of blocked threads according to an embodiment of the present application.
  • FIG. 10 is a schematic diagram of a flow chart of performing an operation of adding 1 to the repeated lock count field according to an embodiment of the present application.
  • FIG. 11 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application.
  • FIG. 12 is a schematic diagram of a flow chart of performing a minus 1 operation on the repeated lock count field according to an embodiment of the application.
  • FIG. 13 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application.
  • FIG. 14 is a schematic diagram of a storage format of a thread waiting list according to an embodiment of the application.
  • 15 is a schematic diagram of a process of deleting a thread ID from a thread waiting list according to an embodiment of the application
  • 16 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application.
  • FIG. 17 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application.
  • FIG. 18 is a schematic diagram of another format of a lock state variable according to an embodiment of the application.
  • FIG. 19 is a schematic structural diagram of another electronic device according to an embodiment of the application.
  • At least one in the embodiments of the present application refers to one or more.
  • “Multiple” means two or more.
  • “And/or” describes the association relationship of the associated objects, indicating that there can be three types of relationships.
  • a and/or B can mean that: A alone exists, A and B exist at the same time, and B exists alone. Among them, A and B can be singular or plural.
  • the character “/” generally indicates that the associated objects before and after are in an “or” relationship.
  • “The following at least one (item)” or similar expressions refers to any combination of these items, including any combination of single item (item) or plural items (item).
  • At least one item (a) of a, b, or c can represent: a, b, c, a and b, a and c, b and c, or a, b and c.
  • each of a, b, and c can be an element itself, or a collection containing one or more elements.
  • the electronic device executes the corresponding program through the operating system (for example, the Android operating system), so that the user can use the electronic device to make calls, send short messages, browse the web, take photos, play games, watch videos, and so on.
  • the operating system of an electronic device can support multi-threaded operation, thereby realizing parallel processing of multi-tasks and improving task processing efficiency.
  • shared resources for example, shared code, shared memory, shared data or variables, etc.
  • shared resources are protected by locks. Take lock 1 to protect shared resource 1 as an example. In multithreading, only the thread holding lock 1 can access shared resource 1. Once lock 1 is held by a thread, other threads can no longer hold lock 1 and only after the lock holding thread releases lock 1 , Other threads get the opportunity to hold lock 1.
  • threads use a lock expansion strategy to compete for locks. Specifically, the thread realizes the competition to hold the lock according to the lock state variable (lockword). In other words, the thread judges whether it can hold the lock 1 according to the lock state variable.
  • lock state variable lockword
  • the format of the lockword is shown in FIG. 1, and includes a status bit, a reserved bit (reserve), a field for the number of repeated locks, and a lock thread identification field.
  • the status bit is used to indicate the lock status.
  • the information indicated by the repeated lock count field and the lock thread identification field of the lockword is related to the lock state. For example, when the status bit is set to 01, it indicates the relock state, and when the status bit is set to 01, the repeated lock count field and the lock thread identification field are used to indicate the monitor object; when the status bit is 00, the light lock state is used. In the case that the status bit is set to 00, the number of repeated locks field is used to indicate the number of locks held by the lock-holding thread for the lock, and the lock-holding thread identification field is used to indicate the lock-holding thread of the lock.
  • Thread 1 requests to acquire lock 1, which specifically includes the following steps:
  • thread 1 acquires the lockword.
  • thread 1 checks the lock-holding thread ID field.
  • thread 1 checks the lock-holding thread identification field to be a thread other than thread 1, thread 1 continues to check the status bit.
  • the thread 1 If the thread 1 checks the status bit for the relocked state, the thread 1 suspends and enters the blocking state.
  • the thread 1 checks the status bit for the light lock state, it performs spin waiting. When the spin waiting reaches a certain threshold, if the check-holding thread identification field is still a thread other than thread 1, thread 1 suspends the lock-holding thread of lock 1, allocates a monitor object, and sets the status bit to relock State, and then wake up the lock holding thread of lock 1, and then thread 1 suspends and enters the blocking state.
  • thread 1 checks the lock thread identification field to be thread 1, thread 1 performs an operation to add 1 to the repeated lock count field. If the check repeated lock count domain reaches a certain threshold, thread 1 allocates a monitor object and sets the status bit to the relock state.
  • thread 1 checks the lock thread identification field as an invalid thread, that is, no thread holds a lock on lock 1, thread 1 sets the lock state identification field as the first thread and initializes the repeated lock count field.
  • the thread judges whether the lock can be held based on the existing lockword. It is easy to increase the length of time that the lock-holding thread accesses the shared resource, resulting in an increase in the response delay of the application in which the lock-holding thread is located in the electronic device, thereby increasing the probability of the electronic device being stuck and reducing the user experience.
  • the embodiment of the present application provides a multi-thread synchronization method.
  • the lock state variable (lockword) is redefined, so that the lock state variable includes the lock thread identification field, the repeated lock count field, and the blocked thread number field.
  • the lock thread identification field is used to indicate the lock thread of the target lock
  • the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock
  • the blocked thread number field is used to indicate that the target lock is in The number of threads in the blocked state, because the lock-holding thread identification field and the repeated lock-holding times field are only set by the lock-holding thread, and the blocked thread number field is only set by the non-locking thread. Therefore, the non-locking thread's spin waiting reaches a certain value.
  • the non-lock-holding thread When the threshold is set, if the lock-holding thread identification field is checked for other threads, the non-lock-holding thread performs an increment operation on the blocked thread number field and suspends into the blocking state. Compared with the prior art, there is no need to hang the lock-holding thread first. As a result, there is no need to allocate monitor objects, and perform an operation of adding 1 to the blocked thread number field, thus reducing the time length of the lock-holding thread accessing shared resources, reducing the probability of electronic device jams, and helping to improve user experience.
  • the electronic devices in the embodiments of the present application may be mobile phones, tablet computers, wearable devices, in-vehicle devices, augmented reality (AR)/virtual reality (VR) devices, notebook computers, super mobile personal computers (ultra-mobile personal computer, UMPC), netbook, personal digital assistant (personal digital assistant, PDA), etc.
  • AR augmented reality
  • VR virtual reality
  • PDA personal digital assistant
  • the electronic device includes a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, and a battery 142 , Antenna 1, antenna 2, mobile communication 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 , A display screen 194, and a subscriber identification module (SIM) card interface 195, etc.
  • SIM subscriber identification module
  • the sensor module 180 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, etc.
  • the processor 110 may include one or more processing units.
  • the processor 110 may include an application processor (AP), a modem (modem), a graphics processing unit (GPU), an image signal processor (ISP), a controller, and a video editor. Decoder, digital signal processor (digital signal processor, DSP), baseband processor, and/or neural network processor (neural-network processing unit, NPU), etc.
  • the different processing units may be independent devices, or two or more different processing units may also be integrated into one device.
  • the controller can be the nerve center and command center of the electronic device.
  • the controller can generate operation control signals according to the instruction operation code and timing signals to complete the control of fetching and executing instructions.
  • a memory may also be provided in the processor 110 to store instructions and data.
  • the memory in the processor 110 is a cache memory.
  • the memory can store instructions or data that the processor 110 has just used or used cyclically. If the processor 110 needs to use the instruction or data again, it can be directly called from the memory. Repeated accesses are avoided, the waiting time of the processor 110 is reduced, and the efficiency of the system is improved.
  • the processor 110 may include one or more interfaces.
  • the processor 110 includes a universal serial bus (USB) interface 130 and a subscriber identity module (SIM) interface 195.
  • the processor 110 may also include an integrated circuit (I2C) interface, an integrated circuit audio (inter-integrated circuit sound, I2S) interface, a pulse code modulation (PCM) interface, and a universal asynchronous A universal asynchronous receiver/transmitter (UART) interface, a mobile industry processor interface (MIPI), and/or a general-purpose input/output (GPIO) interface, etc.
  • I2C integrated circuit
  • I2S integrated circuit audio
  • PCM pulse code modulation
  • UART universal asynchronous A universal asynchronous receiver/transmitter
  • MIPI mobile industry processor interface
  • GPIO general-purpose input/output
  • the interface connection relationship between the modules illustrated in the embodiment of the present application is merely a schematic description, and does not constitute a structural limitation of the electronic device.
  • the electronic device may also adopt different interface connection modes in the foregoing embodiments, or a combination of multiple interface connection modes.
  • the USB interface 130 is an interface that complies with the USB standard specification, and specifically may be a Mini USB interface, a Micro USB interface, a USB Type C interface, and the like.
  • the USB interface 130 can be used to connect a charger to charge the electronic device, and can also be used to transfer data between the electronic device and the peripheral device. It can also be used to connect headphones and play audio through the headphones. This interface can also be used to connect to other electronic devices, such as AR devices.
  • the SIM card interface 195 is used to connect to the SIM card.
  • the SIM card can be inserted into the SIM card interface 195 or pulled out from the SIM card interface 195 to achieve contact and separation with the electronic device.
  • the electronic device can support 1 or N SIM card interfaces, and N is a positive integer greater than 1.
  • the SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, etc.
  • the same SIM card interface 195 can insert multiple cards at the same time. The types of the multiple cards can be the same or different.
  • the SIM card interface 195 can also be compatible with different types of SIM cards.
  • the SIM card interface 195 may also be compatible with external memory cards.
  • the electronic device interacts with the network through the SIM card to realize functions such as call and data communication.
  • the electronic device adopts an eSIM, that is, an embedded SIM card.
  • the eSIM card can be embedded in the electronic device and cannot be separated from the electronic device.
  • the charging management module 140 is used to receive charging input from the charger.
  • the charger can be a wireless charger or a wired charger.
  • the charging management module 140 may receive the charging input of the wired charger through the USB interface 130.
  • the charging management module 140 may receive the wireless charging input through the wireless charging coil of the electronic device. While the charging management module 140 charges the battery 142, it can also supply power to the electronic device through the power management module 141.
  • the power management module 141 is used to connect the battery 142, the charging management module 140, and the processor 110.
  • the power management module 141 receives input from the battery 142 and/or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the external memory, the display screen 194, the camera 193, and the wireless communication module 160.
  • the power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle times, and battery health status (leakage, impedance).
  • the power management module 141 may also be provided in the processor 110.
  • the power management module 141 and the charging management module 140 may also be provided in the same device.
  • the wireless communication function of the electronic device can be realized by the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem, and the baseband processor.
  • the antenna 1 and the antenna 2 are used to transmit and receive electromagnetic wave signals.
  • Each antenna in an electronic device can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve antenna utilization.
  • Antenna 1 can be multiplexed as a diversity antenna of a wireless local area network.
  • the antenna can be used in combination with a tuning switch.
  • the mobile communication module 150 can provide a wireless communication solution including 2G/3G/4G/5G and other standards applied to electronic devices.
  • the mobile communication module 150 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), and the like.
  • LNA low noise amplifier
  • the wireless communication module 160 includes wireless local area networks (WLAN) (such as Wi-Fi networks), Bluetooth (BT), and global navigation satellite system (GNSS) that can be applied to electronic devices. ), frequency modulation (FM), near field communication (NFC), infrared technology (infrared, IR) and other wireless communication solutions.
  • WLAN wireless local area networks
  • BT Bluetooth
  • GNSS global navigation satellite system
  • FM frequency modulation
  • NFC near field communication
  • IR infrared technology
  • the antenna 1 of the electronic device is coupled with the mobile communication module 150, and the antenna 2 is coupled with the wireless communication module 160, so that the electronic device can communicate with the network and other devices through wireless communication technology.
  • the wireless communication technology may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), broadband Code division multiple access (wideband code division multiple access, WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC , FM and/or IR technology, etc.
  • GSM global system for mobile communications
  • GPRS general packet radio service
  • CDMA code division multiple access
  • WCDMA broadband Code division multiple access
  • TD-SCDMA time-division code division multiple access
  • LTE long term evolution
  • BT GNSS
  • WLAN wireless local area network
  • NFC long term evolution
  • FM and/or IR technology etc.
  • the GNSS may include global positioning system (GPS), global navigation satellite system
  • the electronic device realizes the display function through the GPU, the display screen 194, and the application processor.
  • the display screen 194 is used to display images, videos, and the like.
  • the display screen 194 includes a display panel.
  • the display panel can adopt liquid crystal display (LCD), organic light-emitting diode (OLED), active matrix organic light-emitting diode or active-matrix organic light-emitting diode (active-matrix organic light-emitting diode).
  • AMOLED organic light-emitting diode
  • FLED flexible light-emitting diode
  • Miniled MicroLed, Micro-oLed, quantum dot light-emitting diode (QLED), etc.
  • the electronic device may include one or N display screens 194, and N is a positive integer greater than one.
  • the electronic device can realize the shooting function through ISP, camera 193, video codec, GPU, display screen 194 and application processor.
  • the ISP is used to process the data fed back by the camera 193. For example, when taking a picture, the shutter is opened, the light is transmitted to the photosensitive element of the camera through the lens, the light signal is converted into an electrical signal, and the photosensitive element of the camera transmits the electrical signal to the ISP for processing and is converted into an image visible to the naked eye.
  • ISP can also optimize the image noise, brightness, and skin color. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene.
  • the ISP may be provided in the camera 193.
  • the camera 193 is used to capture still images or videos.
  • the object generates an optical image through the lens and is projected to the photosensitive element.
  • the photosensitive element may be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor.
  • CMOS complementary metal-oxide-semiconductor
  • the photosensitive element converts the optical signal into an electrical signal, and then transfers the electrical signal to the ISP to convert it into a digital image signal.
  • ISP outputs digital image signals to DSP for processing.
  • DSP converts digital image signals into standard RGB, YUV and other formats of image signals.
  • the electronic device may include 1 or N cameras 193, 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.
  • 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 includes a running memory (memory) and a built-in memory.
  • the running memory can be used to store computer executable program code or data.
  • the executable program code includes instructions.
  • the processor 110 executes various functional applications and data processing of the electronic device by running instructions stored in the running memory.
  • the running memory may include high-speed random access memory.
  • the built-in memory can also be called a built-in external memory, etc., which can be used to store programs and/or data.
  • the built-in memory can store an operating system, application programs, and so on.
  • the electronic device usually loads the program and/or data in the built-in memory into the running memory, so that the processor 110 runs the corresponding program and/or data to realize the corresponding function.
  • the internal memory 121 may 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
  • the electronic device can implement audio functions through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the earphone interface 170D, and the application processor. For example, music playback, recording, etc.
  • the button 190 includes a power button, a volume button, and so on.
  • the button 190 may be a mechanical button. It can also be a touch button.
  • the electronic device can receive key input, and generate key signal input related to user settings and function control of the electronic device.
  • the motor 191 can generate vibration prompts.
  • the motor 191 can be used for incoming call vibration notification, and can also be used for touch vibration feedback.
  • touch operations that act on different applications can correspond to different vibration feedback effects.
  • Acting on touch operations in different areas of the display screen 194, the motor 191 can also correspond to different vibration feedback effects.
  • Different application scenarios for example: time reminding, receiving information, alarm clock, games, etc.
  • the touch vibration feedback effect can also support customization.
  • the indicator 192 may be an indicator light, which may be used to indicate the charging status, power change, or to indicate messages, missed calls, notifications, and the like.
  • the structure illustrated in the embodiments of the present application does not constitute a specific limitation on the electronic device.
  • the electronic device may include more or fewer components than those shown in the figure, or combine certain components, or split certain components, or arrange different components.
  • the illustrated components can be implemented in hardware, software, or a combination of software and hardware.
  • the software system of the electronic device in the embodiment of the present application may also be referred to as an operating system, which supports multi-threaded operation, and may adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture.
  • the layered architecture (for example, the Android operating system) divides the software into several layers, each layer has a clear role and division of labor, and communication between layers is through software interfaces.
  • FIG. 3 shows a structural block diagram of a software system of an electronic device according to an embodiment of the present application.
  • 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 Android system is divided into four layers, from top to bottom, the application layer, the application framework layer, the Android runtime and system library, and the kernel layer.
  • the application layer can 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 an application programming interface (application programming interface, API) and a programming framework 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, a notification manager, and so on.
  • 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, take a screenshot, etc.
  • the content provider is used to store and retrieve data and make these data accessible to applications.
  • the data may include video, image, audio, phone calls made and received, browsing history and bookmarks, phone book, etc.
  • the view system includes visual controls, such as controls that display text, controls that display pictures, and so on.
  • 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 a short message 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 status (including connected, hanged 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 the application to display notification information in the status bar, which can be used to convey notification-type messages, and it can disappear automatically after a short stay without user interaction.
  • the notification manager is used to notify download completion, message reminders, and so on.
  • 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 a 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. For example, text messages are prompted in the status bar, prompt sounds, electronic devices vibrate, and indicator lights flash.
  • Android Runtime includes a core library and a virtual machine (the virtual machine can become a java virtual machine, such as the Dalvik virtual machine). Android runtime is responsible for the scheduling and management of the Android system.
  • the core library consists of two parts: one part is the function functions that the 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 a virtual machine.
  • the virtual machine translates the Java bytecode of the application layer and the application framework layer into a binary executable file.
  • the virtual machine is used to perform functions such as object life cycle management, stack management, thread management, security and exception management, and garbage collection.
  • the multithreaded access method in the embodiment of the present application is executed through a virtual machine.
  • the software architecture shown in FIG. 3 is an Android operating system
  • the virtual machine may be Dalvik/ART, or HUAWEI maple, etc.
  • the software architecture shown in FIG. 3 is an operating system such as Linux or Windows
  • the virtual machine may be an openJDK virtual machine.
  • the virtual machine may be created when the application is started, or it may be pre-installed, which is not limited.
  • the virtual machine may be created when the application is started.
  • the virtual machine can be installed on the electronic device by the user according to his own needs.
  • the system library can include multiple functional modules. For example: surface manager (surface manager), media library (Media Libraries), three-dimensional graphics processing library (for example: OpenGL ES), 2D graphics engine (for example: SGL), etc.
  • 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 playback and recording of a variety of commonly used audio and video formats, as well as still image files.
  • the media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
  • the 3D graphics processing library is used to realize 3D graphics drawing, image rendering, synthesis and layer processing, etc.
  • the 2D graphics engine is a drawing engine for 2D drawing.
  • the kernel layer is the layer between hardware and software.
  • the kernel layer includes at least display driver, camera driver, audio driver, and sensor driver.
  • Lock The lock in the embodiment of the present application is used to protect shared resources from being accessed by multiple threads at the same time.
  • the shared resources in the embodiments of the present application can be accessed by multiple threads, such as memory, code, variables, or data, which is not limited.
  • a lock may be an object (object, obj) or a part of an object, where the object is used to protect shared resources and is an instance of a class, where the class is a template that describes the behavior and state of a class of objects.
  • the lock may also be an address or other uniquely determined parameters or structures.
  • the lock state variable (lockword).
  • the lockword includes a lock thread identification field, a repeated lock count field, and a blocked thread number field, which may be 32 bits or 64 bits.
  • the embodiment of the present application does not limit the number of bits of the lockword.
  • the lockword may also include reserved bits to facilitate subsequent expansion of functions or operations.
  • lockword and lock have a one-to-one correspondence, and different locks have different lockwords.
  • lockword of the target lock Take the lockword of the target lock as an example.
  • the lock-holding thread identification field is used to indicate the lock-holding thread of the target lock.
  • the first thread may obtain the target lock by setting the lock-holding thread identification field to the first thread.
  • the first thread may set the lock-holding thread identification field as the first thread by setting the thread ID of the first thread in the lock-holding thread identification field.
  • the Blocked Threads field is used to indicate the number of threads that are blocked for the target lock.
  • the blocked thread number field includes the first value and the high-order flag of the blocked thread number.
  • the first value is the low value of the number of threads in the blocked state for the target lock
  • the high bit flag of the number of blocked threads is used to indicate whether the second value corresponding to the ID of the target lock is stored in the high bit list of the number of blocked threads.
  • the second value is the high value of the number of threads in the blocked state for the target lock. This helps to avoid overflow when there are too many threads in a blocked state for the target lock. For example, when the high-order flag of the number of blocked threads is a valid flag, it indicates that the second value corresponding to the ID of the target lock is stored in the high-order list of the number of blocked threads.
  • the high flag of the number of blocked threads When the high flag of the number of blocked threads is an invalid flag, it indicates that the second value corresponding to the ID of the target lock is not stored in the high list of the number of blocked threads, or the second value corresponding to the ID of the target lock is 0. For example, when the high flag of the number of blocked threads is set to 1, the high flag of the number of blocked threads is a valid flag. For another example, when the high bit flag of the number of blocked threads is set to 0, that is, the high bit flag of the number of blocked threads is an invalid flag.
  • the high list of the number of blocked threads may be a global variable and stored in the electronic device in the form of a map.
  • the second value corresponding to the ID of the target lock for example, the format of the second value corresponding to the ID of the target lock stored in the high list of blocked threads is shown in Figure 4, the ID of the target lock is key, and the second value It is vlaue, so that the thread requesting to acquire the target lock can index the second value from the high list of blocked threads through the ID of the target lock.
  • the number of repeated locks field is used to indicate the number of locks held by the lock-holding thread for the target lock.
  • the lock-holding thread may indicate the number of lock-holding times of the lock-holding thread for the target lock by setting the repeated lock-holding coefficient domain to the number of times the lock-holding thread repeatedly holds the lock for the target lock.
  • the repeated lock holding coefficient field can also be directly set to the number of times the lock holding thread holds the lock for the target lock.
  • the lock-holding thread can set the repeated lock-holding coefficient domain to 0, or it can Set to 1 to indicate that the number of times the lock-holding thread holds the lock for the target lock is 1.
  • the lock-holding thread may indicate that the number of lock-holding times of the target lock by the lock-holding thread is zero by setting the number of repeated locks field to be empty.
  • the lock-holding thread may indicate that the lock-holding number of the target lock by the lock-holding thread is 0 by setting the number of repeated locks field to a specific symbol or value.
  • the lock-holding thread can indicate the lock-holding thread by setting the number of repeated locks field to 0.
  • the number of holding locks for the target lock is 0.
  • the lock-holding thread can indicate the lock holding by setting the repeated lock-holding times field to none. The number of times the thread holds the lock for the target lock is 0.
  • the repeated lock-holding coefficient field includes a third value and a high-level indicator of the number of repeated lock-holding times.
  • the third value is the low value of the number of locks held by the lock thread for the target lock
  • the high flag of the number of repeated locks is used to indicate whether the ID of the target lock and the lock thread are stored in the high list of the number of repeated locks.
  • the fourth value corresponding to the thread ID of the thread ID, and the fourth value is the high value of the number of times the lock-holding thread holds the lock for the target lock. This helps to avoid overflow when the lock-holding thread locks the target too many times.
  • the high flag of the number of repeated locks when the high flag of the number of repeated locks is a valid flag, it indicates that the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread is stored in the high list of the number of repeated locks.
  • the high flag of the number of repeated locks is an invalid flag, it indicates that the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread is not stored in the high list of the number of repeated locks, or the ID and the lock of the target lock
  • the fourth value corresponding to the thread ID of the thread is 0.
  • the high flag of the number of repeated locks when the high flag of the number of repeated locks is set to 1, the high flag of the number of repeated locks is a valid flag.
  • the high flag of the number of repeated locks is set to 0, that is, the high flag of the number of repeated locks is an invalid flag.
  • the high-order list of the number of repeated locks may be a global variable and stored in the electronic device in the form of a map.
  • the format of the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread stored in the high-order list of the number of repeated locks is as follows As shown in Figure 5, the ID of the target lock and the thread ID of the thread holding the lock are the key, and the fourth value is vlaue, so that the thread requesting to reacquire the target lock can use the ID and thread ID of the target lock from the high list of the number of repeated locks The fourth value of the middle index.
  • the lockword of the target lock may be as shown in FIG. 6, including a lock thread identification field, a repeated lock count field, a blocked thread number field, and reserved bits.
  • the domain of the number of repeated locks includes the Counter field and the M1 field
  • the field of the number of blocked threads includes the WaitNum field and the M2 field.
  • the Counter field is used to set the first value
  • the M1 field is used to set the high-level flag of the number of repeated locks.
  • the WaitNum field is used to set the third value
  • the M2 field is used to set the high flag of the number of blocked threads.
  • the first 16 bits of the lockword (that is, the 0th to 15th bits) are the lock thread identification field, and the 16th to 18th bits of the lockword are the Counter field.
  • the 19th bit of the lockword is the M1 field.
  • the 20th to 28th bits of the lockword are the WaitNum field, and the 30th and 31st bits are reserved bits. It should be noted that the reserved bits may not be set in the lockword. Or, set the number of bits in each field in the lockword according to actual needs.
  • FIG. 6 is only an example of the lockword format of the target lock, and does not constitute a limitation on the lockword format.
  • the number of bits in each field in the lockword can also be set according to actual needs.
  • the hashcode (hash value) of the target lock is used to indicate whether the target lock exists.
  • the hashcode of the target lock is stored in the memory space corresponding to a negative offset of 9 bits from the address of the object header of the object.
  • the electronic device can store the hashcode of the target lock in the memory space corresponding to the address of the lockword used to store the target lock. Contribute to the rational use of memory space.
  • the hashcode of the target lock may be stored in the form of a map.
  • the storage format of the hashcode of the target lock may be as shown in Figure 7, where the ID of the target lock is key and the hashcode is value.
  • the thread can index to the hashcode of the target lock through the ID of the target lock.
  • Example 1 Take the first thread's request to acquire the target lock as an example.
  • the request by the first thread to acquire the target lock can be understood as: the first thread requests to lock the target lock.
  • the process of executing the first thread's request to acquire the target lock is triggered.
  • the function or program instruction requesting to acquire the target lock is MonitorEnter.
  • the first thread requests to acquire the target lock, including the following steps, as shown in FIG. 8 in detail.
  • Step 801 The first thread acquires the lockword of the target lock.
  • the first thread obtains the lockword of the target lock according to the address of the target lock.
  • the address of the target lock may be a forward offset of 4 bytes from the address of the object header of the object where the target lock is located.
  • Step 802 The first thread checks whether the lock-holding thread identification field is a valid thread. That is, the first thread judges whether the lock-holding thread identification field is set as a valid thread. If yes, go to step 803, otherwise go to 809.
  • lock-holding thread identification field is not a valid thread, which is equivalent to the lock-holding thread identification field being an invalid thread.
  • setting the lock-holding thread identification field as a valid thread can be understood as: the thread ID set in the lock-holding thread identification field is a valid value.
  • [1,1000] is the effective value of the thread ID
  • the thread ID set on the lock-holding thread ID field is in [1,1000]
  • the lock-holding thread field is set as a valid thread.
  • the lock-holding thread ID is set to an invalid thread.
  • the thread ID set in the lock-holding thread identification field is 0 or 1001, and 0 and 1001 are not in [1,1000]
  • the lock-holding thread identification field is set as an invalid thread.
  • the target lock is in a non-lock state (non_lock).
  • the lock-holding thread identification field is set to be an invalid thread, and it can also be implemented by setting a specific value or symbol on the lock-holding thread identification field. For example, when the lock-holding thread identification field is set to NA, it is about to be held. The lock thread ID field is set to an invalid thread.
  • Step 803 The first thread checks whether the lock-holding thread domain is the first thread, if yes, execute step 808; otherwise, execute step 804.
  • Step 804 The first thread checks whether the blocked thread number domain is less than the first threshold, if yes, execute step 805, otherwise, execute step 807.
  • the first threshold may be 3, 4, 5, etc., specifically it may be set before the electronic device leaves the factory, or it may be determined by the electronic device according to its own operating conditions, which is not limited.
  • the first threshold value may be a larger value, and in the case where the electronic device itself is running stuck, the value of the first threshold value may be smaller.
  • Step 805 The first thread performs spin waiting.
  • the first thread can spin-wait in the following ways:
  • the first thread calls the sched_yield system function and sends a yield instruction to the processor. After receiving the yield instruction, the processor suspends the first thread for a certain period of time, so that the first thread relinquishes control of the processor for a certain period of time and performs spin waiting.
  • the electronic device may also perform spin waiting in other ways, which is not limited.
  • Step 806 When the number of spin waiting times of the first thread reaches the second threshold, if it is checked that the lock-holding thread identification field is a valid thread and not the first thread, the first thread performs an operation of adding 1 to the blocked thread number field. And hang into the blocking state.
  • the blocked thread number field includes the first value and the high-order flag of the blocked thread number.
  • the operation of adding 1 to the number field of blocked threads by the first thread may be as shown in FIG. 9, and may specifically include the following steps:
  • Step 901 The first thread checks whether the first value is equal to the first target value, and the first target value is the maximum value in the low order of the number of threads in the blocked state for the target lock. If yes, go to step 903, otherwise, go to step 902.
  • the first thread compares or judges whether the first value is the same as the first target value.
  • Step 902 The first thread adds 1 to the first value.
  • the first target value is 7.
  • the first thread adds 1 to the first value and sets the first value to 5.
  • Step 903 The first thread checks whether the high-level flag of the number of blocked threads is a valid flag, if yes, execute step 904, otherwise, execute step 905.
  • the high-level identifier of the number of blocked threads is not a valid identifier, and is equivalent to the invalid identifier of the number of blocked threads.
  • Step 904 The first thread sets the first value to 0, and adds 1 to the second value corresponding to the ID of the target lock in the blocked thread list.
  • Step 905 The first thread sets the first value to 0, sets the high flag of the number of blocked threads as a valid flag, sets the second value corresponding to the ID of the target lock to 1, and sets the ID of the target lock and the ID of the target lock The corresponding second value is added or stored in the blocked thread list.
  • the first thread can be suspended and enter the blocking state in the following ways:
  • the first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock.
  • the first thread makes a system call based on the fast user mutex (futex) mechanism, sets the state of the first thread to sleep, and adds the first thread to the address corresponding to the target lock Waiting in the queue.
  • the first thread may suspend and enter the blocking state through the system call futex_wait.
  • the first thread sets the lock-holding thread identification field to the first thread when the number of spin waits is less than the second threshold, and the first thread has a shared resource protected by the target lock Make a visit.
  • Step 807 The first thread performs an operation of adding 1 to the blocked thread number field, and suspends and enters a blocking state.
  • the first thread performs an operation of adding 1 to the blocked thread number field and suspends and enters the blocking state for a specific implementation manner, which can be referred to the related introduction in step 806, which will not be repeated here.
  • Step 808 The first thread performs an operation of adding 1 to the repeated lock count field.
  • the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks.
  • the operation of adding 1 to the number of repeated lock holdings performed by the first thread may be as shown in FIG. 10, which may specifically include the following steps:
  • Step 1001 The first thread checks whether the third value is equal to the second target value, and the second target value is the maximum value in the low order of the number of lock holding times of the lock holding thread for the target lock. If yes, go to step 1003; otherwise, go to step 1002.
  • Step 1002 the first thread adds 1 to the third value.
  • the second target value is 31.
  • the first thread adds 1 to the third value and sets the third value to 21.
  • Step 1003 The first thread checks whether the high-level flag of the number of repeated locks is a valid flag, if it is, step 1004 is executed, otherwise, step 1005 is executed.
  • Step 1004 The first thread sets the third value to 0, and adds 1 to the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks.
  • Step 1005 The first thread sets the third value to 0, sets the high flag of the number of repeated locks as a valid flag, sets the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread to 1, and sets the target lock
  • the ID of the lock thread, the thread ID of the lock thread, and the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread are added or stored in the blocked thread list.
  • step 809 the first thread sets the lock-holding thread identification field as the first thread, and initializes the repeated lock-holding times field.
  • the first thread initializes the repeated lock count field, which can be understood as the first thread sets the repeated lock count field to an initial value. For example, when the initial value is 0, the first thread sets the number of repeated locks field to 0. In the case where the initial value is 1, the first thread sets the number of repeated locks field to 1.
  • the initial value may be 1.
  • the initial value may be 0.
  • the first thread since the number of spin waiting times of the first thread reaches the second threshold, the first thread only needs to perform an operation of adding 1 to the blocked thread number field and suspend into the blocking state.
  • the lock-holding thread of the lock is suspended, and there is no need to allocate a monitor object, which helps to reduce the length of time that the lock-holding thread accesses shared resources, thereby helping to reduce the probability of electronic device jams and improving user experience.
  • the first thread when the first thread is a lock-holding thread, if the target lock is repeatedly held, it is only necessary to add 1 to the number of repeated lock-holding threads, and there is no need to allocate monitor objects and modify the lock status. Therefore, it helps to improve the processing efficiency of the first thread and the operating speed of the electronic device, thereby helping to improve the user experience.
  • Example 2 Take the first thread's request to release the target lock as an example.
  • the process of executing the first thread's request to release the target lock is triggered.
  • the function or program instruction that requests the release of the target lock is MonitorExit.
  • the first thread requesting to release the target lock includes the following steps, as shown in FIG. 11 in detail.
  • Step 1101 The first thread acquires the lockword of the target lock. For details, refer to the related introduction of step 801.
  • step 1102 the first thread checks whether the lock-holding thread identification field is the first thread, if so, execute step 1103; otherwise, execute step 1107.
  • the first thread compares whether the lock-holding thread identification field is the thread ID of the first thread.
  • step 1103 the first thread checks whether the repeated lock count field is 0, and if so, execute step 1106; otherwise, execute step 1104.
  • the number of repeated lock holdings is not 0, which can be understood as the number of lock holding threads for the target lock indicated by the number of repeated locks is greater than 0.
  • the number of repeated locks is 0, which can be understood as the number of locks held by the lock thread indicated by the number of repeated locks for the target lock is 0.
  • step 1104 the first thread performs a minus 1 operation on the repeated lock count field.
  • the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks.
  • the operation of subtracting 1 from the number of repeated lock holdings by the first thread may be as shown in FIG. 12, which may specifically include the following steps:
  • Step 1201 The first thread checks whether the third value is 0, that is, the first thread determines whether the number of times the lock holding thread (ie, the first thread) holds the target lock is 0. If not, step 1202 is performed, and if yes, step 1203 is performed.
  • Step 1202 the first thread subtracts 1 from the third value.
  • Step 1203 The first thread checks whether the high-level flag of the number of repeated locks is a valid flag, if it is, step 1204 is executed, otherwise, step 1207 is executed.
  • Step 1204 The first thread checks whether the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks is 1, if yes, execute step 1205, otherwise, execute step 1206.
  • the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks is not 1, which can be understood as the ID and hold of the target lock in the high list of the number of repeated locks.
  • the fourth value corresponding to the thread ID of the locked thread is greater than 1.
  • Step 1205 The first thread sets the third value to the second target value, and deletes the ID of the target lock, the thread ID of the lock-holding thread and the thread ID of the target lock and the thread ID of the lock-holding thread in the high list of repeated locks.
  • the fourth value of, and the high flag of the number of repeated locks is set to an invalid flag.
  • Step 1206 The first thread sets the third value to the second target value, and subtracts 1 from the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread.
  • Step 1207 The first thread throws an exception.
  • Step 1105 After the first thread performs an operation of subtracting 1 from the number of repeated locks field, the first thread rechecks whether the number of repeated locks field is 0, if so, execute step 1106, otherwise the process ends.
  • Step 1106 The first thread sets the lock thread identification field as an invalid thread, and checks whether the blocked thread number field is 0, if not, execute step 1107, otherwise the process ends.
  • Step 1107 The first thread wakes up N threads that are blocked for the target lock. Further, after the first thread wakes up the N threads in the blocked state of the target lock, it may also perform an operation of subtracting N on the blocked thread number field. Or, the first thread wakes up N threads that are blocked for the target lock, and does not perform the N reduction operation on the blocked thread number field.
  • the The thread performs the operation of subtracting 1 from the blocked thread number field, and the other N-1 threads among the N threads can be suspended again and enter the blocking state. For example, when the third thread among the N threads sets the lock-holding thread identification field to the third thread, the third thread performs an operation of subtracting 1 on the blocked thread number field.
  • N is a positive integer greater than or equal to 1.
  • N can be 1 or 2, or 3, or the number of all threads that are blocked for the target lock.
  • the value of N can be preset in the code by the developer as needed.
  • the first thread may wake up at least one thread that is blocked for the target lock based on the following methods:
  • the first thread makes a system call, sets the state of at least one thread in the waiting queue corresponding to the address of the target lock to the running state, and adds at least one thread to the run queue corresponding to the address of the target lock;
  • the waiting queue corresponding to the address of includes at least one thread that is blocked for the target lock.
  • the first thread may make a system call based on the futex mechanism to wake up at least one thread that is blocked for the target lock.
  • the first thread system calls futex_wake to wake up at least one thread that is blocked for the target lock.
  • Step 1108 The first thread checks whether the lock-holding thread identification field is an invalid thread, if yes, execute step 1109, otherwise, execute step 1110.
  • Step 1109 The first thread throws a no-lock exception.
  • Step 1110 the first thread throws an other-lock exception.
  • Example 3 Take the first thread's request to suspend the lock on the target as an example.
  • the request of the first thread to suspend the lock lock on the target can be understood as: the first thread actively requests to give up the lock lock on the target when the lock is locked on the target and the target lock is not released.
  • the first thread runs to a function or program instruction that requests to suspend the lock on the target, it triggers the execution of the process in which the first thread requests to suspend the lock on the target.
  • the function or program instruction requesting to suspend the lock on the target is MonitorWait.
  • the first thread requests to suspend the lock on the target, including the following steps, as shown in FIG. 13 specifically.
  • Step 1301 The first thread acquires the lockword of the target lock. For details, please refer to the related introduction in step 801, which will not be repeated here.
  • Step 1302 the first thread checks whether the lock-holding thread identification field is the first thread, if yes, execute step 1303, otherwise exit.
  • Step 1303 The first thread saves the repeated lock count field as the context of the first thread for the target lock.
  • the first thread saves the field of the number of repeated locks as the context of the first thread for the target lock, which can be understood as: the first thread saves the number of locks for the target lock as the context of the first thread for the target lock.
  • the first thread saves the lock count for the target lock as the first
  • the context of the thread for the target lock can mean that the first thread sets the third value, the high mark of the number of repeated locks, and the first thread ID corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the third value, the number of repeated locks.
  • the four value is saved as the context of the first thread for the target lock.
  • the first thread determines the fifth value according to the third value, the high flag of the number of repeated locks, and the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks, where the fifth The value is the number of times the first thread holds the lock for the target lock, and the fifth value is saved as the context of the first thread for the target lock.
  • the third value is 7, the high mark of the repeated lock coefficient is a valid mark, and the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread in the high list of the number of repeated locks is 1.
  • the fifth value is 15.
  • the first thread may save 15 as the context of the first thread for the target lock, or it may save 7, the high bit flag of the repeated lock holding coefficient and 1 as the context of the first thread for the target lock.
  • the first thread saves the lock count for the target lock as the first
  • the context of a thread for the target lock can mean that the first thread saves the third value as the context of the first thread for the target lock, or the first thread saves the third value and the high-level identifier of the number of repeated locks as the first The context of the thread for the target lock.
  • the first thread may store the context of the first thread for the target lock in a designated storage space.
  • Step 1304 The first thread adds the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock.
  • the first thread can add the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock in the following manner:
  • the first thread looks up the thread waiting list corresponding to the ID of the target lock according to the ID of the target lock. If the first thread does not find the thread waiting list corresponding to the ID of the target lock, the first thread creates a thread waiting list corresponding to the ID of the target lock, and adds the thread ID of the first thread to the ID corresponding to the target lock Thread waiting list. If the first thread finds the thread waiting list corresponding to the ID of the target lock, the first thread adds the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock.
  • the thread waiting list corresponding to the ID of the target lock may be a global variable and stored in the electronic device in the form of a single necklace.
  • the format of the thread ID of the first thread stored in the thread waiting list corresponding to the ID of the target lock can be as shown in Figure 14.
  • the ID of the target lock is the key, and the thread ID of the first thread Is the value. Therefore, the thread ID of the first thread can be indexed from the thread waiting list corresponding to the ID of the target lock by the ID of the target lock.
  • Step 1305 The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field to be an invalid thread, and suspends and enters a blocking state. Therefore, when the first thread locks the target but does not release the lock, it actively gives up control of the target lock, so that other threads, especially threads with a higher business priority than the first thread, can achieve priority locks. Shared resource access to meet the needs of users.
  • the first thread sets the number of repeated locks field to 0 means that the first thread clears the number of locks indicated by the number of repeated locks field to 0.
  • the number of repeated locks field includes the third value and the high flag of the number of repeated locks, and the high flag of the number of repeated locks is a valid flag
  • the first thread sets the number of repeated locks field to 0, which means: A thread sets the third value to 0, sets the high flag of the number of repeated locks to an invalid flag, and sets the fourth value corresponding to the ID of the target lock and the thread ID of the first thread in the high list of the number of repeated locks to 0.
  • the first thread may request to acquire the target lock again, which specifically includes the following steps:
  • Step 1306 The first thread reacquires the lock state variable of the target lock.
  • step 1307 the first thread checks whether the lock-holding thread identification field is an invalid thread, and if so, executes step 1308; otherwise, it continues to check whether the lock-holding thread identification field is an invalid thread.
  • the first thread sets the lock-holding thread identification field as the first thread, and sets the repeated lock-holding times field according to the context of the first thread for the target lock.
  • the context of the first thread for the target lock includes that the number of locks held by the first thread for the target lock is 15, and the first thread sets the number of repeated locks field to 14 or 15.
  • the first thread sets the repeated lock-holding number field to 14; by setting the lock-holding thread to hold the lock for the target lock In the case of setting the repeated lock-holding coefficient field for the number of times, the first thread sets the repeated lock-holding times field to 15.
  • the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks.
  • the third value is set by 3 bits in the repeated lock count field
  • the first thread sets the third value to 6 or 7, set the high flag of the number of repeated locks to the effective flag, and set the fourth value corresponding to the ID of the target lock and the thread ID of the first thread to 1, and set the ID of the target lock and the thread of the first thread.
  • the ID and the fourth value corresponding to the ID of the target lock and the thread ID of the first thread are added or stored in the high list of the number of repeated locks.
  • Step 1309 The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
  • the first thread deleting the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock may specifically include the following steps:
  • Step 1501 the first thread searches for a thread waiting list corresponding to the ID of the target lock according to the ID of the target lock.
  • Step 1502 if the first thread does not find the thread waiting list corresponding to the ID of the target lock, it will hang abnormally.
  • Step 1503 If the first thread finds the thread waiting list corresponding to the ID of the target lock, the first thread searches for the thread ID of the first thread from the thread waiting list corresponding to the ID of the target lock.
  • Step 1504 If the first thread finds the thread ID of the first thread, the first thread deletes the thread ID of the first thread.
  • Step 1505 If the first thread does not find the thread ID of the first thread, the first thread hangs abnormally.
  • threads that voluntarily give up holding the target lock can be awakened in the following ways:
  • Method 1 The second thread searches the thread waiting list corresponding to the ID of the target lock according to the ID of the target lock. After finding the thread waiting list corresponding to the ID of the target lock, the second thread checks the thread waiting list corresponding to the ID of the target lock. If the second thread checks that the thread waiting list corresponding to the ID of the target lock is not empty, that is, the thread ID is stored in the thread waiting list corresponding to the ID of the target lock, the second thread waits from the thread corresponding to the ID of the target lock Select at least one thread ID from the list, and wake up the thread identified by the at least one thread ID.
  • the second thread may be a thread that belongs to the same process and is in a running state, and threads identified by one or more thread IDs in the thread waiting list corresponding to the ID of the target lock.
  • the second thread can check whether the thread waiting list corresponding to the ID of the target lock is empty every certain period of time.
  • the second thread selects the first thread ID from the thread waiting list corresponding to the ID of the target lock, and wakes up the thread identified by the first thread ID.
  • the first thread ID indicates the first thread
  • the first thread is awakened.
  • Method 2 When the system checks or detects that the thread ID of the first thread is in the thread waiting list corresponding to the ID of the target lock, the system suspends the first thread and enters the blocking state to start the timer, and after the timer reaches the specified duration , Wake up the first thread.
  • the interrupt state of the first thread when the first thread requests to suspend the lock on the target, if the interrupt state of the first thread changes, an exception is thrown. For example, when the first thread suspends and enters the blocking state in the execution of step 1305, if the interrupt state of the first thread changes, an exception is thrown and the subsequent steps are not executed. For another example, when the first thread is awakened, if the interrupt state of the first thread changes, an exception is thrown, and the subsequent steps are not continued.
  • the target lock Take the target lock as lock 1 as an example.
  • the lock-holding thread is thread 2, and while thread 2 requests to release lock 1, thread 1 can request to acquire lock 1, where thread 1 is a non-lock-holding thread of lock 1.
  • thread 1 requests to acquire lock 1 and thread 2 requests to release lock 1 specifically including the following steps:
  • Step 1601_1 thread 1 obtains the lock state variable of lock 1; step 1601_2, thread 2 obtains the lock state variable of lock 1.
  • Step 1602_1 Thread 1 checks that the lock-holding thread identification field is thread 2, and checks whether the blocked thread number field is less than the first threshold. If yes, execute step 1603_1; otherwise, execute step 1605_1.
  • Step 1603_1 Thread 1 performs spin waiting.
  • Step 1604_1 When the number of spin waits for thread 1 reaches the second threshold, if it is checked that the lock-holding thread identification field is a valid thread and not the first thread, thread 1 performs an increment operation on the blocked thread number field and suspends the entry Blocked state.
  • Step 1605_1 Thread 1 performs an operation of adding 1 to the blocked thread number field, and suspends and enters a blocking state.
  • Step 1602_2 thread 2 checks that the lock thread identification field is thread 2, and checks whether the repeated lock count field is 0, if it is not 0, execute step 1603_2, otherwise execute step 1605_2.
  • Step 1603_2 Thread 2 performs a minus 1 operation on the repeated lock count field.
  • Step 1604_2 thread 2 rechecks whether the repeated lock count field is 0, if yes, execute step 1605_2, otherwise thread 2 continues to hold lock 1.
  • Step 1605_2 thread 2 sets the lock-holding thread identification field to an invalid thread, and checks whether the blocked thread number field is 0, if yes, execute step 1606_2, otherwise the process ends.
  • Step 1606_2 thread 2 wakes up at least one thread that is blocked for the target lock.
  • step 1605_2 is executed after checking that the lock-holding thread identification field is thread 2 in step 1602_1.
  • the target lock Take the target lock as lock 1 as an example.
  • the lock holding thread is thread 2
  • thread 1 can request to acquire lock 1, where thread 1 is a non-lock holding thread of lock 1.
  • the thread 1 requesting lock 1 and the thread 2 requesting lock 1 again specifically include the following steps:
  • Step 1701_1 thread 1 obtains the lock state variable of lock 1; step 1701_2, thread 2 obtains the lock state variable of lock 1.
  • Step 1702_1 Thread 1 checks that the lock-holding thread identification field is thread 2, and checks whether the blocked thread number field is less than the first threshold. If yes, execute step 1703_1; otherwise, execute step 1705_1.
  • Step 1703_1 Thread 1 performs spin waiting.
  • Step 1704_1 When the number of spin waits for thread 1 reaches the second threshold, if it is checked that the lock-holding thread identification field is a valid thread and not the first thread, thread 1 performs an increment operation on the blocked thread number field and suspends the entry Blocked state.
  • Step 1705_1 Thread 1 performs an operation of adding 1 to the blocked thread number field, and suspends and enters a blocking state.
  • Step 1702_2 thread 2 checks that the lock-holding thread identification field is thread 2, and performs an operation of adding 1 to the repeated lock-holding times field.
  • the lock state variable of lock 1 is shown in Figure 18, the number of blocked threads is N1, the number of repeated locks is N2, the first threshold is L1, and the second threshold is L2.
  • the lock-holding thread identification domain is abbreviated as domain 1
  • the repeated lock-holding identification domain is abbreviated as domain 2
  • the blocking thread identification domain is abbreviated as domain 3.
  • the above description of the multi-thread synchronization method is based on the thread as an example.
  • the threads involved above can also be replaced by the program execution
  • the smallest unit is not limited.
  • the method provided in the embodiments of the present application is introduced from the perspective of the mobile phone as the execution subject.
  • the mobile phone may include a hardware structure and/or a software module, and the above functions are implemented in the form of a software module or a hardware structure combined with a software module. Whether a certain function of the above-mentioned functions is executed by a hardware structure, a software module, or a hardware structure plus a software module depends on the specific application and design constraint conditions of the technical solution.
  • an embodiment of the present application discloses an electronic device 1900.
  • the electronic device 1900 may include: one or more processors 1901 and a memory 1902.
  • the above-mentioned devices can be connected through one or more communication buses.
  • the program instructions are stored in the aforementioned memory 1902 and configured to be executed by the one or more processors 1901 to implement the multi-thread synchronization method shown in FIGS. 8-13 and 15-17 in the embodiments of the present application.
  • the processors involved in the above embodiments may be general-purpose processors, digital signal processors (digital signal processors, DSP), application specific integrated circuits (ASICs), ready-made programmable gate arrays (field programmable gate arrays, FPGAs). ) Or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components.
  • the methods, steps, and logical block diagrams disclosed in the embodiments of the present application can be implemented or executed.
  • the general-purpose processor may be a microprocessor or the processor may also be any conventional processor or the like.
  • the steps of the method disclosed in the embodiments of the present application can be directly embodied as being executed and completed by a hardware decoding processor, or executed and completed by a combination of hardware and software modules in the decoding processor.
  • the software module can be located in random access memory (RAM), flash memory, read-only memory (read-only memory, ROM), programmable read-only memory or electrically erasable programmable memory, registers, etc. mature in the field Storage medium.
  • RAM random access memory
  • flash memory read-only memory
  • read-only memory read-only memory
  • ROM programmable read-only memory or electrically erasable programmable memory, registers, etc. mature in the field Storage medium.
  • the storage medium is located in the memory, and the processor reads the instructions in the memory and completes the steps of the above method in combination with its hardware.
  • the disclosed system, device, and method may be implemented in other ways.
  • the device embodiments described above are merely illustrative, for example, the division of the units 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 It 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 objectives of the solutions of the embodiments.
  • the functional units in the various embodiments 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 the part that contributes to the existing technology or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including A number of instructions are used to make a computer device (which may be a personal computer, a server, or a network device, etc.) or a processor (processor) execute all or part of the steps of the methods described in the various embodiments of the present application.
  • the aforementioned storage media include: U disk, mobile hard disk, ROM, RAM, magnetic disk or optical disk and other media that can store program codes.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Mobile Radio Communication Systems (AREA)

Abstract

A multi-thread synchronization method and an electronic device, wherein same relate to the technical field of terminals. The method comprises: a first thread requesting the acquisition of a target lock. Specifically, a first thread requesting the acquisition of a target lock involves the following steps: the first thread acquiring a lock state variable of the target lock, wherein the lock state variable comprises a lock holding thread identification field and a blocked thread number field; the first thread then checking the lock holding thread identification field; if it is checked and found that the lock holding thread identification field is a valid thread and is not the first thread, the first thread checking the blocked thread number field; if it is checked and found that the blocked thread number field is less than a first threshold value, the first thread performing spin waiting; and when the number of times of spin waiting reaches a second threshold value, if it is checked and found that the lock holding thread identification field is a valid thread and is not the first thread, the first thread adding one to the blocked thread number field, and being suspended from entering a blocked state. The technical solution is conducive to reducing the duration needed for a lock holding thread to access a shared resource, thereby reducing the probability of lagging of an electronic device.

Description

一种多线程同步方法及电子设备Multithread synchronization method and electronic equipment
相关申请的交叉引用Cross-references to related applications
本申请要求在2019年09月25日提交中国专利局、申请号为201910912971.6、申请名称为“一种多线程访问方法及电子设备”的中国专利申请的优先权,其全部内容通过引用结合在本申请中;本申请要求在2020年03月31日提交中国专利局、申请号为202010245880.4、申请名称为“一种多线程同步方法及电子设备”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims the priority of a Chinese patent application filed with the Chinese Patent Office on September 25, 2019, the application number is 201910912971.6, and the application name is "a multi-threaded access method and electronic equipment", the entire content of which is incorporated herein by reference In application; this application requires the priority of a Chinese patent application filed with the Chinese Patent Office, the application number is 202010245880.4, and the application name is "a multi-thread synchronization method and electronic equipment" on March 31, 2020, the entire content of which is incorporated by reference Incorporated in this application.
技术领域Technical field
本申请涉及终端技术领域,特别涉及一种多线程同步方法及电子设备。This application relates to the field of terminal technology, and in particular to a multi-thread synchronization method and electronic equipment.
背景技术Background technique
安卓(android)操作系统通常应用在手机、平板电脑等电子设备中,用于为用户使用电子设备提供统一的接口和友好的交互界面,可以支持多线程运行。目前,在安卓操作系统中,共享资源(例如共享内存、共享代码、共享数据等)可以被多线程访问,为了避免多线程同时访问共享资源,通过锁实现对共享资源的保护。以锁1保护共享资源1为例。多线程中只有持锁1的线程能够实现对共享资源1的访问,而一旦有线程获取到锁1,则其它线程无法再对锁1持锁,即锁1在同一时刻只能被一个线程持有。只有当持锁1的线程释放锁1后,其它线程才获得对锁1的持锁机会。The Android operating system is usually used in electronic devices such as mobile phones and tablet computers to provide a unified interface and a friendly interactive interface for users to use electronic devices, and can support multi-threaded operation. At present, in the Android operating system, shared resources (such as shared memory, shared code, shared data, etc.) can be accessed by multiple threads. In order to prevent multiple threads from simultaneously accessing shared resources, the shared resources are protected by locks. Take lock 1 to protect shared resource 1 as an example. In multithreading, only the thread holding lock 1 can access shared resource 1. Once a thread acquires lock 1, other threads can no longer hold lock 1, that is, lock 1 can only be held by one thread at the same time. Have. Only when the thread holding lock 1 releases lock 1, other threads get the opportunity to hold lock 1.
然而,现有技术中,线程通常是采用锁膨胀策略来竞争持锁的。虽然这种方式针对一个锁来说实现了在同一时刻只能被一个线程持有,但是容易导致持锁线程访问共享资源的时长增加,电子设备卡顿的概率增大,降低用户体验。However, in the prior art, threads usually use a lock expansion strategy to compete for locks. Although this method realizes that a lock can only be held by one thread at the same time, it easily leads to an increase in the length of time that the lock-holding thread accesses shared resources, and the probability of electronic device jamming increases, which reduces user experience.
发明内容Summary of the invention
本申请实施例提供了一种多线程同步方法及电子设备,有助于降低持锁线程访问共享资源的时长,从而减小电子设备卡顿的概率,提高用户体验。The embodiments of the present application provide a multi-thread synchronization method and an electronic device, which help reduce the length of time that a lock-holding thread accesses a shared resource, thereby reducing the probability of the electronic device being stuck and improving the user experience.
第一方面,为本申请实施例的一种多线程同步方法,具体包括:第一线程请求获取目标锁。The first aspect is a multi-thread synchronization method according to an embodiment of this application, which specifically includes: a first thread requests to acquire a target lock.
其中,第一线程请求获取目标锁,包括:Among them, the first thread requests to acquire the target lock, including:
步骤一、第一线程获取目标锁的锁状态变量,该锁状态变量包括持锁线程标识域和阻塞线程数域,持锁线程标识域用于指示目标锁的持锁线程,阻塞线程数域用于指示针对目标锁处于阻塞状态的线程数; Step 1. The first thread acquires the lock state variable of the target lock. The lock state variable includes the lock-holding thread identification field and the blocked thread number field. The lock-holding thread identification field is used to indicate the lock-holding thread of the target lock, and the blocked thread number field is used To indicate the number of threads that are blocked for the target lock;
步骤二、第一线程检查持锁线程标识域;Step 2: The first thread checks the lock thread identification field;
步骤三、第一线程若检查持锁线程标识域为有效线程且不为第一线程,第一线程检查阻塞线程数域;Step 3: If the first thread checks that the lock-holding thread identification field is a valid thread and is not the first thread, the first thread checks the blocked thread number field;
步骤四、第一线程若检查阻塞线程数域小于第一阈值,第一线程进行自旋等待;Step 4: If the first thread checks that the number of blocked threads is less than the first threshold, the first thread performs spin waiting;
步骤五、第一线程当自旋等待的次数达到第二阈值时,若检查持锁线程标识域为有效线程且不为第一线程,第一线程对阻塞线程数域执行加1操作,并挂起进入阻塞 状态。Step 5. When the number of spin waits for the first thread reaches the second threshold, if the check that the lock-holding thread identification field is a valid thread and not the first thread, the first thread performs an operation of incrementing the number field of blocked threads and hangs. Start to enter the blocking state.
本申请实施例中,由于目标锁的锁状态变量包括持锁线程标识域和阻塞线程数域,而阻塞线程数域用于指示针对目标锁处于阻塞状态的线程数,持锁线程不会对阻塞线程域进行修改,从而使得第一线程当自旋等待的次数达到第二阈值时,若检查持锁线程标识域为除第一线程以外的其它有效线程,第一线程无需将持锁线程挂起,也无需分配montior对象,就能够对阻塞线程数域执行加1操作,因而有助于降低持锁线程访问共享资源的时长,从而减小电子设备卡顿的概率,提高用户体验。In the embodiment of the present application, since the lock state variable of the target lock includes the lock thread identification field and the blocked thread number field, and the blocked thread number field is used to indicate the number of threads in the blocked state for the target lock, the lock-holding thread will not be blocked. The thread field is modified so that when the number of spin waits for the first thread reaches the second threshold, if the lock-holding thread identification field is checked as a valid thread other than the first thread, the first thread does not need to suspend the lock-holding thread , Without allocating montior objects, it is possible to add 1 to the blocked thread number field, which helps to reduce the length of time that the lock-holding thread accesses shared resources, thereby reducing the probability of electronic device jams and improving user experience.
在一种可能的设计中,在步骤三之后还包括:In a possible design, after step three, it also includes:
步骤六、第一线程若检查阻塞线程数域大于或等于第一阈值,第一线程对阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 6. If the first thread checks that the blocked thread number field is greater than or equal to the first threshold, the first thread performs an operation of adding 1 to the blocked thread number field and suspends and enters the blocking state.
在一种可能的设计中,锁状态变量还包括重复持锁次数域,重复持锁次数域用于指示持锁线程针对目标锁的持锁次数。从而使得锁状态变量能够记录持锁线程针对目标锁的持锁次数,有助于降低持锁线程释放锁时发生异常的可能性。In a possible design, the lock state variable also includes a repeated lock count field, and the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock. As a result, the lock state variable can record the number of locks held by the lock-holding thread for the target lock, which helps reduce the possibility of exceptions when the lock-holding thread releases the lock.
在一种可能的设计中,在步骤二之后还包括:In a possible design, after step two, it also includes:
步骤七、第一线程若检查持锁线程标识域为第一线程,第一线程对重复持锁次数域执行加1操作。由于重复持锁次数域用于指示持锁线程针对目标锁的持锁次数,而第一线程对重复持锁次数域执行加1操作,无需分配monitor对象,因此有助于提高第一线程运行的效率,进一步减小电子设备卡顿的概率,从而提高用户提验。Step 7. If the first thread checks that the lock-holding thread identification field is the first thread, the first thread performs an operation of adding 1 to the repeated lock-holding times field. Since the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock, and the first thread adds 1 to the repeated lock count field, there is no need to allocate a monitor object, which helps to improve the running performance of the first thread. Efficiency, further reducing the probability of electronic equipment jams, thereby improving user experience.
在一种可能的设计中,在步骤二之后还包括:In a possible design, after step two, it also includes:
步骤八、第一线程若检查持锁线程标识域为无效线程,第一线程将持锁线程标识域设置为第一线程,以及将重复持锁次数域设置为初始值。Step 8. If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks field to the initial value.
在一种可能的设计中,所述方法还包括:第一线程请求释放目标锁。In a possible design, the method further includes: the first thread requests to release the target lock.
其中,第一线程请求释放目标锁,包括:Among them, the first thread requests to release the target lock, including:
步骤九、第一线程获取目标锁的锁状态变量:Step 9. The first thread acquires the lock state variable of the target lock:
步骤十、第一线程检持锁线程标识域; Step 10. The first thread checks the lock thread identification field;
步骤十一、第一线程若检查持锁线程标识域为第一线程,第一线程检查重复持锁次数域;Step 11. If the first thread checks the lock-holding thread identification field as the first thread, the first thread checks the repeated lock-holding times field;
步骤十二、第一线程若检查重复持锁次数域不为0,第一线程对重复持锁次数域执行减1操作。Step 12: If the first thread checks that the repeated lock count field is not 0, the first thread performs an operation to subtract 1 from the repeated lock count field.
通过上述技术方案,有助于简化第一线程请求释放目标锁的过程。Through the above technical solution, it is helpful to simplify the process of the first thread requesting to release the target lock.
在一种可能的设计中,在步骤十二之后还包括:In a possible design, after step twelve, it also includes:
步骤十三、第一线程若检查重复持锁次数域为0,第一线程将持锁线程标识域设置为无效线程;Step 13. If the first thread checks that the repeated lock count field is 0, the first thread sets the lock thread identification field as an invalid thread;
步骤十四、第一线程检查阻塞线程数域;Step 14. The first thread checks the blocked thread count field;
步骤十五、第一线程若检查阻塞线程数域不为0,第一线程唤醒针对目标锁处于阻塞状态的至少一个线程。Step 15. If the first thread checks that the blocked thread count field is not 0, the first thread wakes up at least one thread that is blocked for the target lock.
由于第一线程在释放对目标锁的控制权后,还可以唤醒针对目标锁处于阻塞状态的至少一个线程,从而提高线程对目标锁持锁的可能性,提高线程运行的效率。After the first thread releases control of the target lock, it can also wake up at least one thread that is blocked for the target lock, thereby increasing the possibility that the thread locks the target and improving the efficiency of thread operation.
在一种可能的设计中,所述方法还包括:第一线程请求中止对目标锁持锁。In a possible design, the method further includes: the first thread requests to suspend the lock on the target.
其中,第一线程请求中止对目标锁持锁,包括:Among them, the first thread's request to suspend the lock on the target includes:
步骤十六、第一线程获取所述目标锁的锁状态变量;Step 16. The first thread acquires the lock state variable of the target lock;
步骤十七、第一线程检查持锁线程标识域;Step 17. The first thread checks the lock thread identification field;
步骤十八、第一线程若检查持锁线程标识域为第一线程,第一线程将重复持锁次数域保存为第一线程针对目标锁的上下文;Step 18. If the first thread checks that the lock-holding thread identification field is the first thread, the first thread saves the repeated lock-holding times field as the context of the first thread for the target lock;
步骤十九、第一线程将第一线程的线程标识ID添加到与目标锁的ID对应的线程等待列表中; Step 19. The first thread adds the thread identification ID of the first thread to the thread waiting list corresponding to the ID of the target lock;
步骤二十、第一线程将重复持锁次数域置0,以及将持锁线程标识域设置为无效线程,并挂起进入阻塞状态。Step 20: The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field to be an invalid thread, and suspends and enters a blocking state.
本申请实施例中,由于第一线程可以将第一线程的线程ID添加到与目标锁的ID对应的线程等待列表中,从而便于第一线程便于被其它线程或系统唤醒。In the embodiment of the present application, since the first thread can add the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock, it is convenient for the first thread to be awakened by other threads or systems.
在一种可能的设计中,在步骤二十之后还包括:In a possible design, after step 20, it also includes:
步骤二十一、第一线程当被唤醒后,获取目标锁的锁状态变量;Step 21: After the first thread is awakened, acquire the lock state variable of the target lock;
步骤二十二、第一线程检查持锁线程标识域;Step 22: The first thread checks the lock thread identification field;
步骤二十三、第一线程若检查持锁线程标识域为无效线程,第一线程将持锁线程标识域设置为第一线程,并根据第一线程针对目标锁的上下文,设置重复持锁次数域;Step 23: If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks according to the context of the first thread for the target lock area;
步骤二十四、第一线程删除与目标锁的ID对应的线程等待列表中的第一线程的线程ID。Step 24: The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
通过上述技术方案,使得在第一线程请求中止对目标锁持锁的情况下,若重新被唤醒,则可以实现重新恢复对目标锁持锁。Through the above technical solution, if the first thread requests to suspend the lock on the target, if it is awakened again, the lock on the target can be resumed.
在一种可能的设计中,第一线程可以通过以下方式被唤醒,获取所述目标锁的锁状态变量:In a possible design, the first thread can be awakened in the following manner to acquire the lock state variable of the target lock:
第一线程当被第二线程唤醒后,获取目标锁的锁状态变量,其中,第二线程是当与目标锁的ID对应的线程等待列表中存储有第一线程的线程ID时,唤醒第二线程的;第二线程与第一线程属于同一进程;或者,After the first thread is awakened by the second thread, it acquires the lock state variable of the target lock. The second thread wakes up the second thread when the thread ID of the first thread is stored in the thread waiting list corresponding to the ID of the target lock. Threaded; the second thread belongs to the same process as the first thread; or,
第一线程当挂起的时长超过第三阈值时被系统唤醒,获取所述目标锁的锁状态变量。The first thread is awakened by the system when the suspended duration exceeds the third threshold, and acquires the lock state variable of the target lock.
从而有助于简化在第一线程请求中止对目标锁持锁的情况下,第一线程被唤醒的方式。This helps to simplify the way in which the first thread is awakened when the first thread requests to suspend the lock on the target.
在一种可能的设计中,阻塞线程数域包括第一值和阻塞线程数高位标识,第一值为针对目标锁处于阻塞状态的线程数的低位上的值,阻塞线程数高位标识用于指示阻塞线程数高位列表中是否存储有与目标锁的ID对应的第二值,第二值为针对目标锁处于阻塞状态的线程数的高位上的值。从而有助于避免针对目标锁处于阻塞状态的线程数较多时导致溢出。In a possible design, the blocked thread number field includes a first value and a high-order flag of the number of blocked threads. The first value is the low-order value of the number of threads in a blocked state for the target lock, and the high-order flag of the number of blocked threads is used to indicate Whether the second value corresponding to the ID of the target lock is stored in the high list of the number of blocked threads, and the second value is the high value of the number of threads in the blocked state for the target lock. This helps to avoid overflow when the number of threads that are blocked for the target lock is large.
在一种可能的设计中,重复持锁次数域包括第三值和重复持锁次数高位标识,第三值为持锁线程针对目标锁的持锁次数的低位上的值,重复持锁次数高位标识用于指示重复持锁次数高位列表中是否存储有与目标锁的ID和持锁线程的线程ID对应的第四值,第四值为持锁线程针对目标锁的持锁次数的高位上的值。从而有助于避免持锁线程针对目标锁的持锁次数较多时导致溢出。In a possible design, the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks. The third value is the low value of the number of locks held by the lock thread for the target lock, and the number of repeated locks is high. The identifier is used to indicate whether the high-order list of the number of repeated locks stores a fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread. The fourth value is the high-order value of the number of times the lock-holding thread holds the lock for the target lock. value. This helps to avoid overflow when the lock-holding thread holds more locks for the target lock.
在一种可能的设计中,第一线程可以基于下述方式挂起进入阻塞状态:In a possible design, the first thread can suspend and enter the blocking state based on the following methods:
第一线程进行系统调用,将第一线程的状态设置为睡眠态,并将第一线程添加到与目标锁的地址对应的等待队列中。从而有助于简化实现方式。The first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock. This helps simplify the implementation.
第二方面,为本申请实施例的一种电子设备,具体包括处理器和存储器,该存储器中 存储有程序指令;处理器调用存储器中存储的程序指令,用于第一线程请求获取目标锁;The second aspect is an electronic device according to an embodiment of this application, which specifically includes a processor and a memory, where program instructions are stored; the processor calls the program instructions stored in the memory for the first thread to request to acquire the target lock;
其中,第一线程请求获取目标锁,包括:Among them, the first thread requests to acquire the target lock, including:
步骤一、第一线程获取目标锁的锁状态变量,该锁状态变量包括持锁线程标识域和阻塞线程数域,持锁线程标识域用于指示目标锁的持锁线程,阻塞线程数域用于指示针对目标锁处于阻塞状态的线程数; Step 1. The first thread acquires the lock state variable of the target lock. The lock state variable includes the lock-holding thread identification field and the blocked thread number field. The lock-holding thread identification field is used to indicate the lock-holding thread of the target lock, and the blocked thread number field is used To indicate the number of threads that are blocked for the target lock;
步骤二、第一线程检查持锁线程标识域:Step 2: The first thread checks the lock thread identification field:
步骤三、第一线程若检查持锁线程标识域为有效线程且不为第一线程,第一线程检查阻塞线程数域;Step 3: If the first thread checks that the lock-holding thread identification field is a valid thread and is not the first thread, the first thread checks the blocked thread number field;
步骤四、第一线程若检查阻塞线程数域小于第一阈值,第一线程进行自旋等待;Step 4: If the first thread checks that the number of blocked threads is less than the first threshold, the first thread performs spin waiting;
步骤五、第一线程当自旋等待的次数达到第二阈值时,若检查持锁线程标识域为有效线程且不为第一线程,第一线程对阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 5. When the number of spin waits for the first thread reaches the second threshold, if the check that the lock-holding thread identification field is a valid thread and not the first thread, the first thread performs an operation of incrementing the number field of blocked threads and hangs. Start to enter the blocking state.
在一种可能的设计中,在步骤三之后还包括:In a possible design, after step three, it also includes:
步骤六、第一线程若检查阻塞线程数域大于或等于第一阈值,第一线程对阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 6. If the first thread checks that the blocked thread number field is greater than or equal to the first threshold, the first thread performs an operation of adding 1 to the blocked thread number field and suspends and enters the blocking state.
在一种可能的设计中,目标锁的锁状态变量还包括重复持锁次数域,重复持锁次数域用于指示持锁线程针对目标锁的持锁次数。In a possible design, the lock state variable of the target lock also includes a repeated lock count field, and the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock.
在一种可能的设计中,在步骤二之后还包括:In a possible design, after step two, it also includes:
步骤七、第一线程若检查持锁线程标识域为第一线程,第一线程对重复持锁次数域执行加1操作。Step 7. If the first thread checks that the lock-holding thread identification field is the first thread, the first thread performs an operation of adding 1 to the repeated lock-holding times field.
在一种可能的设计中,在步骤二之后还包括:In a possible design, after step two, it also includes:
步骤八、第一线程若检查持锁线程标识域为无效线程,第一线程将持锁线程标识域设置为第一线程,以及将重复持锁次数域设置为初始值。Step 8. If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks field to the initial value.
在一种可能的设计中,处理器调用程序指令,还用于第一线程请求释放目标锁:In a possible design, the processor calls the program instructions, which are also used by the first thread to request the release of the target lock:
其中,第一线程请求释放目标锁,包括:Among them, the first thread requests to release the target lock, including:
步骤九、第一线程获取目标锁的锁状态变量:Step 9. The first thread acquires the lock state variable of the target lock:
步骤十、第一线程检查持锁线程标识域; Step 10. The first thread checks the lock thread identification field;
步骤十一、第一线程若检查持锁线程标识域为第一线程,第一线程检查重复持锁次数域;Step 11. If the first thread checks the lock-holding thread identification field as the first thread, the first thread checks the repeated lock-holding times field;
步骤十二、第一线程若检查重复持锁次数域不为0,第一线程对重复持锁次数域执行减1操作。Step 12: If the first thread checks that the repeated lock count field is not 0, the first thread performs an operation to subtract 1 from the repeated lock count field.
在一种可能的设计中,在步骤十二之后还包括:In a possible design, after step twelve, it also includes:
步骤十三、第一线程若检查重复持锁次数域为0,第一线程将持锁线程标识域设置为无效线程;Step 13. If the first thread checks that the repeated lock count field is 0, the first thread sets the lock thread identification field as an invalid thread;
步骤十四、第一线程检查阻塞线程数域;Step 14. The first thread checks the blocked thread count field;
步骤十五、第一线程若检查阻塞线程数域不为0,第一线程唤醒针对目标锁处于阻塞状态的至少一个线程。Step 15. If the first thread checks that the blocked thread count field is not 0, the first thread wakes up at least one thread that is blocked for the target lock.
在一种可能的设计中,处理器调用程序指令,还用于第一线程请求中止对目标锁持锁:In a possible design, the processor calls the program instructions, which are also used by the first thread to request the suspension of the lock on the target:
其中,第一线程请求中止对目标锁持锁,包括:Among them, the first thread's request to suspend the lock on the target includes:
步骤十六、第一线程获取目标锁的锁状态变量;Step 16. The first thread acquires the lock state variable of the target lock;
步骤十七、第一线程检查持锁线程标识域;Step 17. The first thread checks the lock thread identification field;
步骤十八、第一线程若检查持锁线程标识域为第一线程,将重复持锁次数域保存为第一线程针对目标锁的上下文;Step 18. If the first thread checks that the lock-holding thread identification field is the first thread, save the repeated lock-holding times field as the context of the first thread for the target lock;
步骤十九、第一线程将第一线程的线程标识ID添加到与目标锁的ID对应的线程等待列表中; Step 19. The first thread adds the thread identification ID of the first thread to the thread waiting list corresponding to the ID of the target lock;
步骤二十、第一线程将重复持锁次数域置0,以及将持锁线程标识域设置为无效线程,并挂起进入阻塞状态。Step 20: The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field to be an invalid thread, and suspends and enters a blocking state.
在一种可能的设计中,在步骤二十之后还包括:In a possible design, after step 20, it also includes:
步骤二十一、第一线程当被唤醒后,获取目标锁的锁状态变量;Step 21: After the first thread is awakened, acquire the lock state variable of the target lock;
步骤二十二、第一线程检查持锁线程标识域;Step 22: The first thread checks the lock thread identification field;
步骤二十三、第一线程若检查持锁线程标识域为无效线程,第一线程将持锁线程标识域设置为第一线程,并根据第一线程针对目标锁的上下文,设置重复持锁次数域;Step 23: If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and sets the number of repeated locks according to the context of the first thread for the target lock area;
步骤二十四、第一线程删除与目标锁的ID对应的线程等待列表中的第一线程的线程ID。Step 24: The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
在一种可能的设计中,第一线程可以基于下列方式被唤醒,然后获取目标锁的锁状态变量:In a possible design, the first thread can be awakened based on the following methods, and then acquire the lock state variable of the target lock:
第一线程当被第二线程唤醒后,获取目标锁的锁状态变量,其中,第二线程是当与目标锁的ID对应的线程等待列表中存储有第一线程的线程ID时,唤醒第二线程的;第二线程与第一线程属于同一进程;或者,When the first thread is awakened by the second thread, it acquires the lock state variable of the target lock. The second thread wakes up the second thread when the thread ID of the first thread is stored in the thread waiting list corresponding to the ID of the target lock. Threaded; the second thread belongs to the same process as the first thread; or,
第一线程当挂起的时长超过第三阈值时被系统唤醒,获取目标锁的锁状态变量。The first thread is awakened by the system when the suspended duration exceeds the third threshold, and acquires the lock state variable of the target lock.
在一种可能的设计中,阻塞线程数域包括第一值和阻塞线程数高位标识,第一值为针对目标锁处于阻塞状态的线程数的低位上的值,阻塞线程数高位标识用于指示阻塞线程数高位列表中是否存储有与目标锁的ID对应的第二值,第二值为针对目标锁处于阻塞状态的线程数的高位上的值。In a possible design, the blocked thread number field includes a first value and a high-order flag of the number of blocked threads. The first value is the low-order value of the number of threads in a blocked state for the target lock, and the high-order flag of the number of blocked threads is used to indicate Whether the second value corresponding to the ID of the target lock is stored in the high list of the number of blocked threads, and the second value is the high value of the number of threads in the blocked state for the target lock.
在一种可能的设计中,重复持锁次数域包括第三值和重复持锁次数高位标识,第三值为持锁线程针对目标锁的持锁次数的低位上的值,重复持锁次数高位标识用于指示重复持锁次数高位列表中是否存储有与目标锁的ID和持锁线程的线程ID对应的第四值,第四值为持锁线程针对目标锁的持锁次数的高位上的值。In a possible design, the field of the number of repeated locks includes a third value and a high flag for the number of repeated locks. The third value is the low value of the number of locks held by the lock thread for the target lock, and the number of repeated locks is high. The identifier is used to indicate whether the high-order list of the number of repeated locks stores the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread. The fourth value is the high-order number of the lock-holding thread for the target lock. value.
在一种可能的设计中,第一线程可以基于下列方式挂起进入阻塞状态:In a possible design, the first thread can suspend and enter the blocking state based on the following methods:
第一线程进行系统调用,将第一线程的状态设置为睡眠态,并将第一线程添加到与目标锁的地址对应的等待队列中。The first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock.
第三方面,本申请实施例提供的一种电子设备,包括执行本申请实施例上述第一方面以及第一方面涉及的任一可能设计的方法的装置。In a third aspect, an electronic device provided by an embodiment of the present application includes a device for executing the foregoing first aspect and any possible design method involved in the first aspect of the embodiment of the present application.
第四方面,本申请实施例提供的一种芯片,包括:处理器和接口,用于从存储器中调用并运行所述存储器中存储的程序指令,执行本申请实施例上述第一方面以及第一方面涉及的任一可能设计的方法。In a fourth aspect, a chip provided by an embodiment of the present application includes a processor and an interface, configured to call and run the program instructions stored in the memory from the memory, and execute the above-mentioned first aspect and the first aspect of the embodiment of the present application. Any possible design method involved in the aspect.
第五方面,本申请实施例的一种计算机可读存储介质,该计算机可读存储介质存储有程序指令,当所述程序指令在电子设备上运行时,使得电子设备执行本申请实施例上述第一方面以及第一方面涉及的任一可能设计的方法。In a fifth aspect, a computer-readable storage medium according to an embodiment of the present application. The computer-readable storage medium stores program instructions. When the program instructions run on an electronic device, the electronic device executes the above-mentioned On the one hand and any possible design method involved in the first aspect.
第六方面,本申请实施例的一种计算机程序产品,当所述计算机程序产品在电子设备 上运行时,使得所述电子设备执行实现本申请实施例上述第一方面以及第一方面涉及的任一可能设计的方法。In a sixth aspect, a computer program product of an embodiment of the present application, when the computer program product runs on an electronic device, causes the electronic device to execute and implement the above-mentioned first aspect and any of the first aspects of the embodiments of the present application. A possible design method.
另外,第二方面至第六方面中任一种可能设计方式所带来的技术效果可参见方法部分相关中不同设计方式所带来的技术效果,此处不再赘述。In addition, the technical effects brought by any of the possible design methods of the second aspect to the sixth aspect can be referred to the technical effects brought about by the different design methods in the related method section, which will not be repeated here.
附图说明Description of the drawings
图1为本申请实施例的一种锁状态变量的格式示意图;FIG. 1 is a schematic diagram of the format of a lock state variable according to an embodiment of the application;
图2为本申请实施例的一种电子设备的硬件结构示意图;2 is a schematic diagram of the hardware structure of an electronic device according to an embodiment of the application;
图3为本申请实施例的一种电子设备的软件结构示意图;FIG. 3 is a schematic diagram of the software structure of an electronic device according to an embodiment of the application;
图4为本申请实施例的一种阻塞线程数的高位上的值的存储格式示意图;FIG. 4 is a schematic diagram of a storage format of the high-order value of the number of blocked threads according to an embodiment of the application;
图5为本申请实施例的一种重复持锁次数的高位上的值的存储格式示意图;FIG. 5 is a schematic diagram of a storage format of the high-order value of the number of repeated locks according to an embodiment of the application; FIG.
图6为本申请实施例的另一种锁状态变量的格式示意图;FIG. 6 is a schematic diagram of the format of another lock state variable according to an embodiment of the application;
图7为本申请实施例的一种哈希值的存储格式示意图;FIG. 7 is a schematic diagram of a storage format of a hash value according to an embodiment of the application;
图8为本申请实施例的一种多线程同步方法的流程示意图;FIG. 8 is a schematic flowchart of a multi-thread synchronization method according to an embodiment of the application;
图9为本申请实施例的一种对阻塞线程数域执行加1操作的流程示意图;FIG. 9 is a schematic flowchart of a process of adding 1 to the number field of blocked threads according to an embodiment of the present application;
图10为本申请实施例的一种对重复持锁次数域执行加1操作的流程示意图;FIG. 10 is a schematic diagram of a flow chart of performing an operation of adding 1 to the repeated lock count field according to an embodiment of the present application; FIG.
图11为本申请实施例的另一种多线程同步方法的流程示意图;FIG. 11 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application;
图12为本申请实施例的一种对重复持锁次数域执行减1操作的流程示意图;FIG. 12 is a schematic diagram of a flow chart of performing a minus 1 operation on the repeated lock count field according to an embodiment of the application; FIG.
图13为本申请实施例的另一种多线程同步方法的流程示意图;FIG. 13 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application;
图14为本申请实施例的一种线程等待列表的存储格式示意图;FIG. 14 is a schematic diagram of a storage format of a thread waiting list according to an embodiment of the application;
图15为本申请实施例的一种从线程等待列表删除线程ID的流程示意图;15 is a schematic diagram of a process of deleting a thread ID from a thread waiting list according to an embodiment of the application;
图16为本申请实施例的另一种多线程同步方法的流程示意图;16 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application;
图17为本申请实施例的另一种多线程同步方法的流程示意图;FIG. 17 is a schematic flowchart of another multi-thread synchronization method according to an embodiment of the application;
图18为本申请实施例的另一种锁状态变量的格式示意图;18 is a schematic diagram of another format of a lock state variable according to an embodiment of the application;
图19为本申请实施例的另一种电子设备的结构示意图。FIG. 19 is a schematic structural diagram of another electronic device according to an embodiment of the application.
具体实施方式detailed description
应理解,本申请实施例中“至少一个”是指一个或者多个。“多个”是指两个或两个以上。“和/或”,描述关联对象的关联关系,表示可以存在三种关系。例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B的三种情况。其中A、B可以是单数或者复数。字符“/”一般表示前后关联对象是一种“或”的关系。“以下至少一(项)个”或其类似表达,是指的这些项中的任意组合,包括单项(个)或复数项(个)的任意组合。例如,a、b或c中的至少一项(个),可以表示:a,b,c,a和b,a和c,b和c,或a、b和c七种情况。其中a、b、c中的每一个本身可以是元素,也可以是包含一个或多个元素的集合。It should be understood that “at least one” in the embodiments of the present application refers to one or more. "Multiple" means two or more. "And/or" describes the association relationship of the associated objects, indicating that there can be three types of relationships. For example, A and/or B can mean that: A alone exists, A and B exist at the same time, and B exists alone. Among them, A and B can be singular or plural. The character "/" generally indicates that the associated objects before and after are in an "or" relationship. "The following at least one (item)" or similar expressions refers to any combination of these items, including any combination of single item (item) or plural items (item). For example, at least one item (a) of a, b, or c can represent: a, b, c, a and b, a and c, b and c, or a, b and c. Among them, each of a, b, and c can be an element itself, or a collection containing one or more elements.
在本申请中,“示例的”、“在一些实施例中”、“在另一些实施例中”等用于表示作例子、例证或说明。本申请中被描述为“示例”的任何实施例或设计方案不应被解释为比其它实施例或设计方案更优选或更具优势。确切而言,使用示例的一词旨在以具体方式呈现概念。In this application, "exemplary", "in some embodiments", "in other embodiments", etc. are used to represent examples, illustrations, or illustrations. Any embodiment or design solution described as an "example" in this application should not be construed as being more preferable or advantageous than other embodiments or design solutions. To be precise, the term example is used to present the concept in a concrete way.
需要指出的是,本申请实施例中涉及的“第一”、“第二”等词汇,仅用于区分描述的目的,而不能理解为指示或暗示相对重要性,也不能理解为指示或暗示顺序。It should be pointed out that the terms "first" and "second" involved in the embodiments of this application are only used for the purpose of distinguishing description, and cannot be understood as indicating or implying relative importance, nor shall they be understood as indicating or implying. order.
电子设备通过操作系统(例如安卓操作系统)执行相应的程序,使得用户可以使用电 子设备打电话、发短消息、浏览网页、拍照、玩游戏、看视频等。通常,线程(thread)是程序执行的最小单位。目前,电子设备的操作系统可以支持多线程运行,从而实现多任务的并行处理,提高任务处理效率。然而,在安卓操作系统中,共享资源(例如,共享代码、共享内存、共享数据或变量等)可以被多线程访问。为了避免多线程同时访问资源,通过锁对共享资源保护。以锁1保护共享资源1为例。多线程中只有持有锁1的线程能够实现对共享资源1的访问,而一旦锁1被某一线程持有,则其它线程无法再对锁1持锁,只有当持锁线程释放锁1后,其它线程才获得对锁1的持锁机会。The electronic device executes the corresponding program through the operating system (for example, the Android operating system), so that the user can use the electronic device to make calls, send short messages, browse the web, take photos, play games, watch videos, and so on. Generally, a thread is the smallest unit of program execution. At present, the operating system of an electronic device can support multi-threaded operation, thereby realizing parallel processing of multi-tasks and improving task processing efficiency. However, in the Android operating system, shared resources (for example, shared code, shared memory, shared data or variables, etc.) can be accessed by multiple threads. In order to prevent multiple threads from accessing resources at the same time, shared resources are protected by locks. Take lock 1 to protect shared resource 1 as an example. In multithreading, only the thread holding lock 1 can access shared resource 1. Once lock 1 is held by a thread, other threads can no longer hold lock 1 and only after the lock holding thread releases lock 1 , Other threads get the opportunity to hold lock 1.
现有技术中,线程是采用锁膨胀策略来竞争持锁的。具体的,线程是根据锁状态变量(lockword)实现竞争持锁的,换句话说,线程是根据锁状态变量,来判断是否能够对锁1持锁的。In the prior art, threads use a lock expansion strategy to compete for locks. Specifically, the thread realizes the competition to hold the lock according to the lock state variable (lockword). In other words, the thread judges whether it can hold the lock 1 according to the lock state variable.
示例的,在安卓操作系统中,lockword的格式如图1所示,包括状态位、预留位(reserve)、重复持锁次数域和持锁线程标识域。状态位用于指示锁状态。lockword的重复持锁次数域和持锁线程标识域指示的信息与锁状态有关。例如,状态位设置为01时指示重锁状态,而在状态位设置为01的情况下,重复持锁次数域和持锁线程标识域用于指示monitor对象;状态位为00时轻锁状态,在状态位设置为00的情况下,重复持锁次数域用于指示持锁线程针对锁的持锁次数,持锁线程标识域用于指示锁的持锁线程。For example, in the Android operating system, the format of the lockword is shown in FIG. 1, and includes a status bit, a reserved bit (reserve), a field for the number of repeated locks, and a lock thread identification field. The status bit is used to indicate the lock status. The information indicated by the repeated lock count field and the lock thread identification field of the lockword is related to the lock state. For example, when the status bit is set to 01, it indicates the relock state, and when the status bit is set to 01, the repeated lock count field and the lock thread identification field are used to indicate the monitor object; when the status bit is 00, the light lock state is used. In the case that the status bit is set to 00, the number of repeated locks field is used to indicate the number of locks held by the lock-holding thread for the lock, and the lock-holding thread identification field is used to indicate the lock-holding thread of the lock.
以线程1请求获取锁1为例。线程1请求获取锁1,具体包括以下步骤:Take thread 1 requesting lock 1 as an example. Thread 1 requests to acquire lock 1, which specifically includes the following steps:
首先,线程1获取lockword。First, thread 1 acquires the lockword.
其次,线程1检查持锁线程标识域。Second, thread 1 checks the lock-holding thread ID field.
一种情况:若线程1检查持锁线程标识域为除线程1以外的其它线程,线程1继续检查状态位。One situation: If thread 1 checks the lock-holding thread identification field to be a thread other than thread 1, thread 1 continues to check the status bit.
若线程1检查状态位为重锁状态,线程1挂起进入阻塞状态。If the thread 1 checks the status bit for the relocked state, the thread 1 suspends and enters the blocking state.
若线程1检查状态位为轻锁状态,则进行自旋等待。在自旋等待达到某一阈值时,若检查持锁线程标识域仍为出线程1以外的其它线程,线程1将锁1的持锁线程挂起,分配monitor对象,将状态位设置为重锁状态,再唤醒锁1的持锁线程,然后线程1挂起进入阻塞状态。If the thread 1 checks the status bit for the light lock state, it performs spin waiting. When the spin waiting reaches a certain threshold, if the check-holding thread identification field is still a thread other than thread 1, thread 1 suspends the lock-holding thread of lock 1, allocates a monitor object, and sets the status bit to relock State, and then wake up the lock holding thread of lock 1, and then thread 1 suspends and enters the blocking state.
另一种情况:若线程1检查锁线程标识域为线程1,线程1对重复持锁次数域执行加1操作。若检查重复持锁次数域达到某一阈值,线程1分配monitor对象,将状态位设置为重锁状态。Another case: if thread 1 checks the lock thread identification field to be thread 1, thread 1 performs an operation to add 1 to the repeated lock count field. If the check repeated lock count domain reaches a certain threshold, thread 1 allocates a monitor object and sets the status bit to the relock state.
又一种情况,若线程1检查锁线程标识域为无效线程,即没有线程对锁1持锁,线程1将锁状态标识域设置为第一线程,并对重复持锁次数域初始化。In another case, if thread 1 checks the lock thread identification field as an invalid thread, that is, no thread holds a lock on lock 1, thread 1 sets the lock state identification field as the first thread and initializes the repeated lock count field.
从上述内容可以看出,线程基于图1所示的锁状态变量,请求获取锁或请求持锁时,由于持锁线程和非持锁线程均可以对状态位进行操作,因此,在状态位为轻锁状态下,如果非持锁线程需要修改状态位,为了避免持锁线程也对状态位进行修改,因此非持锁线程需要将持锁线程挂起,并在状态位修改好后,再唤醒持锁线程。另外,不管是持锁线程,还是非持锁线程,将状态位由轻锁状态修改为重锁状态,均需要分配monitor对象,因此,线程基于现有的lockword来判断是否能够持锁的机制,容易增加持锁线程访问共享资源的时长,导致电子设备中持锁线程所在的应用程序的响应时延增大,从而增大了电子设备卡顿的概率,降低用户体验。As can be seen from the above content, based on the lock state variable shown in Figure 1, when a thread requests to acquire a lock or request to hold a lock, since both the lock-holding thread and the non-lock-holding thread can operate on the status bit, the status bit is In the light lock state, if the non-locking thread needs to modify the status bit, in order to avoid that the lock-holding thread also modifies the status bit, the non-locking thread needs to suspend the lock-holding thread and wake up after the status bit is modified. Lock thread. In addition, whether it is a lock-holding thread or a non-lock-holding thread, to modify the status bit from the light-locked state to the heavy-locked state, a monitor object needs to be allocated. Therefore, the thread judges whether the lock can be held based on the existing lockword. It is easy to increase the length of time that the lock-holding thread accesses the shared resource, resulting in an increase in the response delay of the application in which the lock-holding thread is located in the electronic device, thereby increasing the probability of the electronic device being stuck and reducing the user experience.
有鉴于此,本申请实施例提供了一种多线程同步方法,重新对锁状态变量(lockword) 进行了定义,使得锁状态变量包括持锁线程标识域、重复持锁次数域和阻塞线程数域,其中,持锁线程标识域用于指示目标锁的持锁线程,重复持锁次数域用于指示持锁线程针对该目标锁的持锁次数,阻塞线程数域用于指示针对该目标锁处于阻塞状态的线程数,由于持锁线程标识域和重复持锁次数域仅由持锁线程设置,阻塞线程数域仅由非持锁线程设置,因而,使得非持锁线程自旋等待达到某一阈值时,若检查持锁线程标识域为其他线程,该非持锁线程对阻塞线程数域执行加1操作,并挂起进入阻塞状态,与现有技术相比,无需先将持锁线程挂起,也无需分配monitor对象,对阻塞线程数域执行加1操作,因而减小了持锁线程访问共享资源的时长,降低了电子设备卡顿的概率,有助于提高用户体验。In view of this, the embodiment of the present application provides a multi-thread synchronization method. The lock state variable (lockword) is redefined, so that the lock state variable includes the lock thread identification field, the repeated lock count field, and the blocked thread number field. , Where the lock thread identification field is used to indicate the lock thread of the target lock, the repeated lock count field is used to indicate the number of locks held by the lock thread for the target lock, and the blocked thread number field is used to indicate that the target lock is in The number of threads in the blocked state, because the lock-holding thread identification field and the repeated lock-holding times field are only set by the lock-holding thread, and the blocked thread number field is only set by the non-locking thread. Therefore, the non-locking thread's spin waiting reaches a certain value. When the threshold is set, if the lock-holding thread identification field is checked for other threads, the non-lock-holding thread performs an increment operation on the blocked thread number field and suspends into the blocking state. Compared with the prior art, there is no need to hang the lock-holding thread first. As a result, there is no need to allocate monitor objects, and perform an operation of adding 1 to the blocked thread number field, thus reducing the time length of the lock-holding thread accessing shared resources, reducing the probability of electronic device jams, and helping to improve user experience.
示例的,本申请实施例的电子设备可以为手机、平板电脑、可穿戴设备、车载设备、增强现实(augmented reality,AR)/虚拟现实(virtual reality,VR)设备、笔记本电脑、超级移动个人计算机(ultra-mobile personal computer,UMPC)、上网本、个人数字助理(personal digital assistant,PDA)等,本申请实施例对电子设备的具体类型不作任何限制。For example, the electronic devices in the embodiments of the present application may be mobile phones, tablet computers, wearable devices, in-vehicle devices, augmented reality (AR)/virtual reality (VR) devices, notebook computers, super mobile personal computers (ultra-mobile personal computer, UMPC), netbook, personal digital assistant (personal digital assistant, PDA), etc. The embodiments of this application do not impose any restrictions on the specific types of electronic devices.
示例的,如图2所示,为本申请实施例的一种电子设备的结构示意图。具体的,如图所示,电子设备包括处理器110、外部存储器接口120、内部存储器121、通用串行总线(universal serial bus,USB)接口130、充电管理模块140、电源管理模块141、电池142、天线1、天线2、移动通信模块150、无线通信模块160、音频模块170、扬声器170A、受话器170B、麦克风170C、耳机接口170D、传感器模块180、按键190、马达191、指示器192、摄像头193、显示屏194、以及用户标识模块(subscriber identification module,SIM)卡接口195等。其中,传感器模块180可以包括压力传感器、陀螺仪传感器、气压传感器、磁传感器、加速度传感器、距离传感器、接近光传感器、指纹传感器、温度传感器、触摸传感器、环境光传感器、骨传导传感器等。As an example, as shown in FIG. 2, it is a schematic structural diagram of an electronic device according to an embodiment of the application. Specifically, as shown in the figure, the electronic device includes a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, and a battery 142 , Antenna 1, antenna 2, mobile communication 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 , A display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 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, etc.
处理器110可以包括一个或多个处理单元。例如:处理器110可以包括应用处理器(application processor,AP)、调制解调器(modem)、图形处理器(graphics processing unit,GPU)、图像信号处理器(image signal processor,ISP)、控制器、视频编解码器、数字信号处理器(digital signal processor,DSP)、基带处理器、和/或神经网络处理器(neural-network processing unit,NPU)等。其中,不同的处理单元可以是独立的器件,或者,两个或更多个不同的处理单元也可以集成在一个器件中。The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (AP), a modem (modem), a graphics processing unit (GPU), an image signal processor (ISP), a controller, and a video editor. Decoder, digital signal processor (digital signal processor, DSP), baseband processor, and/or neural network processor (neural-network processing unit, NPU), etc. Among them, the different processing units may be independent devices, or two or more different processing units may also be integrated into one device.
控制器可以是电子设备的神经中枢和指挥中心。控制器可以根据指令操作码和时序信号,产生操作控制信号,完成取指令和执行指令的控制。The controller can be the nerve center and command center of the electronic device. The controller can generate operation control signals according to the instruction operation code and timing signals to complete the control of fetching and executing instructions.
处理器110中还可以设置存储器,用于存储指令和数据。在一些实施例中,处理器110中的存储器为高速缓冲存储器。该存储器可以保存处理器110刚用过或循环使用的指令或数据。如果处理器110需要再次使用该指令或数据,可从所述存储器中直接调用。避免了重复存取,减少了处理器110的等待时间,因而提高了系统的效率。A memory may also be provided in the processor 110 to store instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory can store instructions or data that the processor 110 has just used or used cyclically. If the processor 110 needs to use the instruction or data again, it can be directly called from the memory. Repeated accesses are avoided, the waiting time of the processor 110 is reduced, and the efficiency of the system is improved.
在一些实施例中,处理器110可以包括一个或多个接口。例如,处理器110包括通用串行总线(universal serial bus,USB)接口130、用户标识模块(subscriber identity module,SIM)接口195。再例如,处理器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)接口等。In some embodiments, the processor 110 may include one or more interfaces. For example, the processor 110 includes a universal serial bus (USB) interface 130 and a subscriber identity module (SIM) interface 195. For another example, the processor 110 may also include an integrated circuit (I2C) interface, an integrated circuit audio (inter-integrated circuit sound, I2S) interface, a pulse code modulation (PCM) interface, and a universal asynchronous A universal asynchronous receiver/transmitter (UART) interface, a mobile industry processor interface (MIPI), and/or a general-purpose input/output (GPIO) interface, etc.
可以理解的是,本申请实施例示意的各模块间的接口连接关系,只是示意性说明,并不构成对电子设备的结构限定。在本申请另一些实施例中,电子设备也可以采用上述实施例中不同的接口连接方式,或多种接口连接方式的组合。It can be understood that the interface connection relationship between the modules illustrated in the embodiment of the present application is merely a schematic description, and does not constitute a structural limitation of the electronic device. In other embodiments of the present application, the electronic device may also adopt different interface connection modes in the foregoing embodiments, or a combination of multiple interface connection modes.
USB接口130是符合USB标准规范的接口,具体可以是Mini USB接口、Micro USB接口、USB Type C接口等。USB接口130可以用于连接充电器为电子设备充电,也可以用于电子设备与外围设备之间传输数据。也可以用于连接耳机,通过耳机播放音频。该接口还可以用于连接其他电子设备,例如AR设备等。The USB interface 130 is an interface that complies with the USB standard specification, and specifically may be a Mini USB interface, a Micro USB interface, a USB Type C interface, and the like. The USB interface 130 can be used to connect a charger to charge the electronic device, and can also be used to transfer data between the electronic device and the peripheral device. It can also be used to connect headphones and play audio through the headphones. This interface can also be used to connect to other electronic devices, such as AR devices.
SIM卡接口195用于连接SIM卡。SIM卡可以通过插入SIM卡接口195,或从SIM卡接口195拔出,实现和电子设备的接触和分离。电子设备可以支持1个或N个SIM卡接口,N为大于1的正整数。SIM卡接口195可以支持Nano SIM卡、Micro SIM卡、SIM卡等。同一个SIM卡接口195可以同时插入多张卡。所述多张卡的类型可以相同,也可以不同。SIM卡接口195也可以兼容不同类型的SIM卡。SIM卡接口195也可以兼容外部存储卡。电子设备通过SIM卡和网络交互,实现通话以及数据通信等功能。在一些实施例中,电子设备采用eSIM,即:嵌入式SIM卡。eSIM卡可以嵌在电子设备中,不能和电子设备分离。The SIM card interface 195 is used to connect to the SIM card. The SIM card can be inserted into the SIM card interface 195 or pulled out from the SIM card interface 195 to achieve contact and separation with the electronic device. The electronic device can support 1 or N SIM card interfaces, and N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, etc. The same SIM card interface 195 can insert multiple cards at the same time. The types of the multiple cards can be the same or different. The SIM card interface 195 can also be compatible with different types of SIM cards. The SIM card interface 195 may also be compatible with external memory cards. The electronic device interacts with the network through the SIM card to realize functions such as call and data communication. In some embodiments, the electronic device adopts an eSIM, that is, an embedded SIM card. The eSIM card can be embedded in the electronic device and cannot be separated from the electronic device.
充电管理模块140用于从充电器接收充电输入。其中,充电器可以是无线充电器,也可以是有线充电器。在一些有线充电的实施例中,充电管理模块140可以通过USB接口130接收有线充电器的充电输入。在一些无线充电的实施例中,充电管理模块140可以通过电子设备的无线充电线圈接收无线充电输入。充电管理模块140为电池142充电的同时,还可以通过电源管理模块141为电子设备供电。The charging management module 140 is used to receive charging input from the charger. Among them, the charger can be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 may receive the charging input of the wired charger through the USB interface 130. In some embodiments of wireless charging, the charging management module 140 may receive the wireless charging input through the wireless charging coil of the electronic device. While the charging management module 140 charges the battery 142, it can also supply power to the electronic device through the power management module 141.
电源管理模块141用于连接电池142、充电管理模块140与处理器110。电源管理模块141接收电池142和/或充电管理模块140的输入,为处理器110、内部存储器121、外部存储器、显示屏194、摄像头193和无线通信模块160等供电。电源管理模块141还可以用于监测电池容量、电池循环次数、电池健康状态(漏电、阻抗)等参数。在其他一些实施例中,电源管理模块141也可以设置于处理器110中。在另一些实施例中,电源管理模块141和充电管理模块140也可以设置于同一个器件中。The power management module 141 is used to connect the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives input from the battery 142 and/or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the external memory, the display screen 194, the camera 193, and the wireless communication module 160. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle times, and battery health status (leakage, impedance). In some other embodiments, the power management module 141 may also be provided in the processor 110. In other embodiments, the power management module 141 and the charging management module 140 may also be provided in the same device.
电子设备的无线通信功能可以通过天线1、天线2、移动通信模块150、无线通信模块160、调制解调器以及基带处理器等实现。The wireless communication function of the electronic device can be realized by the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem, and the baseband processor.
天线1和天线2用于发射和接收电磁波信号。电子设备中的每个天线可用于覆盖单个或多个通信频带。不同的天线还可以复用,以提高天线的利用率。例如:可以将天线1复用为无线局域网的分集天线。在另外一些实施例中,天线可以和调谐开关结合使用。The antenna 1 and the antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in an electronic device can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve antenna utilization. For example: Antenna 1 can be multiplexed as a diversity antenna of a wireless local area network. In other embodiments, the antenna can be used in combination with a tuning switch.
移动通信模块150可以提供应用在电子设备上的包括2G/3G/4G/5G等制式的无线通信的解决方案。移动通信模块150可以包括至少一个滤波器、开关、功率放大器、低噪声放大器(low noise amplifier,LNA)等。The mobile communication module 150 can provide a wireless communication solution including 2G/3G/4G/5G and other standards applied to electronic devices. The mobile communication module 150 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), and the like.
无线通信模块160包括可以提供应用在电子设备上的包括无线局域网(wireless local area networks,WLAN)(如Wi-Fi网络)、蓝牙(bluetooth,BT)、全球导航卫星系统(global navigation satellite system,GNSS)、调频(frequency modulation,FM)、近距离无线通信技术(near field communication,NFC)、红外技术(infrared,IR)等无线通信的解决方案。The wireless communication module 160 includes wireless local area networks (WLAN) (such as Wi-Fi networks), Bluetooth (BT), and global navigation satellite system (GNSS) that can be applied to electronic devices. ), frequency modulation (FM), near field communication (NFC), infrared technology (infrared, IR) and other wireless communication solutions.
在一些实施例中,电子设备的天线1和移动通信模块150耦合,天线2和无线通信模块160耦合,使得电子设备可以通过无线通信技术与网络以及其他设备通信。所述无线通 信技术可以包括全球移动通讯系统(global system for mobile communications,GSM)、通用分组无线服务(general packet radio service,GPRS)、码分多址接入(code division multiple access,CDMA)、宽带码分多址(wideband code division multiple access,WCDMA)、时分码分多址(time-division code division multiple access,TD-SCDMA)、长期演进(long term evolution,LTE)、BT、GNSS、WLAN、NFC、FM和/或IR技术等。所述GNSS可以包括全球卫星定位系统(global positioning system,GPS)、全球导航卫星系统(global navigation satellite system,GLONASS)、北斗卫星导航系统(beidou navigation satellite system,BDS)、准天顶卫星系统(quasi-zenith satellite system,QZSS)和/或星基增强系统(satellite based augmentation systems,SBAS)。In some embodiments, the antenna 1 of the electronic device is coupled with the mobile communication module 150, and the antenna 2 is coupled with the wireless communication module 160, so that the electronic device can communicate with the network and other devices through wireless communication technology. The wireless communication technology may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), broadband Code division multiple access (wideband code division multiple access, WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC , FM and/or IR technology, etc. The GNSS may include global positioning system (GPS), global navigation satellite system (GLONASS), Beidou navigation satellite system (BDS), and quasi-zenith satellite system (quasi). -zenith satellite system, QZSS) and/or satellite-based augmentation systems (SBAS).
电子设备通过GPU、显示屏194以及应用处理器等实现显示功能。显示屏194用于显示图像、视频等。显示屏194包括显示面板。显示面板可以采用液晶显示屏(liquid crystal display,LCD)、有机发光二极管(organic light-emitting diode,OLED)、有源矩阵有机发光二极体或主动矩阵有机发光二极体(active-matrix organic light emitting diode的,AMOLED)、柔性发光二极管(flex light-emitting diode,FLED)、Miniled、MicroLed、Micro-oLed、量子点发光二极管(quantum dot light emitting diodes,QLED)等。在一些实施例中,电子设备可以包括1个或N个显示屏194,N为大于1的正整数。The electronic device realizes the display function through the GPU, the display screen 194, and the application processor. The display screen 194 is used to display images, videos, and the like. The display screen 194 includes a display panel. The display panel can adopt liquid crystal display (LCD), organic light-emitting diode (OLED), 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. In some embodiments, the electronic device may include one or N display screens 194, and N is a positive integer greater than one.
电子设备可以通过ISP、摄像头193、视频编解码器、GPU、显示屏194以及应用处理器等实现拍摄功能。ISP用于处理摄像头193反馈的数据。例如,拍照时,打开快门,光线通过镜头被传递到摄像头感光元件上,光信号转换为电信号,摄像头感光元件将所述电信号传递给ISP处理,转化为肉眼可见的图像。ISP还可以对图像的噪点,亮度,肤色进行算法优化。ISP还可以对拍摄场景的曝光、色温等参数优化。在一些实施例中,ISP可以设置在摄像头193中。The electronic device can realize the shooting function through ISP, camera 193, video codec, GPU, display screen 194 and application processor. The ISP is used to process the data fed back by the camera 193. For example, when taking a picture, the shutter is opened, the light is transmitted to the photosensitive element of the camera through the lens, the light signal is converted into an electrical signal, and the photosensitive element of the camera transmits the electrical signal to the ISP for processing and is converted into an image visible to the naked eye. ISP can also optimize the image noise, brightness, and skin color. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, the ISP may be provided in the camera 193.
摄像头193用于捕获静态图像或视频。物体通过镜头生成光学图像投射到感光元件。感光元件可以是电荷耦合器件(charge coupled device,CCD)或互补金属氧化物半导体(complementary metal-oxide-semiconductor,CMOS)光电晶体管。感光元件把光信号转换成电信号,之后将电信号传递给ISP转换成数字图像信号。ISP将数字图像信号输出到DSP加工处理。DSP将数字图像信号转换成标准的RGB,YUV等格式的图像信号。在一些实施例中,电子设备可以包括1个或N个摄像头193,N为大于1的正整数。The camera 193 is used to capture still images or videos. The object generates an optical image through the lens and is projected to the photosensitive element. The photosensitive element may be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the optical signal into an electrical signal, and then transfers the electrical signal to the ISP to convert it into a digital image signal. ISP outputs digital image signals to DSP for processing. DSP converts digital image signals into standard RGB, YUV and other formats of image signals. In some embodiments, the electronic device may include 1 or N cameras 193, and N is a positive integer greater than 1.
外部存储器接口120可以用于连接外部存储卡,例如Micro SD卡,实现扩展电子设备的存储能力。外部存储卡通过外部存储器接口120与处理器110通信,实现数据存储功能。例如将音乐、视频等文件保存在外部存储卡中。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. 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.
内部存储器121包括运行内存(memory)和内置存储器。其中,运行内存可以用于存储计算机可执行程序代码或数据等。所述可执行程序代码包括指令。处理器110通过运行存储在运行内存的指令,从而执行电子设备的各种功能应用以及数据处理。例如,运行内存可以包括高速随机存取存储器。而内置存储器又可以称之为内置外存等,可以用于存储程序和/或数据。例如,内置存储器可以存储操作系统、应用程序等。电子设备通常将内置存储器中的程序和/或数据加载到运行内存后,使得处理器110运行相应的程序和/或数据,实现相应的功能。此外,内部存储器121可以包括非易失性存储器,例如至少一个磁盘存储器件、闪存器件、通用闪存存储器(universal flash storage,UFS)等。The internal memory 121 includes a running memory (memory) and a built-in memory. Among them, the running memory can be used to store computer executable program code or data. The executable program code includes instructions. The processor 110 executes various functional applications and data processing of the electronic device by running instructions stored in the running memory. For example, the running memory may include high-speed random access memory. The built-in memory can also be called a built-in external memory, etc., which can be used to store programs and/or data. For example, the built-in memory can store an operating system, application programs, and so on. The electronic device usually loads the program and/or data in the built-in memory into the running memory, so that the processor 110 runs the corresponding program and/or data to realize the corresponding function. In addition, the internal memory 121 may 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.
电子设备可以通过音频模块170、扬声器170A、受话器170B、麦克风170C、耳机接 口170D、以及应用处理器等实现音频功能。例如音乐播放、录音等。The electronic device can implement audio functions through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the earphone interface 170D, and the application processor. For example, music playback, recording, etc.
按键190包括开机键、音量键等。按键190可以是机械按键。也可以是触摸式按键。电子设备可以接收按键输入,产生与电子设备的用户设置以及功能控制有关的键信号输入。The button 190 includes a power button, a volume button, and so on. The button 190 may be a mechanical button. It can also be a touch button. The electronic device can receive key input, and generate key signal input related to user settings and function control of the electronic device.
马达191可以产生振动提示。马达191可以用于来电振动提示,也可以用于触摸振动反馈。例如,作用于不同应用(例如拍照、音频播放等)的触摸操作,可以对应不同的振动反馈效果。作用于显示屏194不同区域的触摸操作,马达191也可对应不同的振动反馈效果。不同的应用场景(例如:时间提醒、接收信息、闹钟、游戏等)也可以对应不同的振动反馈效果。触摸振动反馈效果还可以支持自定义。The motor 191 can generate vibration prompts. The motor 191 can be used for incoming call vibration notification, and can also be used for touch vibration feedback. For example, touch operations that act on different applications (such as photographing, audio playback, etc.) can correspond to different vibration feedback effects. Acting on touch operations in different areas of the display screen 194, the motor 191 can also correspond to different vibration feedback effects. Different application scenarios (for example: time reminding, receiving information, alarm clock, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also support customization.
指示器192可以是指示灯,可以用于指示充电状态、电量变化,也可以用于指示消息、未接来电、通知等。The indicator 192 may be an indicator light, which may be used to indicate the charging status, power change, or to indicate messages, missed calls, notifications, and the like.
可以理解的是,本申请实施例示意的结构并不构成对电子设备的具体限定。在本申请另一些实施例中,电子设备可以包括比图示更多或更少的部件,或者组合某些部件,或者拆分某些部件,或者不同的部件布置。图示的部件可以以硬件,软件或软件和硬件的组合实现。It can be understood that the structure illustrated in the embodiments of the present application does not constitute a specific limitation on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than those shown in the figure, or combine certain components, or split certain components, or arrange different components. The illustrated components can be implemented in hardware, software, or a combination of software and hardware.
本申请实施例中电子设备的软件系统又可以称之为操作系统,支持多线程运行,可以采用分层架构、事件驱动架构、微核架构、微服务架构、或云架构。其中,分层架构(如,安卓操作系统)将软件分成若干个层,每一层都有清晰的角色和分工,层与层之间通过软件接口通信。The software system of the electronic device in the embodiment of the present application may also be referred to as an operating system, which supports multi-threaded operation, and may adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. Among them, the layered architecture (for example, the Android operating system) divides the software into several layers, each layer has a clear role and division of labor, and communication between layers is through software interfaces.
具体的,图3示出本申请实施例的电子设备的一种软件系统的结构框图。分层架构将软件分成若干个层,每一层都有清晰的角色和分工。层与层之间通过软件接口通信。在一些实施例中,将Android系统分为四层,从上至下分别为应用程序层、应用程序框架层、安卓运行时(Android runtime)和系统库、以及内核层。应用程序层可以包括一系列应用程序包。Specifically, FIG. 3 shows a structural block diagram of a software system of an electronic device according to an embodiment of the present application. 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. In some embodiments, the Android system is divided into four layers, from top to bottom, the application layer, the application framework layer, the Android runtime and system library, and the kernel layer. The application layer can include a series of application packages.
如图3所示,应用程序包可以包括相机、图库、日历、通话、地图、导航、WLAN、蓝牙、音乐、视频、短信息等应用程序。As shown in Figure 3, the application package may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message, etc.
应用程序框架层为应用程序层的应用程序提供应用编程接口(application programming interface,API)和编程框架。应用程序框架层包括一些预先定义的函数。The application framework layer provides an application programming interface (application programming interface, API) and a programming framework for applications in the application layer. The application framework layer includes some predefined functions.
如图3所示,应用程序框架层可以包括窗口管理器、内容提供器、视图系统、电话管理器、资源管理器、通知管理器等。As shown in Figure 3, the application framework layer can include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and so on.
窗口管理器用于管理窗口程序。窗口管理器可以获取显示屏大小,判断是否有状态栏、锁定屏幕、截取屏幕等。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, take a screenshot, etc.
内容提供器用来存放和获取数据,并使这些数据可以被应用程序访问。所述数据可以包括视频、图像、音频、拨打和接听的电话、浏览历史和书签、电话簿等。The content provider is used to store and retrieve data and make these data accessible to applications. The data may include video, image, audio, phone calls made and received, browsing history and bookmarks, phone book, etc.
视图系统包括可视控件,例如显示文字的控件,显示图片的控件等。视图系统可用于构建应用程序。显示界面可以由一个或多个视图组成的。例如,包括短信通知图标的显示界面,可以包括显示文字的视图以及显示图片的视图。The view system includes visual controls, such as controls that display text, controls that display pictures, and so on. 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 that includes a short message notification icon may include a view that displays text and a view that displays pictures.
电话管理器用于提供电子设备100的通信功能。例如通话状态的管理(包括接通、挂断等)。The phone manager is used to provide the communication function of the electronic device 100. For example, the management of the call status (including connected, hanged 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 the application to display notification information in the status bar, which can be used to convey notification-type messages, and it can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, and so on. 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 a 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. For example, text messages are prompted in the status bar, prompt sounds, electronic devices vibrate, and indicator lights flash.
Android Runtime包括核心库和虚拟机(该虚拟机又可以成为java虚拟机,如Dalvik虚拟机)。Android runtime负责安卓系统的调度和管理。Android Runtime includes a core library and a virtual machine (the virtual machine can become a java virtual machine, such as the Dalvik virtual machine). Android runtime is responsible for the scheduling and management of the Android system.
核心库包含两部分:一部分是java语言需要调用的功能函数,另一部分是安卓的核心库。The core library consists of two parts: one part is the function functions that the java language needs to call, and the other part is the core library of Android.
应用程序层和应用程序框架层运行在虚拟机中。虚拟机将应用程序层和应用程序框架层的java字节码(bytecode)翻译为二进制执行文件。虚拟机用于执行对象生命周期的管理、堆栈管理、线程管理、安全和异常的管理、以及垃圾回收等功能。示例的,本申请实施例的多线程访问方法是通过虚拟机执行的。其中,当图3所示的软件架构为安卓操作系统时,虚拟机可以为Dalvik/ART、或者HUAWEI maple等。当图3所示的软件架构为Linux或者Windows等操作系统时,虚拟机可以为openJDK虚拟机。The application layer and the application framework layer run in a virtual machine. The virtual machine translates the Java bytecode of the application layer and the application framework layer into a binary executable file. The virtual machine is used to perform functions such as object life cycle management, stack management, thread management, security and exception management, and garbage collection. By way of example, the multithreaded access method in the embodiment of the present application is executed through a virtual machine. Wherein, when the software architecture shown in FIG. 3 is an Android operating system, the virtual machine may be Dalvik/ART, or HUAWEI maple, etc. When the software architecture shown in FIG. 3 is an operating system such as Linux or Windows, the virtual machine may be an openJDK virtual machine.
需要说明的是,虚拟机可以是在启动应用程序时创建的,也可以是在预先安装好的,对此不作限定。例如,在安卓操作系统中,虚拟机可以是在启动应用程序时创建的。再例如,在Widows操作系统中,虚拟机可以是用户根据自身需求安装在电子设备上的。It should be noted that the virtual machine may be created when the application is started, or it may be pre-installed, which is not limited. For example, in the Android operating system, the virtual machine may be created when the application is started. For another example, in the Widows operating system, the virtual machine can be installed on the electronic device by the user according to his own needs.
系统库可以包括多个功能模块。例如:表面管理器(surface manager)、媒体库(Media Libraries)、三维图形处理库(例如:OpenGL ES)、2D图形引擎(例如:SGL)等。The system library can include multiple functional modules. For example: surface manager (surface manager), media library (Media Libraries), three-dimensional graphics processing library (for example: OpenGL ES), 2D graphics engine (for example: SGL), etc.
表面管理器用于对显示子系统进行管理,并且为多个应用程序提供了2D和3D图层的融合。The surface manager is used to manage the display subsystem and provides a combination of 2D and 3D layers for multiple applications.
媒体库支持多种常用的音频,视频格式回放和录制,以及静态图像文件等。媒体库可以支持多种音视频编码格式,例如:MPEG4、H.264,MP3、AAC、AMR、JPG、PNG等。The media library supports playback and recording of a variety of commonly used audio and video formats, as well as still image files. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
三维图形处理库用于实现三维图形绘图,图像渲染、合成和图层处理等。The 3D graphics processing library is used to realize 3D graphics drawing, image rendering, synthesis and layer processing, etc.
2D图形引擎是2D绘图的绘图引擎。The 2D graphics engine is a drawing engine for 2D drawing.
内核层是硬件和软件之间的层。内核层至少包含显示驱动、摄像头驱动、音频驱动、传感器驱动。The kernel layer is the layer between hardware and software. The kernel layer includes at least display driver, camera driver, audio driver, and sensor driver.
首先,对本申请实施例中涉及的部分名词进行解释,以便于本领域技术人员理解。First, some terms involved in the embodiments of the present application are explained to facilitate the understanding of those skilled in the art.
1、锁。本申请实施例的锁用于保护共享资源不被多个线程同时访问。本申请实施例中的共享资源可以被多个线程访问,例如内存、代码、变量或数据等,对此不作限定。示例的,锁可以为对象(object,obj)或者对象的一部分,其中,对象用于保护共享资源,为一个类的实例,其中,类为一个模板,描述了一类对象的行为和状态。又示例的,锁还可以为地址或者其他唯一确定的参数或结构体等。1. Lock. The lock in the embodiment of the present application is used to protect shared resources from being accessed by multiple threads at the same time. The shared resources in the embodiments of the present application can be accessed by multiple threads, such as memory, code, variables, or data, which is not limited. For example, a lock may be an object (object, obj) or a part of an object, where the object is used to protect shared resources and is an instance of a class, where the class is a template that describes the behavior and state of a class of objects. For another example, the lock may also be an address or other uniquely determined parameters or structures.
2、锁状态变量(lockword)。本申请实施例中,lockword包括持锁线程标识域、重复持锁次数域和阻塞线程数域,可以为32比特,也可以为64比特,本申请实施例对lockword的比特数不作限定。进一步的,在另一些实施例中,lockword还可以包括预留位,以便于后续对进行功能或操作扩展。2. The lock state variable (lockword). In the embodiment of the present application, the lockword includes a lock thread identification field, a repeated lock count field, and a blocked thread number field, which may be 32 bits or 64 bits. The embodiment of the present application does not limit the number of bits of the lockword. Further, in some other embodiments, the lockword may also include reserved bits to facilitate subsequent expansion of functions or operations.
具体的,lockword与锁是一一对应的,不同的锁的lockword是不同的。以目标锁的 lockword为例。Specifically, lockword and lock have a one-to-one correspondence, and different locks have different lockwords. Take the lockword of the target lock as an example.
持锁线程标识域用于指示目标锁的持锁线程。示例的,以第一线程为例,第一线程可以通过将持锁线程标识域设置为第一线程获得目标锁。具体的,第一线程可以通过将第一线程的线程ID设置在持锁线程标识域上,实现将持锁线程标识域设置为第一线程。The lock-holding thread identification field is used to indicate the lock-holding thread of the target lock. For example, taking the first thread as an example, the first thread may obtain the target lock by setting the lock-holding thread identification field to the first thread. Specifically, the first thread may set the lock-holding thread identification field as the first thread by setting the thread ID of the first thread in the lock-holding thread identification field.
阻塞线程数域用于指示针对目标锁处于阻塞状态的线程数。The Blocked Threads field is used to indicate the number of threads that are blocked for the target lock.
进一步的,在一些实施例中,阻塞线程数域包括第一值和阻塞线程数高位标识。第一值为针对目标锁处于阻塞状态的线程数的低位上的值,阻塞线程数高位标识用于指示阻塞线程数高位列表中是否存储有与目标锁的ID对应的第二值。第二值为针对目标锁处于阻塞状态的线程数的高位上的值。从而有助于避免针对目标锁处于阻塞状态的线程数过多时,导致溢出。示例的,当阻塞线程数高位标识为有效标识时,指示阻塞线程数高位列表中存储有与目标锁的ID对应的第二值。当阻塞线程数高位标识为无效标识时,指示阻塞线程数高位列表中未存储有与目标锁的ID对应的第二值,或与目标锁的ID对应的第二值为0。例如,将阻塞线程数高位标识置1时,即阻塞线程数高位标识为有效标识。再例如,将阻塞线程数高位标识置0时,即阻塞线程数高位标识为无效标识。Further, in some embodiments, the blocked thread number field includes the first value and the high-order flag of the blocked thread number. The first value is the low value of the number of threads in the blocked state for the target lock, and the high bit flag of the number of blocked threads is used to indicate whether the second value corresponding to the ID of the target lock is stored in the high bit list of the number of blocked threads. The second value is the high value of the number of threads in the blocked state for the target lock. This helps to avoid overflow when there are too many threads in a blocked state for the target lock. For example, when the high-order flag of the number of blocked threads is a valid flag, it indicates that the second value corresponding to the ID of the target lock is stored in the high-order list of the number of blocked threads. When the high flag of the number of blocked threads is an invalid flag, it indicates that the second value corresponding to the ID of the target lock is not stored in the high list of the number of blocked threads, or the second value corresponding to the ID of the target lock is 0. For example, when the high flag of the number of blocked threads is set to 1, the high flag of the number of blocked threads is a valid flag. For another example, when the high bit flag of the number of blocked threads is set to 0, that is, the high bit flag of the number of blocked threads is an invalid flag.
示例的,阻塞线程数高位列表可以为全局变量,以map的形式存储在电子设备中。以与目标锁的ID对应的第二值为例,阻塞线程数高位列表中存储的与目标锁的ID对应的第二值的格式如图4所示,目标锁的ID为key,第二值为vlaue,从而使得请求获取目标锁的线程可以通过目标锁的ID从阻塞线程数高位列表中索引第二值。For example, the high list of the number of blocked threads may be a global variable and stored in the electronic device in the form of a map. Taking the second value corresponding to the ID of the target lock for example, the format of the second value corresponding to the ID of the target lock stored in the high list of blocked threads is shown in Figure 4, the ID of the target lock is key, and the second value It is vlaue, so that the thread requesting to acquire the target lock can index the second value from the high list of blocked threads through the ID of the target lock.
重复持锁次数域用于指示持锁线程针对目标锁的持锁次数。具体的,在本申请实施例中,持锁线程可以通过将重复持锁系数域设置为持锁线程针对目标锁的重复持锁次数,来指示持锁线程针对目标锁的持锁次数。或者,也可以将重复持锁系数域直接设置为持锁线程针对目标锁的持锁次数。例如,在持锁线程针对目标锁的持锁次数为1的情况下,持锁线程针对目标锁的重复持锁次数为0,即持锁线程可以将重复持锁系数域设置为0,也可以设置为1,来指示持锁线程针对目标锁的持锁次数为1。此外,在一些实施例中,持锁线程可以通过将重复持锁次数域设置为空,来指示持锁线程针对目标锁的持锁次数为0。或者,持锁线程可以通过将重复持锁次数域设置为特定符号或值指示持锁线程针对目标锁的持锁次数为0。例如,在持锁线程通过将重复持锁系数域直接设置为持锁线程针对目标锁的持锁次数的情况下,持锁线程可以通过将重复持锁次数域设置为0,来指示持锁线程针对目标锁的持锁次数为0。再例如,在持锁线程通过将重复持锁系数域设置为持锁线程针对目标锁的重复持锁次数的情况下,持锁线程可以通过将重复持锁次数域设置为none,来指示持锁线程针对目标锁的持锁次数为0。The number of repeated locks field is used to indicate the number of locks held by the lock-holding thread for the target lock. Specifically, in the embodiment of the present application, the lock-holding thread may indicate the number of lock-holding times of the lock-holding thread for the target lock by setting the repeated lock-holding coefficient domain to the number of times the lock-holding thread repeatedly holds the lock for the target lock. Alternatively, the repeated lock holding coefficient field can also be directly set to the number of times the lock holding thread holds the lock for the target lock. For example, in the case where the number of times the lock-holding thread holds the lock for the target lock is 1, the number of repeated locks held by the lock-holding thread for the target lock is 0, that is, the lock-holding thread can set the repeated lock-holding coefficient domain to 0, or it can Set to 1 to indicate that the number of times the lock-holding thread holds the lock for the target lock is 1. In addition, in some embodiments, the lock-holding thread may indicate that the number of lock-holding times of the target lock by the lock-holding thread is zero by setting the number of repeated locks field to be empty. Alternatively, the lock-holding thread may indicate that the lock-holding number of the target lock by the lock-holding thread is 0 by setting the number of repeated locks field to a specific symbol or value. For example, in the case that the lock-holding thread directly sets the repeated lock-holding coefficient field to the number of locks held by the lock-holding thread for the target lock, the lock-holding thread can indicate the lock-holding thread by setting the number of repeated locks field to 0. The number of holding locks for the target lock is 0. For another example, when the lock-holding thread sets the repeated lock-holding coefficient field to the number of times the lock-holding thread has repeatedly held the lock for the target lock, the lock-holding thread can indicate the lock holding by setting the repeated lock-holding times field to none. The number of times the thread holds the lock for the target lock is 0.
进一步的,在一些实施例中,重复持锁系数域包括第三值和重复持锁次数高位标识。其中,第三值为持锁线程针对目标锁的持锁次数的低位上的值,重复持锁次数高位标识用于指示重复持锁次数高位列表中是否存储有与目标锁的ID和持锁线程的线程ID对应的第四值,第四值为持锁线程针对所述目标锁的持锁次数的高位上的值。从而有助于避免持锁线程对目标锁持锁次数过多时,导致溢出。示例的,当重复持锁次数高位标识为有效标识时,指示重复持锁次数高位列表中存储有与目标锁的ID和持锁线程的线程ID对应的第四值。当重复持锁次数高位标识为无效标识时,指示重复持锁次数高位列表中未存储有与目标锁的ID和持锁线程的线程ID对应的第四值,或与目标锁的ID和持锁线程的线程ID对应的第四值为0。例如,将重复持锁次数高位标识置1时,即重复持锁次数高位标识为有 效标识。再例如,将重复持锁次数高位标识置0时,即重复持锁次数高位标识为无效标识。Further, in some embodiments, the repeated lock-holding coefficient field includes a third value and a high-level indicator of the number of repeated lock-holding times. Among them, the third value is the low value of the number of locks held by the lock thread for the target lock, and the high flag of the number of repeated locks is used to indicate whether the ID of the target lock and the lock thread are stored in the high list of the number of repeated locks. The fourth value corresponding to the thread ID of the thread ID, and the fourth value is the high value of the number of times the lock-holding thread holds the lock for the target lock. This helps to avoid overflow when the lock-holding thread locks the target too many times. For example, when the high flag of the number of repeated locks is a valid flag, it indicates that the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread is stored in the high list of the number of repeated locks. When the high flag of the number of repeated locks is an invalid flag, it indicates that the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread is not stored in the high list of the number of repeated locks, or the ID and the lock of the target lock The fourth value corresponding to the thread ID of the thread is 0. For example, when the high flag of the number of repeated locks is set to 1, the high flag of the number of repeated locks is a valid flag. For another example, when the high flag of the number of repeated locks is set to 0, that is, the high flag of the number of repeated locks is an invalid flag.
示例的,重复持锁次数高位列表可以为全局变量,以map的形式存储在电子设备中。以与目标锁的ID和持锁线程的线程ID对应的第四值为例,重复持锁次数高位列表中存储的与目标锁的ID和持锁线程的线程ID对应的第四值的格式如图5所示,目标锁的ID和持锁线程的线程ID为key,第四值为vlaue,从而使得请求重新获取目标锁的线程可以通过目标锁的ID和线程ID从重复持锁次数高位列表中索引第四值。For example, the high-order list of the number of repeated locks may be a global variable and stored in the electronic device in the form of a map. Taking the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread as an example, the format of the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread stored in the high-order list of the number of repeated locks is as follows As shown in Figure 5, the ID of the target lock and the thread ID of the thread holding the lock are the key, and the fourth value is vlaue, so that the thread requesting to reacquire the target lock can use the ID and thread ID of the target lock from the high list of the number of repeated locks The fourth value of the middle index.
例如,在lockword为32比特的情况下,目标锁的lockword可以如图6所示,包括持锁线程标识域、重复持锁次数域、阻塞线程数域和预留位。其中,重复持锁次数域包括Counter域和M1域,阻塞线程数域包括WaitNum域和M2域。Counter域用于设置第一值,M1域用于设置重复持锁次数高位标识。WaitNum域用于设置第三值,M2域用于设置阻塞线程数高位标识。For example, in the case where the lockword is 32 bits, the lockword of the target lock may be as shown in FIG. 6, including a lock thread identification field, a repeated lock count field, a blocked thread number field, and reserved bits. Among them, the domain of the number of repeated locks includes the Counter field and the M1 field, and the field of the number of blocked threads includes the WaitNum field and the M2 field. The Counter field is used to set the first value, and the M1 field is used to set the high-level flag of the number of repeated locks. The WaitNum field is used to set the third value, and the M2 field is used to set the high flag of the number of blocked threads.
如图6所示,lockword的前16比特(即第0~15比特)为持锁线程标识域,lockword的第16~18比特为Counter域。lockword的第19比特为M1域。lockword的第20~28比特为WaitNum域,第30和31比特为预留位。需要说明的是,在lockword中还可以不设置预留位。或者,根据实际需要设置lockword中各个域的比特数等。As shown in Figure 6, the first 16 bits of the lockword (that is, the 0th to 15th bits) are the lock thread identification field, and the 16th to 18th bits of the lockword are the Counter field. The 19th bit of the lockword is the M1 field. The 20th to 28th bits of the lockword are the WaitNum field, and the 30th and 31st bits are reserved bits. It should be noted that the reserved bits may not be set in the lockword. Or, set the number of bits in each field in the lockword according to actual needs.
需要说明的是,图6仅为对目标锁的lockword的格式的举例说明,并不构成对lockword格式的限定。例如,本申请实施例中还可以根据实际需要设置lockword中各个域的比特数等。It should be noted that FIG. 6 is only an example of the lockword format of the target lock, and does not constitute a limitation on the lockword format. For example, in the embodiment of the present application, the number of bits in each field in the lockword can also be set according to actual needs.
另外,在又一些实施例中,目标锁的hashcode(哈希值)用于指示是否存在目标锁。以目标锁为某一对象为例,该目标锁的hashcode存储在该对象的对象头的地址负偏移9个比特对应的内存空间中。或者,在用于存储目标锁的lockword的地址对应的内存空间中为空的情况下,电子设备可以将目标锁的hashcode存储在用于存储目标锁的lockword的地址对应的内存空间中,从而有助于内存空间的合理利用。进一步的,本申请实施例可以目标锁的hashcode可以以map形式存储。示例的,目标锁的hashcode的存储格式可以如图7所示,目标锁的ID为key,hashcode为value。具体的,线程可以通过目标锁的ID索引到目标锁的hashcode。In addition, in other embodiments, the hashcode (hash value) of the target lock is used to indicate whether the target lock exists. Taking the target lock as an object as an example, the hashcode of the target lock is stored in the memory space corresponding to a negative offset of 9 bits from the address of the object header of the object. Or, in the case that the memory space corresponding to the address of the lockword used to store the target lock is empty, the electronic device can store the hashcode of the target lock in the memory space corresponding to the address of the lockword used to store the target lock. Contribute to the rational use of memory space. Further, in the embodiment of the present application, the hashcode of the target lock may be stored in the form of a map. For example, the storage format of the hashcode of the target lock may be as shown in Figure 7, where the ID of the target lock is key and the hashcode is value. Specifically, the thread can index to the hashcode of the target lock through the ID of the target lock.
以具有图2和图3所示结构的电子设备为例,对本申请实施例的多进程同步方法进行详细介绍。Taking the electronic device with the structure shown in FIG. 2 and FIG. 3 as an example, the multi-process synchronization method in the embodiment of the present application will be introduced in detail.
示例一:以第一线程请求获取目标锁为例。其中,第一线程请求获取目标锁可以理解为:第一线程请求对目标锁持锁。在一些实施例中,第一线程运行到请求获取目标锁持锁的函数或程序指令时,触发执行第一线程请求获取目标锁的过程。例如,请求获取目标锁持锁的函数或程序指令为MonitorEnter。Example 1: Take the first thread's request to acquire the target lock as an example. Wherein, the request by the first thread to acquire the target lock can be understood as: the first thread requests to lock the target lock. In some embodiments, when the first thread runs to a function or program instruction requesting to acquire the target lock, the process of executing the first thread's request to acquire the target lock is triggered. For example, the function or program instruction requesting to acquire the target lock is MonitorEnter.
示例的,第一线程请求获取目标锁,包括以下步骤,具体如图8所示。As an example, the first thread requests to acquire the target lock, including the following steps, as shown in FIG. 8 in detail.
步骤801、第一线程获取目标锁的lockword。Step 801: The first thread acquires the lockword of the target lock.
示例的,第一线程根据目标锁的地址,获取目标锁的lockword。例如,在lockword为32比特的情况下,目标锁的地址可以为目标锁所在对象的对象头的地址正向偏移4字节。For example, the first thread obtains the lockword of the target lock according to the address of the target lock. For example, in the case where the lockword is 32 bits, the address of the target lock may be a forward offset of 4 bytes from the address of the object header of the object where the target lock is located.
步骤802、第一线程检查持锁线程标识域是否为有效线程。即第一线程判断持锁线程标识域是否设置为有效线程。若是,则执行步骤803,否则执行809。Step 802: The first thread checks whether the lock-holding thread identification field is a valid thread. That is, the first thread judges whether the lock-holding thread identification field is set as a valid thread. If yes, go to step 803, otherwise go to 809.
需要说明的是,持锁线程标识域不为有效线程,与持锁线程标识域为无效线程等同。It should be noted that the lock-holding thread identification field is not a valid thread, which is equivalent to the lock-holding thread identification field being an invalid thread.
具体的,持锁线程标识域设置为有效线程,可以理解为:在持锁线程标识域上设置的 线程ID为有效值。例如,[1,1000]为线程ID的有效值,则在持锁线程标识域上设置的线程ID在[1,1000]中,即持锁线程域设置为有效线程。可以理解的是,当在持锁线程标识域上设置的线程ID不是有效值,可以理解为持锁线程ID设置为无效线程。例如,持锁线程标识域设置的线程ID为0、或1001,0、1001均不在[1,1000]中,则持锁线程标识域设置为无效线程。需要说明的是,当持锁线程标识域设置为无效线程时,可以理解为目标锁处于无锁状态(non_lock)。Specifically, setting the lock-holding thread identification field as a valid thread can be understood as: the thread ID set in the lock-holding thread identification field is a valid value. For example, [1,1000] is the effective value of the thread ID, and the thread ID set on the lock-holding thread ID field is in [1,1000], that is, the lock-holding thread field is set as a valid thread. It can be understood that when the thread ID set in the lock-holding thread identification field is not a valid value, it can be understood that the lock-holding thread ID is set to an invalid thread. For example, if the thread ID set in the lock-holding thread identification field is 0 or 1001, and 0 and 1001 are not in [1,1000], the lock-holding thread identification field is set as an invalid thread. It should be noted that when the lock-holding thread identification field is set to an invalid thread, it can be understood that the target lock is in a non-lock state (non_lock).
在另一些实施例中,持锁线程标识域设置为无效线程,还可以通过在持锁线程标识域上设置特定的值或符号实现,例如,将持锁线程标识域设置为NA时,即将持锁线程标识域设置为无效线程。In other embodiments, the lock-holding thread identification field is set to be an invalid thread, and it can also be implemented by setting a specific value or symbol on the lock-holding thread identification field. For example, when the lock-holding thread identification field is set to NA, it is about to be held. The lock thread ID field is set to an invalid thread.
步骤803、第一线程检查持锁线程域是否为第一线程,若是,则执行步骤808,否则执行步骤804。Step 803: The first thread checks whether the lock-holding thread domain is the first thread, if yes, execute step 808; otherwise, execute step 804.
步骤804、第一线程检查阻塞线程数域是否小于第一阈值,若是,则执行步骤805,否则执行步骤807。Step 804: The first thread checks whether the blocked thread number domain is less than the first threshold, if yes, execute step 805, otherwise, execute step 807.
示例的,第一阈值可以为3、4或5等,具体可以是电子设备出厂之前设定好的,也可以是电子设备根据自身的运行情况确定的,对此不作限定。例如,电子设备自身运行速度较快的情况下,第一阈值可以取值大一些,在电子设备自身运行卡顿的情况下,第一阈值的取值可以小一些。For example, the first threshold may be 3, 4, 5, etc., specifically it may be set before the electronic device leaves the factory, or it may be determined by the electronic device according to its own operating conditions, which is not limited. For example, in the case where the electronic device itself runs fast, the first threshold value may be a larger value, and in the case where the electronic device itself is running stuck, the value of the first threshold value may be smaller.
步骤805、第一线程进行自旋等待。Step 805: The first thread performs spin waiting.
在一些实施例中,第一线程可以通过下列方式进行自旋等待:In some embodiments, the first thread can spin-wait in the following ways:
第一线程调用sched_yield系统函数,向处理器发送yield指令。处理器接收到yield指令后,在一定时长内将第一线程挂起,从而使得第一线程放弃对处理器的在一定时长内控制权,进行自旋等待。The first thread calls the sched_yield system function and sends a yield instruction to the processor. After receiving the yield instruction, the processor suspends the first thread for a certain period of time, so that the first thread relinquishes control of the processor for a certain period of time and performs spin waiting.
需要说明的是,本申请实施例中,电子设备还可以通过其它方式进行自旋等待,对此不作限定。It should be noted that in the embodiment of the present application, the electronic device may also perform spin waiting in other ways, which is not limited.
步骤806、第一线程在自旋等待的次数达到第二阈值时,若检查持锁线程标识域为有效线程且不为第一线程,则第一线程对阻塞线程数域执行加1的操作,并挂起进入阻塞状态。Step 806: When the number of spin waiting times of the first thread reaches the second threshold, if it is checked that the lock-holding thread identification field is a valid thread and not the first thread, the first thread performs an operation of adding 1 to the blocked thread number field. And hang into the blocking state.
在一些实施例中,阻塞线程数域包括第一值和阻塞线程数高位标识,具体可以参见上述相关介绍。在这种情况下,第一线程对阻塞线程数域执行加1的操作可以如图9所示,具体可以包括以下步骤:In some embodiments, the blocked thread number field includes the first value and the high-order flag of the blocked thread number. For details, please refer to the above related introduction. In this case, the operation of adding 1 to the number field of blocked threads by the first thread may be as shown in FIG. 9, and may specifically include the following steps:
步骤901、第一线程检查第一值是否等于第一目标值,第一目标值为针对目标锁处于阻塞状态的线程数的低位上的最大值。若是,则执行步骤903,否则,执行步骤902。Step 901: The first thread checks whether the first value is equal to the first target value, and the first target value is the maximum value in the low order of the number of threads in the blocked state for the target lock. If yes, go to step 903, otherwise, go to step 902.
即第一线程比较或判断第一值与第一目标值是否相同。That is, the first thread compares or judges whether the first value is the same as the first target value.
步骤902、第一线程将第一值加1。Step 902: The first thread adds 1 to the first value.
例如,在lockword中的3比特上设置针对目标锁处于阻塞状态的线程数的低位上的值。在这种情况下,第一目标值为7,当第一值为4时,第一线程对第一值加1,将第一值设置为5。For example, set the low-order value of the number of threads in the blocked state for the target lock on the 3 bits in the lockword. In this case, the first target value is 7. When the first value is 4, the first thread adds 1 to the first value and sets the first value to 5.
步骤903、第一线程检查阻塞线程数高位标识是否为有效标识,若是,则执行步骤904,否则执行步骤905。Step 903: The first thread checks whether the high-level flag of the number of blocked threads is a valid flag, if yes, execute step 904, otherwise, execute step 905.
可以理解的是,阻塞线程数高位标识不为有效标识,与阻塞线程数为无效标识等同。It is understandable that the high-level identifier of the number of blocked threads is not a valid identifier, and is equivalent to the invalid identifier of the number of blocked threads.
步骤904、第一线程将第一值置0,以及将阻塞线程列表中与目标锁的ID对应的第二值加1。Step 904: The first thread sets the first value to 0, and adds 1 to the second value corresponding to the ID of the target lock in the blocked thread list.
步骤905、第一线程将第一值置0,将阻塞线程数高位标识设置为有效标识,将与目标锁的ID对应的第二值置1,以及将目标锁的ID和与目标锁的ID对应的第二值添加或存储到阻塞线程列表中。Step 905: The first thread sets the first value to 0, sets the high flag of the number of blocked threads as a valid flag, sets the second value corresponding to the ID of the target lock to 1, and sets the ID of the target lock and the ID of the target lock The corresponding second value is added or stored in the blocked thread list.
在一些实施例中,第一线程可以通过下述方式挂起进入阻塞状态:In some embodiments, the first thread can be suspended and enter the blocking state in the following ways:
第一线程进行系统调用,将第一线程的状态设置为睡眠态,并将第一线程添加到与目标锁的地址对应的等待队列中。示例的,第一线程基于快速用户空间互斥体(fast user mutex,futex)机制进行系统调用,将第一线程的状态设置为睡眠态,并将第一线程添加到与目标锁的地址对应的等待队列中。例如,第一线程可以通过系统调用futex_wait,挂起进入阻塞状态。The first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock. For example, the first thread makes a system call based on the fast user mutex (futex) mechanism, sets the state of the first thread to sleep, and adds the first thread to the address corresponding to the target lock Waiting in the queue. For example, the first thread may suspend and enter the blocking state through the system call futex_wait.
在本申请的另一些实施例中,第一线程在自旋等待的次数小于第二阈值的情况下,将持锁线程标识域设置为第一线程,第一线程对目标锁所保护的共享资源进行访问。In some other embodiments of the present application, the first thread sets the lock-holding thread identification field to the first thread when the number of spin waits is less than the second threshold, and the first thread has a shared resource protected by the target lock Make a visit.
步骤807、第一线程对阻塞线程数域执行加1的操作,并挂起进入阻塞状态。Step 807: The first thread performs an operation of adding 1 to the blocked thread number field, and suspends and enters a blocking state.
其中,第一线程对阻塞线程数域执行加1的操作,并挂起进入阻塞状态的具体实现方式,可以参见步骤806中的相关介绍,在此不再赘述。Among them, the first thread performs an operation of adding 1 to the blocked thread number field and suspends and enters the blocking state for a specific implementation manner, which can be referred to the related introduction in step 806, which will not be repeated here.
步骤808、第一线程对重复持锁次数域执行加1操作。Step 808: The first thread performs an operation of adding 1 to the repeated lock count field.
在一些实施例中,重复持锁次数域包括第三值和重复持锁次数高位标识,具体可以参见上述相关介绍。在这种情况下,第一线程对重复持锁次数执行加1的操作可以如图10所示,具体可以包括以下步骤:In some embodiments, the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks. For details, please refer to the above related introduction. In this case, the operation of adding 1 to the number of repeated lock holdings performed by the first thread may be as shown in FIG. 10, which may specifically include the following steps:
步骤1001、第一线程检查第三值是否等于第二目标值,第二目标值为持锁线程针对目标锁的持锁次数的低位上的最大值。若是,则执行步骤1003,否则,执行步骤1002。Step 1001: The first thread checks whether the third value is equal to the second target value, and the second target value is the maximum value in the low order of the number of lock holding times of the lock holding thread for the target lock. If yes, go to step 1003; otherwise, go to step 1002.
步骤1002、第一线程将第三值加1。 Step 1002, the first thread adds 1 to the third value.
例如,在lockword中的5比特上设置持锁线程针对目标锁的持锁次数的低位上的值。在这种情况下,第二目标值为31,当第三值为20时,第一线程将第三值加1,将第三值设置为21。For example, set the low value of the number of locks held by the lock thread for the target lock on the 5 bits in the lockword. In this case, the second target value is 31. When the third value is 20, the first thread adds 1 to the third value and sets the third value to 21.
步骤1003、第一线程检查重复持锁次数高位标识是否为有效标识,若是,则执行步骤1004,否则,执行步骤1005。Step 1003: The first thread checks whether the high-level flag of the number of repeated locks is a valid flag, if it is, step 1004 is executed, otherwise, step 1005 is executed.
步骤1004、第一线程将第三值置0,将重复持锁次数高位列表中与目标锁的ID和持锁线程的线程ID对应的第四值加1。Step 1004: The first thread sets the third value to 0, and adds 1 to the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks.
步骤1005、第一线程将第三值置0,将重复持锁次数高位标识设置为有效标识,并与目标锁的ID和持锁线程的线程ID对应的第四值置1,以及将目标锁的ID、持锁线程的线程ID和与目标锁的ID和持锁线程的线程ID对应的第四值添加或存储到阻塞线程列表中。Step 1005: The first thread sets the third value to 0, sets the high flag of the number of repeated locks as a valid flag, sets the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread to 1, and sets the target lock The ID of the lock thread, the thread ID of the lock thread, and the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread are added or stored in the blocked thread list.
步骤809,第一线程将持锁线程标识域设置为第一线程,以及初始化重复持锁次数域。In step 809, the first thread sets the lock-holding thread identification field as the first thread, and initializes the repeated lock-holding times field.
其中,第一线程初始化重复持锁次数域,可以理解为,第一线程将重复持锁次数域设置为初始值。例如,在初始值为0的情况下,第一线程将重复持锁次数域设置为0。在初始值为1的情况下,第一线程将重复持锁次数域设置为1。Wherein, the first thread initializes the repeated lock count field, which can be understood as the first thread sets the repeated lock count field to an initial value. For example, when the initial value is 0, the first thread sets the number of repeated locks field to 0. In the case where the initial value is 1, the first thread sets the number of repeated locks field to 1.
示例的,在持锁线程通过将重复持锁系数域直接设置为持锁线程针对目标锁的持锁次数的情况下,初始值可以为1。示例的,在持锁线程通过将重复持锁系数域设置为持锁线程针对目标锁的重复持锁次数的情况下,初始值可以为0。For example, in the case where the lock-holding thread directly sets the repeated lock-holding coefficient domain to the number of times the lock-holding thread holds the target lock, the initial value may be 1. For example, when the lock-holding thread sets the repeated lock-holding coefficient domain to the number of times the lock-holding thread repeatedly holds the target lock, the initial value may be 0.
本申请实施例中,由于第一线程在自旋等待的次数达到第二阈值时,第一线程只需对阻塞线程数域执行加1操作,并挂起进入阻塞状态即可,无需将针对目标锁的持锁线程挂起,也无需分配monitor对象,从而有助于减小持锁线程访问共享资源的时长,从而有助于降低电子设备卡顿的概率,提高用户体验。In the embodiment of the present application, since the number of spin waiting times of the first thread reaches the second threshold, the first thread only needs to perform an operation of adding 1 to the blocked thread number field and suspend into the blocking state. The lock-holding thread of the lock is suspended, and there is no need to allocate a monitor object, which helps to reduce the length of time that the lock-holding thread accesses shared resources, thereby helping to reduce the probability of electronic device jams and improving user experience.
另外,本申请实施例中,当第一线程为持锁线程时,如果对目标锁重复持锁,只需对重复持锁线程数执行加1操作即可,无需分配monitor对象,修改锁状态,因而,有助于提高第一线程的处理效率,提高电子设备的运行速度,从而有助于提高用户体验。In addition, in the embodiment of the present application, when the first thread is a lock-holding thread, if the target lock is repeatedly held, it is only necessary to add 1 to the number of repeated lock-holding threads, and there is no need to allocate monitor objects and modify the lock status. Therefore, it helps to improve the processing efficiency of the first thread and the operating speed of the electronic device, thereby helping to improve the user experience.
示例二:以第一线程请求释放目标锁为例。在一些实施例中,第一线程运行到请求释放目标锁的函数或程序指令时,触发执行第一线程请求释放目标锁的过程。例如,请求释放目标锁的函数或程序指令为MonitorExit。Example 2: Take the first thread's request to release the target lock as an example. In some embodiments, when the first thread runs to a function or program instruction requesting the release of the target lock, the process of executing the first thread's request to release the target lock is triggered. For example, the function or program instruction that requests the release of the target lock is MonitorExit.
示例的,第一线程请求释放目标锁,包括以下步骤,具体如图11所示。For example, the first thread requesting to release the target lock includes the following steps, as shown in FIG. 11 in detail.
步骤1101、第一线程获取目标锁的lockword。具体可参见步骤801的相关介绍。Step 1101: The first thread acquires the lockword of the target lock. For details, refer to the related introduction of step 801.
步骤1102,第一线程检查持锁线程标识域是否为第一线程,若是,则执行步骤1103,否则执行步骤1107。In step 1102, the first thread checks whether the lock-holding thread identification field is the first thread, if so, execute step 1103; otherwise, execute step 1107.
例如,第一线程比较持锁线程标识域是否为第一线程的线程ID。For example, the first thread compares whether the lock-holding thread identification field is the thread ID of the first thread.
步骤1103,第一线程检查重复持锁次数域是否为0,若是,则执行步骤1106,否则执行步骤1104。In step 1103, the first thread checks whether the repeated lock count field is 0, and if so, execute step 1106; otherwise, execute step 1104.
需要说明的是,重复持锁次数不为0,可以理解为重复持锁次数指示的持锁线程针对目标锁的持锁次数大于0。重复持锁次数为0,可以理解为重复持锁次数指示的持锁线程针对目标锁的持锁次数为0。It should be noted that the number of repeated lock holdings is not 0, which can be understood as the number of lock holding threads for the target lock indicated by the number of repeated locks is greater than 0. The number of repeated locks is 0, which can be understood as the number of locks held by the lock thread indicated by the number of repeated locks for the target lock is 0.
步骤1104,第一线程对重复持锁次数域执行减1操作。In step 1104, the first thread performs a minus 1 operation on the repeated lock count field.
在一些实施例中,重复持锁次数域包括第三值和重复持锁次数高位标识,具体可以参见上述相关介绍。在这种情况下,第一线程对重复持锁次数执行减1的操作可以如图12所示,具体可以包括以下步骤:In some embodiments, the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks. For details, please refer to the above related introduction. In this case, the operation of subtracting 1 from the number of repeated lock holdings by the first thread may be as shown in FIG. 12, which may specifically include the following steps:
步骤1201、第一线程检查第三值是否为0,即第一线程判断持锁线程(即第一线程)针对目标锁的持锁次数是否为0。若否,则执行步骤1202,若是,则执行步骤1203。Step 1201: The first thread checks whether the third value is 0, that is, the first thread determines whether the number of times the lock holding thread (ie, the first thread) holds the target lock is 0. If not, step 1202 is performed, and if yes, step 1203 is performed.
步骤1202、第一线程将第三值减1。 Step 1202, the first thread subtracts 1 from the third value.
例如,在lockword中的5比特上设置持锁线程针对目标锁的持锁次数的低位上的值。在这种情况下,当第三值为20时,第一线程将第三值减1,将第三值设置为19。For example, set the low value of the number of locks held by the lock thread for the target lock on the 5 bits in the lockword. In this case, when the third value is 20, the first thread subtracts 1 from the third value and sets the third value to 19.
步骤1203、第一线程检查重复持锁次数高位标识是否为有效标识,若是,则执行步骤1204,否则执行步骤1207。Step 1203: The first thread checks whether the high-level flag of the number of repeated locks is a valid flag, if it is, step 1204 is executed, otherwise, step 1207 is executed.
步骤1204、第一线程检查重复持锁次数高位列表中与目标锁的ID和持锁线程的线程ID对应的第四值是否为1,若是,则执行步骤1205,否则执行步骤1206。Step 1204: The first thread checks whether the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks is 1, if yes, execute step 1205, otherwise, execute step 1206.
需要说明的是,重复持锁次数高位列表中与目标锁的ID和持锁线程的线程ID对应的第四值不为1,可以理解为重复持锁次数高位列表中与目标锁的ID和持锁线程的线程ID对应的第四值大于1。It should be noted that the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks is not 1, which can be understood as the ID and hold of the target lock in the high list of the number of repeated locks. The fourth value corresponding to the thread ID of the locked thread is greater than 1.
步骤1205、第一线程将第三值置为第二目标值,删除重复持锁次数高位列表中目标锁的ID、持锁线程的线程ID和与目标锁的ID和持锁线程的线程ID对应的第四值,以及将重复持锁次数高位标识设置为无效标识。Step 1205: The first thread sets the third value to the second target value, and deletes the ID of the target lock, the thread ID of the lock-holding thread and the thread ID of the target lock and the thread ID of the lock-holding thread in the high list of repeated locks. The fourth value of, and the high flag of the number of repeated locks is set to an invalid flag.
步骤1206、第一线程将第三值置为第二目标值,以及将与目标锁的ID和持锁线程的线程ID对应的第四值减1。Step 1206: The first thread sets the third value to the second target value, and subtracts 1 from the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread.
步骤1207、第一线程抛出异常。Step 1207: The first thread throws an exception.
步骤1105、第一线程对重复持锁次数域执行减1操作后,第一线程重新检查重复持锁次数域是否为0,若是,则执行步骤1106,否则本流程结束。Step 1105: After the first thread performs an operation of subtracting 1 from the number of repeated locks field, the first thread rechecks whether the number of repeated locks field is 0, if so, execute step 1106, otherwise the process ends.
步骤1106、第一线程将持锁线程标识域设置为无效线程,以及检查阻塞线程数域是否为0,若否,则执行步骤1107,否则本流程结束。Step 1106: The first thread sets the lock thread identification field as an invalid thread, and checks whether the blocked thread number field is 0, if not, execute step 1107, otherwise the process ends.
步骤1107、第一线程唤醒针对目标锁处于阻塞状态的N个线程。进一步的,第一线程在唤醒目标锁处于阻塞状态的N个线程后,还可以对阻塞线程数域执行减N的操作。或者,第一线程唤醒针对目标锁处于阻塞状态的N个线程,不对阻塞线程数域执行减N操作,当N个线程中的某一个线程将持锁线程标识域设置为该线程后,由该线程对阻塞线程数域执行减1的操作,N个线程中的其它N-1个线程可以重新挂起进入阻塞状态。例如,当N个线程中的第三线程将持锁线程标识域设置为第三线程,第三线程对阻塞线程数域执行减1的操作。Step 1107: The first thread wakes up N threads that are blocked for the target lock. Further, after the first thread wakes up the N threads in the blocked state of the target lock, it may also perform an operation of subtracting N on the blocked thread number field. Or, the first thread wakes up N threads that are blocked for the target lock, and does not perform the N reduction operation on the blocked thread number field. When one of the N threads sets the lock-holding thread identification field to this thread, the The thread performs the operation of subtracting 1 from the blocked thread number field, and the other N-1 threads among the N threads can be suspended again and enter the blocking state. For example, when the third thread among the N threads sets the lock-holding thread identification field to the third thread, the third thread performs an operation of subtracting 1 on the blocked thread number field.
其中,N为大于或等于1的正整数。例如,N可以为1或2,也可以为3,还可以为针对目标锁处于阻塞状态的所有线程的个数。其中,N的取值可以是开发人员根据需要在代码中预先设置好的。Among them, N is a positive integer greater than or equal to 1. For example, N can be 1 or 2, or 3, or the number of all threads that are blocked for the target lock. Among them, the value of N can be preset in the code by the developer as needed.
在一些实施例中,第一线程可以基于下列方式唤醒针对目标锁处于阻塞状态的至少一个线程:In some embodiments, the first thread may wake up at least one thread that is blocked for the target lock based on the following methods:
第一线程进行系统调用,将与目标锁的地址对应的等待队列中的至少一个线程的状态设置为运行态,并将至少一个线程添加到与目标锁的地址对应的运行队列中;与目标锁的地址对应的等待队列中包括针对目标锁处于阻塞状态的至少一个线程。The first thread makes a system call, sets the state of at least one thread in the waiting queue corresponding to the address of the target lock to the running state, and adds at least one thread to the run queue corresponding to the address of the target lock; The waiting queue corresponding to the address of includes at least one thread that is blocked for the target lock.
示例的,第一线程可以基于futex机制进行系统调用,唤醒针对目标锁处于阻塞状态的至少一个线程。例如,第一线程系统调用futex_wake,唤醒针对目标锁处于阻塞状态的至少一个线程。For example, the first thread may make a system call based on the futex mechanism to wake up at least one thread that is blocked for the target lock. For example, the first thread system calls futex_wake to wake up at least one thread that is blocked for the target lock.
步骤1108、第一线程检查持锁线程标识域是否为无效线程,若是,执行步骤1109,否则执行步骤1110。Step 1108: The first thread checks whether the lock-holding thread identification field is an invalid thread, if yes, execute step 1109, otherwise, execute step 1110.
步骤1109、第一线程抛出no-lock异常。Step 1109: The first thread throws a no-lock exception.
步骤1110,第一线程抛出other-lock异常。 Step 1110, the first thread throws an other-lock exception.
示例三:以第一线程请求中止对目标锁持锁为例。其中,第一线程请求中止对目标锁持锁可以理解为:第一线程在对目标锁持锁且未释放目标锁的情况下主动请求放弃对目标锁持锁。在一些实施例中,第一线程运行到请求中止对目标锁持锁的函数或程序指令时,触发执行第一线程请求中止对目标锁持锁的过程。例如,请求中止对目标锁持锁的函数或程序指令为MonitorWait。Example 3: Take the first thread's request to suspend the lock on the target as an example. Wherein, the request of the first thread to suspend the lock lock on the target can be understood as: the first thread actively requests to give up the lock lock on the target when the lock is locked on the target and the target lock is not released. In some embodiments, when the first thread runs to a function or program instruction that requests to suspend the lock on the target, it triggers the execution of the process in which the first thread requests to suspend the lock on the target. For example, the function or program instruction requesting to suspend the lock on the target is MonitorWait.
示例的,第一线程请求中止对目标锁持锁,包括以下步骤,具体如图13所示。As an example, the first thread requests to suspend the lock on the target, including the following steps, as shown in FIG. 13 specifically.
步骤1301、第一线程获取目标锁的lockword。具体可以参见步骤801中的相关介绍,在此不再赘述。Step 1301: The first thread acquires the lockword of the target lock. For details, please refer to the related introduction in step 801, which will not be repeated here.
步骤1302、第一线程检查持锁线程标识域是否为第一线程,若是,则执行步骤1303,否则退出。 Step 1302, the first thread checks whether the lock-holding thread identification field is the first thread, if yes, execute step 1303, otherwise exit.
步骤1303、第一线程将重复持锁次数域保存为第一线程针对目标锁的上下文。Step 1303: The first thread saves the repeated lock count field as the context of the first thread for the target lock.
其中,第一线程将重复持锁次数域保存为第一线程针对目标锁的上下文,可以理解为:第一线程将针对目标锁的持锁次数保存为第一线程针对目标锁的上下文。Wherein, the first thread saves the field of the number of repeated locks as the context of the first thread for the target lock, which can be understood as: the first thread saves the number of locks for the target lock as the context of the first thread for the target lock.
示例的,在重复持锁次数域包括第三值和重复持锁次数高位标识、且重复持锁次数高位标识为有效标识的情况下,第一线程将针对目标锁的持锁次数保存为第一线程针对目标锁的上下文,可以指的是,第一线程将第三值、重复持锁次数高位标识、以及重复持锁次数高位列表中与目标锁的ID和持锁线程的线程ID对应的第四值保存为第一线程针对目标锁的上下文。或者,第一线程根据第三值、重复持锁次数高位标识和重复持锁次数高位列表中与目标锁的ID和持锁线程的线程ID对应的第四值,确定第五值,其中第五值为第一线程针对目标锁的持锁次数,并将第五值保存为第一线程针对目标锁的上下文。For example, in the case that the repeated lock count field includes the third value and the high mark of the repeated lock count, and the high mark of the repeated lock count is the valid mark, the first thread saves the lock count for the target lock as the first The context of the thread for the target lock can mean that the first thread sets the third value, the high mark of the number of repeated locks, and the first thread ID corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the third value, the number of repeated locks. The four value is saved as the context of the first thread for the target lock. Alternatively, the first thread determines the fifth value according to the third value, the high flag of the number of repeated locks, and the fourth value corresponding to the ID of the target lock and the thread ID of the lock-holding thread in the high list of the number of repeated locks, where the fifth The value is the number of times the first thread holds the lock for the target lock, and the fifth value is saved as the context of the first thread for the target lock.
例如,第三值为7,重复持锁系数高位标识为有效标识、重复持锁次数高位列表中与目标锁的ID和持锁线程的线程ID对应的第四值为1,在持锁线程针对目标锁的持锁次数的低位上的最大值为7的情况下,第五值为15。第一线程可以将15保存为第一线程针对目标锁的上下文,也可以将7、重复持锁系数高位标识和1保存为第一线程针对目标锁的上下文。For example, the third value is 7, the high mark of the repeated lock coefficient is a valid mark, and the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread in the high list of the number of repeated locks is 1. In the case where the maximum value in the low order of the number of holding times of the target lock is 7, the fifth value is 15. The first thread may save 15 as the context of the first thread for the target lock, or it may save 7, the high bit flag of the repeated lock holding coefficient and 1 as the context of the first thread for the target lock.
又示例的,在重复持锁次数域包括第三值和重复持锁次数高位标识、且重复持锁次数高位标识为无效标识的情况下,第一线程将针对目标锁的持锁次数保存为第一线程针对目标锁的上下文,可以指的是,第一线程将第三值保存为第一线程针对目标锁的上下文、或者第一线程将第三值和重复持锁次数高位标识保存为第一线程针对目标锁的上下文。As another example, in the case that the repeated lock count field includes the third value and the high mark of the repeated lock count, and the high mark of the repeated lock count is the invalid mark, the first thread saves the lock count for the target lock as the first The context of a thread for the target lock can mean that the first thread saves the third value as the context of the first thread for the target lock, or the first thread saves the third value and the high-level identifier of the number of repeated locks as the first The context of the thread for the target lock.
示例的,第一线程可以将第一线程针对目标锁的上下文可以存储在指定的存储空间中。For example, the first thread may store the context of the first thread for the target lock in a designated storage space.
步骤1304,第一线程将第一线程的线程ID添加到与目标锁的ID对应的线程等待列表中。Step 1304: The first thread adds the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock.
示例的,第一线程可以通过以下方式将第一线程的线程ID添加到目标锁的ID对应的线程等待列表中:For example, the first thread can add the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock in the following manner:
首先,第一线程根据目标锁的ID,查找与目标锁的ID对应的线程等待列表。第一线程若未查找到与目标锁的ID对应的线程等待列表,第一线程创建与目标锁的ID对应的线程等待列表,并将第一线程的线程ID添加到与目标锁的ID对应的线程等待列表中。第一线程若查找到与目标锁的ID对应的线程等待列表,第一线程将第一线程的线程ID添加到与目标锁的ID对应的线程等待列表中。First, the first thread looks up the thread waiting list corresponding to the ID of the target lock according to the ID of the target lock. If the first thread does not find the thread waiting list corresponding to the ID of the target lock, the first thread creates a thread waiting list corresponding to the ID of the target lock, and adds the thread ID of the first thread to the ID corresponding to the target lock Thread waiting list. If the first thread finds the thread waiting list corresponding to the ID of the target lock, the first thread adds the thread ID of the first thread to the thread waiting list corresponding to the ID of the target lock.
在一些实施例中,与目标锁的ID对应的线程等待列表可以为全局变量,以单项链表的形式存储在电子设备中。以第一线程的线程ID为例,第一线程的线程ID在与目标锁的ID对应的线程等待列表存储的格式可以如图14所示,目标锁的ID为key,第一线程的线程ID为value。从而可以通过目标锁的ID可以从与目标锁的ID对应的线程等待列表中索引到第一线程的线程ID。In some embodiments, the thread waiting list corresponding to the ID of the target lock may be a global variable and stored in the electronic device in the form of a single necklace. Taking the thread ID of the first thread as an example, the format of the thread ID of the first thread stored in the thread waiting list corresponding to the ID of the target lock can be as shown in Figure 14. The ID of the target lock is the key, and the thread ID of the first thread Is the value. Therefore, the thread ID of the first thread can be indexed from the thread waiting list corresponding to the ID of the target lock by the ID of the target lock.
步骤1305、第一线程将重复持锁次数域置0,以及将持锁线程标识域设置为无效线程,并挂起进入阻塞状态。从而在第一线程对目标锁持锁、但未释放锁的情况下主动放弃对目标锁的控制权,使得其它线程,尤其是业务优先级高于第一线程的线程可以实现优先持锁,对共享资源访问,满足用户的需求。Step 1305: The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field to be an invalid thread, and suspends and enters a blocking state. Therefore, when the first thread locks the target but does not release the lock, it actively gives up control of the target lock, so that other threads, especially threads with a higher business priority than the first thread, can achieve priority locks. Shared resource access to meet the needs of users.
具体的,第一线程将重复持锁次数域置0指的是:第一线程将重复持锁次数域指示的持锁次数清0。例如,在重复持锁次数域包括第三值和重复持锁次数高位标识、且重复持 锁次数高位标识为有效标识的情况下,第一线程将重复持锁次数域置0指的是:第一线程将第三值置0,将重复持锁次数高位标识设置为无效标识,以及将重复持锁次数高位列表中与目标锁的ID和第一线程的线程ID对应的第四值置0。Specifically, that the first thread sets the number of repeated locks field to 0 means that the first thread clears the number of locks indicated by the number of repeated locks field to 0. For example, in the case that the number of repeated locks field includes the third value and the high flag of the number of repeated locks, and the high flag of the number of repeated locks is a valid flag, the first thread sets the number of repeated locks field to 0, which means: A thread sets the third value to 0, sets the high flag of the number of repeated locks to an invalid flag, and sets the fourth value corresponding to the ID of the target lock and the thread ID of the first thread in the high list of the number of repeated locks to 0.
需要说明的是,第一线程挂起进入阻塞状态的具体实现方式,可以参见步骤805中的相关介绍,在此不再赘述。It should be noted that, for the specific implementation manner of the first thread suspending and entering the blocking state, refer to the related introduction in step 805, which will not be repeated here.
进一步的,第一线程在执行步骤1305后,若重新被唤醒,第一线程可以重新请求获取目标锁,具体包括下述步骤:Further, if the first thread is awakened again after performing step 1305, the first thread may request to acquire the target lock again, which specifically includes the following steps:
步骤1306,第一线程重新获取目标锁的锁状态变量。Step 1306: The first thread reacquires the lock state variable of the target lock.
步骤1307,第一线程检查持锁线程标识域是否为无效线程,若是,则执行步骤1308,否则继续检查持锁线程标识域是否为无效线程。In step 1307, the first thread checks whether the lock-holding thread identification field is an invalid thread, and if so, executes step 1308; otherwise, it continues to check whether the lock-holding thread identification field is an invalid thread.
步骤1308,第一线程将持锁线程标识域设置为第一线程,以及根据第一线程针对目标锁的上下文,设置重复持锁次数域。In step 1308, the first thread sets the lock-holding thread identification field as the first thread, and sets the repeated lock-holding times field according to the context of the first thread for the target lock.
例如,第一线程针对目标锁的上下文包括的第一线程针对目标锁的持锁次数为15,第一线程将重复持锁次数域设置为14或15。具体的,通过将持锁线程针对目标锁的重复持锁次数设置重复持锁系数域的情况下,第一线程将重复持锁次数域设置为14;通过将持锁线程针对目标锁的持锁次数设置重复持锁系数域的情况下,第一线程将重复持锁次数域设置为15。For example, the context of the first thread for the target lock includes that the number of locks held by the first thread for the target lock is 15, and the first thread sets the number of repeated locks field to 14 or 15. Specifically, when the lock-holding thread sets the repeated lock-holding coefficient field for the number of repeated locks of the target lock, the first thread sets the repeated lock-holding number field to 14; by setting the lock-holding thread to hold the lock for the target lock In the case of setting the repeated lock-holding coefficient field for the number of times, the first thread sets the repeated lock-holding times field to 15.
进一步的,在一些实施例中,重复持锁次数域包括第三值和重复持锁次数高位标识。在重复持锁次数域中通过3比特设置第三值时,若第一线程针对目标锁的上下文包括的第一线程针对目标锁的持锁次数为15,则第一线程将第三值设置为6或7,将重复持锁次数高位标识设置为有效标识,以及与目标锁的ID和第一线程的线程ID对应的第四值设置为1,并将目标锁的ID、第一线程的线程ID和与目标锁的ID和第一线程的线程ID对应的第四值添加或存储到重复持锁次数高位列表中。Further, in some embodiments, the field of the number of repeated locks includes a third value and a high flag of the number of repeated locks. When the third value is set by 3 bits in the repeated lock count field, if the first thread's context for the target lock includes the first thread's lock count for the target lock, the first thread sets the third value to 6 or 7, set the high flag of the number of repeated locks to the effective flag, and set the fourth value corresponding to the ID of the target lock and the thread ID of the first thread to 1, and set the ID of the target lock and the thread of the first thread The ID and the fourth value corresponding to the ID of the target lock and the thread ID of the first thread are added or stored in the high list of the number of repeated locks.
步骤1309,第一线程删除与目标锁的ID对应的线程等待列表中第一线程的线程ID。Step 1309: The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
示例的,如图15所示,第一线程删除与目标锁的ID对应的线程等待列表中第一线程的线程ID,具体可以包括以下步骤:For example, as shown in FIG. 15, the first thread deleting the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock may specifically include the following steps:
步骤1501、第一线程根据目标锁的ID,查找与目标锁的ID对应的线程等待列表。 Step 1501, the first thread searches for a thread waiting list corresponding to the ID of the target lock according to the ID of the target lock.
步骤1502、第一线程若未查找到与目标锁的ID对应的线程等待列表,则异常挂死。 Step 1502, if the first thread does not find the thread waiting list corresponding to the ID of the target lock, it will hang abnormally.
步骤1503、第一线程若查找到与目标锁的ID对应的线程等待列表,第一线程从与目标锁的ID对应的线程等待列表中查找第一线程的线程ID。Step 1503: If the first thread finds the thread waiting list corresponding to the ID of the target lock, the first thread searches for the thread ID of the first thread from the thread waiting list corresponding to the ID of the target lock.
步骤1504、第一线程若查找到第一线程的线程ID,第一线程删除第一线程的线程ID。Step 1504: If the first thread finds the thread ID of the first thread, the first thread deletes the thread ID of the first thread.
步骤1505、第一线程若未查找到第一线程的线程ID,第一线程异常挂死。Step 1505: If the first thread does not find the thread ID of the first thread, the first thread hangs abnormally.
此外,针对主动放弃持目标锁的线程可以通过下述方式唤醒:In addition, threads that voluntarily give up holding the target lock can be awakened in the following ways:
方式一:第二线程根据目标锁的ID,查找与目标锁的ID对应的线程等待列表。第二线程当查找到与目标锁的ID对应的线程等待列表后,检查与目标锁的ID对应的线程等待列表。第二线程若检查与目标锁的ID对应的线程等待列表不为空,即与目标锁的ID对应的线程等待列表中存储有线程ID,则第二线程从与目标锁的ID对应的线程等待列表中选择至少一个线程ID,并唤醒与至少一个线程ID标识的线程。Method 1: The second thread searches the thread waiting list corresponding to the ID of the target lock according to the ID of the target lock. After finding the thread waiting list corresponding to the ID of the target lock, the second thread checks the thread waiting list corresponding to the ID of the target lock. If the second thread checks that the thread waiting list corresponding to the ID of the target lock is not empty, that is, the thread ID is stored in the thread waiting list corresponding to the ID of the target lock, the second thread waits from the thread corresponding to the ID of the target lock Select at least one thread ID from the list, and wake up the thread identified by the at least one thread ID.
其中,第二线程可以为与目标锁的ID对应的线程等待列表中一个或多个线程ID所标识的线程属于同一进程、且处于运行态的线程。第二线程可以每隔一定时长检查与目标锁 的ID对应的线程等待列表是否为空。Wherein, the second thread may be a thread that belongs to the same process and is in a running state, and threads identified by one or more thread IDs in the thread waiting list corresponding to the ID of the target lock. The second thread can check whether the thread waiting list corresponding to the ID of the target lock is empty every certain period of time.
例如,第二线程从与目标锁的ID对应的线程等待列表中选择第一个线程ID,并唤醒与第一个线程ID标识的线程。在第一个线程ID指示第一线程的情况下,第一线程被唤醒。For example, the second thread selects the first thread ID from the thread waiting list corresponding to the ID of the target lock, and wakes up the thread identified by the first thread ID. In the case where the first thread ID indicates the first thread, the first thread is awakened.
方式二、系统在检查或检测到第一线程的线程ID位于与目标锁的ID对应的线程等待列表中时,系统从第一线程挂起进入阻塞状态启动定时器,在定时器达到定时时长后,唤醒第一线程。Method 2: When the system checks or detects that the thread ID of the first thread is in the thread waiting list corresponding to the ID of the target lock, the system suspends the first thread and enters the blocking state to start the timer, and after the timer reaches the specified duration , Wake up the first thread.
上述仅为针对主动放弃持目标锁的线程的唤醒方式的举例说明,并不构成对针对主动放弃持目标锁的线程的唤醒方式的限定。The foregoing is only an example of the wake-up method for the thread that actively abandons the target lock, and does not constitute a limitation on the wake-up method for the thread that actively abandons the target lock.
另外,可以理解的是,在第一线程请求中止对目标锁持锁的过程中,第一线程的中断(interrupt)状态发生变化,则抛出异常。例如,第一线程在执行步骤1305中挂起进入阻塞状态的过程中,第一线程的中断(interrupt)状态发生变化,则抛出异常,不再继续执行后续步骤。再例如,第一线程在被唤醒的过程中,第一线程的中断(interrupt)状态发生变化,则抛出异常,不再继续执行后续步骤。In addition, it can be understood that when the first thread requests to suspend the lock on the target, if the interrupt state of the first thread changes, an exception is thrown. For example, when the first thread suspends and enters the blocking state in the execution of step 1305, if the interrupt state of the first thread changes, an exception is thrown and the subsequent steps are not executed. For another example, when the first thread is awakened, if the interrupt state of the first thread changes, an exception is thrown, and the subsequent steps are not continued.
以下结合具体场景对本申请实施例的多线程同步方法进行介绍。The following describes the multi-thread synchronization method of the embodiment of the present application in combination with specific scenarios.
以目标锁为锁1为例。例如,对于锁1来说,持锁线程为线程2,线程2请求释放锁1的同时,线程1可以请求获取锁1,其中,线程1为锁1的非持锁线程。在这种情况下,如图16所示,线程1请求获取锁1和线程2请求释放锁1具体包括以下步骤:Take the target lock as lock 1 as an example. For example, for lock 1, the lock-holding thread is thread 2, and while thread 2 requests to release lock 1, thread 1 can request to acquire lock 1, where thread 1 is a non-lock-holding thread of lock 1. In this case, as shown in Figure 16, thread 1 requests to acquire lock 1 and thread 2 requests to release lock 1 specifically including the following steps:
步骤1601_1、线程1获取锁1的锁状态变量;步骤1601_2、线程2获取锁1的锁状态变量。Step 1601_1, thread 1 obtains the lock state variable of lock 1; step 1601_2, thread 2 obtains the lock state variable of lock 1.
步骤1602_1、线程1检查持锁线程标识域为线程2,检查阻塞线程数域是否为小于第一阈值,若是,则执行步骤1603_1,否则,执行步骤1605_1。Step 1602_1: Thread 1 checks that the lock-holding thread identification field is thread 2, and checks whether the blocked thread number field is less than the first threshold. If yes, execute step 1603_1; otherwise, execute step 1605_1.
步骤1603_1、线程1进行自旋等待。Step 1603_1: Thread 1 performs spin waiting.
步骤1604_1、线程1当自旋等待的次数达到第二阈值时,若检查持锁线程标识域为有效线程且不为第一线程,线程1对阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 1604_1. When the number of spin waits for thread 1 reaches the second threshold, if it is checked that the lock-holding thread identification field is a valid thread and not the first thread, thread 1 performs an increment operation on the blocked thread number field and suspends the entry Blocked state.
步骤1605_1、线程1对阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 1605_1: Thread 1 performs an operation of adding 1 to the blocked thread number field, and suspends and enters a blocking state.
步骤1602_2、线程2检查持锁线程标识域为线程2,检查重复持锁次数域是否为0,若为不为0,则执行步骤1603_2,否则执行步骤1605_2。Step 1602_2, thread 2 checks that the lock thread identification field is thread 2, and checks whether the repeated lock count field is 0, if it is not 0, execute step 1603_2, otherwise execute step 1605_2.
步骤1603_2、线程2对重复持锁次数域执行减1操作。Step 1603_2, Thread 2 performs a minus 1 operation on the repeated lock count field.
步骤1604_2、线程2重新检查重复持锁次数域是否为0,若是,则执行步骤1605_2,否则线程2继续对锁1持锁。Step 1604_2, thread 2 rechecks whether the repeated lock count field is 0, if yes, execute step 1605_2, otherwise thread 2 continues to hold lock 1.
步骤1605_2、线程2将持锁线程标识域设置为无效线程,以及检查阻塞线程数域是否为0,若是则执行步骤1606_2,否则本流程结束。Step 1605_2, thread 2 sets the lock-holding thread identification field to an invalid thread, and checks whether the blocked thread number field is 0, if yes, execute step 1606_2, otherwise the process ends.
步骤1606_2、线程2唤醒针对目标锁处于阻塞状态的至少一个线程。Step 1606_2, thread 2 wakes up at least one thread that is blocked for the target lock.
需要说明的是,步骤1605_2在步骤1602_1中的检查持锁线程标识域为线程2之后执行。It should be noted that step 1605_2 is executed after checking that the lock-holding thread identification field is thread 2 in step 1602_1.
以目标锁为锁1为例。例如,对于锁1来说,持锁线程为线程2,线程2再次请求获取锁1的同时,线程1可以请求获取锁1,其中,线程1为锁1的非持锁线程。在这种情况下,如图17所示,线程1请求获取锁1和线程2再次请求获取锁1具体包括以下步骤:Take the target lock as lock 1 as an example. For example, for lock 1, the lock holding thread is thread 2, and while thread 2 requests to acquire lock 1 again, thread 1 can request to acquire lock 1, where thread 1 is a non-lock holding thread of lock 1. In this case, as shown in Figure 17, the thread 1 requesting lock 1 and the thread 2 requesting lock 1 again specifically include the following steps:
步骤1701_1、线程1获取锁1的锁状态变量;步骤1701_2、线程2获取锁1的锁状态变量。Step 1701_1, thread 1 obtains the lock state variable of lock 1; step 1701_2, thread 2 obtains the lock state variable of lock 1.
步骤1702_1、线程1检查持锁线程标识域为线程2,检查阻塞线程数域是否为小于第一阈值,若是,则执行步骤1703_1,否则,执行步骤1705_1。Step 1702_1: Thread 1 checks that the lock-holding thread identification field is thread 2, and checks whether the blocked thread number field is less than the first threshold. If yes, execute step 1703_1; otherwise, execute step 1705_1.
步骤1703_1、线程1进行自旋等待。Step 1703_1: Thread 1 performs spin waiting.
步骤1704_1、线程1当自旋等待的次数达到第二阈值时,若检查持锁线程标识域为有效线程且不为第一线程,线程1对阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 1704_1. When the number of spin waits for thread 1 reaches the second threshold, if it is checked that the lock-holding thread identification field is a valid thread and not the first thread, thread 1 performs an increment operation on the blocked thread number field and suspends the entry Blocked state.
步骤1705_1、线程1对阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 1705_1: Thread 1 performs an operation of adding 1 to the blocked thread number field, and suspends and enters a blocking state.
步骤1702_2、线程2检查持锁线程标识域为线程2,对重复持锁次数域执行加1操作。Step 1702_2, thread 2 checks that the lock-holding thread identification field is thread 2, and performs an operation of adding 1 to the repeated lock-holding times field.
例如,以锁1的锁状态变量如图18所示,阻塞线程数域为N1,重复持锁次数域为N2,第一阈值为L1,第二阈值L2线程2请求释放锁1的同时,线程1可以请求获取锁1不同时间锁状态变量不同域的变化情况可以参见表1、表2和表3所示。为表1描述,将持锁线程标识域简称为域1,将重复持锁标识域简称为域2,将阻塞线程标识域简称为域3。For example, the lock state variable of lock 1 is shown in Figure 18, the number of blocked threads is N1, the number of repeated locks is N2, the first threshold is L1, and the second threshold is L2. At the same time when thread 2 requests to release lock 1, thread 1 You can request to acquire a lock. 1 The changes in different fields of the lock state variable at different times can be seen in Table 1, Table 2 and Table 3. For the description in Table 1, the lock-holding thread identification domain is abbreviated as domain 1, the repeated lock-holding identification domain is abbreviated as domain 2, and the blocking thread identification domain is abbreviated as domain 3.
表1Table 1
Figure PCTCN2020116437-appb-000001
Figure PCTCN2020116437-appb-000001
表2Table 2
Figure PCTCN2020116437-appb-000002
Figure PCTCN2020116437-appb-000002
表3table 3
Figure PCTCN2020116437-appb-000003
Figure PCTCN2020116437-appb-000003
需要说明的是,上述是以线程为例对多线程同步方法进行介绍的,当程序执行的最小单位为比线程更小或更大的粒度时,上述涉及到的线程还可以替换为程序执行的最小单位,对此不作限定。It should be noted that the above description of the multi-thread synchronization method is based on the thread as an example. When the smallest unit of program execution is a smaller or larger granularity than the thread, the threads involved above can also be replaced by the program execution The smallest unit is not limited.
以上各实施例可以单独使用,也可以相互结合使用,以达到不同的技术效果。The above embodiments can be used alone or in combination with each other to achieve different technical effects.
上述本申请提供的实施例中,从手机作为执行主体的角度对本申请实施例提供的方法进行了介绍。为了实现上述本申请实施例提供的方法中的各功能,手机可以包括硬件结构和/或软件模块,以软件模块或者硬件结构结合软件模块的形式来实现上述各功能。上述各功能中的某个功能以硬件结构、软件模块、还是硬件结构加软件模块的方式来执行,取决于技术方案的特定应用和设计约束条件。In the above-mentioned embodiments provided in the present application, the method provided in the embodiments of the present application is introduced from the perspective of the mobile phone as the execution subject. In order to realize the functions in the methods provided in the above embodiments of the present application, the mobile phone may include a hardware structure and/or a software module, and the above functions are implemented in the form of a software module or a hardware structure combined with a software module. Whether a certain function of the above-mentioned functions is executed by a hardware structure, a software module, or a hardware structure plus a software module depends on the specific application and design constraint conditions of the technical solution.
如图19所示,本申请实施例公开了一种电子设备1900,该电子设备1900可以包括:一个或多个处理器1901和存储器1902。上述各器件可以通过一个或多个通信总线连接。其中该程序指令被存储在上述存储器1902中并被配置为被该一个或多个处理器1901执行,实现本申请实施例图8~图13以及图15~图17所示的多线程同步方法。As shown in FIG. 19, an embodiment of the present application discloses an electronic device 1900. The electronic device 1900 may include: one or more processors 1901 and a memory 1902. The above-mentioned devices can be connected through one or more communication buses. The program instructions are stored in the aforementioned memory 1902 and configured to be executed by the one or more processors 1901 to implement the multi-thread synchronization method shown in FIGS. 8-13 and 15-17 in the embodiments of the present application.
上述各个实施例中涉及处理器可以是通用处理器、数字信号处理器(digital signal processor,DSP)、专用集成电路(application specific integrated circuit,ASIC)、现成可编程门阵列(field programmable gate array,FPGA)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件。可以实现或者执行本申请实施例中的公开的各方法、步骤及逻辑框图。通用处理器可以是微处理器或者该处理器也可以是任何常规的处理器等。结合本申请实施例所公开的方法的步骤可以直接体现为硬件译码处理器执行完成,或者用译码处理器中的硬件及软件模块组合执行完成。软件模块可以位于随机存取存储器(random access memory,RAM)、闪存、只读存储器(read-only memory,ROM)、可编程只读存储器或者电可擦写可编程存储器、寄存器等本领域成熟的存储介质中。该存储介质位于存储器,处理器读取存储器中的指令,结合其硬件完成上述方法的步骤。The processors involved in the above embodiments may be general-purpose processors, digital signal processors (digital signal processors, DSP), application specific integrated circuits (ASICs), ready-made programmable gate arrays (field programmable gate arrays, FPGAs). ) Or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps, and logical block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor or the like. The steps of the method disclosed in the embodiments of the present application can be directly embodied as being executed and completed by a hardware decoding processor, or executed and completed by a combination of hardware and software modules in the decoding processor. The software module can be located in random access memory (RAM), flash memory, read-only memory (read-only memory, ROM), programmable read-only memory or electrically erasable programmable memory, registers, etc. mature in the field Storage medium. The storage medium is located in the memory, and the processor reads the instructions in the memory and completes the steps of the above method in combination with its hardware.
具体的,上述电子设备1900的具体实现方式可以参见方法部分的相关介绍,在此不再赘述。Specifically, for the specific implementation of the above-mentioned electronic device 1900, reference may be made to the related introduction in the method section, and details are not described herein again.
本领域普通技术人员可以意识到,结合本文中所公开的实施例描述的各示例的单元及算法步骤,能够以电子硬件、或者计算机软件和电子硬件的结合来实现。这些功能究竟以硬件还是软件方式来执行,取决于技术方案的特定应用和设计约束条件。专业技术人员可以对每个特定的应用来使用不同方法来实现所描述的功能。A person of ordinary skill in the art may realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware or a combination of computer software and electronic hardware. Whether these functions are performed by hardware or software depends on the specific application and design constraint conditions of the technical solution. Professional technicians can use different methods for each specific application to achieve the described functions.
所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,上述描述的系统、装置和单元的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。Those skilled in the art can clearly understand that, for the convenience and conciseness of description, the specific working process of the system, device and unit described above can refer to the corresponding process in the foregoing method embodiment, which will not be repeated here.
在本申请所提供的几个实施例中,应该理解到,所揭露的系统、装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个系统,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。In the several embodiments provided in this application, it should be understood that the disclosed system, device, and method may be implemented in other ways. For example, the device embodiments described above are merely illustrative, for example, the division of the units 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 It can be integrated into another system, or some features can be ignored or not implemented. In addition, 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 objectives of the solutions of the embodiments.
另外,在本申请各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。In addition, the functional units in the various embodiments 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.
所述功能如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储 在一个计算机可读取存储介质中。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)或处理器(processor)执行本申请各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、ROM、RAM、磁碟或者光盘等各种可以存储程序代码的介质。If 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. Based on this understanding, the technical solution of the present application essentially or the part that contributes to the existing technology or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including A number of instructions are used to make a computer device (which may be a personal computer, a server, or a network device, etc.) or a processor (processor) execute all or part of the steps of the methods described in the various embodiments of the present application. The aforementioned storage media include: U disk, mobile hard disk, ROM, RAM, magnetic disk or optical disk and other media that can store program codes.
以上所述,仅为本申请的具体实施方式,但本申请的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本申请揭露的技术范围内,可轻易想到的变化或替换,都应涵盖在本申请的保护范围之内,因此本申请的保护范围应以权利要求的保护范围为准。The above are only specific implementations of this application, but the protection scope of this application is not limited to this. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed in this application, All should be covered within the protection scope of this application, so the protection scope of this application should be subject to the protection scope of the claims.

Claims (28)

  1. 一种多线程同步方法,其特征在于,所述方法包括:A multi-thread synchronization method, characterized in that the method includes:
    第一线程请求获取目标锁,包括:The first thread requests to acquire the target lock, including:
    步骤一、所述第一线程获取所述目标锁的锁状态变量,其中,所述锁状态变量包括持锁线程标识域和阻塞线程数域,所述持锁线程标识域用于指示所述目标锁的持锁线程,所述阻塞线程数域用于指示针对所述目标锁处于阻塞状态的线程数;Step 1. The first thread acquires the lock state variable of the target lock, where the lock state variable includes a lock-holding thread identification field and a blocked thread number field, and the lock-holding thread identification field is used to indicate the target The lock-holding thread of the lock, where the blocked thread count field is used to indicate the number of threads that are in a blocked state for the target lock;
    步骤二、所述第一线程检查所述持锁线程标识域:Step 2: The first thread checks the lock-holding thread identification field:
    步骤三、所述第一线程若检查所述持锁线程标识域为有效线程且不为所述第一线程,所述第一线程检查所述阻塞线程数域;Step 3: If the first thread checks that the lock-holding thread identification field is a valid thread and not the first thread, the first thread checks the blocked thread number field;
    步骤四、所述第一线程若检查所述阻塞线程数域小于第一阈值,所述第一线程进行自旋等待;Step 4: If the first thread checks that the blocked thread number domain is less than a first threshold, the first thread performs spin waiting;
    步骤五、所述第一线程当自旋等待的次数达到第二阈值时,若检查所述持锁线程标识域为有效线程且不为所述第一线程,所述第一线程对所述阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 5. When the number of spin waits for the first thread reaches the second threshold, if the lock-holding thread identification field is checked as a valid thread and not the first thread, the first thread blocks the The thread number field executes the increment operation, and suspends and enters the blocking state.
  2. 如权利要求1所述的方法,其特征在于,所述步骤三之后还包括:The method according to claim 1, characterized in that, after the step three, the method further comprises:
    步骤六、所述第一线程若检查所述阻塞线程数域大于或等于所述第一阈值,所述第一线程对所述阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 6. If the first thread checks that the blocked thread number field is greater than or equal to the first threshold, the first thread performs an increment operation on the blocked thread number field and suspends and enters a blocking state.
  3. 如权利要求1或2所述的方法,其特征在于,所述锁状态变量还包括重复持锁次数域,所述重复持锁次数域用于指示持锁线程针对所述目标锁的持锁次数。The method according to claim 1 or 2, wherein the lock state variable further comprises a repeated lock count field, and the repeated lock count field is used to indicate the number of times the lock holding thread holds the lock for the target lock .
  4. 如权利要求3所述的方法,其特征在于,所述步骤二之后还包括:The method according to claim 3, characterized in that, after the step two, the method further comprises:
    步骤七、所述第一线程若检查所述持锁线程标识域为所述第一线程,所述第一线程对所述重复持锁次数域执行加1操作。Step 7. If the first thread checks that the lock-holding thread identification field is the first thread, the first thread performs an increment operation on the number of repeated lock-holding times field.
  5. 如权利要求3所述的方法,其特征在于,所述步骤二之后还包括:The method according to claim 3, characterized in that, after the step two, the method further comprises:
    步骤八、所述第一线程若检查所述持锁线程标识域为无效线程,所述第一线程将所述持锁线程标识域设置为所述第一线程,以及将所述重复持锁次数域设置为初始值。Step 8. If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread and sets the number of repeated locks The domain is set to the initial value.
  6. 如权利要求3所述的方法,其特征在于,所述方法还包括:The method of claim 3, wherein the method further comprises:
    所述第一线程请求释放所述目标锁,包括The request by the first thread to release the target lock includes
    步骤九、所述第一线程获取所述目标锁的锁状态变量:Step 9. The first thread acquires the lock state variable of the target lock:
    步骤十、所述第一线程检查所述持锁线程标识域;Step 10. The first thread checks the lock thread identification field;
    步骤十一、所述第一线程若检查所述持锁线程标识域为所述第一线程,所述第一线程检查所述重复持锁次数域;Step eleven, if the first thread checks that the lock-holding thread identification field is the first thread, the first thread checks the repeated lock-holding times field;
    步骤十二、所述第一线程若检查所述重复持锁次数域不为0,所述第一线程对所述重复持锁次数域执行减1操作。Step 12. If the first thread checks that the number of repeated locks field is not 0, the first thread performs a subtraction operation on the number of repeated locks field.
  7. 如权利要求6所述的方法,其特征在于,所述步骤十二之后还包括:The method according to claim 6, characterized in that, after the step twelve, the method further comprises:
    步骤十三、所述第一线程若检查所述重复持锁次数域为0,所述第一线程将所述持锁线程标识域设置为无效线程;Step 13. If the first thread checks that the number of repeated locks field is 0, the first thread sets the lock thread identification field as an invalid thread;
    步骤十四、所述第一线程检查所述阻塞线程数域;Step 14. The first thread checks the blocked thread number field;
    步骤十五、所述第一线程若检查所述阻塞线程数域不为0,所述第一线程唤醒针对所述目标锁处于阻塞状态的至少一个线程。Step 15. If the first thread checks that the blocked thread count field is not 0, the first thread wakes up at least one thread that is blocked for the target lock.
  8. 如权利要求3所述的方法,其特征在于,所述方法还包括:The method of claim 3, wherein the method further comprises:
    所述第一线程请求中止对所述目标锁持锁,包括:The request by the first thread to suspend the locking of the target includes:
    步骤十六、所述第一线程获取所述目标锁的锁状态变量;Step 16. The first thread acquires the lock state variable of the target lock;
    步骤十七、所述第一线程检查所述持锁线程标识域;Step 17. The first thread checks the lock-holding thread identification field;
    步骤十八、所述第一线程若检查所述持锁线程标识域为所述第一线程,第一线程将所述重复持锁次数域保存为所述第一线程针对所述目标锁的上下文;Step 18. If the first thread checks that the lock-holding thread identification field is the first thread, the first thread saves the repeated lock-holding times field as the context of the first thread for the target lock ;
    步骤十九、所述第一线程将所述第一线程的线程标识ID添加到与所述目标锁的ID对应的线程等待列表中;Step 19: The first thread adds the thread identification ID of the first thread to the thread waiting list corresponding to the ID of the target lock;
    步骤二十、所述第一线程将所述重复持锁次数域置0,以及将所述持锁线程标识域设置为无效线程,并挂起进入阻塞状态。Step 20: The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field as an invalid thread, and suspends and enters a blocking state.
  9. 如权利要求8所述的方法,其特征在于,所述步骤二十之后还包括:The method according to claim 8, characterized in that, after the step 20, the method further comprises:
    步骤二十一、所述第一线程当被唤醒后,获取所述目标锁的锁状态变量;Step 21: After being awakened, the first thread acquires the lock state variable of the target lock;
    步骤二十二、所述第一线程检查所述持锁线程标识域;Step 22: The first thread checks the lock thread identification field;
    步骤二十三、所述第一线程若检查所述持锁线程标识域为无效线程,所述第一线程将所述持锁线程标识域设置为所述第一线程,并根据所述第一线程针对所述目标锁的上下文,设置所述重复持锁次数域;Step 23: If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and according to the first thread For the context of the target lock, the thread sets the repeated lock count field;
    步骤二十四、所述第一线程删除与所述目标锁的ID对应的线程等待列表中的第一线程的线程ID。Step 24: The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
  10. 如权利要求9所述的方法,其特征在于,所述步骤二十一包括:The method according to claim 9, wherein the step 21 comprises:
    所述第一线程当被第二线程唤醒后,获取所述目标锁的锁状态变量,其中,所述第二线程是当与所述目标锁的ID对应的线程等待列表中存储有所述第一线程的线程ID时,唤醒所述第二线程的;所述第二线程与所述第一线程属于同一进程;或者,The first thread acquires the lock state variable of the target lock after being awakened by the second thread, wherein the second thread is when the thread waiting list corresponding to the ID of the target lock stores the first thread When the thread ID of a thread, wake up the second thread; the second thread and the first thread belong to the same process; or,
    所述第一线程当挂起的时长超过第三阈值时被系统唤醒,获取所述目标锁的锁状态变量。The first thread is awakened by the system when the suspended duration exceeds a third threshold, and acquires the lock state variable of the target lock.
  11. 如权利要求1至10任一所述的方法,其特征在于,所述阻塞线程数域包括第一值和阻塞线程数高位标识,所述第一值为针对所述目标锁处于阻塞状态的线程数的低位上的值,所述阻塞线程数高位标识用于指示阻塞线程数高位列表中是否存储有与所述目标锁的ID对应的第二值,所述第二值为针对所述目标锁处于阻塞状态的线程数的高位上的值。The method according to any one of claims 1 to 10, wherein the field of the number of blocked threads includes a first value and a high-order flag of the number of blocked threads, and the first value is a thread that is in a blocked state for the target lock The high-order flag of the number of blocked threads is used to indicate whether a second value corresponding to the ID of the target lock is stored in the high-order list of the number of blocked threads, and the second value is for the target lock The high value of the number of threads in a blocked state.
  12. 如权利要求3至10任一所述的方法,其特征在于,所述重复持锁次数域包括第三值和重复持锁次数高位标识,所述第三值为持锁线程针对所述目标锁的持锁次数的低位上的值,所述重复持锁次数高位标识用于指示重复持锁次数高位列表中是否存储有与所述目标锁的ID和持锁线程的线程ID对应的第四值,所述第四值为持锁线程针对所述目标锁的持锁次数的高位上的值。The method according to any one of claims 3 to 10, wherein the domain of the number of repeated locks includes a third value and a high flag of the number of repeated locks, and the third value is the lock-holding thread for the target lock The low value of the number of lock holding times, the high bit identifier of the number of repeated lock times is used to indicate whether the fourth value corresponding to the ID of the target lock and the thread ID of the lock thread is stored in the high list of the number of times of repeated lock The fourth value is a high value of the number of lock holding times of the lock holding thread for the target lock.
  13. 如权利要求1至12任一所述的方法,其特征在于,所述第一线程挂起进入阻塞状态,包括:The method according to any one of claims 1 to 12, wherein the suspending of the first thread and entering the blocking state comprises:
    所述第一线程进行系统调用,将所述第一线程的状态设置为睡眠态,并将所述第一线程添加到与所述目标锁的地址对应的等待队列中。The first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock.
  14. 一种电子设备,其特征在于,所述电子设备包括处理器和存储器,所述存储器存储有程序指令;所述处理器调用所述程序指令,用于第一线程请求获取目标锁;An electronic device, wherein the electronic device includes a processor and a memory, and the memory stores program instructions; the processor calls the program instructions for a first thread to request to acquire a target lock;
    所述第一线程请求获取目标锁,包括:The request by the first thread to acquire the target lock includes:
    步骤一、所述第一线程获取所述目标锁的锁状态变量,其中,所述锁状态变量包括持锁线程标识域和阻塞线程数域,所述持锁线程标识域用于指示所述目标锁的持锁线程,所述阻塞线程数域用于指示针对所述目标锁处于阻塞状态的线程数;Step 1. The first thread acquires the lock state variable of the target lock, where the lock state variable includes a lock-holding thread identification field and a blocked thread number field, and the lock-holding thread identification field is used to indicate the target The lock-holding thread of the lock, where the blocked thread count field is used to indicate the number of threads that are in a blocked state for the target lock;
    步骤二、所述第一线程检查所述持锁线程标识域:Step 2: The first thread checks the lock-holding thread identification field:
    步骤三、所述第一线程若检查所述持锁线程标识域为有效线程且不为所述第一线程,所述第一线程检查所述阻塞线程数域;Step 3: If the first thread checks that the lock-holding thread identification field is a valid thread and not the first thread, the first thread checks the blocked thread number field;
    步骤四、所述第一线程若检查所述阻塞线程数域小于第一阈值,所述第一线程进行自旋等待;Step 4: If the first thread checks that the blocked thread number domain is less than a first threshold, the first thread performs spin waiting;
    步骤五、所述第一线程当自旋等待的次数达到第二阈值时,若检查所述持锁线程标识域为有效线程且不为所述第一线程,所述第一线程对所述阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 5. When the number of spin waits for the first thread reaches the second threshold, if the lock-holding thread identification field is checked as a valid thread and not the first thread, the first thread blocks the The thread number field executes the increment operation, and suspends and enters the blocking state.
  15. 如权利要求14所述的电子设备,其特征在于,所述步骤三之后,还包括:The electronic device according to claim 14, characterized in that, after the step three, it further comprises:
    步骤六、所述第一线程若检查所述阻塞线程数域大于或等于所述第一阈值,所述第一线程对所述阻塞线程数域执行加1操作,并挂起进入阻塞状态。Step 6. If the first thread checks that the blocked thread number field is greater than or equal to the first threshold, the first thread performs an increment operation on the blocked thread number field and suspends and enters a blocking state.
  16. 如权利要求14或15所述的电子设备,其特征在于,所述锁状态变量还包括重复持锁次数域,所述重复持锁次数域用于指示持锁线程针对所述目标锁的持锁次数。The electronic device according to claim 14 or 15, wherein the lock state variable further comprises a repeated lock hold count field, and the repeated lock hold count field is used to indicate the lock holding thread for the target lock. frequency.
  17. 如权利要求16所述的电子设备,其特征在于,所述步骤二之后还包括:The electronic device according to claim 16, characterized in that, after the step two, it further comprises:
    步骤七、所述第一线程若检查所述持锁线程标识域为所述第一线程,所述第一线程对所述重复持锁次数域执行加1操作。Step 7. If the first thread checks that the lock-holding thread identification field is the first thread, the first thread performs an increment operation on the number of repeated lock-holding times field.
  18. 如权利要求16所述的电子设备,其特征在于,所述步骤二之后还包括:The electronic device according to claim 16, characterized in that, after the step two, it further comprises:
    步骤八、所述第一线程若检查所述持锁线程标识域为无效线程,所述第一线程将所述持锁线程标识域设置为所述第一线程,以及将所述重复持锁次数域设置为初始值。Step 8. If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread and sets the number of repeated locks The domain is set to the initial value.
  19. 如权利要求16所述的电子设备,其特征在于,所述处理器调用所述程序指令,还用于所述第一线程请求释放所述目标锁:The electronic device according to claim 16, wherein the processor calls the program instructions and is also used by the first thread to request the release of the target lock:
    所述第一线程请求释放所述目标锁,包括The request by the first thread to release the target lock includes
    步骤九、所述第一线程获取所述目标锁的锁状态变量:Step 9. The first thread acquires the lock state variable of the target lock:
    步骤十、所述第一线程检查所述持锁线程标识域;Step 10. The first thread checks the lock thread identification field;
    步骤十一、所述第一线程若检查所述持锁线程标识域为所述第一线程,所述第一线程检查所述重复持锁次数域;Step eleven, if the first thread checks that the lock-holding thread identification field is the first thread, the first thread checks the repeated lock-holding times field;
    步骤十二、所述第一线程若检查所述重复持锁次数域不为0,所述第一线程对所述重复持锁次数域执行减1操作。Step 12. If the first thread checks that the number of repeated locks field is not 0, the first thread performs a subtraction operation on the number of repeated locks field.
  20. 如权利要求19所述的电子设备,其特征在于,所述步骤十二之后还包括:The electronic device according to claim 19, characterized in that, after the step twelve, it further comprises:
    步骤十三、所述第一线程若检查所述重复持锁次数域为0,所述第一线程将所述持锁线程标识域设置为无效线程;Step 13. If the first thread checks that the number of repeated locks field is 0, the first thread sets the lock thread identification field as an invalid thread;
    步骤十四、所述第一线程检查所述阻塞线程数域;Step 14. The first thread checks the blocked thread number field;
    步骤十五、所述第一线程若检查所述阻塞线程数域不为0,所述第一线程唤醒针对所述目标锁处于阻塞状态的至少一个线程。Step 15. If the first thread checks that the blocked thread count field is not 0, the first thread wakes up at least one thread that is blocked for the target lock.
  21. 如权利要求16所述的电子设备,其特征在于,所述处理器调用所述程序指令,还用于所述第一线程请求中止对所述目标锁持锁:The electronic device according to claim 16, wherein the processor calls the program instructions and is further used for the first thread to request suspension of the lock lock on the target:
    所述第一线程请求中止对所述目标锁持锁,包括:The request by the first thread to suspend the locking of the target includes:
    步骤十六、所述第一线程获取所述目标锁的锁状态变量;Step 16. The first thread acquires the lock state variable of the target lock;
    步骤十七、所述第一线程检查所述持锁线程标识域;Step 17. The first thread checks the lock-holding thread identification field;
    步骤十八、所述第一线程若检查所述持锁线程标识域为所述第一线程,将所述重复持锁次数域保存为所述第一线程针对所述目标锁的上下文;Step 18. If the first thread checks that the lock-holding thread identification field is the first thread, save the repeated lock-holding times field as the context of the first thread for the target lock;
    步骤十九、所述第一线程将所述第一线程的线程标识ID添加到与所述目标锁的ID对应的线程等待列表中;Step 19: The first thread adds the thread identification ID of the first thread to the thread waiting list corresponding to the ID of the target lock;
    步骤二十、所述第一线程将所述重复持锁次数域置0,以及将所述持锁线程标识域设置为无效线程,并挂起进入阻塞状态。Step 20: The first thread sets the number of repeated locks field to 0, and sets the lock thread identification field as an invalid thread, and suspends and enters a blocking state.
  22. 如权利要求21所述的电子设备,其特征在于,所述步骤二十之后还包括:The electronic device according to claim 21, characterized in that, after the step 20, it further comprises:
    步骤二十一、所述第一线程当被唤醒后,获取所述目标锁的锁状态变量;Step 21: After being awakened, the first thread acquires the lock state variable of the target lock;
    步骤二十二、所述第一线程检查所述持锁线程标识域;Step 22: The first thread checks the lock thread identification field;
    步骤二十三、所述第一线程若检查所述持锁线程标识域为无效线程,所述第一线程将所述持锁线程标识域设置为所述第一线程,并根据所述第一线程针对所述目标锁的上下文,设置所述重复持锁次数域;Step 23: If the first thread checks that the lock-holding thread identification field is an invalid thread, the first thread sets the lock-holding thread identification field to the first thread, and according to the first thread For the context of the target lock, the thread sets the repeated lock count field;
    步骤二十四、所述第一线程删除与所述目标锁的ID对应的线程等待列表中的第一线程的线程ID。Step 24: The first thread deletes the thread ID of the first thread in the thread waiting list corresponding to the ID of the target lock.
  23. 如权利要求22所述的电子设备,其特征在于,所述步骤二十一包括:The electronic device of claim 22, wherein the step 21 comprises:
    所述第一线程当被第二线程唤醒后,获取所述目标锁的锁状态变量,其中,所述第二线程是当与所述目标锁的ID对应的线程等待列表中存储有所述第一线程的线程ID时,唤醒所述第二线程的;所述第二线程与所述第一线程属于同一进程;或者,The first thread acquires the lock state variable of the target lock after being awakened by the second thread, wherein the second thread is when the thread waiting list corresponding to the ID of the target lock stores the first thread When the thread ID of a thread, wake up the second thread; the second thread and the first thread belong to the same process; or,
    所述第一线程当挂起的时长超过第三阈值时被系统唤醒,获取所述目标锁的锁状态变量。The first thread is awakened by the system when the suspended duration exceeds a third threshold, and acquires the lock state variable of the target lock.
  24. 如权利要求14至23任一所述的电子设备,其特征在于,所述阻塞线程数域包括第一值和阻塞线程数高位标识,所述第一值为针对所述目标锁处于阻塞状态的线程数的低位上的值,所述阻塞线程数高位标识用于指示阻塞线程数高位列表中是否存储有与所述目标锁的ID对应的第二值,所述第二值为针对所述目标锁处于阻塞状态的线程数的高位上的值。The electronic device according to any one of claims 14 to 23, wherein the blocked thread number field comprises a first value and a high-order flag of the blocked thread number, and the first value is a value for the target lock in a blocked state The low-order value of the number of threads, the high-order flag of the number of blocked threads is used to indicate whether a second value corresponding to the ID of the target lock is stored in the high-order list of the number of blocked threads, and the second value is for the target The high value of the number of threads whose lock is in a blocked state.
  25. 如权利要求16至24任一所述的电子设备,其特征在于,所述重复持锁次数域包括第三值和重复持锁次数高位标识,所述第三值为持锁线程针对所述目标锁的持锁次数的低位上的值,所述重复持锁次数高位标识用于指示重复持锁次数高位列表中是否存储有与所述目标锁的ID和持锁线程的线程ID对应的第四值,所述第四值为持锁线程针对所述目标锁的持锁次数的高位上的值。The electronic device according to any one of claims 16 to 24, wherein the domain of the number of repeated locks includes a third value and a high flag of the number of repeated locks, and the third value is that the lock-holding thread targets the target The value of the low-order value of the number of lock holdings, the high-order flag of the number of repeated locks is used to indicate whether the high-order list of the number of repeated locks stores the fourth corresponding to the ID of the target lock and the thread ID of the lock-holding thread The fourth value is the high value of the number of lock holding times of the lock holding thread for the target lock.
  26. 如权利要求14至25任一所述的电子设备,其特征在于,所述第一线程挂起进入阻塞状态,包括:The electronic device according to any one of claims 14 to 25, wherein the suspension of the first thread into a blocking state comprises:
    所述第一线程进行系统调用,将所述第一线程的状态设置为睡眠态,并将所述第一线程添加到与所述目标锁的地址对应的等待队列中。The first thread makes a system call, sets the state of the first thread to a sleep state, and adds the first thread to the waiting queue corresponding to the address of the target lock.
  27. 一种芯片,其特征在于,所述装置与存储器耦合,使得所述装置在运行时调用所述存储器中存储的程序指令,使得电子设备执行如权利要求1至13任一所述的方法。A chip, characterized in that the device is coupled with a memory, so that the device calls the program instructions stored in the memory during operation, so that an electronic device executes the method according to any one of claims 1 to 13.
  28. 一种计算机可读存储介质,其特征在于,所述计算机可读存储介质包括程序指令,当所述程序指令在所述电子设备上运行时,使得所述电子设备执行如权利要求1至13任 一所述的方法。A computer-readable storage medium, wherein the computer-readable storage medium includes program instructions, when the program instructions run on the electronic device, the electronic device executes any of claims 1 to 13 One described method.
PCT/CN2020/116437 2019-09-25 2020-09-21 Multi-thread synchronization method and electronic device WO2021057643A1 (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
EP20869498.4A EP4020210A4 (en) 2019-09-25 2020-09-21 Multi-thread synchronization method and electronic device
US17/763,490 US20220350602A1 (en) 2019-09-25 2020-09-21 Multi-Thread Synchronization Method and Electronic Device

Applications Claiming Priority (4)

Application Number Priority Date Filing Date Title
CN201910912971 2019-09-25
CN201910912971.6 2019-09-25
CN202010245880.4A CN111552574A (en) 2019-09-25 2020-03-31 Multithreading synchronization method and electronic equipment
CN202010245880.4 2020-03-31

Publications (1)

Publication Number Publication Date
WO2021057643A1 true WO2021057643A1 (en) 2021-04-01

Family

ID=72007305

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/116437 WO2021057643A1 (en) 2019-09-25 2020-09-21 Multi-thread synchronization method and electronic device

Country Status (4)

Country Link
US (1) US20220350602A1 (en)
EP (1) EP4020210A4 (en)
CN (1) CN111552574A (en)
WO (1) WO2021057643A1 (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115185718A (en) * 2022-09-14 2022-10-14 北京云枢创新软件技术有限公司 System SC and C + + based multi-thread data transmission system
CN115408153A (en) * 2022-08-26 2022-11-29 海光信息技术股份有限公司 Instruction distribution method, apparatus and storage medium for multithreaded processor

Families Citing this family (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111552574A (en) * 2019-09-25 2020-08-18 华为技术有限公司 Multithreading synchronization method and electronic equipment
CN112231131B (en) * 2020-09-28 2024-05-28 北京金山云网络技术有限公司 Method, device and equipment for realizing database lock and readable storage medium
CN113760369A (en) * 2020-09-29 2021-12-07 北京沃东天骏信息技术有限公司 Concurrent thread processing method, concurrent thread processing device, electronic equipment and storage medium
US20230056500A1 (en) * 2021-08-18 2023-02-23 Micron Technology, Inc. Chained resource locking
CN116700817B (en) * 2022-11-10 2024-05-31 荣耀终端有限公司 Application program running method and electronic equipment
CN116700818B (en) * 2022-12-05 2024-04-12 荣耀终端有限公司 Application program running method and electronic equipment
CN116860462B (en) * 2023-09-04 2023-11-17 奇点数联(北京)科技有限公司 Multithreading data acquisition method based on multi-bin slicing
CN117271144B (en) * 2023-11-22 2024-04-23 荣耀终端有限公司 Thread processing method and electronic equipment

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1601478A (en) * 2003-09-25 2005-03-30 国际商业机器公司 Method and system for dynamically bounded spinning threads on a contested mutex
US20060288192A1 (en) * 2005-06-16 2006-12-21 Abernathy Christopher M Fine grained multi-thread dispatch block mechanism
CN102566979A (en) * 2011-12-02 2012-07-11 华为技术有限公司 Method, device and multi-core processor system for realizing self-adaptive lock
CN105700939A (en) * 2016-04-21 2016-06-22 北京京东尚科信息技术有限公司 Method and system for multi-thread synchronization in distributed system
CN111552574A (en) * 2019-09-25 2020-08-18 华为技术有限公司 Multithreading synchronization method and electronic equipment

Family Cites Families (15)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5862376A (en) * 1996-06-24 1999-01-19 Sun Microsystems, Inc. System and method for space and time efficient object locking
US6952827B1 (en) * 1998-11-13 2005-10-04 Cray Inc. User program and operating system interface in a multithreaded environment
US6173442B1 (en) * 1999-02-05 2001-01-09 Sun Microsystems, Inc. Busy-wait-free synchronization
US7058948B2 (en) * 2001-08-10 2006-06-06 Hewlett-Packard Development Company, L.P. Synchronization objects for multi-computer systems
US20040068607A1 (en) * 2002-10-07 2004-04-08 Narad Charles E. Locking memory locations
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
US8752057B1 (en) * 2008-06-30 2014-06-10 Emc Corporation Techniques for synchronizing processing of at least two code threads
US10579413B2 (en) * 2013-08-14 2020-03-03 International Business Machines Corporation Efficient task scheduling using a locking mechanism
US9223574B2 (en) * 2014-03-27 2015-12-29 International Business Machines Corporation Start virtual execution instruction for dispatching multiple threads in a computer
US9996402B2 (en) * 2014-04-07 2018-06-12 Oracle International Corporation System and method for implementing scalable adaptive reader-writer locks
US9471400B1 (en) * 2015-07-28 2016-10-18 International Business Machines Corporation Reentrant read-write lock algorithm
US10417056B2 (en) * 2015-08-04 2019-09-17 Oracle International Corporation Systems and methods for performing concurrency restriction and throttling over contended locks
US10108453B2 (en) * 2016-06-30 2018-10-23 International Business Machines Corporation Averting lock contention associated with core-based hardware threading in a split core environment
US11226852B2 (en) * 2016-11-25 2022-01-18 Genetec Inc. System for inter-process communication
US10922147B2 (en) * 2018-07-19 2021-02-16 EMC IP Holding Company LLC Storage system destaging based on synchronization object with watermark

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1601478A (en) * 2003-09-25 2005-03-30 国际商业机器公司 Method and system for dynamically bounded spinning threads on a contested mutex
US20060288192A1 (en) * 2005-06-16 2006-12-21 Abernathy Christopher M Fine grained multi-thread dispatch block mechanism
CN102566979A (en) * 2011-12-02 2012-07-11 华为技术有限公司 Method, device and multi-core processor system for realizing self-adaptive lock
CN105700939A (en) * 2016-04-21 2016-06-22 北京京东尚科信息技术有限公司 Method and system for multi-thread synchronization in distributed system
CN111552574A (en) * 2019-09-25 2020-08-18 华为技术有限公司 Multithreading synchronization method and electronic equipment

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
See also references of EP4020210A4 *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115408153A (en) * 2022-08-26 2022-11-29 海光信息技术股份有限公司 Instruction distribution method, apparatus and storage medium for multithreaded processor
CN115185718A (en) * 2022-09-14 2022-10-14 北京云枢创新软件技术有限公司 System SC and C + + based multi-thread data transmission system

Also Published As

Publication number Publication date
CN111552574A (en) 2020-08-18
EP4020210A4 (en) 2022-09-28
EP4020210A1 (en) 2022-06-29
US20220350602A1 (en) 2022-11-03

Similar Documents

Publication Publication Date Title
WO2021057643A1 (en) Multi-thread synchronization method and electronic device
EP4002108A1 (en) Application start method and electronic device
WO2021083378A1 (en) Method for accelerating starting of application, and electronic device
WO2020103764A1 (en) Voice control method and electronic device
WO2022068483A1 (en) Application startup method and apparatus, and electronic device
KR20150066083A (en) Multi tasking method of electronic apparatus and electronic apparatus thereof
CN113553130B (en) Method for executing drawing operation by application and electronic equipment
WO2021253975A1 (en) Permission management method and apparatus for application, and electronic device
WO2021052311A1 (en) Method for displaying user interface according to color of rear case, and electronic device
WO2021223539A1 (en) Radio frequency resource allocation method and apparatus
WO2021218429A1 (en) Method for managing application window, and terminal device and computer-readable storage medium
CN115629884B (en) Thread scheduling method, electronic equipment and storage medium
WO2022262434A1 (en) Power optimization method and electronic device
CN113806105A (en) Message processing method and device, electronic equipment and readable storage medium
WO2022052897A1 (en) Method and device for adjusting memory configuration parameter
WO2021151350A1 (en) Method and apparatus for loading dynamic link library
WO2021175272A1 (en) Method for displaying application information and related device
WO2021185352A1 (en) Version upgrade method and related apparatus
WO2022078105A1 (en) Memory management method, electronic device, and computer-readable storage medium
US20150062352A1 (en) Method for transmitting contents and electronic device thereof
WO2021238387A1 (en) Application execution method and apparatus
WO2021190489A1 (en) Method for displaying time, and electronic device
WO2023103800A1 (en) Drawing method and electronic device
CN116700913B (en) Scheduling method, equipment and storage medium of embedded file system
WO2023005751A1 (en) Rendering method and electronic device

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: 20869498

Country of ref document: EP

Kind code of ref document: A1

ENP Entry into the national phase

Ref document number: 2020869498

Country of ref document: EP

Effective date: 20220321

NENP Non-entry into the national phase

Ref country code: DE