Read lock operation method, write lock operation method and system
By setting private reference counts on each core and performing read lock operations independently, the inefficiency problem of threads reading and writing operations on the same data in multiple computing core systems is solved, and efficient concurrent read lock operations are achieved.
Patent Information
- Application Number
- CN202111082328.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2013-10-15
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2033-10-15
AI Technical Summary
In a computer system with multiple computing cores, when threads of different cores read and write operations on the same data, in the prior art, the communication time and atomic operation time are increased, and the execution efficiency is achieved due to the need to synchronize the read_ref values in the cache of each core. Lower.
Use the private reference count corresponding to each core for read lock and write lock operations to avoid synchronizing private reference counts between each core and performing independent read lock and interpretation lock operations.
It improves the execution efficiency of different core threads when reading the same data, reduces the overhead of communication between cores, and improves the scalability of read locks, ensuring efficient concurrent addition and interpretation lock operations in multi-core environments.
Smart Images

Figure CN113835901B_ABST
Abstract
Description
Technical Field
[0001] Multiple embodiments of the present specification relate to the field of information technology, and more particularly to a read lock operation method, a write lock operation method and a system. Background Art
[0002] A thread, also known as a lightweight process (LWP), is a single sequential control flow in a process and is the smallest unit of program execution. In an operating system that introduces threads, processes are usually used as the basic unit for allocating resources, and threads are used as the basic unit for independent operation and independent scheduling. Threads can be executed concurrently, for example, multiple threads in a process can be executed concurrently. Threads in different processes can also be executed concurrently. In particular, in a computer system with multiple computing cores, such as a computer system with multiple CPU cores, threads on different cores can also be executed concurrently.
[0003] When multiple threads execute concurrently, they often need to access the same data. From the perspective of the accessed data, the data is shared by different threads. When multiple threads access the shared data, the integrity of the shared data needs to be guaranteed. For example, two threads cannot modify shared data at the same time; a thread cannot read half-modified shared data. The classic way is to use a lock mechanism. For example, a "read lock" is added to the data during a thread's read operation, and a "write lock" is added to the data during a thread's write operation. Before a process reads a piece of data, it first adds a read lock to the data, and then releases the read lock after the read operation is completed. Similarly, before a process writes a piece of data, it first adds a write lock to the data, and then releases the write lock after the write operation is completed. Usually read_ref is used as the reference count of the read operation thread, and writer_ID is used to represent the ID of the write operation thread.
[0004] If different threads perform read operations on the same data, multiple read locks can be added. For example, if thread 1 wants to perform a read operation on a piece of data, it will add a read lock to the data before performing the read operation. Specifically, it will add 1 to the value of read_ref (for example, the data type of read_ref is an integer, and the initial value is 0), and then read the data. During the reading process, thread 2 also wants to perform a read operation on the same data, so it will add 1 to the value of read_ref and read the data. At this time, the value of read_ref is 2. After thread 1 completes the read operation, it will subtract 1 from the value of read_ref and unlock it. At this time, the value of read_ref is 1. After that, thread 2 completes the read operation on the data, subtracts 1 from the value of read_ref, and unlocks it. At this time, the value of read_ref is 0. Read locks for the same data can be added repeatedly, so read locks are shared.
[0005] For different threads that perform write operations on the same data, only one write lock can be added. For example, if thread 1 wants to perform a write operation on a piece of data, it will add a write lock to the data before performing the write operation. Specifically, the value of writer_ID is updated to the ID of thread 1 (for example, the data type of writer_ID is integer, and the initial value is 0; the ID of any thread is not 0), and then the data is written. During the writing process, thread 2 also wants to perform a write operation on the same data, but because the value of writer_ID is not 0 at this time, thread 2 cannot add a write lock or write to the data. After thread 1 completes the write operation, the write lock is released, and the value of writer_ID is updated to 0. After thread 2 fails to add a write lock last time and waits for a period of time, it learns that the value of writer_ID is 0 at this time, and can add a write lock. After that, the value of writer_ID is updated to the ID of thread 2, and then the data is written. After thread 2 completes the write operation, the write lock is released, and the value of writer_ID is updated to 0. It can be seen that the write lock for the same data cannot be added repeatedly, so the write locks are mutually exclusive.
[0006] In addition, write lock and read lock are also mutually exclusive, that is, at any time, if a read lock is added to the same data, a write lock cannot be added again, and if a write lock is added, a read lock cannot be added again. In this way, before a thread reads a piece of data, it needs to check whether the writer_ID value of the data is 0. If it is 0, the read operation can be performed; if it is not 0, it needs to wait for the writer_ID value to become 0. Similarly, before a thread writes a piece of data, it needs to check whether the read_ref value of the data is 0. If it is 0, the write operation can be performed; if it is not 0, it needs to wait for the read_ref value to become 0. In fact, for adding a read lock, in more cases, in order to further avoid another thread adding a write lock for a write operation between the operation of checking the writer_ID value and the corresponding read operation, that is, in order to avoid the failure of conflict detection caused by this situation, after adding 1 to the read_ref value, it will also check again whether the writer_ID value at this time is 0. If it is not 0, the read operation is performed. Similarly, for write lock, in most cases, in order to further avoid another thread performing a read lock operation for a read operation between the operation of checking the read_ref value and the corresponding write operation, that is, to avoid the failure of conflict detection caused by this situation, after updating the value of writer_ID to the ID of the writing thread, it will be checked again whether the read_ref value at this time is 0. If it is not 0, the write operation will be performed.
[0007] The above thread changes the read_ref value and writer_ID value, which are atomic operations. Atomic operations are generally instructions provided by the CPU and are indivisible. When a thread executes an atomic operation, it will not be interrupted by other threads and will not switch to other threads. In other words, once this atomic operation starts, it will run until the operation ends.
[0008] In the process of implementing this application, the inventors found that there are at least the following problems in the prior art:
[0009] In a computer system with multiple computing cores, threads of different cores may perform read and write operations on the same data. In particular, it is often the case that a large number of read operations are performed on the same data within a period of time, but no write operations. Each core generally corresponds to a cache. Each core maintains a read_ref value in its corresponding cache. And, according to the implementation method in the prior art, the read_ref value in the cache corresponding to each core must remain consistent. In this way, for a computer system with multiple computing cores, once the read_ref value in the cache corresponding to a core changes, it must communicate with other cores to notify the change. After receiving the notification, other cores update the read_ref value in their corresponding cache.
[0010] Thus, this method in the prior art results in that when multiple threads of different cores perform read operations on the same data, since communication between cores takes a certain amount of time, the atomic operation of changing the read_ref value in the corresponding cache of each core also takes a certain amount of time, resulting in low execution efficiency. Summary of the invention
[0011] The purpose of the embodiments of the present application is to provide a read lock operation method, a write lock operation method and a system to improve execution efficiency.
[0012] To solve the above technical problems, the embodiments of the present application provide a read lock operation method, a write lock operation method and a system which are implemented as follows:
[0013] A read lock operation method, comprising:
[0014] Set the private reference count corresponding to each core;
[0015] When threads of different cores read the same data, read locks and decode locks are added based on the private reference counts corresponding to different cores.
[0016] A read lock operating system includes a data unit, a cache unit, a second cache unit, a first computing core and a second computing core, wherein:
[0017] A data unit, used to store data;
[0018] A first cache unit, for storing a first private reference count allocated for a first computing core;
[0019] A second cache unit, for storing a second private reference count allocated for a second computing core;
[0020] The first computing core and the second computing core are used to read the same data in the data unit; and,
[0021] During the process of reading the data by the thread of the first computing core, a read lock and an unread lock operation are performed using the private reference count corresponding to the first core;
[0022] During the process of reading the data by the thread of the second computing core, read lock and decode lock operations are performed using the private reference count corresponding to the second core.
[0023] A write lock operation method, comprising:
[0024] Before performing a write operation on data, determine whether there is a read operation process for the data in all computing cores;
[0025] Before performing a write operation on the data, determining whether the data is in another write operation process;
[0026] If both of the above judgment results are negative, in the process of writing the data, the global write lock is used to perform write lock and unlock operations.
[0027] A write lock operating system includes a data unit, a first judgment unit, a second judgment unit, and a write lock adding and unlocking unit, wherein:
[0028] A data unit, used to store data;
[0029] A first judgment unit is used to judge whether there is a read operation process for the data in all computing cores before performing a write operation on the data;
[0030] A second judgment unit, used for judging whether the data is in another writing operation process before performing a writing operation on the data;
[0031] The write lock adding and unlocking unit is used to add and unlock the write lock with the global write lock during the process of writing the data when the judgment results of the first judgment unit and the second judgment unit are both negative.
[0032] It can be seen from the technical solution provided by the above embodiment of the present application that the embodiment of the present application enables threads of different cores to independently perform private reference count operations corresponding to the core when performing read operations on the same data. The private reference counts corresponding to these different cores do not need to be synchronized between the cores, so the execution efficiency is improved. Moreover, the scalability of the read lock is also improved, that is, no matter how many threads of the cores concurrently add and read the lock, the time for adding and reading the lock will hardly increase, thereby improving the execution efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0034] Figure 1 A flowchart of an embodiment of the read lock operation method of the present application;
[0035] Figure 2 A module diagram of an embodiment of a read lock operating system for this application;
[0036] Figure 3 A flowchart of an embodiment of the write lock operation method of the present application;
[0037] Figure 4This is a module diagram of an embodiment of a write lock operating system for this application. DETAILED DESCRIPTION
[0038] Embodiments of the present application provide a read lock operation method, a write lock operation method and a system.
[0039] In order to enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this application.
[0040] First, an embodiment of the read lock operation method of the present application is introduced.
[0041] Figure 1 A flowchart of an embodiment of the read lock operation method of the present application is shown. Figure 1 As shown, the method of this embodiment includes:
[0042] S100: Set the private reference count corresponding to each core.
[0043] Modern CPUs use a lot of techniques to offset the latency caused by memory access. During the reading and writing of memory data, the CPU can execute hundreds or thousands of instructions. Multi-level static random access memory (SRAM) cache (hereinafter referred to as cache) is the main means to reduce the impact of this latency.
[0044] For example, for a dual-core computer system, core 1 and core 2 have corresponding cache 1 and cache 2, respectively. The cache may be the cache of the computing core. For example, for a CPU, the CPU often has a first-level cache, a second-level cache, and some CPUs even have a third-level cache. For a CPU including a first-level cache and a second-level cache, the data that the CPU needs to operate on is first read from the memory into the second-level cache, then read from the second-level cache into the first-level cache, and then read from the first-level cache into the CPU for execution. Generally speaking, the closer the memory is to the CPU, the faster it is, but the more expensive it is; the farther the memory is from the CPU, the slower it is, but the cheaper it is. Data that is frequently read and written by the CPU is generally stored in a memory close to the CPU to improve the utilization rate of the expensive memory.
[0045] In this step, preferably, a private reference count (private_read_ref) can be placed in the cache. For example, the private reference count can be set in the first-level cache of the CPU. Of course, depending on the CPU architecture and the capacity of memories at different levels, the private reference count can also be set in the second-level cache, or in other memories whose reading speed is of the same order of magnitude as the CPU atomic operation speed. The embodiments of the present application do not specifically limit this. In fact, the cache is often transparent to the program, that is, the program has no way to control whether a variable is to be placed in the cache, and which level of cache to place it in. When the program wants to operate a variable, the CPU can check whether the variable is in the first-level cache. If so, read it directly from the first-level cache; if not, it can check whether it is in the second-level cache: if so, load the variable from the second-level cache to the first-level cache, if there is no second-level cache, load the variable from the memory to the second-level cache and the first-level cache.
[0046] In the prior art, the read operations of different threads on the same data involve the same reference count, that is, the same reference count operation. This reference count read_ref is called a global read_ref according to the general rules in the computer field. In particular, different threads of the same computing core, including different threads in the same process, or different threads in different processes, perform self-increment (++) or self-decrement (--) operations on the same global read_ref when performing read operations on the same data. If in a multi-core computer system, for the case of multiple cores, only one global reference count is still used, the problems analyzed in the background technology will arise.
[0047] In this step, for different cores, a private application count is set for each core. For example, for core 1, its corresponding private reference count is set, such as read_ref_core1; for core 2, its corresponding private reference count is also set, such as read_ref_core2. For the case where other cores are also included, the same can be applied.
[0048] The private reference count corresponding to each core may not be permanently (or fixedly) allocated, but temporarily allocated. For example, it can be allocated before the thread of each core performs the first read lock on the data; the private reference count is reclaimed after the thread of the core completes the read operation on the data. Specifically, an array [read_ref] of private reference counts can be set. Before the thread of each core performs the first read lock on the data, an application is made to allocate an item in the [read_ref] array. The space of the array [read_ref] can be set large enough. Each item in the array can be set to an integer (int). The initial value of each item in the array can be initialized to 0. Of course, for the read operation of a certain data, each item in the [read_ref] array can also be fixedly allocated to each core.
[0049] Preferably, in actual operation, each item in the [read_ref] array can be assigned to a cache line in the cache. The cache line is the smallest unit for a multi-core CPU to maintain cache consistency, and is also the actual unit of memory exchange. In practice, a cache line on most platforms is larger than 8 bytes, and most cache lines are 64 bytes. If the [read_ref] array is defined as an int type, it is 8 bytes. It can be seen that a cache line can store 8 read_refs. If more than one read_ref is stored in a cache line, there will be conflicts when operating different elements in the array. To avoid conflicts, each read_ref in the [read_ref] array can be stored in a cache line. For example, each item in the [read_ref] array can be declared as a structure, and the size of a structure can be declared to be 64 bytes. In this way, each item in the [read_ref] array occupies a cache line exclusively, which can avoid conflicts during operations.
[0050] S110: When threads of different cores read the same data, read lock and decode lock operations are performed using private reference counts corresponding to different cores.
[0051] For example, the same computer system includes two computing cores, core 1 and core 2. For another example, core 1 and core 2 both need to read the same data. According to S100, core 1 can apply for a private reference count, marked as read_ref_core1; similarly, core 2 can also apply for a private reference count, such as read_ref_core2.
[0052] In this way, when the thread of core 1 reads the data, it first adds a read lock. That is, the private reference count read_ref_core1 of core 1 is incremented by 1. In this way, read_ref_core1 changes from the initial value 0 to 1. After that, the thread of core 1 reads the data. After the read operation is completed, the read lock is released. That is, the private reference count read_ref_core1 of core 1 is decremented by 1. In this way, read_ref_core1 changes from 1 to 0.
[0053] Similarly, when the thread of core 2 reads the data, it first adds a read lock. That is, the private reference count read_ref_core2 of core 2 is incremented by 1. In this way, read_ref_core2 changes from the initial value 0 to 1. After that, the thread of core 2 reads the data. After the read operation is completed, it performs an unread lock operation. That is, the private reference count read_ref_core2 of core 2 is decremented by 1. In this way, read_ref_core2 changes from 1 to 0.
[0054] The above method adopted in the embodiment of the present application enables the threads of different cores to independently operate the private reference counts corresponding to the core when reading the same data. The private reference counts corresponding to these different cores do not need to be synchronized between the cores, so the execution efficiency is improved. Moreover, the scalability of the read lock is also improved, that is, no matter how many threads of the cores concurrently add and read the lock, the time for adding and reading the lock will hardly increase.
[0055] In addition, the private reference counts corresponding to different cores do not need to be synchronized between the cores, eliminating the communication process between the cores, thereby eliminating the bandwidth, time, and other overheads required for inter-core communication.
[0056] The S110 may specifically include the following steps:
[0057] S111: When the thread of the first core reads data, a read lock and an unread lock operation are performed using the private reference count corresponding to the first core.
[0058] S112: When the thread of the second core reads the data, a read lock and an unread lock operation are performed using the private reference count corresponding to the second core.
[0059] The above-mentioned read lock operation performed by private reference count specifically includes: the processes of different cores perform an add 1 operation with the private reference count corresponding to each core. The read lock operation performed by private reference count specifically includes: the processes of different cores perform a subtract 1 operation with the private reference count corresponding to each core. Between the read lock operation and the read lock operation, the processes of each core can read the data.
[0060] It should be noted that when multiple different threads of the same core perform read operations on the same data, the same private counter can be used to perform read lock and unlock operations. For example, when thread 1 of core 1 reads the data, a read lock is first performed. The private reference count read_ref_core1 of core 1 is incremented by 1. In this way, read_ref_core1 changes from an initial value of 0 to 1. After that, thread 1 of core 1 reads the data. When thread 1 of core 1 reads the data, thread 2 of core 1 also wants to perform a read operation on the same data, then the value of read_ref_core1 is incremented by 1 and the data is read. At this time, the value of read_ref is 2. After thread 1 of core 1 completes the read operation, the value of read_ref_core1 is decremented by 1 and the unlock is released. At this time, the value of read_ref_core1 becomes 1. After that, thread 2 of core 1 completes the read operation on the data, the value of read_ref_core1 is decremented by 1 and the unlock is released. At this time, the value of read_ref_core1 becomes 0. In this way, for the same core, no matter how many threads concurrently add and read locks, the time to add and read locks will hardly increase.
[0061] It should also be noted that in order to avoid data inconsistency, the read lock in the embodiment of the present application is still mutually exclusive with the write lock. For example, in a computer system with multiple cores, a global write lock is set, such as global_writer_id. If a thread wants to perform a write operation on a piece of data, a write lock is added to the data before the write operation is performed. For example, thread 1 of a core updates the value of global_writer_id to the ID of thread 1 (for example, the data type of global_writer_id is an integer, and the initial value is 0; the ID of any thread is not 0), and then performs a write operation on the data. During the writing process, a thread of a core (which may be the same core or a different core as the previous thread with a write lock), here referred to as thread 2, wants to perform a read operation on the same data and applies for a private reference count corresponding to the core. The private reference count is initialized to 0, for example. However, because the value of global_writer_id is not 0 at this time, thread 2 cannot add a read lock or perform a read operation on the data. After the write operation of thread 1 is completed, the write lock is released, that is, the value of global_writer_id is updated to 0. After thread 2 failed to obtain a read lock last time and waited for a while, it learned that the value of global_writer_id is now 0, so it can obtain a read lock. Thread 2 may also try to obtain a read lock at regular intervals after failing to obtain a read lock last time; when the value of global_writer_id is 0, the retry to obtain a read lock is successful. In this way, thread 2 adds 1 to the value of the private reference count it applied for, and then performs a read operation on the data. At this time, the value of thread 2's private reference count is 1. After thread 2's read operation is completed, the read lock is unread, that is, the value of its corresponding private reference count is reduced by 1 to 0.
[0062] Based on this, before threads of different cores in S110 perform a read lock operation on the corresponding private reference count, the following steps may also be included:
[0063] S101: Threads of different cores check whether the data is in a write operation process, and trigger execution of S110 when the check result is no.
[0064] Whether the write operation is in progress can be determined by checking the state of the global write lock. For example, it can be checked whether the global write lock is 0, and when the check result is 0, S110 is executed.
[0065] On the contrary, if the value of the global write lock is not 0, it means that there is a write operation on the data. Based on the mutual exclusion of the write lock and the read lock, the data cannot be read locked at this time, and the data cannot be read. In this case, it is necessary to wait until the global write lock becomes 0 before executing S110.
[0066] S101 may be executed after S100 or before S100.
[0067] It should be noted that for read lock, in order to further avoid another thread performing a write lock operation for a write operation between the operation of checking the global_writer_id value and the corresponding read operation, that is, to avoid the failure of conflict detection caused by this situation, after the value of the private reference count corresponding to the core is increased by 1, it will be checked again whether the global_writer_id value at this time is 0. If it is not 0, the read operation will be performed.
[0068] An embodiment of the read lock operating system of the present application is introduced below. Figure 2 A module diagram of an embodiment of the system is shown.
[0069] like Figure 2 As shown, the read lock operating system in one embodiment of the present application includes a first computing core 11a, a second computing core 11b, a first cache unit 12a, a second cache unit 12b, and a data unit 13, and each of the computing cores corresponds to a unique cache unit.
[0070] in:
[0071] A data unit 13, used for storing data;
[0072] A first cache unit 12a, used to store a first private reference count allocated to a first computing core;
[0073] A second cache unit 12b, used to store a second private reference count allocated to the second computing core;
[0074] The first computing core 11a and the second computing core 11b are used to read the same data in the data unit; and,
[0075] When the thread of the first computing core 11a reads the data, the private reference count corresponding to the first core is used to perform read lock and decode lock operations;
[0076] When the thread of the second computing core 11b reads the data, a read lock and an unread lock operation are performed using the private reference count corresponding to the second core.
[0077] in:
[0078] The first cache unit 12a may be a cache of a first computing core;
[0079] The second cache unit 12b may be a cache of a second computing core.
[0080] As mentioned in the method embodiment above, the private reference count corresponding to each core is set. The private reference count corresponding to the core can be allocated before the thread of each core adds a read lock to the data for the first time, or the private reference count of each core can be fixedly allocated. For example, an array [read_ref] of private reference counts can be set. Before the thread of each core adds a read lock to the data for the first time, an application is made to allocate an item in the [read_ref] array. The space of the array [read_ref] can be set large enough. Each item in the array can be set to an integer (int). The initial value of each item in the array can be initialized to 0. Of course, for a read operation of a certain data, each item in the [read_ref] array can also be fixedly allocated to each core. Preferably, in actual operation, each item in the [read_ref] array can be allocated to a cache line in the cache. The cache line is the smallest unit for a multi-core CPU to maintain cache consistency, and it is also the actual unit of memory exchange. In practice, a cache line on most platforms is larger than 8 bytes, and most cache lines are 64 bytes. If the [read_ref] array is defined as an int type, it is 8 bytes. It can be seen that one cache line can store 8 read_refs. If more than one read_ref is stored in one cache line, there will be conflicts when operating different elements in the array. To avoid conflicts, each read_ref in the [read_ref] array can be stored in a cache line. For example, each item in the [read_ref] array can be declared as a structure, and the size of the structure can be declared to be 64 bytes. In this way, each item in the [read_ref] array occupies a cache line exclusively, which can avoid conflicts during operations.
[0081] In combination with the above content, in one embodiment of the read lock operating system of the present application, the caches of different cores may correspond to different cache lines. For example, the first cache unit corresponds to the first cache line, and the second cache unit corresponds to the second cache line.
[0082] The read lock operating system embodiment may further include a checking unit 14 for checking whether the data is in a write operation process, and if not, triggering each computing core to perform read lock and unread lock operations on the corresponding private reference count.
[0083] The above-mentioned read lock operation on the private reference count specifically includes: the process of each core performs an add 1 operation on the private reference count corresponding to the core. The read lock operation on the private reference count specifically includes: the process of each core performs a subtract 1 operation on the private reference count corresponding to the core. Between the read lock operation and the read lock operation, the process of each core can read the data.
[0084] An embodiment of the write lock operation method of the present application is introduced below. Figure 3 A flow chart of an embodiment of the method is shown. Figure 3 As shown, the write lock operation method embodiment of the present application includes:
[0085] S300: Before performing a write operation on the data, determine whether there is a read operation process on the data in all computing cores.
[0086] The determination of whether all computing cores are in the process of reading the data can be implemented by checking whether the private reference count of the data corresponding to each computing core is 0. If it is 0, it means that the data is in the process of reading; if it is not 0, it means that the data is not in the process of reading.
[0087] S310: Before performing a write operation on the data, determine whether the data is in another write operation process.
[0088] S310 may be specifically implemented by determining whether the global write lock for the data is 0. If it is 0, it means that the data is not in another write operation process; if it is not 0, it means that another write operation process exists.
[0089] S320: If the judgment results of the above S310 and S320 are both no, in the process of writing the data, the global write lock is used to perform write lock and unlock operations.
[0090] Specifically, before performing a write operation, a write lock is added to the data; after the write operation, the write lock is released to the data.
[0091] The global variable in S320 is, for example, global_writer_id. Adding a write lock may update the value of the global_writer_id to the ID of the writing thread; and releasing the write lock may update the value of the global_writer_id to 0.
[0092] Similarly, for write lock, in order to further avoid another thread performing a read lock operation for a read operation between the operation of checking the private reference count value of each core and the corresponding write operation, that is, to avoid the failure of conflict detection caused by this situation, after the value of global_writer_id is updated to the ID of the writing thread, it will be checked again whether the private reference count value of each core is 0 at this time. If it is not 0, the write operation will be performed.
[0093] The above-mentioned write lock operation method may be based on the above-mentioned read lock operation method or read lock operation system.
[0094] An embodiment of the write lock operating system of the present application is introduced below. Figure 4 FIG. 1 shows a module diagram of an embodiment of the system. Figure 4 As shown, the write lock operating system embodiment of the present application includes:
[0095] Data unit 3, used for storing data;
[0096] The first judgment unit 21a is used to judge whether there is a read operation process for the data in all computing cores before performing a write operation on the data;
[0097] The second judging unit 21b is used to judge whether the data is in another writing operation process before performing a writing operation on the data;
[0098] The write lock adding and unlocking unit 22 is used to add and unlock write locks with the global write lock during the process of writing the data when the judgment results of the first judgment unit and the second judgment unit are both negative.
[0099] Specifically, before performing a write operation, a write lock is added to the data; after the write operation, the write lock is released to the data.
[0100] The global variable is, for example, global_writer_id. Adding a write lock may update the value of the global_writer_id to the ID of the writing thread; releasing the write lock may update the value of the global_writer_id to 0.
[0101] The above-mentioned write lock operating system may be based on the above-mentioned read lock operating method or read lock operating system.
[0102] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions.
[0103] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0104] It can be known from the description of the above implementation methods that those skilled in the art can clearly understand that the present application can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be essentially or partly contributed to the prior art in the form of a software product, which can be stored in a storage medium such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in the various embodiments of the present application or certain parts of the embodiments.
[0105] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0106] The present application can be used in many general or special computer system environments or configurations, such as personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronic devices, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, etc.
[0107] The present application may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules may be located in local and remote computer storage media, including storage devices.
[0108] Although the present application has been described through embodiments, persons skilled in the art will appreciate that there are many modifications and variations of the present application without departing from the spirit of the present application. It is intended that the appended claims include these modifications and variations without departing from the spirit of the present application.
Claims
1. A read lock operation method, applied to a multi-core computer system, the method comprising: Set different private reference counts for different cores; When threads of different cores read the same data, threads of each core perform read lock and unread lock operations on the same data based on the corresponding private reference count; The thread of each core performs a read lock operation on the same data based on the corresponding private reference count, including: the thread of each core performs a plus 1 operation on the private reference count corresponding to itself; The thread of each core performs a read lock operation on the same data based on the corresponding private reference count, including: the thread of each core performs a subtraction operation on its own corresponding private reference count.
2. The read lock operation method according to claim 1, wherein different private reference counts corresponding to different cores are set, comprising: Before the thread of each core performs the first read lock on the same data, a private reference count corresponding to the core is allocated, and after the thread of the core completes the read operation on the same data, the private reference count is reclaimed; or, Fixed allocation of private reference count per core.
3. The read lock operation method according to claim 1 or 2, wherein different private reference counts corresponding to different cores are set, comprising: Set up an array of private reference counts and assign different items in the array to different cores.
4. The read lock operation method according to claim 3, allocating different items in the array to different cores, comprising: Different items in the array are allocated to different cache lines in the cache, and different cache lines correspond to different cores.
5. The read lock operation method according to claim 1, wherein in the process of threads in different cores reading the same data, each core performs read lock and unread lock operations on the same data based on the corresponding private reference count, including: When the thread of the first core reads the same data, the private reference count corresponding to the first core is used to perform read lock and decode lock operations; When the thread of the second core reads the same data, the read lock and the decode lock operation are performed using the private reference count corresponding to the second core.
6. The read lock operation method according to claim 1, before adding read locks to the private reference counts corresponding to the different cores, further comprising: Threads of different cores check whether the same data is in the process of being written, and the result of the check is no.
7. The read lock operation method according to claim 1, after each core thread performs an increment operation on its corresponding private reference count and before performing a read operation, further comprising: Each core's thread checks whether the same data is in the process of being written; Each core's thread performs read operations, including: If the thread of each core determines that the check result indicates that the same data is not in the process of being written, then the thread performs a read operation.
8. A read lock operating system, comprising a data unit, a first cache unit, a second cache unit, a first core and a second core, wherein the first core and the second core are different cores in a computer system, wherein: A data unit, used to store data; A first cache unit, for storing a first private reference count allocated for a first core; A second cache unit, for storing a second private reference count allocated for a second core; The first core and the second core are used to read the same data in the data unit; and, During the process of reading the same data by the thread of the first core, a read lock and an unread lock operation are performed on the same data based on the private reference count corresponding to the first core; During the process of reading the same data by the thread of the second core, a read lock and an unread lock operation are performed on the same data based on the private reference count corresponding to the second core; Among them, for each core in the first core and the second core, respectively, based on the private reference count corresponding to the core, a read lock operation is performed on the same data, which is to add 1 to the private reference count corresponding to the core, and based on the private reference count corresponding to the core, a read lock operation is performed on the same data, which is to subtract 1 from the private reference count corresponding to the core.
9. The read lock operating system according to claim 8, wherein the first cache unit is a cache of the first core; The second cache unit is a cache of the second core.
10. The read lock operating system according to claim 9, wherein the first cache unit corresponds to a first cache line; The second cache unit corresponds to a second cache line.
11. The read lock operating system as described in claim 8 further includes a checking unit for checking whether the same data is in a write operation process, and if not, triggering each core to perform read lock and unread lock operations on the corresponding private reference count.
12. A write lock operation method, applied to a multi-core computer system, setting different private reference counts corresponding to different cores, so as to implement the read lock operation method according to any one of claims 1 to 7, the write lock operation method comprising: Before performing a write operation on the specified data, determine whether there is a read operation process for the specified data in all cores of the computer system by traversing the private reference counts corresponding to each core; Before performing a write operation on the designated data, determining whether the designated data is in another write operation process based on a global write lock for the designated data; If both of the above judgment results are negative, in the process of writing the designated data, the global write lock is used to perform write lock and unlock operations.
13. The write lock operation method according to claim 12, wherein the step of performing a write lock operation with the global write lock specifically comprises: Update the value of the global write lock variable to the ID of the write thread; The performing a write lock unlocking operation with the global write lock specifically includes: updating the value of the global write lock variable to 0.
14. The write lock operation method according to claim 13, after updating the value of the global write lock variable to the ID of the write thread and before performing the write operation, further comprising: Check the private reference count value corresponding to each core at this time; Perform write operations, including: If the check result is 0, the write operation is performed.
15. A write lock operating system, comprising a data unit, a first judgment unit, a second judgment unit, a write lock adding and unlocking unit, and setting different private reference counts corresponding to different cores to implement the read lock operation method according to any one of claims 1 to 7, wherein: A data unit, used to store data; A first judgment unit is used to judge whether there is a read operation process for the data in all cores of the computer system by traversing the private reference counts corresponding to each core before performing a write operation on the designated data stored in the data unit; A second judgment unit, configured to judge whether the data is in another write operation process based on a global write lock for the designated data before performing a write operation on the designated data; The write lock adding and unlocking unit is used to add and unlock write locks with the global write lock during the write operation on the designated data when the judgment results of the first judgment unit and the second judgment unit are both negative.
Citation Information
Patent Citations
Data management method and system
CN101039278A
Explicit multi-core Cache consistency active management method facing flow application
CN103279428A