CN111984428A - Method, device and equipment for realizing spin lock during resource access - Google Patents

Method, device and equipment for realizing spin lock during resource access Download PDF

Info

Publication number
CN111984428A
CN111984428A CN202010697107.1A CN202010697107A CN111984428A CN 111984428 A CN111984428 A CN 111984428A CN 202010697107 A CN202010697107 A CN 202010697107A CN 111984428 A CN111984428 A CN 111984428A
Authority
CN
China
Prior art keywords
spin lock
resource
target resource
resource access
lock
Prior art date
Legal status (The legal status 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 status listed.)
Pending
Application number
CN202010697107.1A
Other languages
Chinese (zh)
Inventor
朱鸿斌
宫耀东
夏之春
左若愚
刘艳云
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shanghai Kingstar Fintech Co Ltd
Original Assignee
Shanghai Kingstar Fintech Co Ltd
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 Shanghai Kingstar Fintech Co Ltd filed Critical Shanghai Kingstar Fintech Co Ltd
Priority to CN202010697107.1A priority Critical patent/CN111984428A/en
Publication of CN111984428A publication Critical patent/CN111984428A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/524Deadlock detection or avoidance
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/526Mutual exclusion algorithms

Landscapes

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

Abstract

The application is applicable to the technical field of computers, and provides a method for realizing spin lock during resource access, which comprises the following steps: acquiring a resource access request, and locking a spin lock corresponding to a target resource according to the identification of the target resource; if the spin lock locking fails and the cycle number is smaller than a preset cycle threshold, adding 1 to the cycle number, and returning to execute the spin lock corresponding to the target resource locking according to the identification of the target resource; and if the spin lock is successfully locked, executing access operation corresponding to the resource access request on the target resource. According to the scheme, a preset cycle threshold value is set, if the spin lock fails to be locked and the cycle number is smaller than the preset cycle threshold value, the cycle number is increased by 1, the spin lock corresponding to the target resource is returned to be locked according to the identification of the target resource, the spin lock firstly spins for a certain number of times, the lock is tried to be obtained, the spin exceeds a certain number of times, the lock is not obtained, and the situation that the spin lock always spins for waiting to cause the competition of the central processing unit resources is avoided.

Description

