技术领域technical field
本发明涉及计算机技术领域,尤其涉及一种获取自旋锁的方法及装置。The invention relates to the field of computer technology, in particular to a method and device for acquiring a spin lock.
背景技术Background technique
随着CPU多核及并行技术的发展,在采用NC(Node Controller,节点控制器)的多节点大型NUMA(Non Uniform Memory AccessAchitecture,非统一内存访问)系统中,为了保证共享资源的一致性,通常使用自旋锁的机制进行多事务并发操作的控制。With the development of CPU multi-core and parallel technology, in the multi-node large-scale NUMA (Non Uniform Memory Access Architecture, non-uniform memory access) system using NC (Node Controller, node controller), in order to ensure the consistency of shared resources, usually use The spinlock mechanism controls the concurrent operations of multiple transactions.
现有技术中,服务器可将申请获取自旋锁的多个任务组成一个链表,并在该多个任务所在的处理器的缓存中,均保存一个相同的且由自旋锁保护的本地变量,从而服务器可以指示该多个任务均按照预先设定的时间间隔周期性的尝试获取该自旋锁,以访问该本地变量。In the prior art, the server can form a linked list of multiple tasks that apply for the spin lock, and store the same local variable protected by the spin lock in the cache of the processor where the multiple tasks are located. Therefore, the server may instruct the multiple tasks to periodically try to acquire the spin lock according to a preset time interval, so as to access the local variable.
然而,在上述获取自旋锁的方法中,无论是否轮到其获取该自旋锁,链表中的任务均会周期性的尝试获取该自旋锁,随着链表中的任务数量的增加,将导致申请获取自旋锁的次数较多,并增加了系统的性能损耗。However, in the above method of acquiring the spin lock, no matter whether it is its turn to acquire the spin lock or not, the tasks in the linked list will periodically try to acquire the spin lock. As the number of tasks in the linked list increases, the This results in more requests to acquire spin locks and increases the performance loss of the system.
发明内容Contents of the invention
本发明的实施例提供一种获取自旋锁的方法及装置,能够降低由于多个任务竞争共享资源而造成的系统的性能损耗,从而提高系统的性能。Embodiments of the present invention provide a method and device for acquiring a spin lock, which can reduce system performance loss caused by multiple tasks competing for shared resources, thereby improving system performance.
为达到上述目的,本发明的实施例采用如下技术方案:In order to achieve the above object, embodiments of the present invention adopt the following technical solutions:
第一方面,本发明实施例提供一种获取自旋锁的方法,包括:In the first aspect, the embodiment of the present invention provides a method for acquiring a spin lock, including:
接收请求获取自旋锁的任务;Receive the task of requesting to acquire the spin lock;
将所述任务保存在排队队列中;saving said task in a queuing queue;
根据所述任务在所述排队队列中的位置,确定所述任务的自旋等待时间,以使得所述任务在所述自旋等待时间结束时,尝试获取所述自旋锁;determining the spin waiting time of the task according to the position of the task in the queuing queue, so that the task tries to acquire the spin lock when the spin waiting time ends;
其中,所述任务在所述排队队列中的位置用于表征所述任务保存至所述排队队列中的先后顺序,先保存至所述排队队列中的任务的位置在后保存至所述排队队列中的任务的位置之前。Wherein, the position of the tasks in the queuing queue is used to represent the order in which the tasks are saved in the queuing queue, and the positions of the tasks saved in the queuing queue first are saved in the queuing queue before the location of the tasks in the .
在第一方面的第一种可能的实现方式中,所述方法还包括:In a first possible implementation manner of the first aspect, the method further includes:
根据预设检测周期,周期性的检测所述任务的自旋等待时间是否结束。According to a preset detection period, it is periodically detected whether the spin waiting time of the task is over.
结合前述的第一方面或第一方面的第一种可能的实现方式,在第二种可能的实现方式中,当所述任务在所述自旋等待时间结束时,尝试获取所述自旋锁之后,所述方法还包括:In combination with the aforementioned first aspect or the first possible implementation of the first aspect, in a second possible implementation, when the task ends at the end of the spin waiting time, try to acquire the spin lock Afterwards, the method also includes:
若所述任务获取所述自旋锁失败,则重新确定所述任务在所述排队队列中的位置;If the task fails to acquire the spin lock, re-determine the position of the task in the queuing queue;
根据重新确定的所述任务在所述排队队列中的位置,重新确定所述任务的自旋等待时间,以使得所述任务在重新确定的所述自旋等待时间结束时,重新尝试获取所述自旋锁。According to the re-determined position of the task in the queuing queue, re-determine the spin waiting time of the task, so that when the re-determined spin waiting time ends, the task re-attempts to acquire the spin lock.
结合前述的第一方面或第一方面的第一种可能的实现方式至第二种可能的实现方式中的任一种实现方式,在第三种可能的实现方式中,所述方法还包括:In combination with the foregoing first aspect or any one of the first possible implementation manner to the second possible implementation manner of the first aspect, in a third possible implementation manner, the method further includes:
当位于所述排队队列首部的任务释放所述自旋锁时,更新所述任务在所述排队队列中的位置。When the task at the head of the queuing queue releases the spin lock, update the position of the task in the queuing queue.
结合第一方面的第三种可能的实现方式,在第四种可能的实现方式中,所述当位于所述排队队列首部的任务释放所述自旋锁时,更新所述任务在所述排队队列中的位置,具体包括:With reference to the third possible implementation of the first aspect, in a fourth possible implementation, when the task at the head of the queuing queue releases the spin lock, update the task in the queue The position in the queue, specifically:
当位于所述排队队列首部的任务释放所述自旋锁时,将所述任务在所述排队队列中的位置更新为所述任务的前一个任务在所述排队队列中的位置。When the task at the head of the queuing queue releases the spin lock, the position of the task in the queuing queue is updated to the position of a previous task of the task in the queuing queue.
结合前述的第一方面或第一方面的第一种可能的实现方式至第四种可能的实现方式中的任一种实现方式,在第五种可能的实现方式中,所述将所述任务保存在排队队列中,具体包括:In combination with any of the aforementioned first aspect or the first possible implementation manner to the fourth possible implementation manner of the first aspect, in the fifth possible implementation manner, the task Stored in the queuing queue, specifically including:
将所述任务添加至所述排队队列的尾部。Add the task to the tail of the queuing queue.
第二方面,本发明实施例提供一种服务器,包括:In a second aspect, an embodiment of the present invention provides a server, including:
接收单元,用于接收请求获取自旋锁的任务;The receiving unit is used to receive the task of requesting to acquire the spin lock;
确定单元,用于将所述接收单元接收的所述任务保存在排队队列中;a determining unit, configured to store the task received by the receiving unit in a queuing queue;
确定单元,用于根据所述保存单元保存的所述任务在所述排队队列中的位置,确定所述任务的自旋等待时间,以使得所述任务在所述自旋等待时间结束时,尝试获取所述自旋锁,其中,所述任务在所述排队队列中的位置用于表征所述任务保存至所述排队队列中的先后顺序,先保存至所述排队队列中的任务的位置在后保存至所述排队队列中的任务的位置之前。a determining unit, configured to determine the spin waiting time of the task according to the position of the task in the queuing queue saved by the saving unit, so that when the spin waiting time ends, the task tries to Acquiring the spin lock, wherein the position of the task in the queuing queue is used to represent the order in which the tasks are saved in the queuing queue, and the position of the task saved in the queuing queue first is at before saving to the position of the task in the enqueue queue.
在第二方面的第一种可能的实现方式中,所述服务器还包括检测单元,In a first possible implementation manner of the second aspect, the server further includes a detection unit,
所述检测单元,用于根据预设检测周期,周期性的检测所述确定单元确定的所述任务的自旋等待时间是否结束。The detection unit is configured to periodically detect whether the spin waiting time of the task determined by the determination unit is over according to a preset detection period.
结合前述的第二方面或第二方面的第一种可能的实现方式,在第二种可能的实现方式中,In combination with the aforementioned second aspect or the first possible implementation of the second aspect, in the second possible implementation,
所述确定单元,还用于当所述任务在所述自选等待时间结束时,尝试获取所述自旋锁之后,若所述任务获取所述自旋锁失败,则重新确定所述任务在所述排队队列中的位置,并根据重新确定的所述任务在所述排队队列中的位置,重新确定所述任务的自旋等待时间,以使得所述任务在重新确定的所述自旋等待时间结束时,重新尝试获取所述自旋锁。The determining unit is further configured to re-determine that the task is in the specified position if the task fails to acquire the spin lock after the task has tried to acquire the spin lock when the optional waiting time ends. The position in the queuing queue, and according to the re-determined position of the task in the queuing queue, re-determine the spin waiting time of the task, so that the task is within the re-determined spin waiting time When finished, retry to acquire said spinlock.
结合前述的第二方面或第二方面的第一种可能的实现方式至第二种可能的实现方式中的任一种实现方式,在第三种可能的实现方式中,所述服务器还包括更新单元,In combination with the aforementioned second aspect or any one of the first possible implementation manner to the second possible implementation manner of the second aspect, in a third possible implementation manner, the server further includes updating unit,
所述更新单元,用于当位于所述排队队列首部的任务释放所述自旋锁时,更新所述保存单元保存的所述任务在所述排队队列中的位置。The updating unit is configured to update the position of the task in the queuing queue saved by the saving unit when the task at the head of the queuing queue releases the spin lock.
结合第二方面的第三种可能的实现方式,在第四种可能的实现方式中,In combination with the third possible implementation of the second aspect, in the fourth possible implementation,
所述更新单元,具体用于当位于所述排队队列首部的任务释放所述自旋锁时,将所述任务在所述排队队列中的位置更新为所述任务的前一个任务在所述排队队列中的位置。The updating unit is specifically configured to update the position of the task in the queuing queue so that the previous task of the task is in the queuing queue when the task at the head of the queuing queue releases the spin lock. position in the queue.
结合前述的第二方面或第二方面的第一种可能的实现方式至第四种可能的实现方式中的任一种实现方式,在第五种可能的实现方式中,Combining the aforementioned second aspect or any one of the first possible implementation manner to the fourth possible implementation manner of the second aspect, in the fifth possible implementation manner,
所述保存单元,具体用于将所述接收单元接收的所述任务添加至所述排队队列的尾部。The saving unit is specifically configured to add the task received by the receiving unit to the tail of the queuing queue.
本发明的实施例提供一种获取自旋锁的方法及装置,通过接收请求获取自旋锁的任务,并将该任务保存在排队队列中,以及根据该任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。通过该方案,可以根据接收到的任务在排队队列中的位置,计算该任务的自旋等待时间,即尝试获取自旋锁的时间间隔,以使得申请获取自旋锁的多个该任务的自旋等待时间得到动态调整,从而使得排队队列中保存的请求获取自旋锁的多个任务的自旋等待时间均可以根据其在排队队列中的位置得到动态调整,因此,解决了现有技术中多个任务均每隔预先设定的时间间隔尝试获取自旋锁的问题,能够降低由于多个任务竞争共享资源而造成的系统的性能损耗,从而提高系统的性能。Embodiments of the present invention provide a method and device for acquiring a spin lock, by receiving a task that requests to acquire a spin lock, storing the task in the queuing queue, and determining the task according to the position of the task in the queuing queue The spin waiting time of the task, so that the task tries to acquire the spin lock when the spin waiting time ends. The position of the task in the queuing queue is used to represent the order in which the tasks are saved in the queuing queue. The position of the task saved to the queuing queue is before the position of the task saved to the queuing queue. Through this scheme, the spin waiting time of the task can be calculated according to the position of the received task in the queuing queue, that is, the time interval for trying to acquire the spin lock, so that the spin of multiple tasks that apply for the spin lock The spin waiting time is dynamically adjusted, so that the spin waiting time of multiple tasks that request to acquire the spin lock stored in the queuing queue can be dynamically adjusted according to their positions in the queuing queue. Therefore, the problems in the prior art are solved. The problem that multiple tasks try to acquire the spin lock at a preset time interval can reduce system performance loss caused by multiple tasks competing for shared resources, thereby improving system performance.
附图说明Description of drawings
图1为本发明实施例提供的一种获取自旋锁的方法的流程图一;Fig. 1 is a flow chart 1 of a method for obtaining a spin lock provided by an embodiment of the present invention;
图2为本发明实施例提供的一种获取自旋锁的方法的流程图二;FIG. 2 is a second flow chart of a method for acquiring a spin lock provided by an embodiment of the present invention;
图3为本发明实施例提供的服务器的结构示意图一;FIG. 3 is a schematic structural diagram of a server provided by an embodiment of the present invention;
图4为本发明实施例提供的服务器的结构示意图二;FIG. 4 is a second structural diagram of a server provided by an embodiment of the present invention;
图5为本发明实施例提供的服务器的结构示意图三;FIG. 5 is a third schematic structural diagram of a server provided by an embodiment of the present invention;
图6为本发明实施例提供的服务器的结构示意图四。FIG. 6 is a fourth structural schematic diagram of a server provided by an embodiment of the present invention.
具体实施方式Detailed ways
下面结合附图对本发明实施例提供的一种获取自旋锁的方法及装置进行详细地描述。A method and device for acquiring a spinlock provided by an embodiment of the present invention will be described in detail below with reference to the accompanying drawings.
实施例一Embodiment one
本发明实施例提供一种获取自旋锁的方法,如图1所示,该方法可以包括:The embodiment of the present invention provides a method for obtaining a spin lock, as shown in Figure 1, the method may include:
S101、服务器接收请求获取自旋锁的任务。S101. The server receives a task requesting to acquire a spin lock.
示例性的,若某一个任务需访问自旋锁保护的共享资源,该任务需向服务器请求获取该自旋锁,服务器首先可接收该任务。Exemplarily, if a certain task needs to access a shared resource protected by a spin lock, the task needs to request the server to obtain the spin lock, and the server can first receive the task.
需要说明的是,自旋锁是为防止多处理器并发而引入的一种锁,它在内核中大量应用于中断处理等部分(对于单处理器来说,防止中断处理中的并发可简单采用关闭中断的方式,不需要自旋锁)。在任何时刻,自旋锁最多只能被一个任务持有,若一个任务试图请求获取已经被其他某一个任务持有的自旋锁,则服务器可指示该任务按照其自旋等待时间,循环尝试获取该自旋锁,等待该自旋锁重新可用。若该自旋锁未被其他任一个任务持有,则请求获取该自旋锁的该任务可直接获得该自旋锁,以便于访问该自旋锁保护的共享资源。It should be noted that a spin lock is a lock introduced to prevent multiprocessor concurrency, which is widely used in interrupt processing and other parts in the kernel (for a single processor, to prevent concurrency in interrupt processing can be simply adopted The way to turn off interrupts, no spin lock is required). At any time, the spin lock can only be held by one task at most. If a task tries to acquire a spin lock that is already held by another task, the server can instruct the task to retry according to its spin waiting time. Acquire the spinlock and wait for the spinlock to become available again. If the spin lock is not held by any other task, the task requesting to acquire the spin lock can directly obtain the spin lock, so as to access the shared resources protected by the spin lock.
S102、服务器将请求获取自旋锁的任务保存在排队队列中。S102. The server saves the task requesting to acquire the spin lock in a queuing queue.
服务器接收到请求获取自旋锁的任务之后,服务器可将该任务保存在排队队列中。After the server receives the task requesting to acquire the spin lock, the server can save the task in the queuing queue.
具体的,服务器接收到请求获取自旋锁的任务之后,服务器可将该任务保存至排队队列的尾部,并确定该任务在排队队列中的位置。Specifically, after the server receives the task requesting to acquire the spin lock, the server may save the task to the tail of the queuing queue and determine the position of the task in the queuing queue.
进一步地,服务器将请求获取自旋锁的任务保存至排队队列中之前,排队队列中可以保存有其他请求获取自旋锁的任务,也可以未保存有其他请求获取自旋锁的任务,若该排队队列中保存有其他请求获取自旋锁的任务,即排队队列不为空队列,则服务器可将接收到的该任务添加至该排队队列的尾部,并根据服务器接收到排队队列中的所有任务的先后顺序,确定该任务在该排队队列中的位置;若该排队队列中未保存有其他请求获取自旋锁的任务,则服务器可将该任务添加至排队队列中,并确定该任务为位于排队队列首部的任务。Further, before the server saves the task requesting to acquire the spin lock into the queuing queue, other tasks requesting acquiring the spin lock may or may not be stored in the queuing queue. There are other tasks requesting to acquire spin locks in the queuing queue, that is, the queuing queue is not an empty queue, then the server can add the received task to the end of the queuing queue, and according to the server receives all the tasks in the queuing queue to determine the position of the task in the queuing queue; if there is no other task requesting to acquire a spin lock in the queuing queue, the server can add the task to the queuing queue and determine that the task is in the queue Enqueue the task at the head of the queue.
S103、服务器根据请求获取自旋锁的任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。S103. The server determines the spin waiting time of the task according to the position in the queuing queue of the task requesting to acquire the spin lock, so that the task tries to acquire the spin lock when the spin waiting time ends. The position in the queuing queue is used to represent the order in which the task is saved in the queuing queue, and the position of the task saved in the queuing queue first is before the position of the task saved in the queuing queue later.
服务器将请求获取自旋锁的任务保存在排队队列中之后,服务器可根据该任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。After the server saves the task requesting to acquire the spin lock in the queuing queue, the server can determine the spin waiting time of the task according to the position of the task in the queuing queue, so that when the spin waiting time ends, the task tries to Acquire a spin lock, where the position of the task in the queuing queue is used to represent the order in which the task is saved in the queuing queue, the position of the task saved in the queuing queue first is followed by the position of the task saved in the queuing queue Before.
需要说明的是,请求获取自旋锁的任务的自旋等待时间为该任务尝试获取自旋锁的时间间隔,并且在该任务的自旋等待时间内该任务的状态为自旋等待状态。It should be noted that the spin waiting time of the task requesting to acquire the spin lock is the time interval during which the task tries to acquire the spin lock, and the state of the task is the spin waiting state during the spin waiting time of the task.
进一步地,为了减少请求获取自旋锁的任务在自旋过程中尝试获取自旋锁的次数,服务器可将该任务的自旋等待时间按照其在排队队列中的位置,即序号进行动态调整,例如,服务器可将该任务在排队队列中的序号与预设时间间隔的乘积确定为该任务的自旋等待时间,该任务的自旋等待时间也可以根据其他有利于减少该任务在自旋过程中尝试获取自旋锁的次数的方法进行调整,本发明不做限制。Further, in order to reduce the number of times the task that requests to acquire the spin lock tries to acquire the spin lock during the spin process, the server can dynamically adjust the spin waiting time of the task according to its position in the queuing queue, that is, the sequence number, For example, the server can determine the product of the sequence number of the task in the queuing queue and the preset time interval as the spin waiting time of the task, and the spin waiting time of the task can also be reduced according to other benefits in the spin process The method for adjusting the number of times of attempting to acquire the spin lock in the method is not limited in the present invention.
进一步地,在服务器接收该请求获取自旋锁的任务之前,若该排队队列中保存有其他请求获取自旋锁的任务,则服务器可根据该任务在排队队列中的位置,确定的该任务的自旋等待时间不为零,该任务即可进入自旋等待状态,并且服务器可每隔一定的时间间隔周期性的检测该任务的自旋等待时间是否结束,当该任务的自旋等待时间结束时,该任务需尝试获取自旋锁,若该任务未获取到自旋锁,即该任务获取自旋锁失败,则服务器需判断该任务在排队队列中的位置是否进行了更新,即服务器重新确定该任务在排队队列中的位置,并根据重新确定该任务在排队队列中的位置,重新确定该任务的自旋等待时间,以便于该任务在重新确定的自选等待时间结束时,重新尝试获取自旋锁,以此类推,直至该任务为排队队列首部的任务,即服务器确定的该任务的自旋等待时间为零,并且该任务在其自选等待时间结束时,该任务可获取到自旋锁;若该排队队列中未保存有其他请求获取自旋锁的任务,则服务器根据该任务在排队队列中的位置,确定的该任务的自旋等待时间为零,则该任务无需进入自旋等待状态,即可直接获取到自旋锁。Further, before the server receives the task requesting to acquire the spin lock, if there are other tasks requesting acquiring the spin lock in the queuing queue, the server can determine the position of the task according to the position of the task in the queuing queue. The spin waiting time is not zero, the task can enter the spin waiting state, and the server can periodically check whether the spin waiting time of the task is over at a certain time interval, when the spin waiting time of the task ends , the task needs to try to acquire the spin lock. If the task does not acquire the spin lock, that is, the task fails to acquire the spin lock, the server needs to judge whether the position of the task in the queue has been updated, that is, the server restarts Determine the position of the task in the queuing queue, and re-determine the spin waiting time of the task according to the re-determined position of the task in the queuing queue, so that the task can retry to acquire Spin lock, and so on, until the task is the task at the head of the queuing queue, that is, the spin waiting time of the task determined by the server is zero, and the task can obtain the spin when its self-selected waiting time ends If there is no other task requesting to acquire a spin lock in the queuing queue, the server determines that the spin waiting time of the task is zero according to the position of the task in the queuing queue, and the task does not need to enter the spin lock In the waiting state, the spin lock can be obtained directly.
具体的,关于更新请求获取自旋锁的任务在排队队列中的位置的过程将在后续实施例进行详细地说明。Specifically, the process of updating the position of the task requesting to acquire the spin lock in the queuing queue will be described in detail in subsequent embodiments.
本发明的实施例提供一种获取自旋锁的方法,通过服务器接收请求获取自旋锁的任务,并将该任务保存在排队队列中,以及根据该任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。通过该方案,服务器可以根据接收到的任务在排队队列中的位置,计算该任务的自旋等待时间,即尝试获取自旋锁的时间间隔,以使得该任务的自旋等待时间得到动态调整,从而使得排队队列中保存的请求获取自旋锁的多个任务的自旋等待时间均可以根据其在排队队列中的位置得到动态调整,因此,解决了现有技术中多个任务均每隔预先设定的时间间隔尝试获取自旋锁的问题,能够降低由于多个任务竞争共享资源而造成的系统的性能损耗,从而提高系统的性能。The embodiment of the present invention provides a method for acquiring a spin lock, the server receives a task requesting to acquire a spin lock, saves the task in the queuing queue, and determines the task according to the position of the task in the queuing queue Spin waiting time, so that the task tries to acquire the spin lock when the spin waiting time ends, where the position of the task in the queuing queue is used to represent the order in which the tasks are saved in the queuing queue, and the task is saved first The position of the task in the enqueue queue is saved before the position of the task in the enqueue queue. Through this solution, the server can calculate the spin waiting time of the task according to the position of the received task in the queue, that is, the time interval for trying to acquire the spin lock, so that the spin waiting time of the task can be dynamically adjusted, Therefore, the spin waiting time of multiple tasks that request to acquire spin locks stored in the queuing queue can be dynamically adjusted according to their positions in the queuing queue. The problem of trying to acquire the spin lock at a set time interval can reduce the performance loss of the system caused by multiple tasks competing for shared resources, thereby improving the performance of the system.
实施例二Embodiment two
本发明实施例提供一种获取自旋锁的方法,如图2所示,该方法可以包括:The embodiment of the present invention provides a method for acquiring a spin lock, as shown in Figure 2, the method may include:
S201、服务器接收请求获取自旋锁的任务。S201. The server receives a task requesting to acquire a spin lock.
示例性的,若某一个任务需访问自旋锁保护的共享资源,该任务需向服务器请求获取该自旋锁,服务器首先可接收该任务。Exemplarily, if a certain task needs to access a shared resource protected by a spin lock, the task needs to request the server to obtain the spin lock, and the server can first receive the task.
需要说明的是,自旋锁是为防止多处理器并发而引入的一种锁,它在内核中大量应用于中断处理等部分(对于单处理器来说,防止中断处理中的并发可简单采用关闭中断的方式,不需要自旋锁)。在任何时刻,自旋锁最多只能被一个任务持有,若一个任务试图请求获取已经被其他某一个任务持有的自旋锁,则服务器可指示该任务按照其自旋等待时间,循环尝试获取该自旋锁,等待该自旋锁重新可用。若该自旋锁未被其他任一个任务持有,则请求获取该自旋锁的该任务可直接获得该自旋锁,以便于访问该自旋锁保护的共享资源。It should be noted that a spin lock is a lock introduced to prevent multiprocessor concurrency, which is widely used in interrupt processing and other parts in the kernel (for a single processor, to prevent concurrency in interrupt processing can be simply adopted The way to turn off interrupts, no spin lock is required). At any time, the spin lock can only be held by one task at most. If a task tries to acquire a spin lock that is already held by another task, the server can instruct the task to retry according to its spin waiting time. Acquire the spinlock and wait for the spinlock to become available again. If the spin lock is not held by any other task, the task requesting to acquire the spin lock can directly obtain the spin lock, so as to access the shared resources protected by the spin lock.
S202、服务器将请求获取自旋锁的任务添加至排队队列的尾部。S202. The server adds the task of requesting to acquire the spin lock to the tail of the queue.
服务器接收到请求获取自旋锁的任务之后,服务器可将该任务添加至排队队列的尾部,并确定该任务在排队队列中的位置。After the server receives the task requesting to acquire the spin lock, the server can add the task to the tail of the queuing queue and determine the position of the task in the queuing queue.
进一步地,本发明实施例中的排队队列是一种FIFO(First InFirst Out,先进先出)的线性表,只允许在其首部,即队列首部进行删除任务的操作,而在其尾部进行插入任务的操作。Further, the queuing queue in the embodiment of the present invention is a FIFO (First In First Out) linear table, which only allows the operation of deleting tasks at its head, that is, the head of the queue, and inserting tasks at its tail operation.
进一步地,为了保证待获取自旋锁的多个任务按照服务器接收到该多个的任务的顺序获取自旋锁,因此,服务器接收到第一任务之后,服务器可将第一任务添加至排队队列的尾部,依次类推,从而能够保证排队队列中保存的待获取自旋锁的任务按照FIFO的顺序获取自旋锁。Further, in order to ensure that multiple tasks to acquire spin locks acquire spin locks in the order in which the server receives the multiple tasks, therefore, after the server receives the first task, the server can add the first task to the queuing queue , and so on, so as to ensure that the tasks to acquire the spin locks stored in the queuing queue acquire the spin locks in the order of the FIFO.
进一步地,服务器将请求获取自旋锁的任务添加至排队队列尾部之前,排队队列中可以保存有其他请求获取自旋锁的任务,也可以未保存有其他请求获取自旋锁的任务,若该排队队列中保存有其他请求获取自旋锁的任务,即排队队列不为空队列,则服务器可将接收到的该任务添加至该排队队列的尾部,并根据服务器接收到排队队列中的所有任务的先后顺序,确定该任务在该排队队列中的位置;若该排队队列中未保存有其他请求获取自旋锁的任务,则服务器可将该任务添加至排队队列中,并确定该任务为位于排队队列首部的任务。Furthermore, before the server adds the task requesting to acquire the spin lock to the end of the queuing queue, there may be other tasks requesting acquiring the spin lock in the queuing queue, or there may not be other tasks requesting acquiring the spin lock. There are other tasks requesting to acquire spin locks in the queuing queue, that is, the queuing queue is not an empty queue, then the server can add the received task to the end of the queuing queue, and according to the server receives all the tasks in the queuing queue to determine the position of the task in the queuing queue; if there is no other task requesting to acquire a spin lock in the queuing queue, the server can add the task to the queuing queue and determine that the task is in the queue Enqueue the task at the head of the queue.
需要说明的是,请求获取自旋锁的任务在排队队列中的位置可以用该任务在排队队列中的序号表示,若该任务在排队队列中的序号为零,则表示该任务为排队队列首部的任务;若该任务在排队队列中的序号不为零,则表示该任务不为排队队列首部的任务。It should be noted that the position of the task requesting to acquire the spin lock in the queuing queue can be represented by the serial number of the task in the queuing queue. If the serial number of the task in the queuing queue is zero, it means that the task is the head of the queuing queue If the sequence number of the task in the queuing queue is not zero, it means that the task is not the task at the head of the queuing queue.
进一步地,请求获取自旋锁的任务在排队队列中的序号可以为绝对序号,也可以为相对序号。若排队队列中的所有任务的序号是以递增的顺序从零开始编号,则该任务在排队队列中的序号为绝对序号;若排队队列中的所有任务的序号是以递增的顺序,但不是从零开始编号,则该任务在排队队列中的序号为相对序号,此时,该任务在排队队列中的序号为该任务在排队队列中的绝对序号与偏移量之差,该偏移量为按照排队队列中的所有任务的编号规则确定的数值。Further, the sequence number of the task requesting to acquire the spin lock in the queuing queue may be an absolute sequence number or a relative sequence number. If the sequence numbers of all tasks in the queue are numbered from zero in increasing order, the sequence number of the task in the queue is an absolute sequence number; if the sequence numbers of all tasks in the queue are in increasing order, but not from If the serial number starts from zero, the serial number of the task in the queue is a relative serial number. At this time, the serial number of the task in the queue is the difference between the absolute serial number and the offset of the task in the queue, and the offset is A numeric value determined by the number sequence for all tasks in the enqueue queue.
S203、服务器根据请求获取自旋锁的任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。S203. The server determines the spin waiting time of the task according to the position in the queuing queue of the task requesting to acquire the spin lock, so that the task tries to acquire the spin lock when the spin waiting time ends. The position in the queuing queue is used to represent the order in which the task is saved in the queuing queue, and the position of the task saved in the queuing queue first is before the position of the task saved in the queuing queue later.
服务器将请求获取自旋锁的任务添加至排队队列的尾部之后,服务器可根据该任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。After the server adds the task requesting to acquire the spin lock to the tail of the queuing queue, the server can determine the spin waiting time of the task according to the position of the task in the queuing queue, so that when the spin waiting time of the task ends, Try to acquire a spin lock, where the position of the task in the queuing queue is used to represent the order in which the task is saved in the queuing queue, the position of the task saved in the queuing queue first is followed by the task saved in the queuing queue before the location.
需要说明的是,请求获取自旋锁的任务的自旋等待时间为该任务尝试获取自旋锁的时间间隔,并且在该任务的自旋等待时间内该任务的状态为自旋等待状态。It should be noted that the spin waiting time of the task requesting to acquire the spin lock is the time interval during which the task tries to acquire the spin lock, and the state of the task is the spin waiting state during the spin waiting time of the task.
进一步地,为了减少请求获取自旋锁的任务在自旋过程中尝试获取自旋锁的次数,服务器可将该任务的自旋等待时间按照其在排队队列中的位置,即序号进行动态调整,例如,服务器可将该任务在排队队列中的序号与预设时间间隔的乘积确定为该任务的自旋等待时间,该任务的自旋等待时间也可以根据其他有利于减少该任务在自旋过程中尝试获取自旋锁的次数的方法进行调整,本发明不做限制。Further, in order to reduce the number of times the task that requests to acquire the spin lock tries to acquire the spin lock during the spin process, the server can dynamically adjust the spin waiting time of the task according to its position in the queuing queue, that is, the sequence number, For example, the server can determine the product of the sequence number of the task in the queuing queue and the preset time interval as the spin waiting time of the task, and the spin waiting time of the task can also be reduced according to other benefits in the spin process The method for adjusting the number of times of attempting to acquire the spin lock in the method is not limited in the present invention.
例如,若预设时间间隔为delta(t),在现有技术中,链表中的请求获取自旋锁的任务的自旋等待时间设定为delta(t),即链表中的请求获取自旋锁的任务可每隔delta(t)尝试获取自旋锁,为了减少请求获取自旋锁的该任务尝试获取自旋锁的次数,本发明至少可以提供两种实现方案:For example, if the preset time interval is delta(t), in the prior art, the spin waiting time of the task requesting to acquire the spin lock in the linked list is set to delta(t), that is, the request to acquire the spin lock in the linked list The task of the lock can try to acquire the spin lock every delta (t). In order to reduce the number of times the task that requests to acquire the spin lock tries to acquire the spin lock, the present invention can provide at least two implementation schemes:
(1)本发明中,服务器可将排队队列中的请求获取自旋锁的任务在排队队列中的序号与预设时间间隔的乘积确定为该任务的自旋等待时间,若该任务在排队队列中的序号为que_seri,则该任务的自旋等待时间可以为que_seri*delta(t),由于该任务在排队队列中的序号取正整数,则本发明相对于现有技术,该任务的自旋等待时间从delta(t)提高到que_seri*delta(t),从而本发明相对于现有技术,请求获取自旋锁的任务尝试获取自旋锁的次数则减少。(1) In the present invention, the server can determine the product of the serial number of the task in the queuing queue and the preset time interval as the spin waiting time of the task, if the task is in the queuing queue The sequence number in is que_seri, then the spin waiting time of this task can be que_seri*delta (t), because the sequence number of this task in the queuing queue takes a positive integer, then the present invention is relative to the prior art, and the spin of this task The waiting time is increased from delta(t) to que_seri*delta(t), so that compared with the prior art, the number of times a task requesting to acquire a spinlock in the present invention tries to acquire the spinlock is reduced.
(2)本发明也可以预设一个最大值MAX,当请求获取自旋锁的任务在排队队列中的序号小于或等于该最大值时,服务器可将请求获取自旋锁的任务在排队队列中的序号与预设时间间隔的乘积,确定为该任务的自旋等待时间,若该任务在排队队列中的序号为que_seri,则该任务的自旋等待时间可以为que_seri*delta(t);当请求获取自旋锁的任务在排队队列中的序号大于该最大值时,服务器可将该最大值与预设时间间隔的乘积确定为该任务的自旋等待时间,即该任务的自旋等待时间可以为MAX*delta(t)。(2) The present invention can also preset a maximum value MAX. When the serial number of the task requesting to acquire the spin lock in the queuing queue is less than or equal to the maximum value, the server can place the task requesting to acquire the spin lock in the queuing queue The product of the serial number of the task and the preset time interval is determined as the spin waiting time of the task. If the serial number of the task in the queuing queue is que_seri, the spin waiting time of the task can be que_seri*delta(t); when When the serial number of the task requesting to acquire the spin lock in the queuing queue is greater than the maximum value, the server can determine the product of the maximum value and the preset time interval as the spin waiting time of the task, that is, the spin waiting time of the task Can be MAX*delta(t).
进一步地,在服务器接收该请求获取自旋锁的任务之前,若该排队队列中保存有其他请求获取自旋锁的任务,则服务器可根据该任务在排队队列中的位置,确定的该任务的自旋等待时间不为零,该任务即可进入自旋等待状态,并且服务器可每隔一定的时间间隔周期性的检测该任务的自旋等待时间是否结束,当该任务的自旋等待时间结束时,该任务需尝试获取自旋锁,若该任务未获取到自旋锁,即该任务获取自旋锁失败,则服务器需判断该任务在排队队列中的位置是否进行了更新,即服务器重新确定该任务在排队队列中的位置,并根据重新确定该任务在排队队列中的位置,重新确定该任务的自旋等待时间,以便于该任务在重新确定的自选等待时间结束时,重新尝试获取自旋锁,以此类推,直至该任务为排队队列首部的任务,即服务器确定的该任务的自旋等待时间为零,并且该任务在其自选等待时间结束时,该任务可获取到自旋锁;若该排队队列中未保存有其他请求获取自旋锁的任务,则服务器根据该任务在排队队列中的位置,确定的该任务的自旋等待时间为零,则该任务无需进入自旋等待状态,即可直接获取到自旋锁。Further, before the server receives the task requesting to acquire the spin lock, if there are other tasks requesting acquiring the spin lock in the queuing queue, the server can determine the position of the task according to the position of the task in the queuing queue. The spin waiting time is not zero, the task can enter the spin waiting state, and the server can periodically check whether the spin waiting time of the task is over at a certain time interval, when the spin waiting time of the task ends , the task needs to try to acquire the spin lock. If the task does not acquire the spin lock, that is, the task fails to acquire the spin lock, the server needs to judge whether the position of the task in the queue has been updated, that is, the server restarts Determine the position of the task in the queuing queue, and re-determine the spin waiting time of the task according to the re-determined position of the task in the queuing queue, so that the task can retry to acquire Spin lock, and so on, until the task is the task at the head of the queuing queue, that is, the spin waiting time of the task determined by the server is zero, and the task can obtain the spin when its self-selected waiting time ends If there is no other task requesting to acquire a spin lock in the queuing queue, the server determines that the spin waiting time of the task is zero according to the position of the task in the queuing queue, and the task does not need to enter the spin lock In the waiting state, the spin lock can be obtained directly.
S204、服务器根据预设检测周期,周期性的检测请求获取自旋锁的任务的自旋等待时间是否结束。S204. The server periodically detects whether the spin waiting time of the task requesting to acquire the spin lock is over according to the preset detection cycle.
服务器将请求获取自旋锁的任务添加至排队队列尾部之后,服务器可确定该任务在排队队列中的位置,若该任务不为排队队列首部的任务,该任务可进入自旋等待状态,在该任务进入自旋等待状态之后,服务器可根据预设检测周期,周期性的检测该任务的自旋等待时间是否结束。After the server adds the task requesting to obtain the spin lock to the end of the queuing queue, the server can determine the position of the task in the queuing queue. If the task is not the task at the head of the queuing queue, the task can enter the spin waiting state. After the task enters the spin waiting state, the server can periodically detect whether the spin waiting time of the task is over according to the preset detection period.
进一步地,若服务器检测出请求获取自旋锁的任务的自旋等待时间结束,则该任务可尝试获取自旋锁;若服务器检测出请求获取自旋锁的任务的自旋等待时间未结束,则该任务仍然保持自旋等待状态。Further, if the server detects that the spin waiting time of the task requesting to acquire the spin lock is over, the task can try to acquire the spin lock; if the server detects that the spin waiting time of the task requesting to acquire the spin lock has not ended, Then the task remains in the spin waiting state.
S205、若请求获取自旋锁的任务获取自旋锁失败,则服务器重新确定该任务在排队队列中的位置。S205. If the task requesting to acquire the spin lock fails to acquire the spin lock, the server re-determines the position of the task in the queuing queue.
当请求获取自旋锁的任务在自旋等待时间结束时,尝试获取自旋锁之后,若该任务获取自旋锁失败,则服务器可重新确定该任务在排队队列中的位置。When the task requesting to acquire the spin lock ends after the spin waiting time, if the task fails to acquire the spin lock after attempting to acquire the spin lock, the server can re-determine the position of the task in the queuing queue.
进一步地,当请求获取自旋锁的任务的自旋等待时间结束,且该任务为排队队列首部的任务,即该任务在排队队列中的序号为零时,该任务获取自旋锁成功;当请求获取自旋锁的任务的自旋等待时间结束,且该任务不为排队队列首部的任务,即该任务在排队队列中的序号不为零时,该任务获取自旋锁失败。Further, when the spin waiting time of the task requesting to acquire the spin lock ends, and the task is the task at the head of the queuing queue, that is, when the sequence number of the task in the queuing queue is zero, the task acquires the spin lock successfully; The spin waiting time of the task requesting to acquire the spin lock is over, and the task is not the task at the head of the queuing queue, that is, when the sequence number of the task in the queuing queue is not zero, the task fails to acquire the spin lock.
S206、服务器根据重新确定的请求获取自旋锁的任务在排队队列中的位置,重新确定该任务的自旋等待时间,以使得该任务在重新确定的自旋等待时间结束时,重新尝试获取自旋锁。S206. The server re-determines the spin waiting time of the task according to the re-determined position in the queuing queue of the task that requests to acquire the spin lock, so that the task re-attempts to acquire the spin lock when the re-determined spin waiting time ends. twist lock.
服务器重新确定请求获取自旋锁的任务在排队队列中的位置之后,服务器可根据重新确定的该任务在排队队列中的位置,重新确定该任务的自旋等待时间,以使得该任务在重新确定的自旋等待时间结束时,重新尝试获取自旋锁。After the server re-determines the position of the task requesting to acquire the spin lock in the queuing queue, the server can re-determine the spin waiting time of the task according to the re-determined position of the task in the queuing queue, so that the task is re-determined At the end of the spin wait time, retry to acquire the spin lock.
进一步地,若服务器重新确定的请求获取自旋锁的任务为排队队列首部的任务,则服务器可在该任务自旋等待时间结束时,直接获取到自旋锁;若服务器重新确定的请求获取自旋锁的任务不为排队队列首部的任务,则该任务可在自旋等待时间结束时,尝试获取自旋锁,直至该任务为排队队列首部的任务,并且该任务的自旋等待时间结束时,该任务可获取到自旋锁。Further, if the server's re-determined request to acquire the spin lock is the task at the head of the queue, the server can directly acquire the spin lock when the task spin waiting time ends; If the task of the spin lock is not the task at the head of the queue, the task can try to acquire the spin lock when the spin waiting time ends, until the task is the task at the head of the queue and the spin waiting time of the task ends , the task can acquire the spin lock.
S207、当位于排队队列首部的任务释放自旋锁时,服务器更新请求获取自旋锁的任务在排队队列中的位置。S207. When the task at the head of the queuing queue releases the spin lock, the server updates the position of the task requesting to acquire the spin lock in the queuing queue.
服务器确定请求获取自旋锁的任务的自旋等待时间之后,服务器可周期性的检测排队队列首部的任务是否释放自旋锁,当位于排队队列首部的任务释放自旋锁时,服务器可更新该任务在排队队列中的位置。After the server determines the spin waiting time of the task requesting to acquire the spin lock, the server can periodically check whether the task at the head of the queue releases the spin lock. When the task at the head of the queue releases the spin lock, the server can update the spin lock The task's position in the enqueuing queue.
具体的,当位于排队队列首部的任务释放自旋锁时,服务器可将请求获取自旋锁的任务在排队队列中的位置更新为该任务的前一个任务在排队队列中的位置。Specifically, when the task at the head of the queuing queue releases the spin lock, the server may update the position of the task requesting to acquire the spin lock in the queuing queue to the position of the previous task of the task in the queuing queue.
例如,服务器将请求获取自旋锁的任务添加至排队队列之后,服务器更新该任务在排队队列中的位置的过程可以包括:若服务器检测出排队队列首部的任务释放了自旋锁,则服务器将排队队列首部的任务之后的其他所有任务在该排队队列中的位置均需进行更新,服务器可将排队队列首部的任务之后的其他所有任务在该排队队列中的位置均更新为其前一个任务在排队队列中的位置,因此,服务器可将该任务在排队队列中的位置更新为该任务的前一个任务在排队队列中的位置,即服务器可将该任务在排队队列中的序号更新为该任务的前一个任务在排队队列中的序号,若更新后的该任务不为排队队列首部的任务,服务器可根据更新后的该任务在排队队列中的位置,确定该任务下一次的自旋等待时间,以便于该任务进入下一次自旋等待状态;若服务器检测出排队队列首部的任务一直持有自旋锁,则服务器可保持第一任务在排队队列中的位置,即序号不变。For example, after the server adds a task requesting to acquire a spin lock to the queuing queue, the process for the server to update the position of the task in the queuing queue may include: if the server detects that the task at the head of the queuing queue has released the spin lock, the server will The positions of all other tasks after the task at the head of the queuing queue in the queuing queue need to be updated, and the server can update the positions of all tasks after the task at the head of the queuing queue in the queuing queue to the positions of the previous tasks in the queuing queue. The position in the queuing queue, therefore, the server can update the position of the task in the queuing queue to the position of the task's previous task in the queuing queue, that is, the server can update the sequence number of the task in the queuing queue to the task The sequence number of the previous task in the queuing queue. If the updated task is not the task at the head of the queuing queue, the server can determine the next spin waiting time of the task according to the updated position of the task in the queuing queue , so that the task enters the next spin waiting state; if the server detects that the task at the head of the queuing queue has always held a spin lock, the server can keep the position of the first task in the queuing queue, that is, the sequence number remains unchanged.
本发明实施例提供的获取自旋锁的方法可以应用于NUMA系统中,也可以应用于中断、进程调度、内存管理、文件系统或分布式文件系统的同步机制等可以应用自旋锁的场景,还可以应用于其他用到自旋锁场景,本发明不做限制。The method for obtaining a spin lock provided by the embodiment of the present invention can be applied to a NUMA system, and can also be applied to scenarios where spin locks can be applied, such as interruption, process scheduling, memory management, file system or synchronization mechanism of a distributed file system, etc. It can also be applied to other scenarios where spin locks are used, which is not limited in the present invention.
本发明的实施例提供一种获取自旋锁的方法,通过服务器接收请求获取自旋锁的任务,并将该任务保存在排队队列中,以及根据该任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。通过该方案,服务器可以根据接收到的任务在排队队列中的位置,计算该任务的自旋等待时间,即尝试获取自旋锁的时间间隔,以使得该任务的自旋等待时间得到动态调整,从而使得排队队列中保存的请求获取自旋锁的多个任务的自旋等待时间均可以根据其在排队队列中的位置得到动态调整,因此,解决了现有技术中多个任务均每隔预先设定的时间间隔尝试获取自旋锁的问题,能够降低由于多个任务竞争共享资源而造成的系统的性能损耗,从而提高系统的性能。The embodiment of the present invention provides a method for acquiring a spin lock, the server receives a task requesting to acquire a spin lock, saves the task in the queuing queue, and determines the task according to the position of the task in the queuing queue Spin waiting time, so that the task tries to acquire the spin lock when the spin waiting time ends, where the position of the task in the queuing queue is used to represent the order in which the tasks are saved in the queuing queue, and the task is saved first The position of the task in the enqueue queue is saved before the position of the task in the enqueue queue. Through this solution, the server can calculate the spin waiting time of the task according to the position of the received task in the queue, that is, the time interval for trying to acquire the spin lock, so that the spin waiting time of the task can be dynamically adjusted, Therefore, the spin waiting time of multiple tasks that request to acquire spin locks stored in the queuing queue can be dynamically adjusted according to their positions in the queuing queue. The problem of trying to acquire the spin lock at a set time interval can reduce the performance loss of the system caused by multiple tasks competing for shared resources, thereby improving the performance of the system.
实施例三Embodiment Three
如图3所示,本发明实施例提供一种服务器1,该服务器1可以包括:As shown in FIG. 3, an embodiment of the present invention provides a server 1, which may include:
接收单元10,用于接收请求获取自旋锁的任务。The receiving unit 10 is configured to receive a task requesting to acquire a spin lock.
保存单元11,用于将所述接收单元10接收的所述任务保存在排队队列中;a saving unit 11, configured to save the task received by the receiving unit 10 in a queuing queue;
确定单元12,用于根据所述保存单元11保存的所述任务在所述排队队列中的位置,确定所述任务的自旋等待时间,以使得所述任务在所述自旋等待时间结束时,尝试获取所述自旋锁,其中,所述任务在所述排队队列中的位置用于表征所述任务保存至所述排队队列中的先后顺序,先保存至所述排队队列中的任务的位置在后保存至所述排队队列中的任务的位置之前。The determining unit 12 is configured to determine the spin waiting time of the task according to the position of the task saved in the queuing queue by the saving unit 11, so that when the spin waiting time of the task ends, the task , trying to acquire the spin lock, wherein the position of the task in the queuing queue is used to represent the order in which the tasks are saved in the queuing queue, and the tasks saved in the queuing queue first The position after is saved to the position of the task in the enqueuing queue.
可选的,如图4所示,所述服务器1还包括检测单元13,所述检测单元13,用于根据预设检测周期,周期性的检测所述确定单元12确定的所述任务的自旋等待时间是否结束。Optionally, as shown in FIG. 4 , the server 1 further includes a detection unit 13 configured to periodically detect the self-time of the task determined by the determination unit 12 according to a preset detection cycle. Whether the spin waiting time is over.
可选的,所述确定单元12,还用于当所述任务在所述自选等待时间结束时,尝试获取所述自旋锁之后,若所述任务获取所述自旋锁失败,则重新确定所述任务在所述排队队列中的位置,并根据重新确定的所述任务在所述排队队列中的位置,重新确定所述任务的自旋等待时间,以使得所述任务在重新确定的所述自旋等待时间结束时,重新尝试获取所述自旋锁。Optionally, the determination unit 12 is further configured to re-determine if the task fails to acquire the spin lock after the task has attempted to acquire the spin lock when the optional waiting time ends. The position of the task in the queuing queue, and according to the re-determined position of the task in the queuing queue, re-determine the spin waiting time of the task, so that the task is in the newly determined position When the spin waiting time is over, retry to acquire the spin lock.
可选的,如图5所示,所述服务器1还包括更新单元14,所述更新单元14,用于当位于所述排队队列首部的任务释放所述自旋锁时,更新所述保存单元11保存的所述任务在所述排队队列中的位置。Optionally, as shown in FIG. 5 , the server 1 further includes an updating unit 14, configured to update the saving unit when the task at the head of the queuing queue releases the spin lock 11 The saved position of the task in the queuing queue.
可选的,所述更新单元14,具体用于当位于所述排队队列首部的任务释放所述自旋锁时,将所述任务在所述排队队列中的位置更新为所述任务的前一个任务在所述排队队列中的位置。Optionally, the updating unit 14 is specifically configured to update the position of the task in the queuing queue to the previous one of the task when the task at the head of the queuing queue releases the spin lock. The task's position in the queuing queue.
可选的,所述保存单元11,具体用于将所述接收单元10接收的所述任务添加至所述排队队列的尾部。Optionally, the saving unit 11 is specifically configured to add the task received by the receiving unit 10 to the tail of the queuing queue.
本发明的实施例提供一种服务器,该服务器接收请求获取自旋锁的任务,并将该任务保存在排队队列中,以及根据该任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。通过该方案,服务器可以根据接收到的任务在排队队列中的位置,计算该任务的自旋等待时间,即尝试获取自旋锁的时间间隔,以使得该任务的自旋等待时间得到动态调整,从而使得排队队列中保存的请求获取自旋锁的多个任务的自旋等待时间均可以根据其在排队队列中的位置得到动态调整,因此,解决了现有技术中多个任务均每隔预先设定的时间间隔尝试获取自旋锁的问题,能够降低由于多个任务竞争共享资源而造成的系统的性能损耗,从而提高系统的性能。An embodiment of the present invention provides a server, which receives a task requesting to acquire a spin lock, stores the task in a queuing queue, and determines the spin waiting time of the task according to the position of the task in the queuing queue , so that the task tries to acquire the spin lock when the spin waiting time ends, where the position of the task in the queuing queue is used to represent the order in which the task is saved in the queuing queue, and the task is saved in the queuing queue first The position of the task is saved after the position of the task in the enqueuing queue. Through this solution, the server can calculate the spin waiting time of the task according to the position of the received task in the queue, that is, the time interval for trying to acquire the spin lock, so that the spin waiting time of the task can be dynamically adjusted, Therefore, the spin waiting time of multiple tasks that request to acquire spin locks stored in the queuing queue can be dynamically adjusted according to their positions in the queuing queue. The problem of trying to acquire the spin lock at a set time interval can reduce the performance loss of the system caused by multiple tasks competing for shared resources, thereby improving the performance of the system.
实施例四Embodiment Four
如图6所示,本发明实施例提供一种服务器,该服务器可以包括:处理器15、存储器16、系统总线17和用户接口18,其中,As shown in FIG. 6, an embodiment of the present invention provides a server, which may include: a processor 15, a memory 16, a system bus 17, and a user interface 18, wherein,
处理器15、存储器16和通信接口18之间通过系统总线17连接并完成相互间的通信。The processor 15 , the memory 16 and the communication interface 18 are connected through the system bus 17 and complete mutual communication.
处理器15可能为单核或多核中央处理单元,或者为特定集成电路或者被配置为实施本发明实施例的一个或多个集成电路。The processor 15 may be a single-core or multi-core central processing unit, or a specific integrated circuit or one or more integrated circuits configured to implement embodiments of the present invention.
存储器16可以为高速RAM(Random Access Memory,随机存取存储器),也可以为非易失性存储器(non-volatile memory),例如至少一个磁盘存储器。The memory 16 may be a high-speed RAM (Random Access Memory, random access memory), or a non-volatile memory (non-volatile memory), such as at least one disk memory.
存储器16用于存储服务器的指令。具体的,服务器的指令可以包括软件程序和软件代码。The memory 16 is used to store instructions of the server. Specifically, the instructions of the server may include software programs and software codes.
具体的,处理器15可用于通过系统总线17从用户接口18接收请求获取自旋锁的任务,并将所述任务保存在排队队列中,以及根据所述任务在所述排队队列中的位置,确定所述任务的自旋等待时间,以使得所述任务在所述自旋等待时间结束时,尝试获取所述自旋锁,其中,所述任务在所述排队队列中的位置用于表征所述任务保存至所述排队队列中的先后顺序,先保存至所述排队队列中的任务的位置在后保存至所述排队队列中的任务的位置之前;所述存储器16可用于存储所述任务的软件代码及控制所述处理器15完成上述步骤的软件程序,从而使得所述处理器15可通过执行上述软件程序并调用上述软件代码,从而完成上述步骤。Specifically, the processor 15 may be configured to receive a task requesting to acquire a spin lock from the user interface 18 through the system bus 17, and store the task in the queuing queue, and according to the position of the task in the queuing queue, determining the spin waiting time of the task, so that the task tries to acquire the spin lock when the spin waiting time ends, wherein the position of the task in the queuing queue is used to represent the The order in which the tasks are saved in the queuing queue, the position of the task saved in the queuing queue first is before the position of the task in the queuing queue; the memory 16 can be used to store the tasks The software code and the software program for controlling the processor 15 to complete the above steps, so that the processor 15 can complete the above steps by executing the above software program and calling the above software code.
可选的,所述处理器15,还用于根据预设检测周期,周期性的检测所述任务的自旋等待时间是否结束。Optionally, the processor 15 is further configured to periodically detect whether the spin waiting time of the task is over according to a preset detection period.
可选的,所述处理器15,还用于当所述任务在所述自选等待时间结束时,尝试获取所述自旋锁之后,若所述任务获取所述自旋锁失败,则重新确定所述任务在所述排队队列中的位置,并根据重新确定的所述任务在所述排队队列中的位置,重新确定所述任务的自旋等待时间,以使得所述任务在重新确定的所述自旋等待时间结束时,重新尝试获取所述自旋锁。Optionally, the processor 15 is further configured to re-determine if the task fails to acquire the spin lock after the task has attempted to acquire the spin lock when the self-selected waiting time ends. The position of the task in the queuing queue, and according to the re-determined position of the task in the queuing queue, re-determine the spin waiting time of the task, so that the task is in the newly determined position When the spin waiting time is over, retry to acquire the spin lock.
可选的,所述处理器15,还用于当位于所述排队队列首部的任务释放所述自旋锁时,更新所述任务在所述排队队列中的位置。Optionally, the processor 15 is further configured to update the position of the task in the queuing queue when the task at the head of the queuing queue releases the spin lock.
可选的,所述处理器15,具体用于当位于所述排队队列首部的任务释放所述自旋锁时,将所述任务在所述排队队列中的位置更新为所述任务的前一个任务在所述排队队列中的位置。Optionally, the processor 15 is specifically configured to, when the task at the head of the queuing queue releases the spin lock, update the position of the task in the queuing queue to be the previous task of the task. The task's position in the queuing queue.
可选的,所述处理器15,具体用于将所述任务添加至所述排队队列的尾部。Optionally, the processor 15 is specifically configured to add the task to the tail of the queuing queue.
本发明的实施例提供一种服务器,该服务器接收请求获取自旋锁的任务,并将该任务保存在排队队列中,以及根据该任务在排队队列中的位置,确定该任务的自旋等待时间,以使得该任务在自旋等待时间结束时,尝试获取自旋锁,其中,该任务在排队队列中的位置用于表征该任务保存至排队队列中的先后顺序,先保存至排队队列中的任务的位置在后保存至排队队列中的任务的位置之前。通过该方案,服务器可以根据接收到的任务在排队队列中的位置,计算该任务的自旋等待时间,即尝试获取自旋锁的时间间隔,以使得该任务的自旋等待时间得到动态调整,从而使得排队队列中保存的请求获取自旋锁的多个任务的自旋等待时间均可以根据其在排队队列中的位置得到动态调整,因此,解决了现有技术中多个任务均每隔预先设定的时间间隔尝试获取自旋锁的问题,能够降低由于多个任务竞争共享资源而造成的系统的性能损耗,从而提高系统的性能。An embodiment of the present invention provides a server, which receives a task requesting to acquire a spin lock, stores the task in a queuing queue, and determines the spin waiting time of the task according to the position of the task in the queuing queue , so that the task tries to acquire the spin lock when the spin waiting time ends, where the position of the task in the queuing queue is used to represent the order in which the task is saved in the queuing queue, and the task is saved in the queuing queue first The position of the task is saved after the position of the task in the enqueuing queue. Through this solution, the server can calculate the spin waiting time of the task according to the position of the received task in the queue, that is, the time interval for trying to acquire the spin lock, so that the spin waiting time of the task can be dynamically adjusted, Therefore, the spin waiting time of multiple tasks that request to acquire spin locks stored in the queuing queue can be dynamically adjusted according to their positions in the queuing queue. The problem of trying to acquire the spin lock at a set time interval can reduce the performance loss of the system caused by multiple tasks competing for shared resources, thereby improving the performance of the system.
所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,仅以上述各功能模块的划分进行举例说明,实际应用中,可以根据需要而将上述功能分配由不同的功能模块完成,即将装置的内部结构划分成不同的功能模块,以完成以上描述的全部或者部分功能。上述描述的系统,装置和单元的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。Those skilled in the art can clearly understand that for the convenience and brevity of description, only the division of the above-mentioned functional modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional modules according to needs. The internal structure of the device is divided into different functional modules to complete all or part of the functions described above. For the specific working process of the above-described system, device, and unit, reference may be made to the corresponding process in the foregoing method embodiments, and details are not repeated here.
在本申请所提供的几个实施例中,应该理解到,所揭露的系统,装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述模块或单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,例如多个单元或组件可以结合或者可以集成到另一个系统,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。In the several embodiments provided in this application, it should be understood that the disclosed system, device and method can be implemented in other ways. For example, the device embodiments described above are only illustrative. For example, the division of the modules or units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be Incorporation may either be integrated into another system, or some features may be omitted, or not implemented. In another point, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces, and the indirect coupling or communication connection of devices or units may be in electrical, mechanical or other forms.
所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place, or may be distributed to multiple network units. Part or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
另外,在本发明各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, each unit may exist separately physically, or two or more units may be integrated into one unit. The above-mentioned integrated units can be implemented in the form of hardware or in the form of software functional units.
所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本发明的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)或处理器(processor)执行本发明各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(ROM,Read-OnlyMemory)、随机存取存储器、磁碟或者光盘等各种可以存储程序代码的介质。If the integrated unit is realized in the form of a software function unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the essence of the technical solution of the present invention or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium , including several instructions to make a computer device (which may be a personal computer, a server, or a network device, etc.) or a processor (processor) execute all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory, magnetic disk or optical disk, and other various media that can store program codes.
以上所述,仅为本发明的具体实施方式,但本发明的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本发明揭露的技术范围内,可轻易想到变化或替换,都应涵盖在本发明的保护范围之内。因此,本发明的保护范围应所述以权利要求的保护范围为准。The above is only a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Anyone skilled in the art can easily think of changes or substitutions within the technical scope disclosed in the present invention. Should be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201410109467.XACN104932933B (en) | 2014-03-21 | 2014-03-21 | A kind of method and device obtaining spin lock |
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201410109467.XACN104932933B (en) | 2014-03-21 | 2014-03-21 | A kind of method and device obtaining spin lock |
| Publication Number | Publication Date |
|---|---|
| CN104932933Atrue CN104932933A (en) | 2015-09-23 |
| CN104932933B CN104932933B (en) | 2018-11-13 |
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN201410109467.XAActiveCN104932933B (en) | 2014-03-21 | 2014-03-21 | A kind of method and device obtaining spin lock |
| Country | Link |
|---|---|
| CN (1) | CN104932933B (en) |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN106020333A (en)* | 2016-05-20 | 2016-10-12 | 京信通信技术(广州)有限公司 | Multi-core timer implementation method and multi-core system |
| CN107783843A (en)* | 2016-08-31 | 2018-03-09 | 中国移动通信有限公司研究院 | A kind of processing method and processing device of cloud data |
| CN109558241A (en)* | 2018-11-13 | 2019-04-02 | 锐捷网络股份有限公司 | A kind of method and device obtaining spin lock |
| CN112306703A (en)* | 2019-07-29 | 2021-02-02 | 华为技术有限公司 | A critical section execution method and device in a NUMA system |
| CN112306698A (en)* | 2019-07-29 | 2021-02-02 | 华为技术有限公司 | A critical section execution method and device in a NUMA system |
| US11379456B2 (en)* | 2020-10-01 | 2022-07-05 | Oracle International Corporation | Efficient adjustment of spin-locking parameter values |
| CN115934370A (en)* | 2022-12-23 | 2023-04-07 | 科东(广州)软件科技有限公司 | Spin lock acquisition method, device, equipment and storage medium |
| US11868261B2 (en) | 2021-07-20 | 2024-01-09 | Oracle International Corporation | Prediction of buffer pool size for transaction processing workloads |
| CN120407242A (en)* | 2025-07-07 | 2025-08-01 | 中国兵器装备集团兵器装备研究所 | A method and device for optimizing queue spin locks in high concurrency scenarios |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20040024797A1 (en)* | 2002-07-31 | 2004-02-05 | International Business Machines Corporation | System and method for monitoring software locks |
| CN102317918A (en)* | 2009-02-17 | 2012-01-11 | 松下电器产业株式会社 | Exclusive control method of resource and exclusive controller of resource |
| CN102566979A (en)* | 2011-12-02 | 2012-07-11 | 华为技术有限公司 | Method, device and multi-core processor system for realizing self-adaptive lock |
| CN103262030A (en)* | 2010-12-16 | 2013-08-21 | 英特尔公司 | A fast and linearizable concurrent priority queue via dynamic aggregation of operations |
| CN103324269A (en)* | 2013-06-13 | 2013-09-25 | 中国科学院计算技术研究所 | Method and system for reducing multithread program power consumption |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20040024797A1 (en)* | 2002-07-31 | 2004-02-05 | International Business Machines Corporation | System and method for monitoring software locks |
| CN102317918A (en)* | 2009-02-17 | 2012-01-11 | 松下电器产业株式会社 | Exclusive control method of resource and exclusive controller of resource |
| CN103262030A (en)* | 2010-12-16 | 2013-08-21 | 英特尔公司 | A fast and linearizable concurrent priority queue via dynamic aggregation of operations |
| CN102566979A (en)* | 2011-12-02 | 2012-07-11 | 华为技术有限公司 | Method, device and multi-core processor system for realizing self-adaptive lock |
| CN103324269A (en)* | 2013-06-13 | 2013-09-25 | 中国科学院计算技术研究所 | Method and system for reducing multithread program power consumption |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN106020333A (en)* | 2016-05-20 | 2016-10-12 | 京信通信技术(广州)有限公司 | Multi-core timer implementation method and multi-core system |
| CN107783843A (en)* | 2016-08-31 | 2018-03-09 | 中国移动通信有限公司研究院 | A kind of processing method and processing device of cloud data |
| CN107783843B (en)* | 2016-08-31 | 2021-08-06 | 中国移动通信有限公司研究院 | Method and device for processing cloud data |
| CN109558241A (en)* | 2018-11-13 | 2019-04-02 | 锐捷网络股份有限公司 | A kind of method and device obtaining spin lock |
| CN112306703A (en)* | 2019-07-29 | 2021-02-02 | 华为技术有限公司 | A critical section execution method and device in a NUMA system |
| CN112306698A (en)* | 2019-07-29 | 2021-02-02 | 华为技术有限公司 | A critical section execution method and device in a NUMA system |
| CN112306698B (en)* | 2019-07-29 | 2025-02-11 | 华为技术有限公司 | A critical section execution method and device in a NUMA system |
| CN112306703B (en)* | 2019-07-29 | 2025-02-28 | 华为技术有限公司 | A critical section execution method and device in a NUMA system |
| US11379456B2 (en)* | 2020-10-01 | 2022-07-05 | Oracle International Corporation | Efficient adjustment of spin-locking parameter values |
| US11868261B2 (en) | 2021-07-20 | 2024-01-09 | Oracle International Corporation | Prediction of buffer pool size for transaction processing workloads |
| CN115934370A (en)* | 2022-12-23 | 2023-04-07 | 科东(广州)软件科技有限公司 | Spin lock acquisition method, device, equipment and storage medium |
| CN120407242A (en)* | 2025-07-07 | 2025-08-01 | 中国兵器装备集团兵器装备研究所 | A method and device for optimizing queue spin locks in high concurrency scenarios |
| Publication number | Publication date |
|---|---|
| CN104932933B (en) | 2018-11-13 |
| Publication | Publication Date | Title |
|---|---|---|
| CN104932933B (en) | A kind of method and device obtaining spin lock | |
| US8209690B2 (en) | System and method for thread handling in multithreaded parallel computing of nested threads | |
| CN108139946B (en) | Method for efficient task scheduling in the presence of conflicts | |
| KR102466984B1 (en) | Improved function callback mechanism between a central processing unit (cpu) and an auxiliary processor | |
| KR20180053359A (en) | Efficient scheduling of multi-version tasks | |
| US20150113252A1 (en) | Thread control and calling method of multi-thread virtual pipeline (mvp) processor, and processor thereof | |
| US8661175B2 (en) | Multiprocessor synchronization using region locks | |
| CN108351783A (en) | Method and device for processing tasks in multi-core digital signal processing system | |
| JP2018534675A (en) | Task subgraph acceleration by remapping synchronization | |
| CN104239134B (en) | The task management method and device of a kind of many-core system | |
| CN107704310B (en) | A method, apparatus and device for realizing container cluster management | |
| CN106385377B (en) | Information processing method and system | |
| JP7122299B2 (en) | Methods, apparatus, devices and storage media for performing processing tasks | |
| EP2908248B1 (en) | Memory data pushing method and device | |
| CN106325996A (en) | GPU resource distribution method and system | |
| CN112241398A (en) | A data migration method and system | |
| WO2016202153A1 (en) | Gpu resource allocation method and system | |
| CN109819674B (en) | Computer storage medium, embedded scheduling method and system | |
| CN109840151B (en) | Load balancing method and device for multi-core processor | |
| JP2014191655A (en) | Multiprocessor, electronic control device, and program | |
| US9542236B2 (en) | Efficiency sequencer for multiple concurrently-executing threads of execution | |
| US20140053162A1 (en) | Thread processing method and thread processing system | |
| CN112306703B (en) | A critical section execution method and device in a NUMA system | |
| CN116243983A (en) | Processor, integrated circuit chip, instruction processing method, electronic device, and medium | |
| CN104932947A (en) | Barrier synchronization method and device |
| Date | Code | Title | Description |
|---|---|---|---|
| C06 | Publication | ||
| PB01 | Publication | ||
| C10 | Entry into substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| GR01 | Patent grant | ||
| GR01 | Patent grant | ||
| TR01 | Transfer of patent right | ||
| TR01 | Transfer of patent right | Effective date of registration:20211221 Address after:450046 Floor 9, building 1, Zhengshang Boya Plaza, Longzihu wisdom Island, Zhengdong New Area, Zhengzhou City, Henan Province Patentee after:xFusion Digital Technologies Co., Ltd. Address before:518129 Bantian HUAWEI headquarters office building, Longgang District, Guangdong, Shenzhen Patentee before:HUAWEI TECHNOLOGIES Co.,Ltd. |