WO2022001303A1 - Lock management method, apparatus, and device - Google Patents

Lock management method, apparatus, and device Download PDF

Info

Publication number
WO2022001303A1
WO2022001303A1 PCT/CN2021/088656 CN2021088656W WO2022001303A1 WO 2022001303 A1 WO2022001303 A1 WO 2022001303A1 CN 2021088656 W CN2021088656 W CN 2021088656W WO 2022001303 A1 WO2022001303 A1 WO 2022001303A1
Authority
WO
WIPO (PCT)
Prior art keywords
lock
cpu core
program
interrupt
module
Prior art date
Application number
PCT/CN2021/088656
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 华为技术有限公司
Publication of WO2022001303A1 publication Critical patent/WO2022001303A1/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/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4812Task transfer initiation or dispatching by interrupt, e.g. masked
    • 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/48Program initiating; Program switching, e.g. by interrupt
    • 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

Definitions

  • the present application relates to the field of computers, and in particular, to a lock management method, device and device.
  • a multi-core central processing unit runs multiple threads in parallel.
  • a thread is a single sequential control flow in program execution, the smallest unit of program execution flow, and the basic unit of processor scheduling and dispatch.
  • a process can have one or more threads, and each thread shares the program's memory space.
  • programs running in parallel In order to ensure the safe access of a program to a certain resource and maintain data consistency, programs running in parallel generally need to use a lock mechanism for synchronization, so as to ensure the consistency of shared resource data during program operation.
  • a thread acquires the lock it can access the critical section and perform operations in the critical section. After the execution of the critical section is completed, the thread releases the lock. At this time, other threads can obtain the lock to execute their own critical section. Since at most one thread can acquire the lock at the same time, the critical section can only be executed by at most one thread at a certain time. In this way, the method of locking ensures that the critical section will not be parallelized.
  • other threads need to wait for the lock to be released, and other threads will keep trying to acquire the lock while waiting for the lock to be released, occupying CPU resources. It can be seen that the longer the thread that obtains the lock executes in the critical section, the longer the waiting time of other threads will be, which will cause unnecessary consumption of CPU resources for a long time.
  • the present application provides a lock management method, device and device to improve performance and save resources.
  • the present application provides a lock management method, which can be applied to a lock management module, which can be arranged in a first CPU and communicate with at least one CPU core and an interrupt distribution module in the first CPU.
  • the at least one CPU core includes the first CPU core.
  • the above lock management method may include: the lock management module obtains a lock application request from the first CPU core to request to allocate a lock for the first program in the first CPU core; the lock management module responds to the lock application request, for The first program distributes the lock; the lock management module sends a first signal to the interrupt distribution module to instruct the interrupt distribution module to stop distributing the interrupt to the first CPU core.
  • step of the lock management module assigning the lock to the first program and the step of the lock management module sending the first signal to the interrupt distribution module can be performed in parallel, that is, the lock management module can respond to the lock application request and assign the lock to the first program. At the same time, the first signal is sent to the interrupt distribution module.
  • the above two steps can also be performed in series, that is, the lock management module first allocates the lock to the first program, and then sends the first signal to the interrupt distribution module.
  • the first program when the first program accesses the critical area, it stops distributing the interrupt to the first CPU core, so that the first program can keep accessing the critical area without interruption after applying for the lock until the completion of the lock. Execution of critical sections. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program finishes executing the critical section, the lock management device releases the lock so that other programs can try to acquire the lock, thus reducing the time for other programs to wait for acquisition, and reducing the consumption of CPU resources and memory resources.
  • the lock management module may first receive a lock application request sent by at least one CPU core in the first CPU to request to allocate locks for the programs in each CPU core, and then the lock management module may request a lock from the program in each CPU core.
  • the first CPU core is determined from at least one CPU core, and then a lock application request from the first CPU core is obtained.
  • the lock management module can determine the first CPU core from at least one CPU core according to a preset rule.
  • the preset rules may include: the CPU core corresponding to the program with the highest priority in at least one CPU core; or, the CPU core that first sends the lock application request to the lock management module in the at least one CPU core ; or, a CPU core corresponding to a group of programs with the highest group priority in at least one CPU core; or, a core corresponding to a program located at the head of the queue in a group of programs.
  • the preset rule may also be other, which is not specifically limited in this embodiment of the present application.
  • the lock management module can obtain the lock application request sent by at least one CPU core, it needs to perform arbitration to confirm which CPU core to assign the lock to, so as to ensure that the critical area is uniquely accessed and execute the critical area. operation.
  • the first program is used to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, and the critical section is used to indicate a program segment that accesses a shared resource.
  • the first program acquires the lock, it keeps accessing the critical section without interruption until the execution of the critical section is completed, and then the execution of the critical section is completed as soon as possible.
  • the lock management module may also release the lock corresponding to the first program, and send a second signal to the interrupt distribution module to instruct the interrupt distribution module to restore the lock to the first program.
  • a CPU core dispatches interrupts.
  • step of the lock management module releasing the lock for the first program and the step of the lock management module sending the second signal to the interrupt distribution module can be performed in parallel, that is, the lock management module can respond to the lock application request and release the lock for the first program. At the same time, the second signal is sent to the interrupt distribution module.
  • the above two steps can also be performed in series, that is, the lock management module first releases the lock for the first program, and then sends the second signal to the interrupt distribution module.
  • the lock management module releases the lock corresponding to the first program, so that other programs can try to acquire the lock. In this way, the time for other programs to wait for acquisition is reduced, and the CPU resources are reduced. and the consumption of memory resources; further, since the interrupt distribution to the first CPU core is resumed when the first program completes the operation of executing the critical section, the performance of the CPU is guaranteed.
  • the lock management module after the lock management module allocates the lock to the first program, if the first program completes the access to the critical section, it can send a lock release request to the lock management module. In this way, the lock management module will The lock release request from the first CPU core can be obtained, and then, the lock management module responds to the lock release request and releases the lock corresponding to the first program; the lock management module can also respond to the lock release request and send a second signal to the interrupt distribution module , to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core.
  • the lock management module instructs the interrupt distribution module to resume the distribution of the interrupt to the first CPU core, so that other programs can try to acquire the lock as soon as possible, This reduces the time for other programs to wait for acquisition, and reduces the consumption of CPU resources and memory resources.
  • the lock management module when it allocates the lock to the first program, it can also start a timer module, that is, the lock management module can set a period of time by itself for the first program to access the critical area , when the timer times out, regardless of whether the first program completes the access to the critical section, the lock management module will send a second signal to the interrupt distribution module to indicate that the interrupt distribution to the first CPU core will be resumed.
  • a timer module that is, the lock management module can set a period of time by itself for the first program to access the critical area , when the timer times out, regardless of whether the first program completes the access to the critical section, the lock management module will send a second signal to the interrupt distribution module to indicate that the interrupt distribution to the first CPU core will be resumed.
  • the lock management module sends the second signal to the interrupt distribution module, if the first program has not completed the access to the critical area, the first program can also continue to access the critical area; if the first program has completed the access to the critical area; access to the area, the first program may send a lock release request to the lock management module to request to release the lock.
  • the lock management module sets a period of time by itself. During this period of time, the first program accesses the critical area. At the end of this period of time, regardless of whether the first program completes the access to the critical area, the managed module will The request resumes distributing the interrupt to the first CPU core to guarantee the performance of the CPU.
  • the first program is a user mode program.
  • the first CPU includes at least one CPU core, a lock management module, and an interrupt distribution module, at least one CPU core, a lock management module, and an interrupt distribution module communicate, and the at least one CPU core includes the first CPU core .
  • the lock management module can be implemented by a hardware module.
  • the present application provides a lock management device.
  • the lock management device may be a chip or a system-on-chip in a processor, or may be a processor for implementing the first aspect or any possible implementation of the first aspect.
  • the functional modules of the method described in the manner may be a chip or a system-on-chip in a processor, or may be a processor for implementing the first aspect or any possible implementation of the first aspect.
  • the lock management device includes: a first communication unit, an allocation unit, and a second communication unit; wherein, the first communication unit is used to obtain a lock application request from the first CPU core, and the lock application request is used to request The first program in the first CPU core distributes the lock; the distribution unit is used to respond to the lock application request and distribute the lock to the first program; the second communication unit is used to send the first signal to the interrupt distribution device, and the first signal is used for The interrupt distribution module is instructed to stop distributing interrupts to the first CPU core.
  • the lock management device stops distributing interrupts to the first CPU core, so that the first program can maintain access to the critical area without being interrupted by the interrupt after applying for the lock. until the execution of the critical section is completed. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program finishes executing the critical section, the lock management device releases the lock so that other programs can try to acquire the lock, thus reducing the time for other programs to wait for acquisition, and reducing the consumption of CPU resources and memory resources. .
  • the above-mentioned apparatus further includes: a determining unit; then, the first communication unit is further configured to obtain a lock application request from at least one CPU core before obtaining a lock application request from the first CPU core , a lock application request from at least one CPU core is used for requesting to allocate locks for programs in at least one CPU core, and at least one CPU core includes a first CPU core; a determination unit is used to determine the first CPU core from at least one CPU core .
  • the above determining unit is specifically configured to determine the first CPU core from at least one CPU core according to a preset rule.
  • the first program is used to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, and the critical section is used to indicate a program segment that accesses a shared resource.
  • the above-mentioned device further includes: a release unit, configured to release the lock corresponding to the first program after the distribution unit allocates the lock to the first program; the second communication unit is further configured to send the interrupt to the The distribution device sends a second signal, where the second signal is used to instruct the interrupt distribution device to resume distribution of the interrupt to the first CPU core.
  • the first communication unit is further configured to obtain a lock release request from the first CPU core before the release unit releases the lock corresponding to the first program, and the lock release request is used to request to release the first program The lock corresponding to the program; the second communication unit is used for sending a second signal to the interrupt distribution device in response to the lock release request.
  • the above-mentioned apparatus further includes: a timing unit for triggering the timer module when the allocation unit allocates the lock to the first program; and a second communication unit for triggering the timer module when the timer module times out. A second signal is sent to the interrupt distribution device.
  • the first program is a user mode program.
  • the present application provides a processor, comprising: at least one CPU core, a bus, a lock management device, and an interrupt distribution device, wherein at least one CPU core, a lock management device, and an interrupt distribution device communicate through a bus, and at least one CPU
  • the core includes a first CPU core; and a lock management apparatus configured to execute the lock management method described in the first party or any possible implementation manner of the first aspect.
  • the interrupt distribution device is configured to receive the first signal sent by the lock management device; in response to the first signal, stop distribution of the interrupt to the first CPU core.
  • the interrupt distribution device is further configured to receive a second signal sent by the lock management device; in response to the second signal, resume distribution of the interrupt to the first CPU core.
  • the present application provides an electronic device, including: a processor, where the processor is configured to execute the lock management method described in the first party or any possible implementation manner of the first aspect.
  • the electronic device may be a computing device, a storage device, a network device, or the like.
  • the present application provides a lock management device, comprising: a processor, a memory, a first interface and a second interface, the processor is respectively coupled to the memory, the first interface and the second interface, and the processor communicates with the first interface through the first interface.
  • At least one CPU core communicates with the interrupt distribution device through the second interface, and the processor is configured to read and execute the instructions in the memory, so as to implement the above-mentioned first party or any possible implementation manner of the first aspect. Lock management method.
  • the present application provides a computer-readable storage medium, where the computer-readable storage medium stores instructions that, when the instructions are executed on a computer, are used to execute the first aspect or any possible implementation of the first aspect.
  • the lock management method described in the method is not limited to:
  • the present application provides a computer program or computer program product that, when the computer program or computer program product is executed on a computer, enables the computer to implement the first aspect or any possible implementation manner of the first aspect.
  • FIG. 1 is a schematic diagram of a use access mode of a lock in an embodiment of the application
  • FIG. 2 is a schematic time sequence diagram of an operation of a thread A executing a critical section in an embodiment of the present application
  • FIG. 3 is a schematic structural diagram of a processor in an embodiment of the present application.
  • FIG. 4 is a schematic structural diagram of a lock management device and an interrupt distribution device in an embodiment of the application
  • FIG. 5 is a schematic flowchart of a lock management method in an embodiment of the present application.
  • FIG. 6 is a schematic diagram of a logical structure of a hardware queue in an embodiment of the present application.
  • FIG. 7 is a schematic structural diagram of a lock management device in an embodiment of the application.
  • FIG. 8 is a schematic structural diagram of an electronic device in an embodiment of the present application.
  • FIG. 1 is a schematic diagram of an access method of a lock in an embodiment of the present application.
  • thread A after acquiring the lock, thread A will perform the operation of its own critical section, and release it after the execution of its critical section is completed. Then, other threads, such as B thread or C thread, can acquire the lock and execute their own critical section code after acquiring the lock.
  • B thread or C thread can acquire the lock and execute their own critical section code after acquiring the lock.
  • the critical section can only be executed by at most one thread at a certain time. In this way, the critical section is protected from being parallelized by locking.
  • a critical section refers to a program segment that accesses shared resources, and these shared resources cannot be accessed by multiple programs at the same time.
  • other programs must wait to ensure that these shared resources are used by mutual exclusion.
  • the locks that can be obtained by programs are used to control resource access, which can include spin locks, mutex locks, and improved mutex locks.
  • resource access can include spin locks, mutex locks, and improved mutex locks.
  • the above locks may also be other locks derived from spin locks and/or mutual exclusion locks, which are not specifically limited in this embodiment of the present application.
  • program mentioned in the embodiments of the present application may be understood as processes or threads running in the CPU core, which are not specifically limited in the embodiments of the present application.
  • Spin lock is a kind of lock used for fast synchronization between multiple threads. It is widely used when multiple threads need fast mutual exclusion access, especially when the critical section is short, the use of spin lock can effectively reduce the lock itself. The impact of overhead on application performance.
  • the above public variables are equivalent to flags.
  • the operation of reading and writing public variables is generally to set or check the value of the public variable; if the value of the variable is set, it means that the lock is occupied, and clearing the value of the variable means that the lock is free and can be used by used by other threads. If the thread tries to acquire the lock and fails, it will continue to try to acquire the lock in a loop until the lock is successfully acquired. That is to say, when a thread fails to acquire a spin lock, the thread will keep trying to acquire the spin lock next time instead of releasing CPU resources to do other work.
  • FIG. 2 is a schematic time sequence diagram of the operation of thread A executing a critical section in an embodiment of the present application. Referring to FIG.
  • programs can generally be divided into user-mode programs and kernel-mode programs.
  • User-mode programs refer to programs that run under the lowest permission of the CPU, while kernel-mode programs can be narrowly defined as A program that needs to run under the highest authority of the CPU can also generally refer to a program that runs under a higher authority than the user mode authority.
  • the instruction to access the memory variable is a low-privilege instruction, then the user-mode program (user-mode thread or user-mode process) can be directly executed, but operations such as prohibiting interruption and prohibiting preemption are all high-privilege operations.
  • the user mode program cannot be executed directly, but needs to be executed by the system call kernel mode program, but the system call is a very time-consuming operation, which usually takes hundreds of nanoseconds or even microseconds to complete. During the process, preemption by other programs will occur, thus affecting the execution time of the program to the critical section.
  • FIG. 3 is a schematic diagram of a hardware structure of a processor in an embodiment of the present application.
  • the processor 100 may include: at least one processor core (core) 101, a bus 102, a lock management device 103, The interrupt distribution device 104 , wherein the processor core 101 includes a CPU core, and each of the processor core 101 , the lock management device 103 , and the interrupt distribution device 104 communicates through the bus 102 .
  • the lock management apparatus 103 and the interrupt distribution apparatus 104 may also communicate directly.
  • the above-mentioned bus may be a ring bus, which is used as an interconnection bus between various cores in the CPU and between other hardware modules.
  • other hardware modules may include a lock management device, an interrupt distribution A device, a memory controller, a PCIe root complex, etc., are not specifically limited in the embodiments of the present application.
  • the above-mentioned lock management device is used to obtain a lock application request from the first CPU core; in response to the lock application request, a lock is allocated for the first program in the first CPU core, and a first signal is sent to the interrupt distribution device to indicate the interrupt distribution The device stops dispatching interrupts to the first CPU core.
  • the lock management device is further configured to release the lock corresponding to the first program, and send a second signal to the interrupt distribution device to instruct the interrupt distribution device to resume distributing the interrupt to the first CPU core.
  • the above-mentioned interrupt distribution device is configured to receive the first signal sent by the lock management device; in response to the first signal, stop distribution of interrupts to the first CPU core.
  • the interrupt distribution device is further configured to receive a second signal sent by the lock management device; and in response to the second signal, resume distribution of the interrupt to the first CPU core.
  • the lock management device allocates a lock to the first CPU core, and notifies the interrupt distribution device to stop distributing interrupts to the first CPU core;
  • the lock management device notifies the interrupt distribution device to resume distribution of the interrupt to the first CPU core.
  • the above lock management apparatus may be implemented based on a hardware queue, a ring, an identification bit stored in a register, or other logic.
  • the lock management device can be realized by using programmable devices, such as application specific integrated circuit (ASIC), register transfer level (RTL), field programmable gate array (FPGA), etc.
  • ASIC application specific integrated circuit
  • RTL register transfer level
  • FPGA field programmable gate array
  • the above-mentioned interrupt distribution device can be realized based on the structure of the interrupt controller of the CPU, and the interrupt distribution device can realize the above-mentioned functions by using a hardware structure, or can realize the above-mentioned functions by software, for example, use any one of the processor cores in the processor 100 to realize the above-mentioned functions. .
  • FIG. 4 is a schematic structural diagram of the lock management device and the interrupt distribution device in the embodiment of the present application.
  • the lock management device 103 may include: a lock management module 401, a first An interface module 402 and a second interface module 403 .
  • the first interface module is used to communicate with the CPU core
  • the second interface module is used to communicate with the interrupt distribution device.
  • each module in the lock management device may be implemented by hardware modules such as independent programmable devices and logic circuits.
  • the above-mentioned interrupt distribution device 104 may include: an interrupt distribution module 411, a third interface module 412 and a fourth interface module 413; a third interface module for communicating with the lock management device, a fourth interface module Used to communicate with at least one CPU core for interrupt dispatch.
  • the third interface module and the second interface module can communicate through a bus, or can communicate directly.
  • first interface module can be implemented by using, for example, a transceiver circuit, an optical transceiver, and the like.
  • the lock allocated by the lock management device to the first program in the first CPU may be a spin lock or an improved mutual exclusion lock. Locks derived from spinlocks and/or mutex locks, which are not specifically limited.
  • the above-mentioned first program may be a user-mode program, and the first program may also be understood as a user-mode thread or a user-mode process.
  • the interrupts that the interrupt distribution module stops or resumes distribution to the first CPU core are maskable interrupts, and the maskable interrupts are generated by peripheral devices with interrupt capability. That is to say, after the first program acquires the lock, it notifies the interrupt distribution apparatus to stop or resume the distribution of maskable interrupts for the first CPU core.
  • FIG. 5 is a schematic flowchart of a lock management method in an embodiment of the present application. Referring to the solid line in FIG. 6 , the method may include:
  • the first CPU core When the first program running in the first CPU core needs to access the critical area, the first CPU core sends a lock application request to the lock management module through the first interface module, so as to request the lock management module to allocate a lock to the first program, so that the first Programs can access critical sections and perform operations in critical sections.
  • other cores of the CPU can also send a lock application request to the lock management module through the first interface module, that is, the first interface module can be at least A CPU core accesses at the same time and receives a lock application request from at least one CPU core.
  • the lock management module allocates a lock to the first program in response to the lock application request.
  • the lock management module When only the first CPU core sends a lock application request to the lock management module, the lock management module responds to the lock application request and allocates the lock to the first program.
  • the lock management module obtains the After a lock request is made, arbitration is required to confirm which CPU core the lock is assigned to.
  • the lock management module arbitrates the decision to assign the lock to the first program.
  • the lock management module can only respond to the lock application request sent by the first CPU core and assign the lock to the first program. In this way, the first program can be unique Access the critical section and perform the operation of the critical section.
  • the lock management module sends a first signal to the interrupt distribution module
  • the first signal is used to instruct the interrupt distribution module to stop distributing interrupts to the first CPU core.
  • the lock management module communicates with the third interface module through the second interface module, sends a first signal to the interrupt distribution module, and notifies the interrupt distribution module to stop distributing interrupts to the first CPU core, so that the first signal is sent to the interrupt distribution module.
  • a program can maintain access to the critical section without being disturbed by interruptions, complete the operation on the critical section as soon as possible, reduce the time that other threads wait to acquire the lock, and reduce the consumption of CPU resources and memory resources.
  • the interrupt distribution module stops distributing interrupts to the first CPU core in response to the first signal
  • S502 and S503 may be executed sequentially during the execution process, for example, S502 is executed first, then S503 is executed, or, S503 is executed first, and then S502 is executed; of course, S502 and S503 can also be executed at the same time.
  • the execution sequence of S502 and S503 is not specifically limited in this embodiment of the present application.
  • the above lock Management methods can also include:
  • the lock management module sends a second signal to the interrupt distribution module
  • the second signal is used to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core.
  • S505 and S506 may be executed sequentially in the execution process, for example, S505 is executed first, then S506 is executed, or, S506 is executed first, and then S505 is executed; of course, S505 and S506 can also be executed at the same time.
  • the execution sequence of S505 and S506 is not specifically limited in this embodiment of the present application.
  • S507 The interrupt distribution module resumes distributing the interrupt to the first CPU core in response to the second signal.
  • the lock management module may send a second signal to the interrupt distribution module through the communication between the second interface module and the third interface module to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core.
  • the interrupt distribution module resumes distributing the interrupt to the first CPU in response to the second signal. Then, when the first CPU core receives the interrupt distributed by the interrupt distribution module, it can respond to the interrupt and process the interrupt.
  • S505 may include: the lock management module obtains the lock from the first CPU core. In response to the lock release request, the lock management module releases the lock corresponding to the first program in response to the lock release request.
  • the first CPU core may send a lock release request to the lock management module through the first interface module, so as to request the first CPU core to release the lock application process described in S501 to S504 as follows
  • the lock management module releases the lock in response to the lock release request sent by the first CPU core.
  • other programs can try to acquire the lock.
  • the first program immediately instructs the lock management module to release the lock after completing the operation in the critical section, so that other programs can acquire the lock as soon as possible, reduce the waiting time of other programs, and reduce the consumption of CPU resources and memory resources.
  • the lock management module sends a second signal to the interrupt distribution module in response to the lock release request to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core.
  • the lock management module may also instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core by itself.
  • the above lock management device 103 may further include: a timer module 404; here, the duration of the timer module 404 can be understood as the duration for which the first program is expected to perform the operation of the critical section. The specific value of can be set according to the performance of the CPU, the requirements of the operating system, etc., which is not specifically limited in this embodiment of the present application.
  • the timer module can be, but is not limited to, implemented by a timer circuit or a timer program, which is not specifically limited in this embodiment of the present application.
  • the lock management module allocates the lock to the first program through S502
  • the timer module is started, and the timer module starts timing.
  • the lock management module sends a second signal to the interrupt distribution module, and the interrupt distribution module responds to the second signal and resumes distributing the interrupt to the first CPU.
  • the first CPU core receives the interrupt distributed by the interrupt distribution module After that, you can respond to the interrupt and process the interrupt.
  • the lock management module may release the lock allocated to the first program before or after the timer module times out, which is not limited in this embodiment of the present application.
  • the lock management module may record which CPU core program is allocated the lock for, such as the first CPU core, in this way, the lock management module is executing
  • the interrupt distribution module may be instructed to stop distributing interrupts to the recorded CPU core, that is, the first CPU core.
  • the lock management module can record which CPU core program has released the lock, such as the first CPU core, so that when the lock management module executes S506, it can indicate The interrupt distribution module resumes distributing the interrupt to the recorded CPU core, that is, the first CPU core.
  • the first program in the user mode can keep accessing the critical area without interruption after applying for the lock until the execution of the critical area is completed. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program finishes executing the critical section, the lock management device releases the lock so that other programs can try to acquire the lock, thus reducing the time for other programs to wait for acquisition, and reducing the consumption of CPU resources and memory resources. Further, since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided.
  • the execution process of the above S501 to S507 will be described by taking the lock management apparatus as an example of hardware queue logic implementation.
  • FIG. 6 is a schematic diagram of the logical structure of a hardware queue in an embodiment of the present application.
  • the read-write interface of the hardware queue allows multiple CPU cores to access simultaneously, and the read-write interface arbitrates to determine the reading of the contents of the queue by multiple CPUs.
  • Write access sequence so as to realize serial writing when finally reading and writing data to the queue storage area (it is understandable that all reading processes are serial, all writing processes are serial, and there can be writing processes at the same time as reading).
  • the subsequent write operation of the CPU core will get a full feedback message and need to wait for rewriting; if there is no valid data in the queue storage area, it is in a full state. In the empty state, subsequent read operations of the CPU core will get an empty feedback message and need to wait for re-reading.
  • a valid data is stored in the queue.
  • at least one CPU core in the CPU accesses the read interface at the same time, sends a lock application request (here, the lock application request can be understood as a read signal), and the read interface arbitrates to determine which program in the CPU core to assign the lock to.
  • the read interface can sort the programs in the CPU core by priority, and decide to allocate a lock to the first program with the highest priority.
  • the read interface responds to the lock request request of the first CPU core, and the first program reads the queue The valid data in the header; alternatively, the read interface can sort the lock request requests sent by the CPU core in chronological order, and decide to allocate a lock to the first program that first sends a lock request request to the read interface. Accordingly, the read interface responds to the first program.
  • the lock application request of the CPU core; the first program reads the valid data at the head of the queue.
  • the read interface can also use other arbitration rules to decide which CPU core program to allocate locks to. For example, the read interface can also decide to allocate locks to a group of programs with the highest group priority, or to a group of programs that are at the head of the queue.
  • the first program assigns a lock, etc., which are not specifically limited in this embodiment of the present application. Then, while allocating the lock to the first program, the read interface may send a first signal to the interrupt distribution device to instruct the interrupt distribution device to stop distributing interrupts to the first CPU core.
  • the first program When the first program completes the execution of the critical section, it sends a lock release request to the write interface in the hardware queue (here, the lock release request can be understood as a write signal), and the write interface responds to the lock release request and writes valid data to the tail of the queue , complete the release of the lock.
  • the write interface may also send a second signal to the interrupt distribution apparatus to instruct the interrupt distribution apparatus to resume distributing the interrupt to the first CPU core.
  • the write interface writes valid data to the tail of the queue
  • the valid data will be automatically moved to the head of the queue so that other programs can read it later, and the relative order in the moved queue remains unchanged.
  • the read interface and the write interface can be physically separate or combined, and the function module in the read interface and the write interface that communicates with the CPU core can correspond to the first interface module, and the function module communicates with the interrupt distribution device. It may correspond to the above-mentioned second interface module, and the function module in the read interface that performs arbitration decision may correspond to the above-mentioned lock management module.
  • An identification bit is set in the register to indicate the state of the lock. If the identification position is 1, it means that the lock is not allocated and can be acquired. On the contrary, if the identification position is 0, it indicates that the lock has been allocated and cannot be acquired.
  • the flag bit is set to 1.
  • the register can sort the programs in the CPU core according to the priority, and decide to allocate the lock to the first program with the highest priority. Accordingly, the register responds to the lock application request of the first CPU core, sets the flag to 0, and informs the first program of the first CPU core.
  • a CPU core, or a register can sort the lock request requests sent by the CPU core in chronological order, and decide to allocate a lock to the first program that first sends a lock request request to the register. Accordingly, the read interface responds to the lock request of the first CPU core Apply for a request, set the flag to 0, and inform the first CPU core.
  • the register can also use other arbitration rules to decide which program in the CPU core to allocate the lock to. For example, the register can also decide to allocate the lock to the group of programs with the highest group priority, or to the first group of programs at the head of the queue.
  • a program allocates locks, etc., which are not specifically limited in this embodiment of the present application. Then, while allocating the lock to the first program, the register may send a first signal to the interrupt distribution apparatus to instruct the interrupt distribution apparatus to stop distributing interrupts to the first CPU core.
  • the first program When the first program completes the execution of the critical section, it sends a lock release request to the register, and the register responds to the lock release request and sets the flag to 1 to complete the lock release.
  • the write interface may also send a second signal to the interrupt distribution apparatus to instruct the interrupt distribution apparatus to resume distributing the interrupt to the first CPU core.
  • lock management device may also be in other implementation forms, which are not specifically limited in the embodiments of the present application.
  • the above-mentioned first program may also be a kernel-mode program, and the first program may also be understood as a kernel-mode thread or a kernel-mode process.
  • the kernel mode program can perform operations such as prohibiting interrupts, prohibiting preemption, etc., but the kernel mode program realizes prohibiting interrupts or prohibiting preemption by not responding to interrupts, which will cause the interrupts to be delayed and not be responded to, increasing the consumption of interrupt response.
  • the methods described in S501 to S505 can be executed, so that the interrupt distribution device can distribute interrupts with higher priority to other CPU cores after stopping distribution of interrupts to the first CPU core. CPU core to process, so that the interrupt can be responded to in time, improve real-time performance.
  • the lock management device may also allocate locks to multiple programs, and notify the interrupt distribution device to stop distributing corresponding interrupts. Specifically, the lock management device receives a lock application request sent by at least one CPU core, and allocates locks to multiple programs with high real-time requirements in one or more CPU cores, so as to ensure that these programs are executed as soon as possible and improve real-time performance.
  • an embodiment of the present application further provides a lock management device.
  • the lock management device may be a chip or a system-on-chip in a processor, or may be used in the processor to implement any of the possible implementations in the foregoing embodiments.
  • the functional modules of the method described in the embodiment For example, FIG. 7 is a schematic structural diagram 1 of a lock management apparatus in an embodiment of the present application. Referring to FIG.
  • the lock management apparatus 700 may include: a first communication unit 701 , an allocation unit 702 and a second communication unit 703 ; Wherein, the first communication unit 701 is used to obtain a lock application request from the first CPU core, and the lock application request is used to request to allocate a lock for the first program in the first CPU core; the allocation unit 702 is used to respond to the lock application request, assign a lock to the first program; the second communication unit 703 is configured to send a first signal to the interrupt distribution apparatus, where the first signal is used to instruct the interrupt distribution module to stop distributing interrupts to the first CPU core.
  • the lock management device stops distributing the interrupt to the first CPU core, so that the first program can not be interrupted by the interrupt after applying for the lock, and the access critical area is maintained. section until the execution of the critical section is complete. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program completes the execution of the critical section as soon as possible, the lock management module releases the lock, so that other programs can try to acquire the lock, reduce the time for other programs to wait for acquisition, and reduce the consumption of CPU resources and memory resources.
  • the apparatus 700 in the embodiment of the present application may be implemented by an application specific integrated circuit (ASIC), or a programmable logic device (programmable logic device, PLD), and the above-mentioned PLD may be a complex programmable logical device (complex programmable logical device, CPLD), Field Programmable Gate Array (FPGA), Generic Array Logic (GAL) or any combination thereof.
  • ASIC application specific integrated circuit
  • PLD programmable logic device
  • the above-mentioned PLD may be a complex programmable logical device (complex programmable logical device, CPLD), Field Programmable Gate Array (FPGA), Generic Array Logic (GAL) or any combination thereof.
  • CPLD complex programmable logical device
  • FPGA Field Programmable Gate Array
  • GAL Generic Array Logic
  • the above-mentioned apparatus further includes: a determining unit; then, the first communication unit is further configured to obtain a lock application request from at least one CPU core before obtaining a lock application request from the first CPU core,
  • the lock application request from at least one CPU core is used for requesting to allocate a lock to a program in at least one CPU core, and the at least one CPU core includes the first CPU core;
  • the determining unit is used for determining the first CPU core from the at least one CPU core.
  • the above determining unit is specifically configured to determine the first CPU core from at least one CPU core according to a preset rule.
  • the first program is used to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, and the critical section is used to indicate a program segment that accesses the shared resource.
  • the above-mentioned apparatus further includes: a release unit, configured to release the lock corresponding to the first program after the distribution unit allocates the lock to the first program; the second communication unit is further configured to send a message to the interrupt distribution apparatus.
  • the second signal is used to instruct the interrupt distribution apparatus to resume distribution of the interrupt to the first CPU core.
  • the first communication unit is further configured to obtain a lock release request from the first CPU core before the release unit releases the lock corresponding to the first program, and the lock release request is used to request to release the lock corresponding to the first program
  • the second communication unit is configured to send a second signal to the interrupt distribution device in response to the lock release request.
  • the above-mentioned apparatus further includes: a timing unit for triggering a timer module when the allocation unit allocates a lock to the first program; a second communication unit for triggering an interrupt when the timer module times out The distribution device transmits the second signal.
  • the first program is a user mode program.
  • the above-mentioned first communication unit may correspond to the first interface module in FIG. 4, and the second communication unit may correspond to the second interface module in FIG. 4; the allocation unit, the determination unit, the release unit and the timing unit may be one or more processor.
  • an embodiment of the present application further provides an electronic device, including: a processor, where the processor is configured to execute the lock management method described in any possible implementation manner of the foregoing embodiments.
  • the above electronic device may be a computing device, such as a server; the electronic device may also be a storage device, such as a storage array, etc.; the electronic device may also be a network device, such as a switch.
  • an embodiment of the present application further provides a lock management device.
  • the management device is the lock management device 8011 in FIG. 8 .
  • the lock management device 8011 may include: a processor 80111, a memory 80112, At least one communication interface (the first interface 80113 and the second interface 80114 are included in FIG. 3 as an example) and the bus 80115, and the processor 80111, the memory 80112, and the at least one communication interface communicate through the bus 80115.
  • the processor 80111 may be a general-purpose central processing unit (CPU), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits for controlling the execution of the lock management method provided by the embodiments of the present application.
  • the processor 80111 may One or more CPUs are included, such as CPU0 and CPU1.
  • At least one communication interface can be implemented using any transceiver-like device for communicating with other functional devices, devices or communication networks, such as Ethernet, radio access network (RAN), wireless local area network (wireless local area network) area networks, WLAN), etc.
  • the first interface 80113 is used to communicate with at least one processor core (eg 8013 ) in the processor 801 (eg CPU) of the electronic device, and the second interface is used to communicate with the interrupt controller in the electronic device.
  • Memory 802 may be volatile memory or nonvolatile memory, or may include both volatile and nonvolatile memory.
  • the non-volatile memory may be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically programmable Erase programmable read-only memory (electrically EPROM, EEPROM) or flash memory.
  • Volatile memory may be random access memory (RAM), which acts as an external cache.
  • RAM random access memory
  • DRAM dynamic random access memory
  • SDRAM synchronous dynamic random access memory
  • Double data rate synchronous dynamic random access memory double data date SDRAM, DDR SDRAM
  • enhanced synchronous dynamic random access memory enhanced SDRAM, ESDRAM
  • synchronous link dynamic random access memory direct rambus RAM, DR RAM
  • the memory 802 is used to store computer-executed instructions for executing the lock management method provided by the embodiments of the present application, wherein the memory 802 is used to store instructions for implementing three modular functions: a first communication instruction, a first processing instruction, and a third
  • the two communication instructions are controlled and executed by the processor 801 .
  • the processor 801 is configured to execute the computer-executed instructions stored in the memory 802, thereby implementing the lock management method provided in any possible implementation manner of the above one or more embodiments.
  • the memory 802 shown in FIG. 8 is only a schematic diagram, and the memory may further include other functionalized instructions, which are not limited in this embodiment of the present application.
  • the lock management device is used for the functions performed by the corresponding subjects in the method embodiments described above in FIG. 1 to FIG. 6 , and details are not described here for brevity.
  • the application also provides an electronic device, as shown in FIG. 8 , the electronic device includes a processor 801 , a memory 802 , a communication interface 803 and a bus 804 , and the processor 801 , the memory 802 , and the communication interface 803 communicate through the bus 804 .
  • the processor 801 includes a lock management device 8011 , an interrupt controller 8012 , a processor core 8013 and a bus 8014 .
  • the interrupt controller 8012 may correspond to the interrupt distribution apparatus described above in FIG. 1 to FIG. 6 , and is configured to implement the operation steps of the method executed by the above-mentioned interrupt distribution apparatus.
  • the electronic device 800 is used to implement the operation steps performed by the corresponding subjects in the methods described in FIG. 1 to FIG. 6 , which are not repeated here for brevity.
  • bus 804, the bus 8014 and the bus 80115 may also include various transmission media such as a power bus, a control bus, and a status signal bus for realizing the internal communication of the device or device.
  • various buses are labeled as bus 804, bus 8014, and bus 80115 in the figures.
  • the number of each part in FIG. 8 does not constitute a limitation to the present application.
  • the processor core 8013 may include multiple ones. For the sake of brevity, only one is used as an example for labeling in the present application.
  • the foregoing electronic device 800 is used for the functions performed by the corresponding subjects in the foregoing method embodiments in FIG. 1 to FIG. 6 , and for brevity, details are not described herein again.
  • an embodiment of the present application provides a computer-readable storage medium, where the computer-readable storage medium stores instructions, and when the instructions are run on a computer, is used to execute any possible implementation manner in the foregoing embodiments The described lock management method.
  • the embodiments of the present application provide a computer program or computer program product, when the computer program or computer program product is executed on a computer, the computer can realize the implementation as described in any possible implementation manner in the foregoing embodiments lock management method.
  • the above embodiments may be implemented in whole or in part by software, hardware, firmware or any other combination.
  • the above-described embodiments may be implemented in whole or in part in the form of a computer program product.
  • the computer program product includes one or more computer instructions. When the computer program instructions are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of the present application are generated.
  • the computer may be a general purpose computer, special purpose computer, computer network, or other programmable device.
  • the computer instructions may be stored in or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be downloaded from a website site, computer, server, or data center Transmission to another website site, computer, server, or data center is by wire (eg, coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (eg, infrared, wireless, microwave, etc.).
  • the computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server, a data center, or the like that contains one or more sets of available media.
  • the usable media may be magnetic media (eg, floppy disks, hard disks, magnetic tapes), optical media (eg, DVDs), or semiconductor media.
  • the semiconductor medium may be a solid state drive (SSD).

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Multi Processors (AREA)

Abstract

A lock management method, apparatus, and device. The lock management method can comprise: a lock management module obtains a lock application request from a first CPU core, the lock application request being used to request to allocate a lock to a first program in the first CPU core; the lock management module allocates a lock to the first program in response to the lock application request; and the lock management module sends a first signal to an interrupt distribution module, the first signal being used to instruct the interrupt distribution module to stop distributing an interrupt to the first CPU core. In the described method, interrupt distribution to the first CPU core is stopped when the first program accesses a critical area, so that after the first program successfully applies for a lock, the first program cannot be interrupted by the interrupt, and maintains access to the critical area until execution in the critical area is completed, thereby reducing the time spent waiting for acquisition by another program, and reducing the consumption of CPU resources and memory resources.

Description

一种锁管理方法、装置及设备A lock management method, device and equipment 技术领域technical field
本申请涉及计算机领域,特别涉及一种锁管理方法、装置及设备。The present application relates to the field of computers, and in particular, to a lock management method, device and device.
背景技术Background technique
多核中央处理单元(central processing unit,CPU)中并行运行多个线程,线程是程序执行中一个单一的顺序控制流程,是程序执行流的最小单元,是处理器调度和分派的基本单位。一个进程可以有一个或多个线程,各个线程之间共享程序的内存空间。为保证程序对某一资源的安全访问,维持数据的一致性,并行运行的程序一般需要使用锁机制进行同步,从而保证程序运行过程中共享资源数据的一致性。A multi-core central processing unit (CPU) runs multiple threads in parallel. A thread is a single sequential control flow in program execution, the smallest unit of program execution flow, and the basic unit of processor scheduling and dispatch. A process can have one or more threads, and each thread shares the program's memory space. In order to ensure the safe access of a program to a certain resource and maintain data consistency, programs running in parallel generally need to use a lock mechanism for synchronization, so as to ensure the consistency of shared resource data during program operation.
目前,一个线程在获取到锁之后,可以访问临界区,执行临界区的操作,在对临界区执行完毕之后,该线程释放锁,此时,其他线程就可以获得锁以执行自己的临界区。由于同一时间最多只能有一个线程获取到锁,所以临界区在某一个时刻最多只会有一个线程执行,如此,通过加锁的方式保证了临界区不会被并行。但是,在一个线程获取锁后,其他线程需要一直等待锁被释放,而在等待锁释放的过程中其他线程会一直尝试获取锁,占用CPU资源。可见,得到锁的线程在临界区执行的时间越长,其他线程的等待的时间也就越长,会造成长时间无谓消耗CPU资源。At present, after a thread acquires the lock, it can access the critical section and perform operations in the critical section. After the execution of the critical section is completed, the thread releases the lock. At this time, other threads can obtain the lock to execute their own critical section. Since at most one thread can acquire the lock at the same time, the critical section can only be executed by at most one thread at a certain time. In this way, the method of locking ensures that the critical section will not be parallelized. However, after a thread acquires the lock, other threads need to wait for the lock to be released, and other threads will keep trying to acquire the lock while waiting for the lock to be released, occupying CPU resources. It can be seen that the longer the thread that obtains the lock executes in the critical section, the longer the waiting time of other threads will be, which will cause unnecessary consumption of CPU resources for a long time.
发明内容SUMMARY OF THE INVENTION
本申请提供了一种锁管理方法、装置及设备,以提升性能,节约资源。The present application provides a lock management method, device and device to improve performance and save resources.
第一方面,本申请提供一种锁管理方法,该方法可以应用于锁管理模块,该锁管理模块可以设置于第一CPU中,并与第一CPU中的至少一个CPU核以及中断分发模块通信,至少一个CPU核包括第一CPU核。In the first aspect, the present application provides a lock management method, which can be applied to a lock management module, which can be arranged in a first CPU and communicate with at least one CPU core and an interrupt distribution module in the first CPU. , the at least one CPU core includes the first CPU core.
在本申请中,上述锁管理方法可以包括:锁管理模块获得来自第一CPU核的锁申请请求,以请求为第一CPU核中的第一程序分配锁;锁管理模块响应锁申请请求,为第一程序分配锁;锁管理模块向中断分发模块发送第一信号,以指示中断分发模块停止向第一CPU核分发中断。In the present application, the above lock management method may include: the lock management module obtains a lock application request from the first CPU core to request to allocate a lock for the first program in the first CPU core; the lock management module responds to the lock application request, for The first program distributes the lock; the lock management module sends a first signal to the interrupt distribution module to instruct the interrupt distribution module to stop distributing the interrupt to the first CPU core.
可以理解的,锁管理模块为第一程序分配锁的步骤与锁管理模块向中断分发模块发送第一信号的步骤可以并行执行,即锁管理模块可以响应锁申请请求,为第一程序分配锁的同时向中断分发模块发送第一信号,当然,上述两个步骤也可以串行执行,即锁管理模块先为第一程序分配锁,再向中断分发模块发送第一信号。It can be understood that the step of the lock management module assigning the lock to the first program and the step of the lock management module sending the first signal to the interrupt distribution module can be performed in parallel, that is, the lock management module can respond to the lock application request and assign the lock to the first program. At the same time, the first signal is sent to the interrupt distribution module. Of course, the above two steps can also be performed in series, that is, the lock management module first allocates the lock to the first program, and then sends the first signal to the interrupt distribution module.
在本申请中,通过在第一程序访问临界区的时候停止向第一CPU核分发中断,使得第一程序在申请到锁之后,能够不受中断的打断,保持访问临界区,直至完成对临界区的执行。由于在第一程序访问临界区的时候停止向第一CPU核分发中断,避免了中断结束后选择更高优先级线程进行执行而不是原程序执行的死锁问题。进一步地,在第一程序完成对临界区的执行后,锁管理装置释放锁,使得其他程序可以尝试获取锁,如此,减少其他程序等待获取的时间,降低对CPU资源以及内存资源的消耗。In this application, when the first program accesses the critical area, it stops distributing the interrupt to the first CPU core, so that the first program can keep accessing the critical area without interruption after applying for the lock until the completion of the lock. Execution of critical sections. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program finishes executing the critical section, the lock management device releases the lock so that other programs can try to acquire the lock, thus reducing the time for other programs to wait for acquisition, and reducing the consumption of CPU resources and memory resources.
在一种可能的实现方式中,锁管理模块可以先接收第一CPU中的至少一个CPU核发送 来的锁申请请求,以请求为各个CPU核中的程序分配锁,然后,锁管理模块再从至少一个CPU核中确定出第一CPU核,进而获得来自第一CPU核的锁申请请求。In a possible implementation manner, the lock management module may first receive a lock application request sent by at least one CPU core in the first CPU to request to allocate locks for the programs in each CPU core, and then the lock management module may request a lock from the program in each CPU core. The first CPU core is determined from at least one CPU core, and then a lock application request from the first CPU core is obtained.
可以理解的,锁管理模块可以根据预设规则从至少一个CPU核中确定出第一CPU核。可选的,预设规则可以包括:至少一个CPU核中优先级最高的程序对应的CPU核;或,至少一个CPU核中的最先向所述锁管理模块发送所述锁申请请求的CPU核;或,至少一个CPU核中组优先级最高的一组程序对应的CPU核;或,一组程序中位于队列头部的程序对应的核。当然,预设规则还可以为其他,本申请实施例不做具体限定。It can be understood that the lock management module can determine the first CPU core from at least one CPU core according to a preset rule. Optionally, the preset rules may include: the CPU core corresponding to the program with the highest priority in at least one CPU core; or, the CPU core that first sends the lock application request to the lock management module in the at least one CPU core ; or, a CPU core corresponding to a group of programs with the highest group priority in at least one CPU core; or, a core corresponding to a program located at the head of the queue in a group of programs. Of course, the preset rule may also be other, which is not specifically limited in this embodiment of the present application.
在本申请中,锁管理模块可以获得至少一个CPU核发送的锁申请请求后,那么,就需要进行仲裁,以确认将锁分配给哪一个CPU核,进而保证临界区被唯一访问,执行临界区的操作。In this application, after the lock management module can obtain the lock application request sent by at least one CPU core, it needs to perform arbitration to confirm which CPU core to assign the lock to, so as to ensure that the critical area is uniquely accessed and execute the critical area. operation.
在另一种可能的实现方式中,第一程序用于在获得锁后保持对临界区的访问,直至完成对临界区的操作,临界区用于指示一个访问共享资源的程序片段。如此,第一程序在获得锁后,一直访问临界区,能够不受中断的打断,直至完成对临界区的执行,进而尽快完成对临界区的执行。In another possible implementation manner, the first program is used to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, and the critical section is used to indicate a program segment that accesses a shared resource. In this way, after the first program acquires the lock, it keeps accessing the critical section without interruption until the execution of the critical section is completed, and then the execution of the critical section is completed as soon as possible.
在另一种可能的实现方式中,锁管理模块在为第一程序分配锁之后,还可以释放第一程序对应的锁,并向中断分发模块发送第二信号,以指示中断分发模块恢复向第一CPU核分发中断。In another possible implementation manner, after allocating the lock to the first program, the lock management module may also release the lock corresponding to the first program, and send a second signal to the interrupt distribution module to instruct the interrupt distribution module to restore the lock to the first program. A CPU core dispatches interrupts.
可以理解的,锁管理模块为第一程序释放锁的步骤与锁管理模块向中断分发模块发送第二信号的步骤可以并行执行,即锁管理模块可以响应锁申请请求,为第一程序释放锁的同时向中断分发模块发送第二信号,当然,上述两个步骤也可以串行执行,即锁管理模块先为第一程序释放锁,再向中断分发模块发送第二信号。It can be understood that the step of the lock management module releasing the lock for the first program and the step of the lock management module sending the second signal to the interrupt distribution module can be performed in parallel, that is, the lock management module can respond to the lock application request and release the lock for the first program. At the same time, the second signal is sent to the interrupt distribution module. Of course, the above two steps can also be performed in series, that is, the lock management module first releases the lock for the first program, and then sends the second signal to the interrupt distribution module.
在本申请中,在第一程序完成对临界区的执行后,锁管理模块释放第一程序对应的锁,使得其他程序可以尝试获取锁,如此,减少其他程序等待获取的时间,降低对CPU资源以及内存资源的消耗;进一步地,由于在第一程序完成执行临界区的操作的时候恢复向第一CPU核分发中断,保证CPU的性能。In the present application, after the first program completes the execution of the critical section, the lock management module releases the lock corresponding to the first program, so that other programs can try to acquire the lock. In this way, the time for other programs to wait for acquisition is reduced, and the CPU resources are reduced. and the consumption of memory resources; further, since the interrupt distribution to the first CPU core is resumed when the first program completes the operation of executing the critical section, the performance of the CPU is guaranteed.
在另一种可能的实现方式中,锁管理模块在为第一程序分配锁之后,如果第一程序完成对临界区的访问后,可以向锁管理模块发送锁释放请求,如此,锁管理模块就可以获得来自第一CPU核的锁释放请求,然后,锁管理模块响应该锁释放请求,释放第一程序对应的锁;锁管理模块还可以响应上述锁释放请求,向中断分发模块发送第二信号,以指示中断分发模块恢复向第一CPU核分发中断。In another possible implementation manner, after the lock management module allocates the lock to the first program, if the first program completes the access to the critical section, it can send a lock release request to the lock management module. In this way, the lock management module will The lock release request from the first CPU core can be obtained, and then, the lock management module responds to the lock release request and releases the lock corresponding to the first program; the lock management module can also respond to the lock release request and send a second signal to the interrupt distribution module , to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core.
可以理解的,第一程序在完成对临界区的访问后,可以主动请求释放锁,并由锁管理模块指示中断分发模块恢复对第一CPU核分发中断,以使得其他程序能够尽快尝试获取锁,进而减少其他程序等待获取的时间,降低对CPU资源以及内存资源的消耗。It can be understood that after the first program completes the access to the critical area, it can actively request to release the lock, and the lock management module instructs the interrupt distribution module to resume the distribution of the interrupt to the first CPU core, so that other programs can try to acquire the lock as soon as possible, This reduces the time for other programs to wait for acquisition, and reduces the consumption of CPU resources and memory resources.
在另一种可能的实现方式中,锁管理模块在为第一程序分配锁的时候,还可以启动一定时器模块,也就是锁管理模块自行设定一段时间,以供第一程序访问临界区,当定时器超时时,无论第一程序是否完成临界区的访问,锁管理模块均会向中断分发模块发送第二信号,以指示会恢复向第一CPU核分发中断。In another possible implementation manner, when the lock management module allocates the lock to the first program, it can also start a timer module, that is, the lock management module can set a period of time by itself for the first program to access the critical area , when the timer times out, regardless of whether the first program completes the access to the critical section, the lock management module will send a second signal to the interrupt distribution module to indicate that the interrupt distribution to the first CPU core will be resumed.
可以理解的,锁管理模块在向中断分发模块发送第二信号之后,如果第一程序未完成对临界区的访问,则第一程序还可以继续对临界区进行访问;如果第一程序完成对临界区的访问,则第一程序可以向锁管理模块发送锁释放请求,以请求释放锁。It can be understood that after the lock management module sends the second signal to the interrupt distribution module, if the first program has not completed the access to the critical area, the first program can also continue to access the critical area; if the first program has completed the access to the critical area; access to the area, the first program may send a lock release request to the lock management module to request to release the lock.
在本申请中,锁管理模块自行设定一段时间,在这段时间中,第一程序访问临界区,在 这一段时间结束时,无论第一程序是否完成临界区的访问,所管理模块均会请求恢复向第一CPU核分发中断,以保证CPU的性能。In the present application, the lock management module sets a period of time by itself. During this period of time, the first program accesses the critical area. At the end of this period of time, regardless of whether the first program completes the access to the critical area, the managed module will The request resumes distributing the interrupt to the first CPU core to guarantee the performance of the CPU.
在另一种可能的实现方式中,第一程序为用户态程序。In another possible implementation manner, the first program is a user mode program.
在另一种可能的实现方式中,第一CPU包括至少一个CPU核、锁管理模块以及中断分发模块,至少一个CPU核、锁管理模块以及中断分发模块通信,至少一个CPU核包括第一CPU核。可以理解的,锁管理模块可以采用硬件模块实现。In another possible implementation manner, the first CPU includes at least one CPU core, a lock management module, and an interrupt distribution module, at least one CPU core, a lock management module, and an interrupt distribution module communicate, and the at least one CPU core includes the first CPU core . It can be understood that the lock management module can be implemented by a hardware module.
第二方面,本申请提供一种锁管理装置,该锁管理装置可以为处理器中的芯片或者片上系统,还可以为处理器中用于实现第一方面或第一方面的任一可能的实施方式所述的方法的功能模块。举例来说,该锁管理装置包括:第一通信单元、分配单元以及第二通信单元;其中,第一通信单元,用于获得来自第一CPU核的锁申请请求,锁申请请求用于请求为第一CPU核中的第一程序分配锁;分配单元,用于响应锁申请请求,为第一程序分配锁;第二通信单元,用于向中断分发装置发送第一信号,第一信号用于指示中断分发模块停止向第一CPU核分发中断。In a second aspect, the present application provides a lock management device. The lock management device may be a chip or a system-on-chip in a processor, or may be a processor for implementing the first aspect or any possible implementation of the first aspect. The functional modules of the method described in the manner. For example, the lock management device includes: a first communication unit, an allocation unit, and a second communication unit; wherein, the first communication unit is used to obtain a lock application request from the first CPU core, and the lock application request is used to request The first program in the first CPU core distributes the lock; the distribution unit is used to respond to the lock application request and distribute the lock to the first program; the second communication unit is used to send the first signal to the interrupt distribution device, and the first signal is used for The interrupt distribution module is instructed to stop distributing interrupts to the first CPU core.
在本申请中,通过锁管理装置在第一程序访问临界区的时候停止向第一CPU核分发中断,使得第一程序在申请到锁之后,能够不受中断的打断,保持访问临界区,直至完成对临界区的执行。由于在第一程序访问临界区的时候停止向第一CPU核分发中断,避免了中断结束后选择更高优先级线程进行执行而不是原程序执行的死锁问题。进一步地,在第一程序完成对临界区的执行后,锁管理装置释放锁,使得其他程序可以尝试获取锁,如此,减少其他程序等待获取的时间,降低对CPU资源以及内存资源的消耗。。In the present application, when the first program accesses the critical area, the lock management device stops distributing interrupts to the first CPU core, so that the first program can maintain access to the critical area without being interrupted by the interrupt after applying for the lock. until the execution of the critical section is completed. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program finishes executing the critical section, the lock management device releases the lock so that other programs can try to acquire the lock, thus reducing the time for other programs to wait for acquisition, and reducing the consumption of CPU resources and memory resources. .
在一种可能的实现方式中,上述装置还包括:确定单元;那么,第一通信单元,还用于在获得来自第一CPU核的锁申请请求之前,获得来自至少一个CPU核的锁申请请求,来自至少一个CPU核的锁申请请求用于请求为至少一个CPU核中的程序分配锁,至少一个CPU核包括第一CPU核;确定单元,用于从至少一个CPU核中确定第一CPU核。In a possible implementation manner, the above-mentioned apparatus further includes: a determining unit; then, the first communication unit is further configured to obtain a lock application request from at least one CPU core before obtaining a lock application request from the first CPU core , a lock application request from at least one CPU core is used for requesting to allocate locks for programs in at least one CPU core, and at least one CPU core includes a first CPU core; a determination unit is used to determine the first CPU core from at least one CPU core .
在另一种可能的实现方式中,上述确定单元,具体用于根据预设规则从至少一个CPU核中确定第一CPU核。In another possible implementation manner, the above determining unit is specifically configured to determine the first CPU core from at least one CPU core according to a preset rule.
在另一种可能的实现方式中,第一程序用于在获得锁后保持对临界区的访问,直至完成对临界区的操作,临界区用于指示一个访问共享资源的程序片段。In another possible implementation manner, the first program is used to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, and the critical section is used to indicate a program segment that accesses a shared resource.
在另一种可能的实现方式中中,上述装置还包括:释放单元,用于在分配单元为第一程序分配锁之后,释放第一程序对应的锁;第二通信单元,还用于向中断分发装置发送第二信号,第二信号用于指示中断分发装置恢复向第一CPU核分发中断。In another possible implementation manner, the above-mentioned device further includes: a release unit, configured to release the lock corresponding to the first program after the distribution unit allocates the lock to the first program; the second communication unit is further configured to send the interrupt to the The distribution device sends a second signal, where the second signal is used to instruct the interrupt distribution device to resume distribution of the interrupt to the first CPU core.
在另一种可能的实现方式中,第一通信单元,还用于在释放单元释放第一程序对应的锁之前,获得来自第一CPU核的锁释放请求,锁释放请求用于请求释放第一程序对应的锁;第二通信单元,用于响应锁释放请求,向中断分发装置发送第二信号。In another possible implementation manner, the first communication unit is further configured to obtain a lock release request from the first CPU core before the release unit releases the lock corresponding to the first program, and the lock release request is used to request to release the first program The lock corresponding to the program; the second communication unit is used for sending a second signal to the interrupt distribution device in response to the lock release request.
在另一种可能的实现方式中,上述装置还包括:定时单元,用于在分配单元为第一程序分配锁时,触发定时器模块;第二通信单元,用于当定时器模块超时时,向中断分发装置发送第二信号。In another possible implementation manner, the above-mentioned apparatus further includes: a timing unit for triggering the timer module when the allocation unit allocates the lock to the first program; and a second communication unit for triggering the timer module when the timer module times out. A second signal is sent to the interrupt distribution device.
在另一种可能的实现方式中,第一程序为用户态程序。In another possible implementation manner, the first program is a user mode program.
第三方面,本申请提供一种处理器,包括:至少一个CPU核、总线、锁管理装置以及中断分发装置,其中,至少一个CPU核、锁管理装置以及中断分发装置通过总线通信,至少一个CPU核包括第一CPU核;锁管理装置,用于执行上述第一方或者第一方面的任一可能的实施方式所述的锁管理方法。In a third aspect, the present application provides a processor, comprising: at least one CPU core, a bus, a lock management device, and an interrupt distribution device, wherein at least one CPU core, a lock management device, and an interrupt distribution device communicate through a bus, and at least one CPU The core includes a first CPU core; and a lock management apparatus configured to execute the lock management method described in the first party or any possible implementation manner of the first aspect.
在一种可能的实现方式中,中断分发装置,用于接收锁管理装置发送的第一信号;响应第一信号,停止向第一CPU核分发中断。In a possible implementation manner, the interrupt distribution device is configured to receive the first signal sent by the lock management device; in response to the first signal, stop distribution of the interrupt to the first CPU core.
在另一种可能的实现方式中,中断分发装置,还用于接收锁管理装置发送的第二信号;响应第二信号,恢复向第一CPU核分发中断。In another possible implementation manner, the interrupt distribution device is further configured to receive a second signal sent by the lock management device; in response to the second signal, resume distribution of the interrupt to the first CPU core.
第四方面,本申请提供一种电子设备,包括:处理器,处理器用于用于执行上述第一方或者第一方面的任一可能的实施方式所述的锁管理方法。In a fourth aspect, the present application provides an electronic device, including: a processor, where the processor is configured to execute the lock management method described in the first party or any possible implementation manner of the first aspect.
在本申请中,该电子设备可以为计算设备、存储设备、网络设备等。In this application, the electronic device may be a computing device, a storage device, a network device, or the like.
第五方面,本申请提供一种锁管理装置,包括:处理器、存储器、第一接口以及第二接口,处理器分别与存储器、第一接口以及第二接口耦合,处理器通过第一接口与至少一个CPU核通信,通过第二接口与中断分发装置通信,处理器被配置为读取并执行存储器中的指令,以实现上述第一方或者第一方面的任一可能的实施方式所述的锁管理方法。In a fifth aspect, the present application provides a lock management device, comprising: a processor, a memory, a first interface and a second interface, the processor is respectively coupled to the memory, the first interface and the second interface, and the processor communicates with the first interface through the first interface. At least one CPU core communicates with the interrupt distribution device through the second interface, and the processor is configured to read and execute the instructions in the memory, so as to implement the above-mentioned first party or any possible implementation manner of the first aspect. Lock management method.
第六方面,本申请提供一种计算机可读存储介质,计算机可读存储介质存储有指令,当指令在计算机上运行时,用于执行如上述第一方面或者第一方面的任一可能的实施方式所述的锁管理方法。In a sixth aspect, the present application provides a computer-readable storage medium, where the computer-readable storage medium stores instructions that, when the instructions are executed on a computer, are used to execute the first aspect or any possible implementation of the first aspect. The lock management method described in the method.
第七方面,本申请提供一种计算机程序或计算机程序产品,当计算机程序或计算机程序产品在计算机上被执行时,使得计算机实现如上述第一方面或者第一方面的任一可能的实施方式所述的锁管理方法。In a seventh aspect, the present application provides a computer program or computer program product that, when the computer program or computer program product is executed on a computer, enables the computer to implement the first aspect or any possible implementation manner of the first aspect. The lock management method described above.
应当理解的是,本申请的第二至七方面与本申请的第一方面的技术方案一致,各方面及对应的可行实施方式所取得的有益效果相似,不再赘述。It should be understood that the second to seventh aspects of the present application are consistent with the technical solutions of the first aspect of the present application, and the beneficial effects obtained by each aspect and the corresponding feasible implementation manner are similar, and will not be repeated.
附图说明Description of drawings
为了更清楚地说明本申请实施例中的技术方案,下面将对本申请实施例中所需要使用的附图进行说明。In order to illustrate the technical solutions in the embodiments of the present application more clearly, the accompanying drawings required in the embodiments of the present application will be described below.
图1为本申请实施例中的一种锁的使用访问方式的示意图;FIG. 1 is a schematic diagram of a use access mode of a lock in an embodiment of the application;
图2为本申请实施例中的A线程执行临界区的操作的时序示意图;2 is a schematic time sequence diagram of an operation of a thread A executing a critical section in an embodiment of the present application;
图3为本申请实施例中的处理器的结构示意图;3 is a schematic structural diagram of a processor in an embodiment of the present application;
图4为本申请实施例中的锁管理装置和中断分发装置的结构示意图;4 is a schematic structural diagram of a lock management device and an interrupt distribution device in an embodiment of the application;
图5为本申请实施例中的锁管理方法的流程示意图;5 is a schematic flowchart of a lock management method in an embodiment of the present application;
图6为本申请实施例中的硬件队列的逻辑结构示意图;6 is a schematic diagram of a logical structure of a hardware queue in an embodiment of the present application;
图7为本申请实施例中的锁管理装置的结构示意图;7 is a schematic structural diagram of a lock management device in an embodiment of the application;
图8为本申请实施例中的电子设备的结构示意。FIG. 8 is a schematic structural diagram of an electronic device in an embodiment of the present application.
具体实施方式detailed description
下面结合本申请实施例中的附图对本申请实施例进行描述。The embodiments of the present application will be described below with reference to the accompanying drawings in the embodiments of the present application.
图1为本申请实施例中的一种锁的使用访问方式的示意图,参见图1所示,A线程获取到锁之后,会执行自身临界区的操作,并在其临界区的执行完毕之后释放锁,然后,其他线程,如B线程或者C线程就可以获取锁,并在获取到锁之后执行自身的临界区代码。但是,由于最多只能有一个线程获取到锁,所以临界区在某一个时刻最多只会有一个线程执行,如此,通过加锁的方式保护了临界区不会被并行。FIG. 1 is a schematic diagram of an access method of a lock in an embodiment of the present application. Referring to FIG. 1 , after acquiring the lock, thread A will perform the operation of its own critical section, and release it after the execution of its critical section is completed. Then, other threads, such as B thread or C thread, can acquire the lock and execute their own critical section code after acquiring the lock. However, since only one thread can acquire the lock at most, the critical section can only be executed by at most one thread at a certain time. In this way, the critical section is protected from being parallelized by locking.
需要说明的是,在本申请实施例中,临界区指的是一个访问共用资源的程序片段,这些 共用资源又无法同时被多个程序访问。当有程序执行临界区的操作时,其他程序必须等待,以确保这些共用资源是被互斥获得使用的。It should be noted that, in this embodiment of the present application, a critical section refers to a program segment that accesses shared resources, and these shared resources cannot be accessed by multiple programs at the same time. When a program executes operations in a critical section, other programs must wait to ensure that these shared resources are used by mutual exclusion.
在多程序软件开发和运行中,程序能够获得的锁是用于控制资源访问的方式,可以包括自旋锁(spin lock),互斥锁(mutex lock)、改进型的互斥锁等。当然,上述锁还可以为其他基于自旋锁和/或互斥锁衍生的锁,本申请实施例不做具体限定。In the development and operation of multi-program software, the locks that can be obtained by programs are used to control resource access, which can include spin locks, mutex locks, and improved mutex locks. Of course, the above locks may also be other locks derived from spin locks and/or mutual exclusion locks, which are not specifically limited in this embodiment of the present application.
需要说明的是,在本申请实施例中所说的“程序”可以理解为在CPU核中运行的进程或者线程,本申请实施例不做具体限定。It should be noted that the "program" mentioned in the embodiments of the present application may be understood as processes or threads running in the CPU core, which are not specifically limited in the embodiments of the present application.
下面以线程申请或释放自旋锁为例来对上述锁的工作机制进行说明。The working mechanism of the above lock is described below by taking the thread applying for or releasing the spin lock as an example.
自旋锁是一种用于多线程间快速同步的一种锁,在多个线程需要快速互斥访问时被大量的应用,特别是临界区较短时,使用自旋锁可以有效降低锁自身开销对应用性能的影响。Spin lock is a kind of lock used for fast synchronization between multiple threads. It is widely used when multiple threads need fast mutual exclusion access, especially when the critical section is short, the use of spin lock can effectively reduce the lock itself. The impact of overhead on application performance.
例如,一种典型的自旋锁获取函数的逻辑可以如下所示:For example, the logic of a typical spinlock acquisition function can look like this:
spin_lock_get{}{spin_lock_get{}{
   while(读写公共变量失败);while (failure to read and write public variables);
}}
其中,上述公共变量相当于标识位(flag),读写公共变量的操作一般是对公共变量设置或者检查变量的值;如果设置变量值就表示锁已经占用,清除变量值表示锁空闲,可以被其他线程使用。如果线程尝试获取锁失败就一直循环继续尝试获取锁,直到成功获取锁。也就是说,当线程尝试获取自旋锁失败时,该线程会不停的继续尝试下一次获取自旋锁,而不是释放CPU资源做其他工作。这样做的优势是在其他线程释放自旋锁后,由于当前线程一直处于执行状态,那么,当前线程就可以迅速的获取到被释放的锁,而不需要经过较复杂的线程调度等流程;与此同时,这么做的劣势也很明显,就是在当前线程等待获取锁的过程中,CPU资源会一直被占用,不能转而执行其他线程的任务,对CPU资源的存在无谓的浪费,特别是在等待时间较长的条件下会有较大的浪费。Among them, the above public variables are equivalent to flags. The operation of reading and writing public variables is generally to set or check the value of the public variable; if the value of the variable is set, it means that the lock is occupied, and clearing the value of the variable means that the lock is free and can be used by used by other threads. If the thread tries to acquire the lock and fails, it will continue to try to acquire the lock in a loop until the lock is successfully acquired. That is to say, when a thread fails to acquire a spin lock, the thread will keep trying to acquire the spin lock next time instead of releasing CPU resources to do other work. The advantage of this is that after other threads release the spin lock, since the current thread is always in the execution state, the current thread can quickly acquire the released lock without going through more complex thread scheduling and other processes; and At the same time, the disadvantage of doing this is also obvious, that is, in the process of the current thread waiting to acquire the lock, the CPU resources will be occupied all the time, and the tasks of other threads cannot be executed, which is an unnecessary waste of CPU resources, especially in the Under the condition of long waiting time, there will be a greater waste.
进一步地,由于程序获取自旋锁的操作是采用读写内存公共变量的方式来实现的,所以在程序等待获取自旋锁的时候,不仅会占用CPU资源,还会占用不必要的内存带宽资源。为了降低等待的线程对CPU资源以及内存资源的消耗,持有自旋锁的线程需要尽快完成临界区的操作进而释放自旋锁。但是,一个线程对临界区的执行时间,除了取决于临界区的逻辑复杂度,还会受到其他代码的抢占运行的影响。例如,图2为本申请实施例中的A线程执行临界区的操作的时序示意图,参见图2所示,A线程在临界区的执行过程中,CPU资源被优先级更高的程序抢占,CPU核优先响应中断,那么,A线程执行临界区就必须要等到中断处理执行完成之后才能继续,A线程释放锁的时间也会相应延后,其他线程,如B线程、C线程的等待时间也会延长,造成对CPU资源的存在无谓的浪费,特别是在等待时间较长的条件下会有较大的浪费。Further, since the operation of the program to acquire the spin lock is realized by reading and writing memory public variables, when the program waits to acquire the spin lock, it will not only occupy CPU resources, but also occupy unnecessary memory bandwidth resources. . In order to reduce the consumption of CPU resources and memory resources by the waiting thread, the thread holding the spin lock needs to complete the operation of the critical section as soon as possible to release the spin lock. However, the execution time of a thread's critical section depends not only on the logical complexity of the critical section, but also affected by the preemptive execution of other codes. For example, FIG. 2 is a schematic time sequence diagram of the operation of thread A executing a critical section in an embodiment of the present application. Referring to FIG. 2 , during the execution of thread A in the critical section, CPU resources are preempted by a program with a higher priority, and the CPU If the core responds to the interrupt first, then the execution of the critical section of thread A must wait until the execution of the interrupt processing is completed before continuing. The time for thread A to release the lock will be delayed accordingly, and the waiting time of other threads, such as thread B and thread C Prolonged, resulting in unnecessary waste of the existence of CPU resources, especially under the condition of a long waiting time, there will be a greater waste.
另外,根据期望程序运行时所处的不同权限状态,一般可以把程序分为用户态程序和内核态程序,用户态程序是指处于CPU最低权限下运行的程序,而内核态程序可以狭义地指需要在CPU最高权限下运行的程序,也可以泛指在比用户态权限更高一些的权限下运行的程序。结合本申请实施例来说,访问内存变量的指令是一个低权限指令,那么,用户态程序(用户态线程或用户态进程)可以直接执行,但是禁止中断、禁止抢占等操作都是高权限操作,在用户态程序不能直接执行,而是需要系统调用内核态程序执行的,但是系统调用是一个非常费时的操作,通常需要几百纳秒乃至微秒时间来完成,那么,可能在系统调用的过程中,就会发生其他程序的抢占,从而影响程序对临界区的执行时间。In addition, according to the different permission states in which the program is expected to run, programs can generally be divided into user-mode programs and kernel-mode programs. User-mode programs refer to programs that run under the lowest permission of the CPU, while kernel-mode programs can be narrowly defined as A program that needs to run under the highest authority of the CPU can also generally refer to a program that runs under a higher authority than the user mode authority. With reference to the embodiment of the present application, the instruction to access the memory variable is a low-privilege instruction, then the user-mode program (user-mode thread or user-mode process) can be directly executed, but operations such as prohibiting interruption and prohibiting preemption are all high-privilege operations. , the user mode program cannot be executed directly, but needs to be executed by the system call kernel mode program, but the system call is a very time-consuming operation, which usually takes hundreds of nanoseconds or even microseconds to complete. During the process, preemption by other programs will occur, thus affecting the execution time of the program to the critical section.
进一步地,如果程序在执行临界区的操作时,被中断打断,那么,还可能会存在中断结束后CPU核选择更高优先级程序进行执行而不是原程序进行执行,导致死锁的问题。Further, if the program is interrupted by an interrupt when the operation of the critical section is executed, there may also be a problem that the CPU core selects a higher priority program to execute instead of the original program after the interrupt ends, resulting in a deadlock problem.
那么,为了解决上述问题,本申请实施例提供一种处理器,如CPU,该处理器可以设置于如计算设备、存储设备、网络设备等设备中。图3为本申请实施例中的处理器的硬件结构示意图,参见图3中实线所示,该处理器100可以包括:至少一个处理器核(core)101、总线102、锁管理装置103、中断分发装置104,其中,处理器核101包括CPU核,每一个处理器核101、锁管理装置103、中断分发装置104通过总线102通信。Then, in order to solve the above problem, an embodiment of the present application provides a processor, such as a CPU, and the processor can be set in a device such as a computing device, a storage device, and a network device. FIG. 3 is a schematic diagram of a hardware structure of a processor in an embodiment of the present application. Referring to the solid line in FIG. 3 , the processor 100 may include: at least one processor core (core) 101, a bus 102, a lock management device 103, The interrupt distribution device 104 , wherein the processor core 101 includes a CPU core, and each of the processor core 101 , the lock management device 103 , and the interrupt distribution device 104 communicates through the bus 102 .
在一些可能的实现方式中,参见图3中虚线所示,锁管理装置103与中断分发装置104之间还可以直接通信。In some possible implementations, referring to the dotted line in FIG. 3 , the lock management apparatus 103 and the interrupt distribution apparatus 104 may also communicate directly.
在本申请实施例中,上述总线可以为环状总线(ring bus),作为CPU内部各个核之间以及与其他硬件模块之间的互联总线,这里,其他硬件模块可以包括锁管理装置、中断分发装置、内存控制器、PCIe根复合器等,本申请实施例不做具体限定。In the embodiment of the present application, the above-mentioned bus may be a ring bus, which is used as an interconnection bus between various cores in the CPU and between other hardware modules. Here, other hardware modules may include a lock management device, an interrupt distribution A device, a memory controller, a PCIe root complex, etc., are not specifically limited in the embodiments of the present application.
上述锁管理装置,用于获得来自第一CPU核的锁申请请求;响应锁申请请求,为第一CPU核中的第一程序分配锁,并向中断分发装置发送第一信号,以指示中断分发装置停止向第一CPU核分发中断。The above-mentioned lock management device is used to obtain a lock application request from the first CPU core; in response to the lock application request, a lock is allocated for the first program in the first CPU core, and a first signal is sent to the interrupt distribution device to indicate the interrupt distribution The device stops dispatching interrupts to the first CPU core.
在本申请其他实施例中,锁管理装置,还用于释放第一程序对应的锁,并向中断分发装置发送第二信号,以指示中断分发装置恢复向第一CPU核分发中断。In other embodiments of the present application, the lock management device is further configured to release the lock corresponding to the first program, and send a second signal to the interrupt distribution device to instruct the interrupt distribution device to resume distributing the interrupt to the first CPU core.
上述中断分发装置,用于接收锁管理装置发送的第一信号;响应第一信号,停止向第一CPU核分发中断。The above-mentioned interrupt distribution device is configured to receive the first signal sent by the lock management device; in response to the first signal, stop distribution of interrupts to the first CPU core.
在本申请其他实施例中,中断分发装置,还用于接收锁管理装置发送的第二信号;响应第二信号,恢复向第一CPU核分发中断。In other embodiments of the present application, the interrupt distribution device is further configured to receive a second signal sent by the lock management device; and in response to the second signal, resume distribution of the interrupt to the first CPU core.
那么,从第一CPU核的角度来看,第一CPU核向锁管理装置申请锁,锁管理装置给第一CPU核分配锁,并通知中断分发装置停止对第一CPU核分发中断;在第一CPU核释放锁的时候,锁管理装置通知中断分发装置恢复对第一CPU核分发中断。Then, from the perspective of the first CPU core, the first CPU core applies for a lock to the lock management device, the lock management device allocates a lock to the first CPU core, and notifies the interrupt distribution device to stop distributing interrupts to the first CPU core; When a CPU core releases the lock, the lock management device notifies the interrupt distribution device to resume distribution of the interrupt to the first CPU core.
在实际应用中,上述锁管理装置可以基于硬件队列、环状链表(ring)、寄存器存储的标识位或其他逻辑实现。锁管理装置可以采用可编程器件,如专用集成电路(application specific integrated circuit,ASIC)、寄存器转换级电路(register transfer level,RTL)、现场可编程逻辑门阵列(field programmable gate array,FPGA)等实现,当然,依赖于CPU的类型,还可以采用其他可编程器件实现,或者,锁管理装置还可以采用逻辑电路实现,本申请实施例不做具体限定。上述中断分发装置可以基于CPU的中断控制器的结构来实现,中断分发装置可以利用硬件结构实现上述功能,也可以由软件实现上述功能,例如,利用处理器100中任意一个处理器核实现上述功能。In practical applications, the above lock management apparatus may be implemented based on a hardware queue, a ring, an identification bit stored in a register, or other logic. The lock management device can be realized by using programmable devices, such as application specific integrated circuit (ASIC), register transfer level (RTL), field programmable gate array (FPGA), etc. Of course, depending on the type of the CPU, other programmable devices may also be used for implementation, or the lock management apparatus may also be implemented with a logic circuit, which is not specifically limited in this embodiment of the present application. The above-mentioned interrupt distribution device can be realized based on the structure of the interrupt controller of the CPU, and the interrupt distribution device can realize the above-mentioned functions by using a hardware structure, or can realize the above-mentioned functions by software, for example, use any one of the processor cores in the processor 100 to realize the above-mentioned functions. .
在本申请实施例中,图4为本申请实施例中的锁管理装置和中断分发装置的结构示意图,参见图4中实线所示,上述锁管理装置103可以包括:锁管理模块401、第一接口模块402以及第二接口模块403。其中,第一接口模块用于与CPU核进行通信,第二接口模块用于与中断分发装置进行通信。在实际应用中,锁管理装置中的各个模块可以由独立的可编程器件、逻辑电路等硬件模块实现。In the embodiment of the present application, FIG. 4 is a schematic structural diagram of the lock management device and the interrupt distribution device in the embodiment of the present application. Referring to the solid line in FIG. 4 , the lock management device 103 may include: a lock management module 401, a first An interface module 402 and a second interface module 403 . Wherein, the first interface module is used to communicate with the CPU core, and the second interface module is used to communicate with the interrupt distribution device. In practical applications, each module in the lock management device may be implemented by hardware modules such as independent programmable devices and logic circuits.
仍参见图4所示,上述中断分发装置104可以包括:中断分发模块411、第三接口模块412以及第四接口模块413;第三接口模块,用于与锁管理装置进行通信,第四接口模块用于与至少一个CPU核通信,以进行中断分发。其中,第三接口模块与第二接口模块可以通过总线通信,也可以直接通信。Still referring to FIG. 4 , the above-mentioned interrupt distribution device 104 may include: an interrupt distribution module 411, a third interface module 412 and a fourth interface module 413; a third interface module for communicating with the lock management device, a fourth interface module Used to communicate with at least one CPU core for interrupt dispatch. Wherein, the third interface module and the second interface module can communicate through a bus, or can communicate directly.
上述第一接口模块、第二接口模块、第三接口模块和/或第四接口模块可以采用如收发电路、光收发器等来实现。The above-mentioned first interface module, second interface module, third interface module and/or fourth interface module can be implemented by using, for example, a transceiver circuit, an optical transceiver, and the like.
需要说明的是,在本申请实施例中,锁管理装置为第一CPU中的第一程序分配的锁可以为自旋锁,也可以为改进型的互斥锁,当然,还可以其他基于自旋锁和/或互斥锁衍生的锁,对此不做具体限定。It should be noted that, in this embodiment of the present application, the lock allocated by the lock management device to the first program in the first CPU may be a spin lock or an improved mutual exclusion lock. Locks derived from spinlocks and/or mutex locks, which are not specifically limited.
上述第一程序可以为用户态程序,第一程序还可以理解为用户态线程或者用户态进程。The above-mentioned first program may be a user-mode program, and the first program may also be understood as a user-mode thread or a user-mode process.
上述中断分发模块停止或者恢复向第一CPU核分发的中断为可屏蔽中断,可屏蔽中断是由具有中断能力的外围设备所产生的。也就是说,当第一程序在获取到锁后,通知中断分发装置停止或恢复为第一CPU核分发可屏蔽中断。The interrupts that the interrupt distribution module stops or resumes distribution to the first CPU core are maskable interrupts, and the maskable interrupts are generated by peripheral devices with interrupt capability. That is to say, after the first program acquires the lock, it notifies the interrupt distribution apparatus to stop or resume the distribution of maskable interrupts for the first CPU core.
下面以锁为自旋锁为例,结合上述CPU的结构,对本申请实施例提供的一种锁管理方法进行说明。Taking the lock as a spin lock as an example, the following describes a lock management method provided by an embodiment of the present application in combination with the structure of the above-mentioned CPU.
图5为本申请实施例中的锁管理方法的流程示意图,参见图6中的实线所示,该方法可以包括:FIG. 5 is a schematic flowchart of a lock management method in an embodiment of the present application. Referring to the solid line in FIG. 6 , the method may include:
S501:第一CPU核向锁管理模块发送锁申请请求;S501: The first CPU core sends a lock application request to the lock management module;
当第一CPU核中运行的第一程序需要访问临界区时,第一CPU核通过第一接口模块向锁管理模块发送锁申请请求,以请求锁管理模块为第一程序分配锁,使得第一程序能够访问临界区,执行临界区的操作。When the first program running in the first CPU core needs to access the critical area, the first CPU core sends a lock application request to the lock management module through the first interface module, so as to request the lock management module to allocate a lock to the first program, so that the first Programs can access critical sections and perform operations in critical sections.
在一些可能的实施方式中,在第一CPU核锁申请请求的同时,CPU的其他核也可以通过第一接口模块向锁管理模块发送锁申请请求,也就是说,第一接口模块可以被至少一个CPU核同时访问,接收来自至少一个CPU核的锁申请请求。In some possible implementations, at the same time as the first CPU core lock application request, other cores of the CPU can also send a lock application request to the lock management module through the first interface module, that is, the first interface module can be at least A CPU core accesses at the same time and receives a lock application request from at least one CPU core.
S502:锁管理模块响应锁申请请求,为第一程序分配锁。S502: The lock management module allocates a lock to the first program in response to the lock application request.
在只有第一CPU核向锁管理模块发送锁申请请求的情况下,锁管理模块响应该锁申请请求,将锁分配给第一程序。When only the first CPU core sends a lock application request to the lock management module, the lock management module responds to the lock application request and allocates the lock to the first program.
在实际应用中,除了第一CPU核中的第一程序请求访问临界区外,还可以存在其他线程同时向锁管理模块发送锁申请请求的情况,那么,锁管理模块获得至少一个CPU核发送的锁申请请求后,需要进行仲裁,以确认将锁分配给哪一个CPU核。假设,锁管理模块仲裁决定将锁分配给第一程序,然后,锁管理模块可以只响应第一CPU核发送的锁申请请求,将锁分配给第一程序,如此,第一程序就可以唯一的访问临界区,执行临界区的操作。In practical applications, in addition to the first program in the first CPU core requesting access to the critical section, other threads may also send lock application requests to the lock management module at the same time. Then, the lock management module obtains the After a lock request is made, arbitration is required to confirm which CPU core the lock is assigned to. Suppose, the lock management module arbitrates the decision to assign the lock to the first program. Then, the lock management module can only respond to the lock application request sent by the first CPU core and assign the lock to the first program. In this way, the first program can be unique Access the critical section and perform the operation of the critical section.
S503:锁管理模块向中断分发模块发送第一信号;S503: The lock management module sends a first signal to the interrupt distribution module;
其中,第一信号用于指示中断分发模块停止向第一CPU核分发中断。The first signal is used to instruct the interrupt distribution module to stop distributing interrupts to the first CPU core.
锁管理模块在S502执行的同时或者在S502执行之后,通过第二接口模块与第三接口模块通信,向中断分发模块发送第一信号,通知中断分发模块停止向第一CPU核分发中断,使得第一程序能够保持访问临界区,不会被中断打扰,尽快完成对临界区的操作,减少其他线程等待获取锁的时长,降低对CPU资源以及内存资源的消耗。While executing S502 or after executing S502, the lock management module communicates with the third interface module through the second interface module, sends a first signal to the interrupt distribution module, and notifies the interrupt distribution module to stop distributing interrupts to the first CPU core, so that the first signal is sent to the interrupt distribution module. A program can maintain access to the critical section without being disturbed by interruptions, complete the operation on the critical section as soon as possible, reduce the time that other threads wait to acquire the lock, and reduce the consumption of CPU resources and memory resources.
S504:中断分发模块响应第一信号,停止向第一CPU核分发中断;S504: The interrupt distribution module stops distributing interrupts to the first CPU core in response to the first signal;
在一些可能的实现方式中,S502和S503在执行过程中可以先后顺序执行,如先执行S502,后执行S503,或者,先执行S503,后执行S502;当然,也可以同时执行S502和S503,对于S502和S503的执行顺序,本申请实施例不做具体限定。In some possible implementations, S502 and S503 may be executed sequentially during the execution process, for example, S502 is executed first, then S503 is executed, or, S503 is executed first, and then S502 is executed; of course, S502 and S503 can also be executed at the same time. The execution sequence of S502 and S503 is not specifically limited in this embodiment of the present application.
至此,便完成了锁申请流程。At this point, the lock application process is completed.
在一些可能的实施方式中,第一程序在完成对临界区的执行之后,就需要释放锁,以便其他程序能够尽快获取锁,那么,参见图6中的虚线所示,在S504之后,上述锁管理方法还 可以包括:In some possible implementations, after the first program completes the execution of the critical section, it needs to release the lock so that other programs can acquire the lock as soon as possible. Then, as shown by the dotted line in FIG. 6 , after S504, the above lock Management methods can also include:
S505:锁管理模块释放第一程序对应的锁;S505: the lock management module releases the lock corresponding to the first program;
S506:锁管理模块向中断分发模块发送第二信号;S506: The lock management module sends a second signal to the interrupt distribution module;
其中,第二信号用于指示中断分发模块恢复向第一CPU核分发中断。The second signal is used to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core.
在一些可能的实现方式中,S505和S506在执行过程中可以先后顺序执行,如先执行S505,后执行S506,或者,先执行S506,后执行S505;当然,也可以同时执行S505和S506,对于S505和S506的执行顺序,本申请实施例不做具体限定。In some possible implementations, S505 and S506 may be executed sequentially in the execution process, for example, S505 is executed first, then S506 is executed, or, S506 is executed first, and then S505 is executed; of course, S505 and S506 can also be executed at the same time. The execution sequence of S505 and S506 is not specifically limited in this embodiment of the present application.
S507:中断分发模块响应第二信号,恢复向第一CPU核分发中断。S507: The interrupt distribution module resumes distributing the interrupt to the first CPU core in response to the second signal.
锁管理模块在释放锁的同时或者在释放锁之后,可以通过第二接口模块与第三接口模块的通信向中断分发模块发送第二信号,以指示中断分发模块恢复向第一CPU核分发中断。中断分发模块响应第二信号,恢复向第一CPU分发中断,那么,当第一CPU核接收到中断分发模块分发的中断后,可以响应该中断,对中断进行处理。When releasing the lock or after releasing the lock, the lock management module may send a second signal to the interrupt distribution module through the communication between the second interface module and the third interface module to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core. The interrupt distribution module resumes distributing the interrupt to the first CPU in response to the second signal. Then, when the first CPU core receives the interrupt distributed by the interrupt distribution module, it can respond to the interrupt and process the interrupt.
在一些可能的实施例中,上述第一程序在完成对临界区的执行后,可以请求锁管理模块释放分配给第一程序的锁,那么,S505可以包括:锁管理模块获得来自第一CPU核的锁释放请求,锁管理模块响应锁释放请求,释放第一程序对应的锁。In some possible embodiments, after the above-mentioned first program completes the execution of the critical section, it may request the lock management module to release the lock allocated to the first program. Then, S505 may include: the lock management module obtains the lock from the first CPU core. In response to the lock release request, the lock management module releases the lock corresponding to the first program in response to the lock release request.
当第一程序完成对临界区的执行之后,第一CPU核通过第一接口模块可以向锁管理模块发送锁释放请求,以请求第一CPU核释放通过上述S501至S504所述的锁申请过程为第一程序分配的锁,锁管理模块响应第一CPU核发送的锁释放请求,释放锁,此时,其他程序可以尝试获取锁。如此,第一程序在完成临界区的操作后,立即指示锁管理模块释放锁,使得其他程序能够尽快的获取到锁,减少其他程序的等待时间,降低对CPU资源以及内存资源的消耗。After the first program completes the execution of the critical section, the first CPU core may send a lock release request to the lock management module through the first interface module, so as to request the first CPU core to release the lock application process described in S501 to S504 as follows For the lock allocated by the first program, the lock management module releases the lock in response to the lock release request sent by the first CPU core. At this time, other programs can try to acquire the lock. In this way, the first program immediately instructs the lock management module to release the lock after completing the operation in the critical section, so that other programs can acquire the lock as soon as possible, reduce the waiting time of other programs, and reduce the consumption of CPU resources and memory resources.
相应地,锁管理模块在执行S505之前、之后或者同时,响应上述锁释放请求,向中断分发模块发送第二信号,以指示中断分发模块恢复向第一CPU核分发中断。Correspondingly, before, after or at the same time as executing S505, the lock management module sends a second signal to the interrupt distribution module in response to the lock release request to instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core.
在一些可能的实施例中,锁管理模块除了根据第一CPU核的请求指示中断分发模块恢复向第一CPU核分发中断之外,还可以自行指示中断分发模块恢复向第一CPU核分发中断。那么,参见图4中虚线所示,上述锁管理装置103还可以包括:定时器模块404;这里,定时器模块404的时长可以理解为期望第一程序执行临界区的操作所用的时长,该时长的具体取值可以根据CPU的性能、操作系统的需求等进行设置,本申请实施例不做具体限定。In some possible embodiments, in addition to instructing the interrupt distribution module to resume distributing the interrupt to the first CPU core according to the request of the first CPU core, the lock management module may also instruct the interrupt distribution module to resume distributing the interrupt to the first CPU core by itself. Then, referring to the dotted line in FIG. 4 , the above lock management device 103 may further include: a timer module 404; here, the duration of the timer module 404 can be understood as the duration for which the first program is expected to perform the operation of the critical section. The specific value of can be set according to the performance of the CPU, the requirements of the operating system, etc., which is not specifically limited in this embodiment of the present application.
在实际应用中,定时器模块可以且不限于以定时器电路或者定时器程序来实现,本申请实施例不做具体限定。In practical applications, the timer module can be, but is not limited to, implemented by a timer circuit or a timer program, which is not specifically limited in this embodiment of the present application.
那么,锁管理模块通过S502为第一程序分配锁之后,启动定时器模块,定时器模块开始计时。当定时器模块超时时,锁管理模块向中断分发模块发送第二信号,中断分发模块响应第二信号,恢复向第一CPU分发中断,那么,当第一CPU核接收到中断分发模块分发的中断后,可以响应该中断,对中断进行处理。Then, after the lock management module allocates the lock to the first program through S502, the timer module is started, and the timer module starts timing. When the timer module times out, the lock management module sends a second signal to the interrupt distribution module, and the interrupt distribution module responds to the second signal and resumes distributing the interrupt to the first CPU. Then, when the first CPU core receives the interrupt distributed by the interrupt distribution module After that, you can respond to the interrupt and process the interrupt.
在一些可能的实施方式中,锁管理模块在定时器模块超时之前或者之后均可以释放分配给第一程序的锁,本申请实施例对此不做限定。In some possible implementations, the lock management module may release the lock allocated to the first program before or after the timer module times out, which is not limited in this embodiment of the present application.
至此,便完成了锁释放流程。At this point, the lock release process is completed.
在一些可能的实施方式中,锁管理模块在通过S502为第一程序分配锁的过程中,可以记录下为哪个CPU核的程序分配了锁,如第一CPU核,这样,锁管理模块在执行S503时,可以指示中断分发模块停止向记录的CPU核,即第一CPU核分发中断。In some possible implementations, in the process of allocating the lock to the first program through S502, the lock management module may record which CPU core program is allocated the lock for, such as the first CPU core, in this way, the lock management module is executing At S503, the interrupt distribution module may be instructed to stop distributing interrupts to the recorded CPU core, that is, the first CPU core.
相应的,锁管理模块在通过S505为第一程序释放锁的过程中,可以记录下为哪个CPU 核的程序释放了锁,如第一CPU核,这样,锁管理模块在执行S506时,可以指示中断分发模块恢复向记录的CPU核,即第一CPU核分发中断。Correspondingly, in the process of releasing the lock for the first program through S505, the lock management module can record which CPU core program has released the lock, such as the first CPU core, so that when the lock management module executes S506, it can indicate The interrupt distribution module resumes distributing the interrupt to the recorded CPU core, that is, the first CPU core.
通过上述锁申请流程以及锁释放流程,使得用户态的第一程序在申请到锁之后,能够不受中断的打断,保持访问临界区,直至完成对临界区的执行。由于在第一程序访问临界区的时候停止向第一CPU核分发中断,避免了中断结束后选择更高优先级线程进行执行而不是原程序执行的死锁问题。进一步地,在第一程序完成对临界区的执行后,锁管理装置释放锁,使得其他程序可以尝试获取锁,如此,减少其他程序等待获取的时间,降低对CPU资源以及内存资源的消耗。进一步地,由于在第一程序访问临界区的时候停止向第一CPU核分发中断,避免了中断结束后选择更高优先级线程进行执行而不是原程序执行的死锁问题。Through the above lock application process and lock release process, the first program in the user mode can keep accessing the critical area without interruption after applying for the lock until the execution of the critical area is completed. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program finishes executing the critical section, the lock management device releases the lock so that other programs can try to acquire the lock, thus reducing the time for other programs to wait for acquisition, and reducing the consumption of CPU resources and memory resources. Further, since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided.
在一些可能的实施例中,以锁管理装置为基于硬件队列逻辑实现为例,对上述S501至S507的执行过程进行说明。In some possible embodiments, the execution process of the above S501 to S507 will be described by taking the lock management apparatus as an example of hardware queue logic implementation.
图6为本申请实施例中的硬件队列的逻辑结构示意图,参见图6所示,硬件队列读写接口允许多个CPU核同时访问,由读写接口自行仲裁决定多个CPU核对队列内容的读写访问顺序,以实现最终向队列存储区域读写数据时是串行写入(可以理解的,所有的读过程串行,所有写过程串行,读的同时可以有写过程)。当某个CPU核向队列写入一个数据时,队列存储中会多一个数据,且新添加的数据位于队列存储区域中有效数据的尾部;当某个CPU核向队列请求读出一个数据时,队列存储中也会减少一个数据,且减少的数据为之前位于存储区域中有效数据的头部的数据。队列存储区域中的数据,会始终按照写入时的顺序来保持相对顺序,当队列由于读取而减少一个数据后,后续的数据会自动前移一个位置,保证队列下一次可以读取到数据。如果队列中的存储区域已经全部都存储了有效数据,即处于满状态,后续CPU核的写操作会得到一个已满的反馈信息而需要等待重新写入;如果队列存储区域无有效数据,即处于空状态,后续CPU核的读操作会得到一个已空的反馈信息而需要等待重新读取。6 is a schematic diagram of the logical structure of a hardware queue in an embodiment of the present application. Referring to FIG. 6 , the read-write interface of the hardware queue allows multiple CPU cores to access simultaneously, and the read-write interface arbitrates to determine the reading of the contents of the queue by multiple CPUs. Write access sequence, so as to realize serial writing when finally reading and writing data to the queue storage area (it is understandable that all reading processes are serial, all writing processes are serial, and there can be writing processes at the same time as reading). When a CPU core writes a data to the queue, there will be one more data in the queue storage, and the newly added data is located at the end of the valid data in the queue storage area; when a CPU core requests to read a data from the queue, One piece of data is also reduced in the queue storage, and the reduced data is the data previously located at the head of the valid data in the storage area. The data in the queue storage area will always keep the relative order according to the order in which it was written. When the queue is reduced by one data due to reading, the subsequent data will automatically move forward one position to ensure that the queue can read the data next time. . If the storage area in the queue has all stored valid data, that is, it is in a full state, the subsequent write operation of the CPU core will get a full feedback message and need to wait for rewriting; if there is no valid data in the queue storage area, it is in a full state. In the empty state, subsequent read operations of the CPU core will get an empty feedback message and need to wait for re-reading.
在硬件队列初始化的时候,在队列中存储一个有效数据。首先,CPU中的至少一个CPU核同时访问读接口,发送锁申请请求(这里,锁申请请求可以理解为读信号),由读接口仲裁决定将锁分配给哪个CPU核中的程序。例如,读接口可以将CPU核中的程序按照优先级进行排序,决定为优先级最高的第一程序分配锁,相应地,读接口响应第一CPU核的锁申请请求,第一程序读取队列头部的有效数据;或者,读接口可以将CPU核发送锁申请请求按时间顺序进行排序,决定为最先向读接口发送锁申请请求的第一程序分配锁,相应地,读接口响应第一CPU核的锁申请请求;第一程序读取队列头部的有效数据。当然,读接口还可以采用其他仲裁规则决定为哪个CPU核中的程序分配锁,例如,读接口还可以决策为组优先级最高的一组程序分配锁,或为一组程序中位于队列头部的第一程序分配锁等,本申请实施例不做具体限定。那么,读接口在给第一程序分配锁的同时,可以向中断分发装置发送第一信号,以指示中断分发装置停止向第一CPU核分发中断。When the hardware queue is initialized, a valid data is stored in the queue. First, at least one CPU core in the CPU accesses the read interface at the same time, sends a lock application request (here, the lock application request can be understood as a read signal), and the read interface arbitrates to determine which program in the CPU core to assign the lock to. For example, the read interface can sort the programs in the CPU core by priority, and decide to allocate a lock to the first program with the highest priority. Accordingly, the read interface responds to the lock request request of the first CPU core, and the first program reads the queue The valid data in the header; alternatively, the read interface can sort the lock request requests sent by the CPU core in chronological order, and decide to allocate a lock to the first program that first sends a lock request request to the read interface. Accordingly, the read interface responds to the first program. The lock application request of the CPU core; the first program reads the valid data at the head of the queue. Of course, the read interface can also use other arbitration rules to decide which CPU core program to allocate locks to. For example, the read interface can also decide to allocate locks to a group of programs with the highest group priority, or to a group of programs that are at the head of the queue. The first program assigns a lock, etc., which are not specifically limited in this embodiment of the present application. Then, while allocating the lock to the first program, the read interface may send a first signal to the interrupt distribution device to instruct the interrupt distribution device to stop distributing interrupts to the first CPU core.
当第一程序完成临界区的执行后,向硬件队列中的写接口发送锁释放请求(这里,锁释放请求可以理解为写信号),写接口响应锁释放请求,将有效数据写入队列的尾部,完成锁的释放。与此同时,写接口还可以向中断分发装置发送第二信号,以指示中断分发装置恢复向第一CPU核分发中断。When the first program completes the execution of the critical section, it sends a lock release request to the write interface in the hardware queue (here, the lock release request can be understood as a write signal), and the write interface responds to the lock release request and writes valid data to the tail of the queue , complete the release of the lock. At the same time, the write interface may also send a second signal to the interrupt distribution apparatus to instruct the interrupt distribution apparatus to resume distributing the interrupt to the first CPU core.
进一步地,写接口在将有效数据写入队列尾部之后,该有效数据将自动移至队列头部,以便后其他程序读取,移动后的队列内相对顺序保持不变。Further, after the write interface writes valid data to the tail of the queue, the valid data will be automatically moved to the head of the queue so that other programs can read it later, and the relative order in the moved queue remains unchanged.
在实际应用中,上述读接口与写接口在物理上可以分设或者合设,读接口和写接口中与 CPU核通信的功能模块可以与上述第一接口模块对应,与中断分发装置通信的功能模块可以与上述第二接口模块对应,读接口中进行仲裁决策的功能模块可以与上述锁管理模块对应。In practical applications, the read interface and the write interface can be physically separate or combined, and the function module in the read interface and the write interface that communicates with the CPU core can correspond to the first interface module, and the function module communicates with the interrupt distribution device. It may correspond to the above-mentioned second interface module, and the function module in the read interface that performs arbitration decision may correspond to the above-mentioned lock management module.
或者,以锁管理装置为基于寄存器标识位逻辑实现为例,对上述S501至S507的执行过程进行说明。Alternatively, the execution process of the above S501 to S507 will be described by taking as an example that the lock management device is implemented based on the register identification bit logic.
在寄存器中设置一标识位来表示锁的状态,如标识位置1,则表示锁未被分配,可以获取,反之,如果标识位置0,则标识锁已被分配,不可以获取。首先,寄存器初始化的时候,将标识位置1。首先,CPU中的至少一个CPU核同时访问寄存器,发送锁申请请求,由寄存器仲裁决定将锁分配给哪个CPU核中的程序。例如,寄存器可以将CPU核中的程序按照优先级进行排序,决定为优先级最高的第一程序分配锁,相应地,寄存器响应第一CPU核的锁申请请求,将标识位置0,并告知第一CPU核,或者,寄存器可以将CPU核发送锁申请请求按时间顺序进行排序,决定为最先向寄存器发送锁申请请求的第一程序分配锁,相应地,读接口响应第一CPU核的锁申请请求,将标识位置0,并告知第一CPU核。当然,寄存器还可以采用其他仲裁规则决定为哪个CPU核中的程序分配锁,例如,寄存器还可以决策为组优先级最高的一组程序分配锁,或为一组程序中位于队列头部的第一程序分配锁等,本申请实施例不做具体限定。那么,寄存器在给第一程序分配锁的同时,可以向中断分发装置发送第一信号,以指示中断分发装置停止向第一CPU核分发中断。An identification bit is set in the register to indicate the state of the lock. If the identification position is 1, it means that the lock is not allocated and can be acquired. On the contrary, if the identification position is 0, it indicates that the lock has been allocated and cannot be acquired. First, when the register is initialized, the flag bit is set to 1. First, at least one CPU core in the CPU accesses the registers at the same time, sends a lock application request, and the register arbitration decides the program in which CPU core to assign the lock to. For example, the register can sort the programs in the CPU core according to the priority, and decide to allocate the lock to the first program with the highest priority. Accordingly, the register responds to the lock application request of the first CPU core, sets the flag to 0, and informs the first program of the first CPU core. A CPU core, or a register can sort the lock request requests sent by the CPU core in chronological order, and decide to allocate a lock to the first program that first sends a lock request request to the register. Accordingly, the read interface responds to the lock request of the first CPU core Apply for a request, set the flag to 0, and inform the first CPU core. Of course, the register can also use other arbitration rules to decide which program in the CPU core to allocate the lock to. For example, the register can also decide to allocate the lock to the group of programs with the highest group priority, or to the first group of programs at the head of the queue. A program allocates locks, etc., which are not specifically limited in this embodiment of the present application. Then, while allocating the lock to the first program, the register may send a first signal to the interrupt distribution apparatus to instruct the interrupt distribution apparatus to stop distributing interrupts to the first CPU core.
当第一程序完成临界区的执行后,向寄存器发送锁释放请求,寄存器响应锁释放请求,将标识位置1,完成锁的释放。与此同时,写接口还可以向中断分发装置发送第二信号,以指示中断分发装置恢复向第一CPU核分发中断。When the first program completes the execution of the critical section, it sends a lock release request to the register, and the register responds to the lock release request and sets the flag to 1 to complete the lock release. At the same time, the write interface may also send a second signal to the interrupt distribution apparatus to instruct the interrupt distribution apparatus to resume distributing the interrupt to the first CPU core.
由上述可知,其他程序访问寄存器,如果读取到标识位为0,则表明锁已被分配,其他程序等待,并继续尝试获取锁;而如果读取到标识位为1,则表明锁未被分配,该程序可以获取锁。It can be seen from the above that other programs access the register. If the flag bit is read as 0, it indicates that the lock has been allocated, and other programs wait and continue to try to acquire the lock; and if the flag bit is read as 1, it indicates that the lock has not been obtained. allocation, the program can acquire the lock.
当然,锁管理装置还可以为其他实现形式,本申请实施例不做具体限定。Of course, the lock management device may also be in other implementation forms, which are not specifically limited in the embodiments of the present application.
在一些可能的实现方式中,上述第一程序还可以为内核态程序,第一程序还可以理解为内核态线程或者内核态进程。由于内核态程序是可以执行禁止中断、禁止抢占等操作,但是内核态程序是通过不响应中断来实现禁止中断或禁止抢占的,这样会使得中断迟迟得不到响应,增加了中断响应的耗时,那么,针对内核态的第一程序,可以执行如S501至S505所述的方法,这样,中断分发装置可以在停止向第一CPU核分发中断之后,将优先级较高的中断分发至其他CPU核去处理,使得中断能够及时被响应,提高实时性能。In some possible implementation manners, the above-mentioned first program may also be a kernel-mode program, and the first program may also be understood as a kernel-mode thread or a kernel-mode process. Because the kernel mode program can perform operations such as prohibiting interrupts, prohibiting preemption, etc., but the kernel mode program realizes prohibiting interrupts or prohibiting preemption by not responding to interrupts, which will cause the interrupts to be delayed and not be responded to, increasing the consumption of interrupt response. , then, for the first program in the kernel state, the methods described in S501 to S505 can be executed, so that the interrupt distribution device can distribute interrupts with higher priority to other CPU cores after stopping distribution of interrupts to the first CPU core. CPU core to process, so that the interrupt can be responded to in time, improve real-time performance.
在一些可能的实施方式中,为了满足实时要求较高的应用场景,如车载系统或者实时操作系统中,锁管理装置还可以为多个程序分配锁,并通知中断分发装置停止分发相应的中断。具体来说,锁管理装置接收至少一个CPU核发送的锁申请请求,并为一个或者多个CPU核中的多个实时性要求较高的程序分配锁,这样就能够保证这些程序尽快执行,提升实时性能。In some possible implementations, in order to meet application scenarios with high real-time requirements, such as in-vehicle systems or real-time operating systems, the lock management device may also allocate locks to multiple programs, and notify the interrupt distribution device to stop distributing corresponding interrupts. Specifically, the lock management device receives a lock application request sent by at least one CPU core, and allocates locks to multiple programs with high real-time requirements in one or more CPU cores, so as to ensure that these programs are executed as soon as possible and improve real-time performance.
基于相同的发明构思,本申请实施例还提供一种锁管理装置,该锁管理装置可以为处理器中的芯片或者片上系统,还可以为处理器中用于实现上述实施例中任一可能的实施方式所述的方法的功能模块。举例来说,图7为本申请实施例中的锁管理装置的结构示意图一,参见图7所示,该锁管理装置700可以包括:第一通信单元701、分配单元702以及第二通信单元703;其中,第一通信单元701,用于获得来自第一CPU核的锁申请请求,锁申请请求用于请求为第一CPU核中的第一程序分配锁;分配单元702,用于响应锁申请请求,为第一程序分配锁;第二通信单元703,用于向中断分发装置发送第一信号,第一信号用于指示中断分发模块停止向第一CPU核分发中断。Based on the same inventive concept, an embodiment of the present application further provides a lock management device. The lock management device may be a chip or a system-on-chip in a processor, or may be used in the processor to implement any of the possible implementations in the foregoing embodiments. The functional modules of the method described in the embodiment. For example, FIG. 7 is a schematic structural diagram 1 of a lock management apparatus in an embodiment of the present application. Referring to FIG. 7 , the lock management apparatus 700 may include: a first communication unit 701 , an allocation unit 702 and a second communication unit 703 ; Wherein, the first communication unit 701 is used to obtain a lock application request from the first CPU core, and the lock application request is used to request to allocate a lock for the first program in the first CPU core; the allocation unit 702 is used to respond to the lock application request, assign a lock to the first program; the second communication unit 703 is configured to send a first signal to the interrupt distribution apparatus, where the first signal is used to instruct the interrupt distribution module to stop distributing interrupts to the first CPU core.
在本申请实施例中,通过锁管理装置在第一程序访问临界区的时候停止向第一CPU核分发中断,使得第一程序在申请到锁之后,能够不受中断的打断,保持访问临界区,直至完成对临界区的执行。由于在第一程序访问临界区的时候停止向第一CPU核分发中断,避免了中断结束后选择更高优先级线程进行执行而不是原程序执行的死锁问题。进一步地,在第一程序尽快完成对临界区的执行后,锁管理模块释放锁,使得其他程序可以尝试获取锁,减少其他程序等待获取的时间,降低对CPU资源以及内存资源的消耗。In the embodiment of the present application, when the first program accesses the critical area, the lock management device stops distributing the interrupt to the first CPU core, so that the first program can not be interrupted by the interrupt after applying for the lock, and the access critical area is maintained. section until the execution of the critical section is complete. Since the interrupt is stopped to the first CPU core when the first program accesses the critical section, the deadlock problem of selecting a higher priority thread for execution instead of the original program execution after the interruption ends is avoided. Further, after the first program completes the execution of the critical section as soon as possible, the lock management module releases the lock, so that other programs can try to acquire the lock, reduce the time for other programs to wait for acquisition, and reduce the consumption of CPU resources and memory resources.
应理解的是,本申请实施例的装置700可以通过专用集成电路(ASIC)实现,或可编程逻辑器件(programmable logic device,PLD)实现,上述PLD可以是复杂程序逻辑器件(complex programmable logical device,CPLD),现场可编程门阵列(FPGA),通用阵列逻辑(generic array logic,GAL)或其任意组合。也可以通过软件实现图1至图7中所示的方法时,装置700及其各个模块也可以为软件模块。It should be understood that the apparatus 700 in the embodiment of the present application may be implemented by an application specific integrated circuit (ASIC), or a programmable logic device (programmable logic device, PLD), and the above-mentioned PLD may be a complex programmable logical device (complex programmable logical device, CPLD), Field Programmable Gate Array (FPGA), Generic Array Logic (GAL) or any combination thereof. When the methods shown in FIGS. 1 to 7 can also be implemented by software, the apparatus 700 and its respective modules can also be software modules.
在一些可能的实施方式中,上述装置还包括:确定单元;那么,第一通信单元,还用于在获得来自第一CPU核的锁申请请求之前,获得来自至少一个CPU核的锁申请请求,来自至少一个CPU核的锁申请请求用于请求为至少一个CPU核中的程序分配锁,至少一个CPU核包括第一CPU核;确定单元,用于从至少一个CPU核中确定第一CPU核。In some possible implementations, the above-mentioned apparatus further includes: a determining unit; then, the first communication unit is further configured to obtain a lock application request from at least one CPU core before obtaining a lock application request from the first CPU core, The lock application request from at least one CPU core is used for requesting to allocate a lock to a program in at least one CPU core, and the at least one CPU core includes the first CPU core; the determining unit is used for determining the first CPU core from the at least one CPU core.
在一些可能的实施方式中,上述确定单元,具体用于根据预设规则从至少一个CPU核中确定第一CPU核。In some possible implementations, the above determining unit is specifically configured to determine the first CPU core from at least one CPU core according to a preset rule.
在一些可能的实施方式中,第一程序用于在获得锁后保持对临界区的访问,直至完成对临界区的操作,临界区用于指示一个访问共享资源的程序片段。In some possible implementations, the first program is used to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, and the critical section is used to indicate a program segment that accesses the shared resource.
在一些可能的实施方式中,上述装置还包括:释放单元,用于在分配单元为第一程序分配锁之后,释放第一程序对应的锁;第二通信单元,还用于向中断分发装置发送第二信号,第二信号用于指示中断分发装置恢复向第一CPU核分发中断。In some possible implementation manners, the above-mentioned apparatus further includes: a release unit, configured to release the lock corresponding to the first program after the distribution unit allocates the lock to the first program; the second communication unit is further configured to send a message to the interrupt distribution apparatus. The second signal is used to instruct the interrupt distribution apparatus to resume distribution of the interrupt to the first CPU core.
在一些可能的实施方式中,第一通信单元,还用于在释放单元释放第一程序对应的锁之前,获得来自第一CPU核的锁释放请求,锁释放请求用于请求释放第一程序对应的锁;第二通信单元,用于响应锁释放请求,向中断分发装置发送第二信号。In some possible implementations, the first communication unit is further configured to obtain a lock release request from the first CPU core before the release unit releases the lock corresponding to the first program, and the lock release request is used to request to release the lock corresponding to the first program The second communication unit is configured to send a second signal to the interrupt distribution device in response to the lock release request.
在一些可能的实施方式中,上述装置还包括:定时单元,用于在分配单元为第一程序分配锁时,触发定时器模块;第二通信单元,用于当定时器模块超时时,向中断分发装置发送第二信号。In some possible implementation manners, the above-mentioned apparatus further includes: a timing unit for triggering a timer module when the allocation unit allocates a lock to the first program; a second communication unit for triggering an interrupt when the timer module times out The distribution device transmits the second signal.
在一些可能的实施方式中,第一程序为用户态程序。In some possible implementations, the first program is a user mode program.
上述第一通信单元可以与图4中的第一接口模块对应,第二通信单元可以与图4中的第二接口模块对应;分配单元、确定单元、释放单元以及定时单元可以为一个或者多个处理器。The above-mentioned first communication unit may correspond to the first interface module in FIG. 4, and the second communication unit may correspond to the second interface module in FIG. 4; the allocation unit, the determination unit, the release unit and the timing unit may be one or more processor.
基于相同的发明构思,本申请实施例还提供一种电子设备,包括:处理器,处理器用于用于执行上述实施例的任一可能的实施方式所述的锁管理方法。Based on the same inventive concept, an embodiment of the present application further provides an electronic device, including: a processor, where the processor is configured to execute the lock management method described in any possible implementation manner of the foregoing embodiments.
在本申请实施例中,上述电子设备可以为计算设备,如服务器;电子设备还可以为存储设备,如存储阵列等;电子设备还可以为网络设备,如交换机等。In the embodiment of the present application, the above electronic device may be a computing device, such as a server; the electronic device may also be a storage device, such as a storage array, etc.; the electronic device may also be a network device, such as a switch.
基于相同的发明构思,本申请实施例还提供一种锁管理装置,该管理装置如图8中锁管理装置8011,如图所示,该锁管理装置8011可以包括:处理器80111、存储器80112、至少一个通信接口(图3中仅是示例性的以包括第一接口80113以及第二接口80114为例进行说明)和总线80115,处理器80111、存储器80112、至少一个通信接口通过总线80115进行通信。Based on the same inventive concept, an embodiment of the present application further provides a lock management device. The management device is the lock management device 8011 in FIG. 8 . As shown in the figure, the lock management device 8011 may include: a processor 80111, a memory 80112, At least one communication interface (the first interface 80113 and the second interface 80114 are included in FIG. 3 as an example) and the bus 80115, and the processor 80111, the memory 80112, and the at least one communication interface communicate through the bus 80115.
处理器80111可以是通用中央处理器(CPU)、微处理器、特定应用集成电路(ASIC)或 一个或多个用于控制本申请实施例提供的锁管理方法执行的集成电路,处理器80111可以包括一个或多个CPU,例如CPU0和CPU1。The processor 80111 may be a general-purpose central processing unit (CPU), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits for controlling the execution of the lock management method provided by the embodiments of the present application. The processor 80111 may One or more CPUs are included, such as CPU0 and CPU1.
至少一个通信接口可以使用任何收发器一类的装置来实现,用于与其他功能器件、设备或通信网络通信,如以太网、无线接入网(radio access network,RAN)、无线局域网(wireless local area networks,WLAN)等。其中,第一接口80113用于与电子设备的处理器801(例如CPU)中至少一个处理器核(例如8013)通信,第二接口用于与电子设备中的中断控制器通信。At least one communication interface can be implemented using any transceiver-like device for communicating with other functional devices, devices or communication networks, such as Ethernet, radio access network (RAN), wireless local area network (wireless local area network) area networks, WLAN), etc. The first interface 80113 is used to communicate with at least one processor core (eg 8013 ) in the processor 801 (eg CPU) of the electronic device, and the second interface is used to communicate with the interrupt controller in the electronic device.
存储器802可以是易失性存储器或非易失性存储器,或可包括易失性和非易失性存储器两者。其中,非易失性存储器可以是只读存储器(read-only memory,ROM)、可编程只读存储器(programmable ROM,PROM)、可擦除可编程只读存储器(erasable PROM,EPROM)、电可擦除可编程只读存储器(electrically EPROM,EEPROM)或闪存。易失性存储器可以是随机存取存储器(random access memory,RAM),其用作外部高速缓存。通过示例性但不是限制性说明,许多形式的RAM可用,例如静态随机存取存储器(static RAM,SRAM)、动态随机存取存储器(DRAM)、同步动态随机存取存储器(synchronous DRAM,SDRAM)、双倍数据速率同步动态随机存取存储器(double data date SDRAM,DDR SDRAM)、增强型同步动态随机存取存储器(enhanced SDRAM,ESDRAM)、同步连接动态随机存取存储器(synchlink DRAM,SLDRAM)和直接内存总线随机存取存储器(direct rambus RAM,DR RAM)。。 Memory 802 may be volatile memory or nonvolatile memory, or may include both volatile and nonvolatile memory. The non-volatile memory may be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically programmable Erase programmable read-only memory (electrically EPROM, EEPROM) or flash memory. Volatile memory may be random access memory (RAM), which acts as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), Double data rate synchronous dynamic random access memory (double data date SDRAM, DDR SDRAM), enhanced synchronous dynamic random access memory (enhanced SDRAM, ESDRAM), synchronous link dynamic random access memory (synchlink DRAM, SLDRAM) and direct Memory bus random access memory (direct rambus RAM, DR RAM). .
其中,存储器802用于存储执行本申请实施例提供的锁管理方法的计算机执行指令,其中,存储器802以存储用于实现三个模块化功能的指令:第一通信指令、第一处理指令和第二通信指令,并由处理器801来控制执行。处理器801用于执行存储器802中存储的计算机执行指令,从而实现上述一个或者多个实施例中任一可能的实现方式中所提供的锁管理方法。图8中示出的存储器802仅为示意图,该存储器还可以包括其他功能化的指令,对此,本申请实施例不进行限定。The memory 802 is used to store computer-executed instructions for executing the lock management method provided by the embodiments of the present application, wherein the memory 802 is used to store instructions for implementing three modular functions: a first communication instruction, a first processing instruction, and a third The two communication instructions are controlled and executed by the processor 801 . The processor 801 is configured to execute the computer-executed instructions stored in the memory 802, thereby implementing the lock management method provided in any possible implementation manner of the above one or more embodiments. The memory 802 shown in FIG. 8 is only a schematic diagram, and the memory may further include other functionalized instructions, which are not limited in this embodiment of the present application.
锁管理装置用于上述图1至图6上述的方法实施例中相应主体执行的功能,为了简洁,在此不再赘述。The lock management device is used for the functions performed by the corresponding subjects in the method embodiments described above in FIG. 1 to FIG. 6 , and details are not described here for brevity.
本申请还提供的一种电子设备,如图8所示,该电子设备包括处理器801、存储器802、通信接口803和总线804,处理器801、存储器802、通信接口803通过总线804进行通信。处理器801中包括锁管理装置8011、中断控制器8012、处理器核8013和总线8014,锁管理装置8011、中断控制器8012、处理器核8013通过总线8014相连。中断控制器8012可以对应上述图1至图6中所述的中断分发装置,用于实现上述中断分发装置所执行的方法的操作步骤。该电子设备800用于实现上述图1至图6所述方法中相应主体所执行的操作步骤,为了简洁,在此不再赘述。The application also provides an electronic device, as shown in FIG. 8 , the electronic device includes a processor 801 , a memory 802 , a communication interface 803 and a bus 804 , and the processor 801 , the memory 802 , and the communication interface 803 communicate through the bus 804 . The processor 801 includes a lock management device 8011 , an interrupt controller 8012 , a processor core 8013 and a bus 8014 . The interrupt controller 8012 may correspond to the interrupt distribution apparatus described above in FIG. 1 to FIG. 6 , and is configured to implement the operation steps of the method executed by the above-mentioned interrupt distribution apparatus. The electronic device 800 is used to implement the operation steps performed by the corresponding subjects in the methods described in FIG. 1 to FIG. 6 , which are not repeated here for brevity.
总线804、总线8014和总线80115除包括数据总线之外,还可以包括电源总线、控制总线和状态信号总线等各种用于实现器件或设备内部通信的传输媒介。但是为了清楚说明起见,在图中将各种总线都标为总线804、总线8014和总线80115。In addition to the data bus, the bus 804, the bus 8014 and the bus 80115 may also include various transmission media such as a power bus, a control bus, and a status signal bus for realizing the internal communication of the device or device. However, for clarity of illustration, the various buses are labeled as bus 804, bus 8014, and bus 80115 in the figures.
值得说明的是,图8中各部分的数量并不构成对本申请的限定,例如,处理器核8013可以包括多个,为了简洁,本申请仅以一个为例进行标标注。It should be noted that the number of each part in FIG. 8 does not constitute a limitation to the present application. For example, the processor core 8013 may include multiple ones. For the sake of brevity, only one is used as an example for labeling in the present application.
上述电子设备800用于上述图1至图6上述的方法实施例中相应主体执行的功能,为了简洁,在此不再赘述。The foregoing electronic device 800 is used for the functions performed by the corresponding subjects in the foregoing method embodiments in FIG. 1 to FIG. 6 , and for brevity, details are not described herein again.
基于相同的发明构思,本申请实施例提供一种计算机可读存储介质,计算机可读存储介质存储有指令,当指令在计算机上运行时,用于执行如上述实施例中任一可能的实 施方式所述的锁管理方法。Based on the same inventive concept, an embodiment of the present application provides a computer-readable storage medium, where the computer-readable storage medium stores instructions, and when the instructions are run on a computer, is used to execute any possible implementation manner in the foregoing embodiments The described lock management method.
基于相同的发明构思,本申请实施例提供一种计算机程序或计算机程序产品,当计算机程序或计算机程序产品在计算机上被执行时,使得计算机实现如上述实施例中任一可能的实施方式所述的锁管理方法。Based on the same inventive concept, the embodiments of the present application provide a computer program or computer program product, when the computer program or computer program product is executed on a computer, the computer can realize the implementation as described in any possible implementation manner in the foregoing embodiments lock management method.
上述实施例,可以全部或部分地通过软件、硬件、固件或其他任意组合来实现。当使用软件实现时,上述实施例可以全部或部分地以计算机程序产品的形式实现。所述计算机程序产品包括一个或多个计算机指令。在计算机上加载或执行所述计算机程序指令时,全部或部分地产生按照本申请实施例所述的流程或功能。所述计算机可以为通用计算机、专用计算机、计算机网络、或者其他可编程装置。所述计算机指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一个计算机可读存储介质传输,例如,所述计算机指令可以从一个网站站点、计算机、服务器或数据中心通过有线(例如同轴电缆、光纤、数字用户线(DSL))或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、服务器或数据中心进行传输。所述计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含一个或多个可用介质集合的服务器、数据中心等数据存储设备。所述可用介质可以是磁性介质(例如,软盘、硬盘、磁带)、光介质(例如,DVD)、或者半导体介质。半导体介质可以是固态硬盘(solid state drive,SSD)。The above embodiments may be implemented in whole or in part by software, hardware, firmware or any other combination. When implemented in software, the above-described embodiments may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of the present application are generated. The computer may be a general purpose computer, special purpose computer, computer network, or other programmable device. The computer instructions may be stored in or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be downloaded from a website site, computer, server, or data center Transmission to another website site, computer, server, or data center is by wire (eg, coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (eg, infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server, a data center, or the like that contains one or more sets of available media. The usable media may be magnetic media (eg, floppy disks, hard disks, magnetic tapes), optical media (eg, DVDs), or semiconductor media. The semiconductor medium may be a solid state drive (SSD).
以上所述,仅为本申请示例性的具体实施方式,但本申请的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本申请揭露的技术范围内,可轻易想到的变化或替换,都应涵盖在本申请的保护范围之内。因此,本申请的保护范围应该以权利要求的保护范围为准。The above is only an exemplary embodiment of the present application, but the protection scope of the present application is not limited to this. Substitutions should be covered within the protection scope of this application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.

Claims (16)

  1. 一种锁管理方法,其特征在于,包括:A lock management method, comprising:
    锁管理模块获得来自第一CPU核的锁申请请求,所述锁申请请求用于请求为所述第一CPU核中的第一程序分配锁;The lock management module obtains a lock application request from the first CPU core, and the lock application request is used for requesting to allocate a lock for the first program in the first CPU core;
    所述锁管理模块响应所述锁申请请求,为所述第一程序分配锁;The lock management module allocates a lock to the first program in response to the lock application request;
    所述锁管理模块向中断分发模块发送第一信号,所述第一信号用于指示所述中断分发模块停止向所述第一CPU核分发中断。The lock management module sends a first signal to the interrupt distribution module, where the first signal is used to instruct the interrupt distribution module to stop distributing the interrupt to the first CPU core.
  2. 根据权利要求1所述的方法,其特征在于,所述锁管理模块从所述至少一个CPU核中确定所述第一CPU核,包括:The method according to claim 1, wherein the lock management module determines the first CPU core from the at least one CPU core, comprising:
    所述锁管理模块按照预设规则从所述至少一个CPU核中确定出所述第一CPU核The lock management module determines the first CPU core from the at least one CPU core according to a preset rule
  3. 根据权利要求1或2所述的方法,其特征在于,所述第一程序用于在获得所述锁后保持对临界区的访问,直至完成对所述临界区的操作,所述临界区用于指示一个访问共享资源的程序片段。The method according to claim 1 or 2, wherein the first program is configured to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, and the critical section uses Used to indicate a program fragment that accesses a shared resource.
  4. 根据权利要求1至3任一项所述的方法,其特征在于,在所述锁管理模块为所述第一程序分配锁之后,所述方法还包括:The method according to any one of claims 1 to 3, wherein after the lock management module allocates a lock to the first program, the method further comprises:
    所述锁管理模块释放所述第一程序对应的锁,并向所述中断分发模块发送第二信号,所述第二信号用于指示所述中断分发模块恢复向所述第一CPU核分发中断。The lock management module releases the lock corresponding to the first program, and sends a second signal to the interrupt distribution module, where the second signal is used to instruct the interrupt distribution module to resume distributing interrupts to the first CPU core .
  5. 根据权利要求4所述的方法,其特征在于,在所述锁管理模块释放所述第一程序对应的锁之前,所述方法还包括:The method according to claim 4, wherein before the lock management module releases the lock corresponding to the first program, the method further comprises:
    所述锁管理模块获得来自所述第一CPU核的锁释放请求,所述锁释放请求用于请求释放所述第一程序对应的锁;The lock management module obtains a lock release request from the first CPU core, and the lock release request is used to request to release the lock corresponding to the first program;
    所述锁管理模块向所述中断分发模块发送第二信号,包括:The lock management module sends a second signal to the interrupt distribution module, including:
    所述锁管理模块响应所述锁释放请求,向所述中断分发模块发送所述第二信号。The lock management module sends the second signal to the interrupt distribution module in response to the lock release request.
  6. 根据权利要求4所述的方法,其特征在于,在所述锁管理模块向所述中断分发模块发送第二信号之前,所述方法还包括:The method according to claim 4, wherein before the lock management module sends the second signal to the interrupt distribution module, the method further comprises:
    所述锁管理模块在为所述第一程序分配锁时,启动定时器模块;The lock management module starts a timer module when allocating a lock to the first program;
    所述锁管理模块向所述中断分发模块发送第二信号,包括:The lock management module sends a second signal to the interrupt distribution module, including:
    当所述定时器模块超时时,所述锁管理模块向所述中断分发模块发送所述第二信号。When the timer module times out, the lock management module sends the second signal to the interrupt distribution module.
  7. 一种锁管理装置,其特征在于,包括:第一通信单元、分配单元以及第二通信单元;A lock management device, comprising: a first communication unit, a distribution unit and a second communication unit;
    所述第一通信单元,用于获得来自第一CPU核的锁申请请求,所述锁申请请求用于请求为所述第一CPU核中的第一程序分配锁;The first communication unit is used to obtain a lock application request from the first CPU core, where the lock application request is used to request to allocate a lock for the first program in the first CPU core;
    所述分配单元,用于响应所述锁申请请求,为所述第一程序分配锁;the allocation unit, configured to allocate a lock to the first program in response to the lock application request;
    所述第二通信单元,用于向中断分发装置发送第一信号,所述第一信号用于指示所述中断分发模块停止向所述第一CPU核分发中断。The second communication unit is configured to send a first signal to the interrupt distribution apparatus, where the first signal is used to instruct the interrupt distribution module to stop distributing interrupts to the first CPU core.
  8. 根据权利要求7所述的装置,其特征在于,所述装置还包括:确定单元;The apparatus according to claim 7, wherein the apparatus further comprises: a determining unit;
    所述第一通信单元,还用于在获得来自第一CPU核的锁申请请求之前,获得来自至少一个CPU核的锁申请请求,所述来自至少一个CPU核的锁申请请求用于请求为所述至少一个CPU核中的程序分配锁,所述至少一个CPU核包括所述第一CPU核;The first communication unit is further configured to obtain a lock application request from at least one CPU core before obtaining a lock application request from the first CPU core, where the lock application request from the at least one CPU core is used to request that all a program allocation lock in the at least one CPU core, the at least one CPU core including the first CPU core;
    所述确定单元,用于从所述至少一个CPU核中确定所述第一CPU核。The determining unit is configured to determine the first CPU core from the at least one CPU core.
  9. 根据权利要求8所述的装置,其特征在于,所述确定单元,具体用于根据预设规则从所述至少一个CPU核中确定所述第一CPU核。The apparatus according to claim 8, wherein the determining unit is specifically configured to determine the first CPU core from the at least one CPU core according to a preset rule.
  10. 根据权利要求7至9任一项所述的装置,其特征在于,所述第一程序用于在获得所述锁后保持对临界区的访问,直至完成对所述临界区的操作,所述临界区用于指示一个访问共享资源的程序片段。The apparatus according to any one of claims 7 to 9, wherein the first program is configured to maintain access to the critical section after acquiring the lock until the operation on the critical section is completed, the A critical section is used to indicate a program fragment that accesses a shared resource.
  11. 根据权利要求7至10任一项所述的装置,其特征在于,所述装置还包括:释放单元,用于在所述分配单元为所述第一程序分配锁之后,释放所述第一程序对应的锁;The device according to any one of claims 7 to 10, characterized in that the device further comprises: a releasing unit, configured to release the first program after the allocating unit allocates the lock to the first program the corresponding lock;
    所述第二通信单元,还用于向所述中断分发装置发送第二信号,所述第二信号用于指示所述中断分发装置恢复向所述第一CPU核分发中断。The second communication unit is further configured to send a second signal to the interrupt distribution device, where the second signal is used to instruct the interrupt distribution device to resume distribution of the interrupt to the first CPU core.
  12. 根据权利要求11所述的装置,其特征在于,所述第一通信单元,还用于在所述释放单元释放所述第一程序对应的锁之前,获得来自所述第一CPU核的锁释放请求,所述锁释放请求用于请求释放所述第一程序对应的锁;The device according to claim 11, wherein the first communication unit is further configured to obtain a lock release from the first CPU core before the release unit releases the lock corresponding to the first program request, the lock release request is used to request to release the lock corresponding to the first program;
    所述第二通信单元,用于响应所述锁释放请求,向所述中断分发装置发送所述第二信号。The second communication unit is configured to send the second signal to the interrupt distribution apparatus in response to the lock release request.
  13. 根据权利要求11所述的装置,其特征在于,所述装置还包括:定时单元,用于在所述分配单元为所述第一程序分配锁时,触发定时器模块;The device according to claim 11, wherein the device further comprises: a timing unit, configured to trigger a timer module when the allocation unit allocates a lock to the first program;
    所述第二通信单元,用于当所述定时器模块超时时,向所述中断分发装置发送所述第二信号。The second communication unit is configured to send the second signal to the interrupt distribution apparatus when the timer module times out.
  14. 一种处理器,其特征在于,包括:至少一个CPU核、总线、锁管理装置以及中断分发装置,其中,所述至少一个CPU核、所述锁管理装置以及所述中断分发装置通过所述总线通信,所述至少一个CPU核包括第一CPU核;A processor, comprising: at least one CPU core, a bus, a lock management device and an interrupt distribution device, wherein the at least one CPU core, the lock management device and the interrupt distribution device pass through the bus communication, the at least one CPU core includes a first CPU core;
    所述锁管理装置,用于执行如权利要求1至6任一项所述的锁管理方法的操作步骤。。The lock management device is configured to perform the operation steps of the lock management method according to any one of claims 1 to 6. .
  15. 根据权利要求14所述的处理器,其特征在于,所述中断分发装置,用于接收所述锁管理装置发送的所述第一信号;响应所述第一信号,停止向所述第一CPU核分发中断。The processor according to claim 14, wherein the interrupt distribution device is configured to receive the first signal sent by the lock management device; and in response to the first signal, stop sending to the first CPU Nuclear distribution interrupted.
  16. 一种电子设备,其特征在于,包括:处理器,所述处理器用于执行如权利要求1至6任一项所述的锁管理方法的操作步骤。An electronic device, comprising: a processor configured to execute the operation steps of the lock management method according to any one of claims 1 to 6.
PCT/CN2021/088656 2020-06-29 2021-04-21 Lock management method, apparatus, and device WO2022001303A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202010605434.XA CN113934516A (en) 2020-06-29 2020-06-29 Lock management method, device and equipment
CN202010605434.X 2020-06-29

Publications (1)

Publication Number Publication Date
WO2022001303A1 true WO2022001303A1 (en) 2022-01-06

Family

ID=79272956

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2021/088656 WO2022001303A1 (en) 2020-06-29 2021-04-21 Lock management method, apparatus, and device

Country Status (2)

Country Link
CN (1) CN113934516A (en)
WO (1) WO2022001303A1 (en)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114691594B (en) * 2022-03-11 2023-05-23 珠海海奇半导体有限公司 Chip architecture based on asymmetric dual-core MCU design and implementation method thereof
CN115145688B (en) * 2022-06-29 2023-06-16 科东(广州)软件科技有限公司 Suspension method and device for virtual cores of user mode virtual machine
CN116860402A (en) * 2023-05-26 2023-10-10 荣耀终端有限公司 Lock resource scheduling method and device

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060259907A1 (en) * 2005-05-10 2006-11-16 Rohit Bhatia Systems and methods of sharing processing resources in a multi-threading environment
CN103778002A (en) * 2012-10-18 2014-05-07 华为技术有限公司 Method and device for recycling critical resources in multi-core system
CN104166587A (en) * 2013-05-17 2014-11-26 杭州华三通信技术有限公司 Access device and method for critical resources
CN105808210A (en) * 2014-12-31 2016-07-27 龙芯中科技术有限公司 Shared resource access method and apparatus
CN108804080A (en) * 2018-05-04 2018-11-13 北京航天自动控制研究所 A kind of High Reliability Design method of multiple interrupt embedded program
CN110737616A (en) * 2018-07-20 2020-01-31 瑞昱半导体股份有限公司 Circuit system for processing interrupt priority
CN111124792A (en) * 2019-12-20 2020-05-08 北京东土科技股份有限公司 Multi-core debugging method and device and storage medium

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060259907A1 (en) * 2005-05-10 2006-11-16 Rohit Bhatia Systems and methods of sharing processing resources in a multi-threading environment
CN103778002A (en) * 2012-10-18 2014-05-07 华为技术有限公司 Method and device for recycling critical resources in multi-core system
CN104166587A (en) * 2013-05-17 2014-11-26 杭州华三通信技术有限公司 Access device and method for critical resources
CN105808210A (en) * 2014-12-31 2016-07-27 龙芯中科技术有限公司 Shared resource access method and apparatus
CN108804080A (en) * 2018-05-04 2018-11-13 北京航天自动控制研究所 A kind of High Reliability Design method of multiple interrupt embedded program
CN110737616A (en) * 2018-07-20 2020-01-31 瑞昱半导体股份有限公司 Circuit system for processing interrupt priority
CN111124792A (en) * 2019-12-20 2020-05-08 北京东土科技股份有限公司 Multi-core debugging method and device and storage medium

Also Published As

Publication number Publication date
CN113934516A (en) 2022-01-14

Similar Documents

Publication Publication Date Title
WO2022001303A1 (en) Lock management method, apparatus, and device
US7895260B2 (en) Processing data access requests among a plurality of compute nodes
US7543295B2 (en) Method for enhancing efficiency in mutual exclusion
CN108885559B (en) Fast transfer of workload between multiple processors
US8707315B2 (en) Method and system for implementing realtime spinlocks
US9984009B2 (en) Dynamic containerized system memory protection for low-energy MCUs
US10599468B2 (en) Housekeeping virtual processor overcommit for real time virtualization
WO2023103296A1 (en) Write data cache method and system, device, and storage medium
US9229765B2 (en) Guarantee real time processing of soft real-time operating system by instructing core to enter a waiting period prior to transferring a high priority task
CN107562685B (en) Method for data interaction between multi-core processor cores based on delay compensation
JP3889879B2 (en) How to control virtual memory translation
CN112306699B (en) Method and device for accessing critical resources, computer equipment and readable storage medium
JPH1115793A (en) Protection method for resource maintainability
CN116244229B (en) Access method and device of hardware controller, storage medium and electronic equipment
US10459747B2 (en) Exitless timer access for virtual machines
US20220035664A1 (en) Reverse restartable sequences for lock polling scalability
CN112306652A (en) Wake-up and scheduling with context-aware functionality
WO2022042127A1 (en) Coroutine switching method and apparatus, and device
JP3546694B2 (en) Multi-thread computer system and multi-thread execution control method
CN111290983A (en) USB transmission equipment and transmission method
CN111989651A (en) Method and device for managing kernel service in multi-core system
JP6364827B2 (en) Information processing apparatus, resource access method thereof, and resource access program
CN117149472B (en) Communication method, communication device, computer readable storage medium, and electronic apparatus
US8135878B1 (en) Method and apparatus for improving throughput on a common bus
JP7449308B2 (en) Lock circuit for contention kernels during hardware acceleration

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

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 21831930

Country of ref document: EP

Kind code of ref document: A1