Method, device and equipment for realizing spin lock during resource access
Technical Field
The application belongs to the technical field of computers, and particularly relates to a method, a device and equipment for realizing a spin lock during resource access.
Background
In order to ensure the consistency of shared resources when accessing resources, a resource lock is usually used to control multiple threads or processes. When the range of the critical area is larger, the probability of conflict occurring when accessing the shared resources of the critical area is larger, the waiting time for acquiring the lock is longer, and the exclusive lock can be adopted in the occasion. When the range of the critical area is smaller, the probability of conflict when the threads access the shared resources of the critical area is smaller, the waiting time for acquiring the lock is also shorter, and the spin lock can be adopted in the occasion.
However, when the critical area range is small but access conflicts of shared resources are frequent, the existing spin lock or the existing mutual exclusion lock cannot be well controlled, which causes excessive consumption of the central processing unit and waste of system resources.
Disclosure of Invention
The embodiment of the application provides a method, a device and equipment for realizing a spin lock during resource access, which can solve the problems that the conventional spin lock or the conventional mutual exclusion lock cannot be well controlled, so that a central processing unit is excessively consumed and system resources are wasted.
In a first aspect, an embodiment of the present application provides a method for implementing a spin lock during resource access, including:
acquiring a resource access request, wherein the resource access request comprises an identifier of a target resource;
locking the spin lock corresponding to the target resource according to the identification of the target resource;
if the spin lock is failed to be locked and the cycle number is smaller than a preset cycle threshold, adding 1 to the cycle number, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource;
and if the spin lock is successfully locked, executing access operation corresponding to the resource access request on the target resource.
Further, after the locking the spin lock corresponding to the target resource according to the identifier of the target resource, the method further includes:
and if the spin lock fails to be locked and the cycle number is equal to the preset cycle threshold value, putting the task corresponding to the resource access request into a preset Central Processing Unit (CPU) scheduling queue.
Further, after the task corresponding to the resource access request is placed in a preset central processing unit CPU scheduling queue if the spin lock fails to be locked and the number of cycles is equal to the preset cycle threshold, the method further includes:
and if the CPU is in a preset state, returning to the spin lock corresponding to the target resource according to the identification of the target resource.
Further, before the locking the spin lock corresponding to the target resource according to the identifier of the target resource, the method further includes:
acquiring a spin lock initialization strategy;
initializing the state information of the spin lock according to the spin lock initialization strategy; the state information is used for identifying whether the spin lock is locked.
Further, after the performing, if the spin lock is successfully locked, an access operation corresponding to the resource access request on the target resource, the method further includes:
and when the execution of the access operation corresponding to the resource access request is completed, restoring the state information of the spin lock to an initial value.
Further, after the restoring the state information of the spin lock to the initial value when the execution of the access operation corresponding to the resource access request is completed, the method further includes:
and when the target resource is detected to be deleted, destroying the spin lock and the state information thereof.
In a second aspect, an embodiment of the present application provides an apparatus for implementing a spin lock during resource access, including:
a first obtaining unit, configured to obtain a resource access request, where the resource access request includes an identifier of a target resource;
the first locking unit is used for locking the spin lock corresponding to the target resource according to the identification of the target resource;
the first processing unit is used for adding 1 to the cycle number if the spin lock fails to be locked and the cycle number is smaller than a preset cycle threshold value, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource;
and the second processing unit is used for executing the access operation corresponding to the resource access request on the target resource if the spin lock is successfully locked.
Further, still include:
and the third processing unit is used for placing the task corresponding to the resource access request into a preset Central Processing Unit (CPU) scheduling queue if the spin lock fails to be locked and the cycle number is equal to the preset cycle threshold value.
Further, still include:
and the fourth processing unit is used for returning to the spin lock corresponding to the target resource according to the identifier of the target resource if the CPU is in a preset state.
Further, still include:
the second acquisition unit is used for acquiring a spin lock initialization strategy;
the fifth processing unit is used for initializing the state information of the spin lock according to the spin lock initialization strategy; the state information is used for identifying whether the spin lock is locked.
Further, still include:
and the sixth processing unit is used for restoring the state information of the spin lock to an initial value when the execution of the access operation corresponding to the resource access request is completed.
Further, still include:
and the seventh processing unit is used for destroying the spin lock and the state information thereof when detecting that the target resource is deleted.
In a fourth aspect, an embodiment of the present application provides an apparatus for implementing a spinlock during resource access, including a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements the method for implementing a spinlock during resource access according to the first aspect when executing the computer program.
In a fourth aspect, the present application provides a computer-readable storage medium, where a computer program is stored, and when executed by a processor, the computer program implements the method for implementing a spinlock during resource access according to the first aspect.
In the embodiment of the application, a resource access request is obtained, wherein the resource access request comprises an identifier of a target resource; locking the spin lock corresponding to the target resource according to the identification of the target resource; if the spin lock is failed to be locked and the cycle number is smaller than a preset cycle threshold, adding 1 to the cycle number, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource; and if the spin lock is successfully locked, executing access operation corresponding to the resource access request on the target resource. According to the scheme, the preset cycle threshold value is set, if the spin lock is failed to be locked and the cycle number is smaller than the preset cycle threshold value, the cycle number is increased by 1, the spin lock corresponding to the target resource is returned to be locked according to the identification of the target resource, the spin lock firstly spins for a certain number of times, the lock is tried to be obtained, the spin exceeds a certain number of times, the lock is not obtained, and the situation that the spin lock always spins for waiting to cause the CPU resource to be contended for is avoided.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiments or the prior art descriptions will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
FIG. 1 is a schematic flow chart diagram illustrating a method for implementing a spin lock during resource access according to a first embodiment of the present application;
FIG. 2 is a schematic flow chart of a method for implementing a spin lock during resource access according to a second embodiment of the present application;
FIG. 3 is a diagram illustrating an apparatus for implementing a spin lock during resource access according to a third embodiment of the present application;
fig. 4 is a schematic diagram of an apparatus for implementing a spin lock during resource access according to a fourth embodiment of the present application.
Detailed Description
In the following description, for purposes of explanation and not limitation, specific details are set forth, such as particular system structures, techniques, etc. in order to provide a thorough understanding of the embodiments of the present application. It will be apparent, however, to one skilled in the art that the present application may be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present application with unnecessary detail.
It will be understood that the terms "comprises" and/or "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It should also be understood that the term "and/or" as used in this specification and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
As used in this specification and the appended claims, the term "if" may be interpreted contextually as "when", "upon" or "in response to" determining "or" in response to detecting ". Similarly, the phrase "if it is determined" or "if a [ described condition or event ] is detected" may be interpreted contextually to mean "upon determining" or "in response to determining" or "upon detecting [ described condition or event ]" or "in response to detecting [ described condition or event ]".
Furthermore, in the description of the present application and the appended claims, the terms "first," "second," "third," and the like are used for distinguishing between descriptions and not necessarily for describing or implying relative importance.
Reference throughout this specification to "one embodiment" or "some embodiments," or the like, means that a particular feature, structure, or characteristic described in connection with the embodiment is included in one or more embodiments of the present application. Thus, appearances of the phrases "in one embodiment," "in some embodiments," "in other embodiments," or the like, in various places throughout this specification are not necessarily all referring to the same embodiment, but rather "one or more but not all embodiments" unless specifically stated otherwise. The terms "comprising," "including," "having," and variations thereof mean "including, but not limited to," unless expressly specified otherwise.
Referring to fig. 1, fig. 1 is a schematic flow chart of a method for implementing a spin lock during resource access according to a first embodiment of the present application. In this embodiment, an execution main body of the method for implementing the spin lock during resource access is a device, such as a desktop computer, a server, and the like, having the function of implementing the spin lock during resource access. The method for implementing the spin lock during resource access as shown in FIG. 1 may include:
s101: acquiring a resource access request, wherein the resource access request comprises an identifier of a target resource.
In order to fully utilize system hardware resources, a common architecture scheme of an application system at present adopts a multi-thread or multi-process structure. In the design and implementation process of a system with a multi-thread or multi-process structure, a very important task is to design a synchronous access mechanism of shared resources among threads or processes. The principle is to reduce shared resources as much as possible, and the most ideal state is that each thread or process can work independently; if the essential shared resource exists, the access conflict needs to be avoided as much as possible, and a common method is to reduce the range of the critical section for accessing the shared resource as much as possible, and the smaller the range of the critical section is, the smaller the probability of the access conflict is.
If the range of the critical zone is larger, the probability of conflict occurring when the threads access the shared resources of the critical zone is larger, the waiting time for acquiring the access control lock is longer, and the exclusive lock is selected in the occasion. If the range of the critical section is smaller, the probability of conflict when the threads access the shared resources of the critical section is smaller, the waiting time for obtaining the access control lock is also shorter, and the spin lock is selected in the occasion.
If the working scene of the application system is that the range of the critical zone is small, the number of the concurrent threads is below the number of CPU cores, but the access conflict of the shared resources is frequent, and the self-locking or mutual exclusion locking is not an optimal scheme. If the mutual exclusion lock is adopted, the thread is suspended when waiting for obtaining the mutual exclusion lock, and the thread is activated when the lock is released. The suspension and activation of the thread involve the queuing queue scheduling of the lock, and at the same time, context switching and cache invalidation occur at the CPU level, thereby affecting the performance of the application system. If the spin lock is adopted, the thread is in a busy state and the like when waiting for obtaining the spin lock, always occupies the CPU, and repeatedly tries to obtain the lock. If the number of threads accessing the critical section is large, shared resource access conflicts are frequent, which causes a large amount of CPU resource consumption and influences the performance of the application system.
Therefore, the present embodiment provides a method for implementing a spin lock during resource access, so as to reduce excessive consumption of a central processing unit and avoid wasting system resources. The device obtains a resource access request, wherein the resource access request may be generated by a user operating a trigger on the device. The resource access request includes an identification of a target resource on which the data operation is to be performed. The device determines that the resource access request includes the identifier of the target resource according to the resource access request.
S102: and locking the spin lock corresponding to the target resource according to the identifier of the target resource.
The device locks the spin lock corresponding to the target resource according to the identification of the target resource, and the step is the locking of the spin lock. If no execution unit holds the lock while the spin lock is acquired, then the lock will be immediately acquired; if the lock already has a keeper at the time the spin lock is acquired, the operation acquiring the lock will spin there until the keeper of the spin lock releases the lock. The locking of the spin lock is realized by adopting atomic operation, wherein the atomic operation is operation which cannot be interrupted by a thread scheduling mechanism; once started, this operation runs to the end without switching to another thread in the middle. An atomic operation may be one step or multiple operation steps, but the order may not be scrambled or cut to perform only a portion of them. Treating the entire operation as a whole is a core feature of atomicity. By adopting atomic operation, when multiple processes or threads access the shared resource, all other processes or threads can be ensured not to access the same resource at the same time. Under the condition that the concurrency conflict exists but is limited, the access control system of the shared resource is constructed through atomic operation, and the optimal performance can be obtained.
S103: and if the spin lock fails to be locked and the cycle number is less than a preset cycle threshold, adding 1 to the cycle number, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource.
Since the spin lock is locked, if unsuccessful, it will continue to attempt to lock. Therefore, a preset cycle threshold is preset in the device, and the preset cycle threshold is used for limiting the number of times the spin lock tries to lock. If the spin lock locking fails and the cycle number is smaller than the preset cycle threshold value, the description does not reach the maximum cycle number, the locking can be continuously attempted, the cycle number is increased by 1, the spin lock corresponding to the target resource is locked according to the identification of the target resource, and the locking is continuously attempted. The preset loop threshold may be set according to the number of instructions in the critical section, for example, the preset loop threshold may be set to 10 times.
Further, after S103, the method may further include: and if the spin lock fails to be locked and the cycle number is equal to the preset cycle threshold value, putting the task corresponding to the resource access request into a preset Central Processing Unit (CPU) scheduling queue. In this embodiment, if the spin lock fails to lock and the cycle number is equal to the preset cycle threshold, and the spin lock still fails to acquire the preset number, the task corresponding to the resource access request is placed in the preset central processing unit CPU scheduling queue. And the CPU can be yielded with the minimum cost by calling a system function, and the task corresponding to the resource access request is put into a preset central processing unit CPU scheduling queue. In this embodiment, the friendly spin lock selects to yield the CPU. The yielding CPU is not suspended into the lock waiting queue like a mutual exclusion lock, but enters into a scheduling queue of the CPU, so that the thread or the process can try to acquire the lock again as soon as possible.
Further, after the task corresponding to the resource access request is placed in a preset central processing unit CPU scheduling queue if the spin lock fails to be locked and the number of cycles is equal to the preset cycle threshold, the method may further include: and if the CPU is in a preset state, returning to the spin lock corresponding to the target resource according to the identification of the target resource. The equipment detects the state of the CPU in real time, sets the preset state of the CPU to be an idle state, and the standard of the idle state can be set according to the actual situation. And if the CPU is in a preset state, returning to locking the spin lock corresponding to the target resource according to the identification of the target resource. The system function immediately returns and the spinlock resumes attempting to acquire the lock. The lock acquisition strategy fully considers the reasonable use of hardware resources, and can effectively improve the performance of an application system.
S104: and if the spin lock is successfully locked, executing access operation corresponding to the resource access request on the target resource.
And if the equipment detects that the spin lock is successfully locked, executing access operation corresponding to the resource access request on the target resource.
In the embodiment of the application, a resource access request is obtained, wherein the resource access request comprises an identifier of a target resource; locking the spin lock corresponding to the target resource according to the identification of the target resource; if the spin lock is failed to be locked and the cycle number is smaller than a preset cycle threshold, adding 1 to the cycle number, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource; and if the spin lock is successfully locked, executing access operation corresponding to the resource access request on the target resource. According to the scheme, the preset cycle threshold value is set, if the spin lock is failed to be locked and the cycle number is smaller than the preset cycle threshold value, the cycle number is increased by 1, the spin lock corresponding to the target resource is returned to be locked according to the identification of the target resource, the spin lock firstly spins for a certain number of times, the lock is tried to be obtained, the spin exceeds a certain number of times, the lock is not obtained, and the situation that the spin lock always spins for waiting to cause the CPU resource to be contended for is avoided.
Referring to fig. 2, fig. 2 is a schematic flow chart of a method for implementing a spin lock during resource access according to a second embodiment of the present application. In this embodiment, an execution main body of the method for implementing the spin lock during resource access is a device, such as a desktop computer, a server, and the like, having the function of implementing the spin lock during resource access. The difference between this embodiment and the first embodiment is S202 to S203, in this embodiment, S201 is the same as S101, S204 is the same as S206, and S202 to S203 may be executed after S204. As shown in fig. 2, S202 to S203 are specifically as follows:
s202: and acquiring a spin lock initialization strategy.
In this embodiment, the method supports cross-platform use, so when performing spin lock initialization, it needs to be determined according to a platform of an operating system. The initialization operation of the spin lock is mainly to create a locking anchor point in a shared memory, the strategies for creating the locking anchor point on different platforms are different, and the equipment acquires a spin lock initialization strategy corresponding to the current platform.
S203: initializing the state information of the spin lock according to the spin lock initialization strategy; the state information is used for identifying whether the spin lock is locked.
The equipment initializes the spin lock according to a spin lock initialization strategy to obtain state information of the spin lock, for example, in a Windows platform, a shared memory creation method adopts a memory mapping mode, and in a Unix-like platform, a shared memory creation method adopts a System V mode. After applying for the shared memory, an anchor point is created in the shared memory area, the anchor point is of a long data type, the initialization of the spin lock is completed, and the state information of the spin lock is obtained. Wherein the state information is used to identify whether the spin lock is locked. For example, when the values of the state information are 0 and 1, the lock successful state and the lock failed state of the spin lock may be respectively corresponded.
Further, after S206, the method may further include: and when the execution of the access operation corresponding to the resource access request is completed, restoring the state information of the spin lock to an initial value. And when the access operation corresponding to the resource access request is completed, restoring the state information of the spin lock to an initial value, namely releasing the spin lock, wherein the release of the spin lock corresponds to the locking of the spin lock, and the release is realized by adopting the atomic operation of each operating system platform.
Further, after restoring the state information of the spin lock to the initial value when the execution of the access operation corresponding to the resource access request is completed, the method may further include: and when the target resource is detected to be deleted, destroying the spin lock and the state information thereof. After the spin lock is used, the applied resources need to be recovered during initialization. The main task of destroying the spin lock is to recycle the shared memory. And when the target resource is detected to be deleted, namely the spin lock of the target resource is used up, destroying the spin lock and the state information thereof.
It should be understood that, the sequence numbers of the steps in the foregoing embodiments do not imply an execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments of the present application.
Referring to fig. 3, fig. 3 is a schematic diagram of an apparatus for implementing a spin lock during resource access according to a third embodiment of the present application. The included units are used for executing steps in the embodiments corresponding to fig. 1 to fig. 2, and refer to the related descriptions in the embodiments corresponding to fig. 1 to fig. 2. For convenience of explanation, only the portions related to the present embodiment are shown. Referring to fig. 3, the spin lock implementation apparatus 3 for resource access includes:
a first obtaining unit 310, configured to obtain a resource access request, where the resource access request includes an identifier of a target resource;
a first locking unit 320, configured to lock a spin lock corresponding to the target resource according to the identifier of the target resource;
the first processing unit 330 is configured to add 1 to the cycle number if the spin lock fails to be locked and the cycle number is smaller than a preset cycle threshold, and return to execute the spin lock corresponding to the target resource being locked according to the identifier of the target resource;
a second processing unit 340, configured to, if the spin lock is successfully locked, execute an access operation corresponding to the resource access request on the target resource.
Further, still include:
and the third processing unit is used for placing the task corresponding to the resource access request into a preset Central Processing Unit (CPU) scheduling queue if the spin lock fails to be locked and the cycle number is equal to the preset cycle threshold value.
Further, still include:
and the fourth processing unit is used for returning to the spin lock corresponding to the target resource according to the identifier of the target resource if the CPU is in a preset state.
Further, still include:
the second acquisition unit is used for acquiring a spin lock initialization strategy;
the fifth processing unit is used for initializing the state information of the spin lock according to the spin lock initialization strategy; the state information is used for identifying whether the spin lock is locked.
Further, still include:
and the sixth processing unit is used for restoring the state information of the spin lock to an initial value when the execution of the access operation corresponding to the resource access request is completed.
Further, still include:
and the seventh processing unit is used for destroying the spin lock and the state information thereof when detecting that the target resource is deleted.
Fig. 4 is a schematic diagram of an apparatus for implementing a spin lock during resource access according to a fourth embodiment of the present application. As shown in fig. 4, the device 4 for implementing a spin lock during resource access according to this embodiment includes: a processor 40, a memory 41 and a computer program 42 stored in said memory 41 and executable on said processor 40, such as a program for implementing a spin lock upon resource access. The processor 40 executes the computer program 42 to implement the steps in the above-mentioned embodiment of the spin lock implementation method for resource access, for example, the steps 101 to 104 shown in fig. 1. Alternatively, the processor 40, when executing the computer program 42, implements the functions of the modules/units in the above-mentioned device embodiments, such as the functions of the modules 310 to 340 shown in fig. 3.
Illustratively, the computer program 42 may be partitioned into one or more modules/units that are stored in the memory 41 and executed by the processor 40 to accomplish the present application. The one or more modules/units may be a series of computer program instruction segments capable of performing specific functions, which are used to describe the execution process of the computer program 42 in the spin lock implementing device 4 at the time of the resource access. For example, the computer program 42 may be divided into a first acquiring unit, a first locking unit, a first processing unit, and a second processing unit, and the specific functions of each unit are as follows:
a first obtaining unit, configured to obtain a resource access request, where the resource access request includes an identifier of a target resource;
the first locking unit is used for locking the spin lock corresponding to the target resource according to the identification of the target resource;
the first processing unit is used for adding 1 to the cycle number if the spin lock fails to be locked and the cycle number is smaller than a preset cycle threshold value, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource;
and the second processing unit is used for executing the access operation corresponding to the resource access request on the target resource if the spin lock is successfully locked.
The device for implementing the spin lock during resource access may include, but is not limited to, a processor 40 and a memory 41. It will be understood by those skilled in the art that fig. 4 is merely an example of the spin lock implementing device 4 at the time of resource access, does not constitute a limitation of the spin lock implementing device 4 at the time of resource access, and may include more or less components than those shown, or combine some components, or different components, for example, the spin lock implementing device at the time of resource access may further include an input-output device, a network access device, a bus, etc.
The Processor 40 may be a Central Processing Unit (CPU), other general purpose Processor, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), an off-the-shelf Programmable Gate Array (FPGA) or other Programmable logic device, discrete Gate or transistor logic, discrete hardware components, etc. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The storage 41 may be an internal storage unit of the spin lock implementation device 4 during resource access, for example, a hard disk or a memory of the spin lock implementation device 4 during resource access. The memory 41 may also be an external storage device of the spin lock implementation device 4 during resource access, for example, a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), or the like, which is equipped on the spin lock implementation device 4 during resource access. Further, the memory 41 may also include both an internal storage unit of the spin lock implementation apparatus 4 and an external storage device at the time of the resource access. The memory 41 is used for storing the computer program and other programs and data required by the implementation device of the spin lock at the time of the resource access. The memory 41 may also be used to temporarily store data that has been output or is to be output.
It will be apparent to those skilled in the art that, for convenience and brevity of description, only the above-mentioned division of the functional units and modules is illustrated, and in practical applications, the above-mentioned function distribution may be performed by different functional units and modules according to needs, that is, the internal structure of the apparatus is divided into different functional units or modules to perform all or part of the above-mentioned functions. Each functional unit and module in the embodiments may be integrated in one processing unit, or each unit may exist alone physically, or two or more units are integrated in one unit, and the integrated unit may be implemented in a form of hardware, or in a form of software functional unit. In addition, specific names of the functional units and modules are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the present application. The specific working processes of the units and modules in the system may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
In the embodiments provided in the present application, it should be understood that the disclosed apparatus/terminal device and method may be implemented in other ways. For example, the above-described embodiments of the apparatus/terminal device are merely illustrative, and for example, the division of the modules or units is only one logical division, and there may be other divisions when actually implemented, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated modules/units, if implemented in the form of software functional units and sold or used as separate products, may be stored in a computer readable storage medium. Based on such understanding, all or part of the flow in the method of the embodiments described above can be realized by a computer program, which can be stored in a computer-readable storage medium and can realize the steps of the embodiments of the methods described above when the computer program is executed by a processor. . Wherein the computer program comprises computer program code, which may be in the form of source code, object code, an executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, usb disk, removable hard disk, magnetic disk, optical disk, computer Memory, Read-Only Memory (ROM), Random Access Memory (RAM), electrical carrier wave signals, telecommunications signals, software distribution medium, and the like. It should be noted that the computer readable medium may contain content that is subject to appropriate increase or decrease as required by legislation and patent practice in jurisdictions, for example, in some jurisdictions, computer readable media does not include electrical carrier signals and telecommunications signals as is required by legislation and patent practice.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present application, and not for limiting the same; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications and substitutions do not substantially depart from the spirit and scope of the embodiments of the present application and are intended to be included within the scope of the present application.

