Spin lock optimization method and electronic equipment
By creating a spin lock structure and a request node structure to form a waiting queue, and processing the thread code in the waiting queue after the spin lock holder completes the critical section code, the performance bottleneck of spin locks in multi-core processors is solved, and cache consistency overhead, queue delay and multi-core scalability are optimized, thereby improving system performance.
Patent Information
- Application Number
- CN202511171852.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-20
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2045-08-20
AI Technical Summary
Existing spin locks have performance bottlenecks in multi-core processors, including cache consistency overhead, wait queue delays and scalability issues. In particular, the performance is insufficient in high-concurrency scenarios, affecting system stability.
Create a spin lock structure, including a lock holder pointer and a request node structure, to form a waiting queue. After completing the critical section code, the spin lock holder traverses the waiting queue to execute the critical section code of other threads, and processes the threads in the waiting queue without releasing the lock immediately, reducing thread wake-up and context switching.
It significantly improves the performance of spin locks in multi-core processor environments, reduces cache inconsistency overhead, and improves lock release efficiency and overall system efficiency, especially throughput in high-contention scenarios.
Smart Images

Figure CN120670180A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a spin lock optimization method and electronic device. Background Art
[0002] In modern multi-core processor architectures, efficient contention-based access to shared resources has become a significant technical bottleneck in areas such as operating systems, cloud computing platforms, and real-time control systems. To address this, spinlocks are widely used as a lightweight synchronization mechanism, but their performance issues are becoming increasingly prominent in high-concurrency scenarios.
[0003] Classic spin lock technology has been gradually evolving. The first generation of spin locks is a simple non-queued lock, represented by the Test-and-Set lock. The lock flag is directly manipulated through atomic instructions, and the thread spins and waits for the flag to become available.
[0004] Second-generation spin locks are queue-based fair locks, exemplified by the MCS lock (developed by John Mellor-Crummey and Michael Scott). These locks construct a one-way linked list queue with waiting threads, each containing a successor pointer and a wait flag. Each waiting thread creates a node containing its own wait state and links it to the end of the linked list. The thread continuously polls its own node to determine whether it has acquired the lock. The predecessor node wakes up by modifying the wait flag of the successor node.
[0005] In the aforementioned non-queued spin lock, all threads compete for the same cache line flag, leading to severe bus storms and suitable only for dual-core or low-contention scenarios. In contrast, the MCS lock holder must strictly follow the "execute critical section → release the lock → wake up the successor" sequence. If the critical section execution time is long, the spin wait time of the successor node will be unnecessarily extended. For example, in real-time control systems, this can lead to excessive task scheduling delays and cause system stability issues. Summary of the Invention
[0006] The present application provides a spin lock optimization method and electronic device to at least solve the performance bottlenecks of spin locks in multi-core processors in related technologies, specifically including cache consistency overhead, waiting queue delay problems, and the problem that its scalability decreases with the increase of the number of cores. It breaks through the performance bottleneck of traditional spin locks in multi-core environments, realizes all-round optimization of cache consistency overhead, queue delay, energy efficiency and multi-core scalability, and significantly improves the performance of spin locks in multi-core processor environments.
[0007] This application provides a spin lock optimization method, including: Creating and initializing a spin lock structure, wherein the spin lock structure includes a lock holder pointer; When each thread acquires the spin lock, a corresponding request node structure is created to form a waiting queue, wherein the request node structure includes a node pointer, a critical section function pointer and a lock structure pointer, the node pointer is used to link the request node structure to the waiting queue, the critical section function pointer points to the critical section code that the thread needs to execute, and the lock structure pointer is used to point to the spin lock structure; In the waiting queue, determining spin lock holders among a plurality of threads based on the lock holder pointer; After the processor core corresponding to the spin lock holder completes execution of the critical section code of the corresponding thread, the waiting queue is traversed, the critical section function pointer of each waiting node is read, and the critical section code in each request node structure is executed in sequence on the current processor core based on the critical section function pointer, wherein, after the processor core corresponding to the spin lock holder completes execution of the critical section code of the corresponding thread, the spin lock is not released immediately, and threads other than the thread corresponding to the processor core corresponding to the spin lock holder are not awakened by the corresponding thread itself to execute the critical section code.
[0008] The present application also provides an electronic device, comprising a memory, a processor, and a program stored in the memory and executable on the processor, wherein when the processor executes the program, the above-mentioned spin lock optimization method is implemented.
[0009] Through the present application, a spin lock structure is created and initialized, wherein the spin lock structure includes a lock holder pointer; when each thread acquires the spin lock, a corresponding request node structure is created to form a waiting queue, wherein the request node structure includes a node pointer, a critical section function pointer and a lock structure pointer, the node pointer is used to link the request node structure to the waiting queue, the critical section function pointer points to the critical section code that the thread needs to execute, and the lock structure pointer is used to point to the spin lock structure; in the waiting queue, the spin lock holder among multiple threads is determined based on the lock holder pointer; after the processor core corresponding to the spin lock holder completes the execution of the critical section code of the corresponding thread, the waiting queue is traversed, the critical section function pointer of each waiting node is read, and the critical section code in each request node structure is executed in sequence on the current processor core based on the critical section function pointer, wherein, after the processor core corresponding to the spin lock holder completes the execution of the critical section code of the corresponding thread, the spin lock is not released immediately, and threads other than the thread corresponding to the processor core corresponding to the spin lock holder are not awakened by the corresponding thread itself to execute the critical section code. As a result, this method breaks through the performance bottleneck of traditional spin locks in multi-core environments, achieves all-round optimization of cache consistency overhead, queue delay, energy efficiency and multi-core scalability, and significantly improves the performance of spin locks in multi-core processor environments. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] In order to more clearly illustrate the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0011] Figure 1 Flowchart of a spin lock optimization method according to an embodiment of the present application; Figure 2 A flowchart of a spin lock optimization method according to a specific example of the present application; Figure 3 Schematic diagram of a block diagram of an electronic device according to an embodiment of the present application.
[0012] Reference numerals: 200 - electronic device, 210 - memory, 220 - processor. DETAILED DESCRIPTION
[0013] The following describes in detail embodiments of the present application, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, and should not be construed as limiting the present application.
[0014] The spin lock optimization method and electronic device proposed in the embodiments of the present application are described below with reference to the accompanying drawings.
[0015] Figure 1 Flowchart of a spin lock optimization method according to an embodiment of the present application.
[0016] like Figure 1 As shown, the spin lock optimization method of the embodiment of the present application may include the following steps: S1, create and initialize a spin lock structure, wherein the spin lock structure includes a lock holder pointer.
[0017] S2, when each thread acquires the spin lock, creates a corresponding request node structure to form a waiting queue, wherein the request node structure includes a node pointer, a critical section function pointer and a lock structure pointer. The node pointer is used to link the request node structure to the waiting queue, the critical section function pointer points to the critical section code that the thread needs to execute, and the lock structure pointer is used to point to the spin lock structure.
[0018] S3, in the waiting queue, determines the spin lock holders among the multiple threads based on the lock holder pointers.
[0019] S4, after the processor core corresponding to the spin lock holder completes execution of the critical section code of the corresponding thread, it traverses the waiting queue, reads the critical section function pointer of each waiting node, and executes the critical section code in each request node structure in sequence on the current processor core based on the critical section function pointer, wherein, after the processor core corresponding to the spin lock holder completes execution of the critical section code of the corresponding thread, the spin lock is not released immediately, and threads other than the thread corresponding to the processor core corresponding to the spin lock holder are not awakened by the corresponding thread itself to execute the critical section code.
[0020] Specifically, a spinlock structure is first created and initialized. The spinlock structure may include a lockholder pointer. Specifically, the lockholder pointer points to the thread currently holding the spinlock. If this pointer is null, the lock is not currently held by any thread. If it points to a thread, the thread is the lockholder. For example, a spinlock structure instance can be created through dynamic memory allocation. For example, a memory allocation mechanism can be used to allocate sufficient memory space to store the spinlock structure members. Alternatively, the spinlock structure instance can be statically allocated in the global or local area of the program. This approach allocates memory at program startup, eliminating the need for dynamic allocation at runtime. Initializing the lockholder pointer sets the lockholder pointer to a null value, indicating that the spinlock is not held by any thread after initialization. Any thread attempting to acquire the lock for the first time can determine that the lock is available by checking that this pointer is null. This ensures that the spinlock structure is correctly initialized, preparing for subsequent threads to acquire the lock and form a waiting queue.
[0021] In a spinlock implementation, each thread creating a request node structure when attempting to acquire the lock must create one. This structure records the thread's request information, including the critical section code the thread needs to execute and the thread's identity. A wait queue is formed, linking multiple request nodes together via node pointers. A lock structure is also created, associating the request node with the spinlock structure via a lock structure pointer to ensure that the thread can interact correctly with the lock. The request node structure consists of three key members: a node pointer, used to link the request node to the wait queue. When creating a request node, the node pointer is typically initialized to NULL, indicating that the node currently has no next node. When a thread adds a request node to the wait queue, the node pointer is set to the end of the queue. A critical section function pointer, used to point to the critical section code the thread needs to execute. When creating a request node, the thread initializes the critical section function pointer to the function that points to the critical section code it needs to execute. When the lock holder traverses the wait queue and executes the critical section code, the function pointed to by this pointer is called. The lock structure pointer is used to point to the spin lock structure to ensure the association between the request node and the lock structure. When creating the request node, the thread will initialize the lock structure pointer to point to the global spin lock structure. Through the lock structure pointer, the request node can access the lock status information, such as the lock holder pointer and the head pointer of the waiting queue.
[0022] When creating a request node structure, sufficient memory space can be allocated for the request node structure. The thread needs to allocate a piece of memory that is large enough to store the three members of the request node structure (node pointer, critical section function pointer, and lock structure pointer. After the allocation is completed, the thread obtains an uninitialized request node structure instance. The node pointer of the request node is initialized to NULL, that is, the thread sets the node pointer of the request node to NULL, indicating that the node does not currently point to the next node. The critical section function pointer of the request node is initialized to point to the critical section code that the thread needs to execute. The lock structure pointer of the request node is initialized to point to the global spin lock structure. When creating and initializing the request node structure After the thread acquires the lock, it needs to add the node to the waiting queue. For example, the thread checks the lock status using the lock holder pointer. If the lock holder pointer is null, it indicates that the lock is not held by any thread, and the thread can attempt to acquire the lock directly. If the lock holder pointer is not null, it indicates that the lock is held by another thread, and the thread needs to enter the waiting queue. If the waiting queue is empty, the thread sets the request node to the head of the waiting queue. If the waiting queue is not empty, the thread links the request node to the tail of the queue. This mechanism enables spin locks to efficiently manage requests from multiple threads, ensuring that threads in the waiting queue are properly processed when the lock is released.
[0023] In the waiting queue, the spin lock holder among multiple threads can be determined based on the lock holder pointer. The lock holder pointer is a key member of the spin lock structure, used to identify the thread currently holding the lock. Its functions include: Quickly determining the lock status: By checking whether the lock holder pointer is null, you can quickly determine whether the lock is already held by another thread. Determining the lock holder: If the lock holder pointer is not null, the thread it points to is the thread currently holding the lock. For example, the spin lock holder can be determined through a pre-set correspondence. For example, the relationship between the lock holder pointer and the spin lock holders in multiple threads can be pre-determined. Once the lock holder pointer is determined, the spin lock holder can be obtained by directly calling the correspondence.
[0024] After determining the spin lock holder, the processor core corresponding to the spin lock holder first executes the critical section code of the corresponding thread. After the spin lock holder's processor core completes execution of the critical section code for the corresponding thread, it traverses the wait queue, reads the critical section function pointer of each wait node, and then, based on the critical section function pointer, executes the critical section code in each request node structure in sequence on the current processor core. Critical section code refers to code segments that need to be executed synchronously in a multi-threaded environment. These code segments often access shared resources (such as global variables, files, and devices), so it is necessary to ensure that only one thread can execute them at a time. After the spin lock holder completes execution of its critical section code, it traverses the wait queue. While traversing the wait queue, the spin lock holder reads the critical section function pointer of each wait node. This step ensures that the holder knows the entry point of the critical section code for each thread. The spin lock holder sequentially executes the critical section code in each request node structure on the current processor core. This is accomplished by calling the critical section function pointer of each thread. By directly executing the critical section code of other threads on the current processor core, the overhead of thread switching and context switching is avoided, significantly improving system performance, especially in high-concurrency scenarios. That is, the lock holder reads the critical section function pointer of each request node and executes the function on the current processor core, which means that the lock holder executes the critical section code of the waiting thread instead of the waiting thread.
[0025] It's important to note that after executing the critical section code of the current thread, the spin lock holder does not immediately release the lock, but instead continues to hold it so that it can execute the critical section code of other threads in the waiting queue. This design allows the lock holder to batch process the critical section code of multiple threads on the current processor core, reducing the overhead of frequent lock release and reacquisition. Furthermore, while the spin lock holder is executing the critical section code of other threads in the waiting queue, these threads are not awakened and attempt to acquire the lock. This prevents threads from repeatedly executing critical section code, ensuring that each thread's critical section code is executed only once. This significantly reduces the overhead of thread wakeups and context switches, improving overall system efficiency.
[0026] For example, suppose threads A, B, and C attempt to acquire a lock in sequence and enter the wait queue. After the lock holder, thread D, completes execution of its own critical section code, it begins traversing the wait queue, beginning at the head node of the wait queue. This means thread D reads the critical section function pointer in thread A's request node and uses it to determine the corresponding critical section code. After execution completes, thread D can continue executing the corresponding critical section code for threads B and C. Thread D then checks whether it has returned to the starting node and, if so, terminates its traversal.
[0027] Therefore, by actively executing the critical section code by the lock holder, context switching between threads is avoided, the system efficiency is improved, the access of multiple threads to the same cache line is reduced, the overhead of cache inconsistency is reduced, and the lock release efficiency is significantly improved by batch processing the requests in the waiting queue. Especially in high-contention scenarios, the throughput is significantly improved.
[0028] According to one embodiment of the present application, when each thread acquires a spin lock, a corresponding request node structure is created to form a waiting queue, including: among multiple threads, there is a thread that first acquires the corresponding spin lock, and the lock holder pointer is a null pointer, the lock holder pointer is set to the identifier of the current thread based on the atomic exchange operation, and the request node structure corresponding to the thread is used as the head of the waiting queue; when the lock holder pointer is not a null pointer, the corresponding request node structure is linked in sequence as the tail of the waiting queue based on the node pointer and the order in which each thread acquires the spin lock to form a waiting queue.
[0029] Specifically, when each thread acquires a spin lock, it creates a corresponding request node structure to form a wait queue. First, among multiple threads, it determines whether there is a thread that has first acquired the corresponding spin lock. The lock holder pointer is then checked. If the lock holder pointer is null, an atomic swap operation is performed to set the lock holder pointer to the current thread's identifier, and the request node structure corresponding to that thread is placed at the head of the wait queue. In other words, when the lock holder pointer is null, it indicates that the lock is not held by any thread. At this point, the thread can attempt to acquire the lock. Specifically, the thread attempts to set the lock holder pointer to its own thread identifier through an atomic swap operation. If the atomic swap operation succeeds, the thread successfully acquires the lock and becomes the lock holder. The thread then creates a request node structure and initializes its members. Since the thread is the first to acquire the lock, its request node structure is set to the head of the wait queue.
[0030] When the lock holder pointer is not null, it indicates that the lock is already held by another thread. At this point, the thread must enter the waiting queue. Specifically, the thread creates a request node structure and initializes its members. The thread then obtains the linked list head pointer from the lock structure, finds the tail node of the waiting queue, sets the node pointer of the current request node to NULL, and sets the node pointer of the tail node to the current request node, thus linking the current request node to the tail of the waiting queue.
[0031] Suppose threads A, B, and C attempt to acquire a lock in sequence. Thread A attempts to acquire the lock: the lock holder pointer is null. Thread A atomically sets the lock holder pointer to its own thread ID. Thread A creates a request node structure and sets it to the head of the wait queue. The linked list head pointer in the lock structure points to thread A's request node. Thread B attempts to acquire the lock: the lock holder pointer is not null (pointing to thread A), and thread B enters the wait queue. Thread B creates a request node structure and links it to the end of the wait queue. The node pointer of thread A's request node points to thread B's request node. Thread C attempts to acquire the lock: the lock holder pointer is not null (pointing to thread A), and thread C enters the wait queue. Thread C creates a request node structure and links it to the end of the wait queue. The node pointer of thread B's request node points to thread C's request node.
[0032] It's important to note that the atomic swap operation is key to ensuring thread-safe lock acquisition. It prevents multiple threads from simultaneously setting the lock holder pointer to their own identifier, thus avoiding data races. A thread identifier must be unique to ensure that each thread can be correctly distinguished. This identifier can take various forms, depending on the operating system and programming environment. Specifically, a thread identifier is a value provided by the operating system or thread library to uniquely identify a thread. The type and representation of a thread identifier may vary across different programming environments.
[0033] Through these steps, threads can effectively manage the waiting queue when acquiring a spin lock. The request node of the first thread to acquire the lock becomes the head of the queue, and subsequent threads link their request nodes to the tail of the queue. This process ensures that threads wait for the lock to be released in order, improving the fairness and efficiency of the lock.
[0034] According to one embodiment of the present application, determining the spin lock holders among multiple threads based on the lock holder pointer includes: detecting whether the lock holder pointer is a null pointer; and determining the current thread as the spin lock holder when the lock holder pointer is not a null pointer.
[0035] Specifically, when determining the spin lock holder among multiple threads based on the lock holder pointer, the lock holder pointer is checked to see if it is a null pointer. If it is not, the current thread is determined to be the spin lock holder. In other words, by checking whether the lock holder pointer is null, it can quickly determine whether the lock is already held by another thread. If the lock holder pointer is null, it indicates that the lock is not held by any thread, and the current thread can attempt to acquire the lock. If the lock holder pointer is not null, it indicates that the lock is already held by another thread, and the current thread must enter the waiting queue.
[0036] Thus, through the above steps, a thread can effectively manage the lock holder state when attempting to acquire a spin lock. By checking whether the lock holder pointer is null, the thread can quickly determine the lock status and decide whether to attempt to acquire the lock or enter the wait queue. Through atomic operations, the thread can safely set the lock holder pointer, ensuring that the lock transfer is thread-safe. This process not only improves lock efficiency but also ensures system fairness and thread safety.
[0037] According to one embodiment of the present application, the request node structure further includes an atomic wait flag. After the corresponding request node structures are linked as the tail of a wait queue based on the node pointer and the order in which each thread acquires the spin lock, the spin lock optimization method further includes setting the atomic wait flag to a preset value to enter a spin wait state. The preset value can be determined based on actual conditions.
[0038] Specifically, the request node structure may also include an atomic wait flag, which is used to mark whether the thread is in a wait state. That is, based on the node pointer and the order in which each thread acquires the spin lock, the corresponding request node structure is linked as the tail of the wait queue, and the atomic wait flag may be set to a preset value to enter the spin wait state.
[0039] Specifically, a thread creates a request node structure and initializes its members. Based on the node pointer and the order in which threads acquire locks, the thread sequentially links the request node structure to the end of the wait queue. The thread then sets the atomic wait flag to a preset value (such as true or 1), indicating that the thread is in a wait state. This means that the thread is not suspended while waiting for the lock to be released. Instead, it uses spin-waiting to reduce CPU usage, significantly improving system performance in high-concurrency scenarios. This reduces thread switching overhead, ensuring lock fairness and high system concurrency.
[0040] According to one embodiment of the present application, the spin lock optimization method also includes: after the spin lock holder completes executing the critical section code corresponding to each thread, clearing the preset value of the atomic wait flag to zero; after clearing the atomic wait flag, recording the corresponding operation log, wherein the operation log includes at least one of the thread identity and the clearing time.
[0041] Specifically, after the spin lock holder completes execution of the critical section code for each thread, the preset value of the atomic wait flag is cleared to 0. This process is an important part of the spin lock mechanism, used to ensure that the thread can correctly exit the wait state and continue execution.
[0042] The atomic wait flag is a flag used to indicate whether a thread is in a wait state. When a thread enters the wait queue, the flag is set to a preset value (such as true or 1), indicating that the thread is waiting for the lock to be released. When the lock holder completes execution of the critical section code, it must clear the flag to notify the waiting thread that it can continue execution. For example, after the lock holder thread completes execution of its critical section code and prepares to release the lock, it begins traversing the wait queue, finds the first node in the queue, and atomically clears the atomic wait flag of the first node to zero. This step ensures that the flag is cleared atomically, preventing race conditions caused by multiple threads modifying the flag simultaneously. After clearing the flag, the lock holder wakes up the corresponding thread. The awakened thread checks its atomic wait flag and, finding that it has been cleared, exits the spin-wait state. Once the thread's atomic wait flag is cleared, it exits the spin-wait state. The thread checks the flag status. If it is zero, it indicates that the lock has been released and the thread can continue execution. This process reduces the waiting time of threads and improves the efficiency of the system.
[0043] After clearing the atomic wait flag, relevant operation logs can be recorded, including at least one of the thread identity and the clearing time. This log information is very helpful for subsequent troubleshooting and performance analysis. The thread identity helps identify the thread that performed the operation, facilitating tracing the source of the problem, while the clearing time helps determine the timing of the operations, facilitating analysis of the time when the problem occurred.
[0044] Therefore, after completing the execution of the critical section code, the spin lock holder can efficiently clear the atomic wait flag and wake up the waiting thread, ensuring that the thread can correctly exit the waiting state and continue execution. The log information is also very helpful for subsequent troubleshooting and performance analysis.
[0045] According to one embodiment of the present application, the spin lock optimization method also includes: after the spin lock holder completes executing the critical section code corresponding to all threads, setting the lock holder pointer to a null pointer based on an atomic operation; after setting the lock holder pointer to a null pointer, recording a corresponding operation log, wherein the operation log includes at least one of a thread identity and an operation time.
[0046] Specifically, in spinlock optimization methods, a key step is to set the lockholder pointer to a null pointer after the lockholder completes the critical section code corresponding to all threads. This operation ensures the lock is correctly released and allows other threads to acquire the lock. The lockholder pointer (owner) is a key member of the spinlock structure, used to identify the thread currently holding the lock. Its functions include: quickly determining the lock status: By checking whether the lockholder pointer is null, you can quickly determine whether the lock is already held by another thread and determine the lockholder: If the lockholder pointer is not null, the thread it points to is the current thread holding the lock.
[0047] In the spin lock optimization method, the lock holder not only executes its own critical section code but also actively executes the critical section code of other threads in the wait queue. Specifically, the lock holder starts at the head of the wait queue and processes each request node in turn. The lock holder reads the critical section function pointer for each request node and executes that function on the current processor core. After executing the critical section code for each request node, the lock holder atomically clears the wait flag for that node and wakes up the corresponding thread. After the lock holder completes the critical section code for all threads, it sets the lock holder pointer to null. That is, after completing its own critical section code, the lock holder begins traversing the wait queue, processing each request node in turn to ensure that the critical section code of all waiting threads has been executed. After completing all tasks in the wait queue, the lock holder sets the lock holder pointer to null, indicating that the lock has been released and other threads can attempt to acquire the lock. Subsequent threads attempting to acquire the lock will find the lock holder pointer null, giving them the opportunity to atomically set the lock holder pointer to their own thread identifier and acquire the lock. Among them, atomic operations can be used to set the lock holder pointer to a null pointer, indicating that the lock is not currently held by any thread. Atomic operations can ensure correctness in a multi-threaded environment, that is, atomic operations can ensure that this setting operation will not be interrupted by other threads, thereby avoiding race conditions.
[0048] In addition, after setting the lock holder pointer to a null pointer, relevant operation logs can be recorded, such as recording the unique identifier of the current thread, which helps in subsequent analysis of which thread performed the operation, and recording the specific timestamp of setting the lock holder pointer to a null pointer, which helps in analyzing the timing relationship of operations, especially in high concurrency scenarios.
[0049] Suppose thread A is the current lock holder, and threads B and C are in the wait queue. After completing its critical section code, thread A prepares to release the lock. Thread A starts at the head of the wait queue and processes each request node in turn. Thread A reads the critical section function pointer in thread B's request node and executes that function. Thread A atomically clears thread B's wait flag and wakes up thread B. Thread A reads the critical section function pointer in thread C's request node and executes that function. Thread A atomically clears thread C's wait flag and wakes up thread C. After completing all tasks in the wait queue, thread A sets the lock holder pointer to null. This operation is performed atomically, ensuring that only one thread can successfully set the lock holder pointer at a time. After waking up, threads B and C find that the lock holder pointer is null and attempt to atomically set the lock holder pointer to their own thread identifier. If thread B successfully sets the lock holder pointer, thread B becomes the new lock holder. If thread C successfully sets the lock holder pointer, thread C becomes the new lock holder.
[0050] Through the above steps, after the spin lock holder has executed the critical section code corresponding to all threads, the lock holder pointer is set to a null pointer using an atomic operation and the relevant operation log is recorded. This ensures that the lock is correctly released and prevents other threads from mistakenly believing that the lock is still held. This mechanism not only improves the reliability of the lock, but also ensures the consistency of the system state, improving the overall system performance and stability.
[0051] According to one embodiment of the present application, the spin lock structure also includes a linked list head pointer, which points to the first node of the waiting queue. Initializing the spin lock structure includes: setting the lock holder pointer to null and setting the linked list head pointer to null.
[0052] Specifically, in the spinlock implementation, the spinlock structure includes not only a lockholder pointer but also a linked list head pointer. The linked list head pointer points to the first node in the wait queue and is used to manage threads waiting to acquire the lock. When initializing the spinlock structure, the lockholder pointer and the linked list head pointer can be set to null. This ensures that when a thread attempts to acquire the lock, it checks whether the lockholder pointer is null. If it is null, the thread can atomically attempt to set the lockholder pointer to its own thread ID, thereby acquiring the lock. If the lockholder pointer is not null, the thread will believe that the lock is already held by another thread and enter the wait queue. Therefore, setting the lockholder pointer to null during initialization ensures that the lock is initially usable and prevents threads from misjudging the lock state. When the lock is released, the lockholder must set the lockholder pointer to null to indicate that the lock has been released. After the lockholder completes execution of the critical section code, it sets the lockholder pointer to null. When other threads try to acquire the lock, they will find that the lock holder pointer is null, giving them the opportunity to acquire the lock through atomic operations. By setting the lock holder pointer to null, the release of the lock is ensured to be fair, and other threads can try to acquire the lock in sequence.
[0053] During spin lock initialization, the linked list head pointer is set to null, indicating that the wait queue is currently empty. When the linked list head pointer is null, it indicates that no threads are waiting to acquire the lock. Therefore, the first thread attempting to acquire the lock can directly acquire the lock without entering the wait queue. If the linked list head pointer is not null, the thread will believe that other threads are waiting to acquire the lock and will enter the wait queue. Therefore, setting the linked list head pointer to null during initialization ensures that the wait queue is initially empty, avoiding unnecessary waiting.
[0054] Therefore, by initializing the lock holder pointer to null, the context switching of the thread when trying to acquire the lock is reduced. By initializing the linked list head pointer to null, the management of the waiting queue is ensured to be efficient and orderly, thereby improving the concurrency performance of the system.
[0055] Furthermore, in a multithreaded environment, some threads may be more important than others and require priority in acquiring locks. A priority scheduling mechanism can be introduced to determine which threads are granted locks based on their priorities. Specifically, a priority queue is introduced within the waiting queue to prioritize high-priority threads. For example, within the waiting queue, the thread's priority is dynamically increased based on its waiting time to prevent thread starvation. When a high-priority thread waits for a lock held by a low-priority thread, the low-priority thread's priority is temporarily increased to expedite the lock's release. Furthermore, in traditional spin lock implementations, threads spin continuously while waiting for a lock to be released, potentially wasting CPU resources. A dynamically adjustable spin-wait strategy can be introduced to dynamically adjust the number or duration of spins based on system load and lock contention, thereby reducing unnecessary CPU usage. A threshold for the number of spins above which threads enter a sleep state reduces CPU usage, or a threshold for the spin time above which threads enter a sleep state is set. Dynamically adjusting the spin strategy based on system load, reducing the number or duration of spins when system load is high, can further improve performance and efficiency.
[0056] According to one embodiment of the present application, when determining the spin lock holder among multiple threads, the spin lock optimization method also includes: comparing the priority of the current thread with the priorities of other waiting threads, or determining the thread that obtains the spin lock earliest among multiple threads; when the priority of the current thread is higher than the priorities of other waiting threads and the lock holder pointer is not empty, the current thread is determined as the spin lock holder, or, when the thread that obtains the spin lock earliest and the lock holder pointer is not empty, the current thread is determined as the spin lock holder.
[0057] Specifically, when determining the spin lock holder among multiple threads, the current thread's priority can be compared with the priorities of other waiting threads to determine which thread should acquire the lock. This mechanism is often used to ensure that higher-priority threads acquire the lock more quickly, thereby improving system responsiveness and fairness. Each thread has a priority, typically set by the operating system or application. The priority can be an integer, with higher values representing higher priority. First, the waiting queue is traversed, the priority of each waiting thread is obtained, and the current thread's priority is compared with the priorities of the other waiting threads. If the current thread's priority is higher than that of all other waiting threads, it is given priority to acquire the lock. If the current thread has the highest priority and the lock holder pointer is not null (indicating that the lock is not currently held by another thread), the current thread is determined to be the lock holder.
[0058] Alternatively, the earliest thread to attempt to acquire a spin lock can be determined among multiple threads. This method is often used to implement fair locks, ensuring that threads acquire locks in the order in which they were requested. When a thread attempts to acquire a lock, the timestamp of the attempt is recorded. The wait queue is then traversed, and the timestamps of each thread's attempts are compared to determine the earliest thread to attempt to acquire the lock. If the current thread is the earliest thread to attempt to acquire the lock, and the lock holder pointer is not null (indicating that the lock is not currently held by another thread), the current thread is determined to be the lock holder.
[0059] Therefore, by comparing priorities, high-priority threads can acquire locks more quickly, improving system responsiveness and fairness. This is suitable for real-time systems and scenarios where critical tasks must be prioritized. By determining the earliest thread to attempt to acquire the lock, threads are ensured to acquire the lock in the order in which they were requested, avoiding thread starvation. This is suitable for scenarios requiring fair scheduling, such as multi-threaded servers and concurrent systems. In summary, by comparing the current thread's priority with the priorities of other waiting threads, or determining the earliest thread among multiple threads to attempt to acquire a spin lock, the appropriate thread can be effectively selected as the lock holder. This mechanism not only improves the fairness of lock allocation but also ensures that high-priority tasks can be executed in a timely manner, improving the overall performance and responsiveness of the system.
[0060] According to one embodiment of the present application, the spin lock optimization method also includes: during the acquisition and release process of the spin lock, detecting whether the spin lock is in an abnormal state, wherein the abnormal state includes at least one of the spin lock being occupied and the spin lock being damaged; switching the spin lock to a backup spin lock, and synchronizing the status information of the spin lock to the backup spin lock.
[0061] Specifically, during the acquisition and release process of the spin lock, it is necessary to detect whether the lock is in an abnormal state. Abnormal states include but are not limited to the spin lock being occupied and the spin lock being damaged. Among them, the spin lock being occupied means that a thread holds the lock for too long, or the lock is held by a thread that should not hold it. A reasonable timeout period can be set. If a thread holds the lock for longer than this timeout period, it is considered that the lock may be occupied. By checking the thread status of the lock holder, if the holder thread has ended or is in an abnormal state, the lock is considered to be occupied. Spin lock damage means that the internal state of the lock is inconsistent, such as the lock holder pointer is invalid, the lock flag bit is wrong, etc. Check whether the lock holder pointer is empty or points to a valid thread, and check whether the lock flag bit is in a valid state, such as whether it is initialized or in an error state.
[0062] If a spinlock is detected to be in an abnormal state, the lock needs to be switched to a backup spinlock. The backup spinlock is a pre-prepared lock designed to take over lock management in the event of an abnormality in the primary lock. For example, during the acquisition and release of a spinlock, if an abnormal lock state is detected, the current thread's lock pointer can be switched from the primary lock to the backup lock, and the primary lock's state information can be synchronized with the backup lock to ensure that the backup lock can correctly take over lock management. This state information may include a pointer to the thread currently holding the lock, identifying which thread currently holds the lock. This involves copying the primary lock's holder pointer value to the backup lock's holder pointer. A wait queue may also be included—a data structure (typically a linked list or queue) containing the request nodes of all threads waiting for the lock. All nodes in the primary lock's wait queue and their status (such as thread identity and atomic wait flag) are copied to the backup lock's wait queue. Lock flags may also be included to indicate various lock states, such as whether the lock is initialized or in an error state. This involves copying the primary lock's flag values to the backup lock's flags. Thus, the lock acquisition or release operation can be continued on the backup lock. If the backup lock also has an exception, it can be further switched to another backup lock or other recovery measures can be taken.
[0063] Therefore, by detecting abnormal conditions during the acquisition and release of spin locks, switching the lock to a backup spin lock and synchronizing the status information when an abnormality is detected, we can significantly improve lock reliability and system stability. This mechanism not only ensures correct lock management, but also reduces system failures caused by lock anomalies, improving overall system performance and reliability.
[0064] The following combination Figure 2 To describe the method of this application.
[0065] As a specific example, the spin lock optimization method of the present application may include the following steps: S101, create and initialize the spin lock structure.
[0066] S102: Is there a thread among multiple threads that first acquires the corresponding spin lock, and is the lock holder pointer a null pointer? If yes, execute step S103; if not, execute step S101.
[0067] S103: Based on the atomic exchange operation, the lock holder pointer is set to the identifier of the current thread, the request node structure corresponding to the thread is used as the head of the waiting queue, and the current thread is determined as the spin lock holder.
[0068] S104: Determine whether the lock holder pointer is a null pointer. If yes, execute step S105; if not, execute step S101.
[0069] S105, based on the node pointer and the order in which each thread acquires the spin lock, the corresponding request node structure is linked to the head of the waiting queue in sequence to form a waiting queue, and the atomic wait flag is set to a preset value to enter the spin wait state.
[0070] S106, after the processor core corresponding to the spin lock holder completes execution of the critical section code of the corresponding thread, it traverses the waiting queue, reads the critical section function pointer of each waiting node, and executes the critical section code in each request node structure in sequence on the current processor core based on the critical section function pointer, and clears the preset value of the atomic wait flag to zero.
[0071] S017, after the spin lock holder completes executing the critical section codes corresponding to all threads, the lock holder pointer is set to a null pointer.
[0072] In summary, according to the spin lock optimization method of the embodiment of the present application, a spin lock structure is created and initialized, wherein the spin lock structure includes a lock holder pointer; when each thread acquires the spin lock, a corresponding request node structure is created to form a waiting queue, wherein the request node structure includes a node pointer, a critical section function pointer and a lock structure pointer, the node pointer is used to link the request node structure to the waiting queue, the critical section function pointer points to the critical section code that the thread needs to execute, and the lock structure pointer is used to point to the spin lock structure; in the waiting queue, the spin lock holder among multiple threads is determined based on the lock holder pointer; after the processor core corresponding to the spin lock holder completes the execution of the critical section code of the corresponding thread, the waiting queue is traversed, the critical section function pointer of each waiting node is read, and the critical section code in each request node structure is executed in sequence on the current processor core based on the critical section function pointer, wherein, after the processor core corresponding to the spin lock holder completes the execution of the critical section code of the corresponding thread, the spin lock is not released immediately, and threads other than the thread corresponding to the processor core corresponding to the spin lock holder are not awakened by the corresponding thread itself to execute the critical section code. As a result, this method breaks through the performance bottleneck of traditional spin locks in multi-core environments, achieves all-round optimization of cache consistency overhead, queue delay, energy efficiency and multi-core scalability, and significantly improves the performance of spin locks in multi-core processor environments.
[0073] Corresponding to the above embodiment, the present application also proposes an electronic device.
[0074] like Figure 3 As shown, the electronic device 200 of the embodiment of the present application may include: a memory 210, a processor 220, and a program stored in the memory 210 and executable on the processor 220. When the processor 220 executes the program, the above-mentioned spin lock optimization method is implemented.
[0075] According to the electronic device of the embodiment of the present application, by executing the above-mentioned spin lock optimization method, the performance bottleneck of traditional spin locks in a multi-core environment is broken through, and all-round optimization of cache consistency overhead, queue delay, energy efficiency and multi-core scalability is achieved, which significantly improves the performance of spin locks in a multi-core processor environment.
[0076] It should be noted that the logic and / or steps represented in flowcharts or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by, or in conjunction with, an instruction execution system, apparatus, or device (e.g., a computer-based system, a system including a processor, or other system that can fetch and execute instructions from an instruction execution system, apparatus, or device). For purposes of this specification, a "computer-readable medium" can be any device that can contain, store, communicate, propagate, or transport a program for use by, or in conjunction with, an instruction execution system, apparatus, or device. More specific examples (non-exhaustive list) of computer-readable media include the following: an electrical connection with one or more wires (electronic device), a portable computer disk cartridge (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and programmable read-only memory (EPROM or flash memory), fiber optic devices, and a portable compact disc read-only memory (CDROM). Furthermore, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium and then editing, interpreting or processing it in another suitable manner if necessary, and then storing it in a computer memory.
[0077] It should be understood that various parts of this application can be implemented using hardware, software, firmware, or a combination thereof. In the above-described embodiments, multiple steps or methods can be implemented using software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented using hardware, as in another embodiment, any one of the following technologies known in the art or a combination thereof can be used: a discrete logic circuit having logic gate circuits for implementing logic functions on data signals, an application-specific integrated circuit having suitable combinational logic gate circuits, a programmable gate array (PGA), a field-programmable gate array (FPGA), etc.
[0078] Throughout this specification, reference to terms such as "one embodiment," "some embodiments," "examples," "specific examples," or "some examples" means that a specific feature, structure, material, or characteristic described in conjunction with that embodiment or example is included in at least one embodiment or example of the present application. In this specification, schematic representations of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples.
[0079] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of the technical features being referred to. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of such features. Throughout the description of this application, "plurality" means at least two, for example, two, three, etc., unless otherwise specifically defined.
[0080] In this application, unless otherwise specified or limited, the terms "installed," "connected," "connect," "fixed," etc. should be understood in a broad sense. For example, they can refer to fixed connection, detachable connection, or integration; mechanical connection or electrical connection; direct connection or indirect connection through an intermediate medium; internal communication between two elements or interaction between two elements, unless otherwise specified. Those skilled in the art will understand the specific meanings of the above terms in this application based on specific circumstances.
[0081] Although the embodiments of the present application have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limitations on the present application. Ordinary technicians in this field can change, modify, replace and modify the above embodiments within the scope of the present application.
Claims
1. A spin lock optimization method, characterized in that: The method comprises: Creating and initializing a spin lock structure, wherein the spin lock structure includes a lock holder pointer; When each thread acquires the spin lock, a corresponding request node structure is created to form a waiting queue, wherein the request node structure includes a node pointer, a critical section function pointer and a lock structure pointer, the node pointer is used to link the request node structure to the waiting queue, the critical section function pointer points to the critical section code that the thread needs to execute, and the lock structure pointer is used to point to the spin lock structure; In the waiting queue, determining spin lock holders among a plurality of threads based on the lock holder pointer; After the processor core corresponding to the spin lock holder completes execution of the critical section code of the corresponding thread, the waiting queue is traversed, the critical section function pointer of each waiting node is read, and the critical section code in each request node structure is executed in sequence on the current processor core based on the critical section function pointer, wherein, after the processor core corresponding to the spin lock holder completes execution of the critical section code of the corresponding thread, the spin lock is not released immediately, and threads other than the thread corresponding to the processor core corresponding to the spin lock holder are not awakened by the corresponding thread itself to execute the critical section code.
2. The spin lock optimization method according to claim 1, wherein: When each thread acquires the spin lock, a corresponding request node structure is created to form a waiting queue, including: Among the multiple threads, if there is a thread that first obtains the spin lock and the lock holder pointer is a null pointer, the lock holder pointer is set to the identifier of the current thread based on the atomic exchange operation, and the request node structure corresponding to the thread is used as the head of the waiting queue; When the lock holder pointer is not a null pointer, the corresponding request node structures are sequentially linked as the tail of the waiting queue based on the node pointer and the order in which each thread acquires the spin lock to form a waiting queue.
3. The spin lock optimization method according to claim 1, wherein: The determining of the spin lock holders in the plurality of threads based on the lock holder pointers comprises: Detecting whether the lock holder pointer is a null pointer; When the lock holder pointer is not a null pointer, the current thread is determined to be the spin lock holder.
4. The spin lock optimization method according to claim 2, wherein: The request node structure further includes an atomic wait flag. After sequentially linking the corresponding request node structures as the tail of the wait queue based on the node pointer and the order in which each thread acquires the spin lock, the method further includes: The atomic wait flag is set to a preset value to enter a spin wait state.
5. The spin lock optimization method according to claim 4, wherein: The method further comprises: After the spin lock holder completes executing the critical section code corresponding to each thread, clearing the preset value of the atomic wait flag to zero; After clearing the atomic wait flag, a corresponding operation log is recorded, wherein the operation log includes at least one of a thread identity and a clearing time.
6. The spin lock optimization method according to claim 1, wherein: The method further comprises: After the spin lock holder completes executing the critical section code corresponding to all threads, setting the lock holder pointer to a null pointer based on an atomic operation; After setting the lock holder pointer to a null pointer, a corresponding operation log is recorded, wherein the operation log includes at least one of a thread identity and an operation time.
7. The spin lock optimization method according to claim 1, wherein: The spin lock structure further includes a linked list head pointer, which points to the first node of the waiting queue. The initialization spin lock structure includes: The lock holder pointer is set to null, and the linked list head pointer is set to null.
8. The spin lock optimization method according to claim 1, wherein: When determining the spin lock holders in the plurality of threads, the method further includes: Comparing the priority of the current thread with the priorities of other waiting threads, or determining the thread that acquires the spin lock earliest among multiple threads; When the priority of the current thread is higher than the priority of other waiting threads and the lock holder pointer is not empty, the current thread is determined as the spin lock holder; or, when the thread that obtains the spin lock earliest and the lock holder pointer is not empty, the current thread is determined as the spin lock holder.
9. The spin lock optimization method according to claim 1, wherein: The method further comprises: During the acquisition and release process of the spin lock, detecting whether the spin lock is in an abnormal state, wherein the abnormal state includes at least one of the spin lock being occupied and the spin lock being damaged; The spin lock is switched to a standby spin lock, and the state information of the spin lock is synchronized to the standby spin lock.
10. An electronic device, characterized in that: include: A memory, a processor, and a program stored in the memory and executable on the processor, wherein when the processor executes the program, the spin lock optimization method according to any one of claims 1 to 9 is implemented.
Citation Information
Patent Citations
Method, device and equipment for realizing spin lock during resource access
CN111984428A
Critical zone execution method and device in NUMA system
CN112306698A
Database concurrent spin lock locking and unlocking optimization method and equipment
CN116541402A
Method for realizing spin lock during resource access of multi-core CPU (Central Processing Unit) and storage medium
CN118331753A
Hardware-Assisted Spinlock
US20250190282A1
Cited By
Multi-core platform spin lock management system based on priority scheduling and control method thereof
CN121880034A
Priority scheduling based multi-core platform spinlock management system and control method thereof
CN121880034B