Claims (10)

1. A method for realizing spin lock during resource access is characterized by comprising the following steps:
acquiring a resource access request, wherein the resource access request comprises an identifier of a target resource;
locking the spin lock corresponding to the target resource according to the identification of the target resource;
if the spin lock is failed to be locked and the cycle number is smaller than a preset cycle threshold, adding 1 to the cycle number, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource;
and if the spin lock is successfully locked, executing access operation corresponding to the resource access request on the target resource.
2. The method for implementing a spinlock during resource access as claimed in claim 1, wherein after said locking the spinlock corresponding to the target resource according to the identification of the target resource, further comprising:
and if the spin lock fails to be locked and the cycle number is equal to the preset cycle threshold value, putting the task corresponding to the resource access request into a preset Central Processing Unit (CPU) scheduling queue.
3. The method for implementing a spin lock during resource access according to claim 2, wherein after the task corresponding to the resource access request is placed in a preset Central Processing Unit (CPU) scheduling queue if the spin lock fails and the number of cycles is equal to the preset cycle threshold, the method further comprises:
and if the CPU is in a preset state, returning to the spin lock corresponding to the target resource according to the identification of the target resource.
4. The method for implementing a spinlock during resource access as claimed in claim 1, wherein before said locking the spinlock corresponding to the target resource according to the identification of the target resource, further comprising:
acquiring a spin lock initialization strategy;
initializing the state information of the spin lock according to the spin lock initialization strategy; the state information is used for identifying whether the spin lock is locked.
5. The method for implementing a spinlock on resource access of claim 4, wherein after the performing the access operation corresponding to the resource access request on the target resource if the spinlock locking is successful, further comprising:
and when the execution of the access operation corresponding to the resource access request is completed, restoring the state information of the spin lock to an initial value.
6. The method for implementing a spin lock during resource access according to claim 5, wherein after restoring the state information of the spin lock to the initial value when the execution of the access operation corresponding to the resource access request is completed, further comprising:
and when the target resource is detected to be deleted, destroying the spin lock and the state information thereof.
7. An apparatus for implementing a spin lock during resource access, comprising:
a first obtaining unit, configured to obtain a resource access request, where the resource access request includes an identifier of a target resource;
the first locking unit is used for locking the spin lock corresponding to the target resource according to the identification of the target resource;
the first processing unit is used for adding 1 to the cycle number if the spin lock fails to be locked and the cycle number is smaller than a preset cycle threshold value, and returning to execute the spin lock corresponding to the target resource locked according to the identification of the target resource;
and the second processing unit is used for executing the access operation corresponding to the resource access request on the target resource if the spin lock is successfully locked.
8. An apparatus for implementing a spinlock on resource access as recited in claim 7, further comprising:
and the third processing unit is used for returning to the spin lock corresponding to the target resource according to the identifier of the target resource if the CPU is in a preset state.
9. An apparatus for implementing a spinlock on resource access, comprising a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the method of any of claims 1 to 5 when executing the computer program.
10. A computer-readable storage medium, in which a computer program is stored which, when being executed by a processor, carries out the method according to any one of claims 1 to 5.
CN202010697107.1A 2020-07-20 2020-07-20 Method, device and equipment for realizing spin lock during resource access Pending CN111984428A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010697107.1A CN111984428A (en) 2020-07-20 2020-07-20 Method, device and equipment for realizing spin lock during resource access

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010697107.1A CN111984428A (en) 2020-07-20 2020-07-20 Method, device and equipment for realizing spin lock during resource access

Publications (1)

Publication Number Publication Date
CN111984428A true CN111984428A (en) 2020-11-24

Family

ID=73439214

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010697107.1A Pending CN111984428A (en) 2020-07-20 2020-07-20 Method, device and equipment for realizing spin lock during resource access

Country Status (1)

Country Link
CN (1) CN111984428A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113806099A (en) * 2021-09-13 2021-12-17 北京计算机技术及应用研究所 Multi-core spin lock design method based on binary computation
CN116028204A (en) * 2022-05-16 2023-04-28 荣耀终端有限公司 Process management and control method and electronic equipment

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080177955A1 (en) * 2007-01-23 2008-07-24 International Business Machines Corporation Achieving Both Locking Fairness and Locking Performance with Spin Locks
CN102129391A (en) * 2011-03-14 2011-07-20 华中科技大学 Method for implementing spin lock in database
CN105224251A (en) * 2015-09-28 2016-01-06 浪潮(北京)电子信息产业有限公司 A kind of Read-Write Locks optimization method based on Linux and system

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080177955A1 (en) * 2007-01-23 2008-07-24 International Business Machines Corporation Achieving Both Locking Fairness and Locking Performance with Spin Locks
CN102129391A (en) * 2011-03-14 2011-07-20 华中科技大学 Method for implementing spin lock in database
CN105224251A (en) * 2015-09-28 2016-01-06 浪潮(北京)电子信息产业有限公司 A kind of Read-Write Locks optimization method based on Linux and system

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
吉林乌拉: "自旋锁", pages 1, Retrieved from the Internet <URL:https://cloud.tencent.com/developer/article/1487113> *

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113806099A (en) * 2021-09-13 2021-12-17 北京计算机技术及应用研究所 Multi-core spin lock design method based on binary computation
CN116028204A (en) * 2022-05-16 2023-04-28 荣耀终端有限公司 Process management and control method and electronic equipment
CN116028204B (en) * 2022-05-16 2023-10-20 荣耀终端有限公司 Process management and control method and electronic equipment

Similar Documents

Publication Publication Date Title
US11314562B2 (en) Systems and methods for performing concurrency restriction and throttling over contended locks
US9342384B1 (en) Function callback mechanism between a central processing unit (CPU) and an auxiliary processor
CN106716348B (en) Shared resources in a data processing apparatus for executing multiple threads
CN109213576B (en) Program deadlock detection method, storage medium, device and system
CN105359101B (en) System management interrupt handling for multi-core processors
CN111984428A (en) Method, device and equipment for realizing spin lock during resource access
CN106681836B (en) Semaphore creation method and semaphore creation device
RU2746155C2 (en) Destroying an object based on the sequence of actions performed
CN112416556B (en) Data read-write priority balancing method, system, device and storage medium
CN115658153A (en) Sleep lock optimization method and device, electronic equipment and storage medium
CN107305507B (en) Deadlock control method and device
CN111124622A (en) Method for accessing critical resources in multithreading mode, storage medium and intelligent terminal
CN111930528A (en) Message writing method, device and equipment of message middleware and readable storage medium
US10310916B2 (en) Scalable spinlocks for non-uniform memory access
US7689992B2 (en) Sharing lock mechanism between protocol layers
CN113961364A (en) Large-scale lock system implementation method and device, storage medium and server
CN110543373B (en) Method for accessing kernel by user thread
US7996848B1 (en) Systems and methods for suspending and resuming threads
CN111026526B (en) Timer configuration method and device for program, storage medium and terminal equipment
CN116010040A (en) Method, device and equipment for acquiring lock resources
CN112231131B (en) Method, device and equipment for realizing database lock and readable storage medium
CN113760492A (en) Program process keep-alive method, system, device, equipment and storage medium
CN111723250A (en) Linked list management method based on reference counting
US8214834B2 (en) Asynchronous wakeup mechanism that places reference to process on sleep queue if execution of copyout functionality is caused to sleep
CN111767155A (en) Deadlock detection method, device, equipment and computer readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination