CN111723250B - Chain table management method based on reference counting - Google Patents
Chain table management method based on reference counting Download PDFInfo
- Publication number
- CN111723250B CN111723250B CN202010441419.6A CN202010441419A CN111723250B CN 111723250 B CN111723250 B CN 111723250B CN 202010441419 A CN202010441419 A CN 202010441419A CN 111723250 B CN111723250 B CN 111723250B
- Authority
- CN
- China
- Prior art keywords
- reference count
- linked list
- thread
- count value
- value
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000007726 management method Methods 0.000 title claims abstract description 32
- 238000000034 method Methods 0.000 claims abstract description 24
- 230000008569 process Effects 0.000 claims abstract description 21
- 238000011084 recovery Methods 0.000 claims abstract description 19
- 238000000889 atomisation Methods 0.000 claims abstract description 11
- 230000007423 decrease Effects 0.000 claims abstract 3
- 238000004064 recycling Methods 0.000 claims 1
- 238000010586 diagram Methods 0.000 description 6
- 230000007246 mechanism Effects 0.000 description 5
- 230000006872 improvement Effects 0.000 description 2
- 230000009467 reduction Effects 0.000 description 2
- 230000004888 barrier function Effects 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 230000007547 defect Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000004044 response Effects 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/90—Details of database functions independent of the retrieved data types
- G06F16/901—Indexing; Data structures therefor; Storage structures
- G06F16/9024—Graphs; Linked lists
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5016—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5011—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
- G06F9/5022—Mechanisms to release resources
Abstract
A linked list management method based on reference counting sets a reference counting value and a linked list protected by the reference counting value, wherein nodes in the linked list at least comprise a link pointer, and the nodes are connected through the link pointer. The method also comprises the steps that a plurality of access threads read data from the linked list at the same time, one management thread is responsible for adding or deleting nodes to the linked list, and one recovery thread releases memory resources of the deleted nodes. In the initial state, the reference count value of 1 represents the initial reference count of the management thread to the linked list, the access thread firstly increases the reference count value by 2 in an atomization mode before reading the data in the linked list, decreases the reference count value by 2 in an atomization mode after completing the process of reading the linked list data if the reference count value is not 0 before increasing the reference count value by 2, and decreases the reference count value by 1 in an atomization mode after completing the process of reading the linked list data if the reference count value is 0 before increasing the reference count value by 2.
Description
Technical Field
The invention relates to the field of computer system software programming, in particular to a linked list management method based on reference counting.
Background
In a computer system, a linked list is a common data structure, and generally, multiple threads access data of each node of a read linked list at the same time is free from problems, and no data exception exists in the access conflict among the multiple threads. When multiple threads add or delete nodes to or from the linked list at the same time, a Lock (Lock) mechanism is required to ensure the integrity of the linked list structure, i.e. when one thread successfully acquires a Lock, the linked list structure can be modified. Conventional locks will result in only one thread accessing or modifying the linked list at any time, and will be significantly less efficient. The current common improvement scheme is that a read-write lock (RWLock) is used, a plurality of threads can acquire the read-write lock in a read mode to read data access to the linked list, only one thread can acquire the read-write lock in a write mode to modify the linked list structure, and other threads cannot acquire the read-write lock in a read mode when one thread acquires the read-write lock in a write mode, and the read-write lock can be acquired again after the linked list structure is modified and the write lock is released. While theoretically using a separate management thread is specially responsible for modifying the linked list structure, other threads can access and read data in the linked list at the same time without any protection of the lock mechanism, another problem arises in that the management thread is absolutely safe to add nodes to the linked list, the actual process of deleting the nodes will eventually release the memory resources of the deleted nodes, and if there is a thread still accessing and reading the data of the node, invalid memory access will result.
Disclosure of Invention
The invention aims to overcome the defects of the prior art and provide a linked list management method based on reference counting, which uses the atomized reference counting to maintain the memory validity of each node during the data access period, and ensures that the access thread does not need to wait and the memory access is safe when reading the data in the linked list at any time.
The technical scheme of the invention is as follows: setting a reference count value and a linked list associated with the reference count value, wherein nodes in the linked list at least comprise a link pointer, and the nodes are connected through the link pointer. The method also comprises the steps that a plurality of access threads read data from the linked list at the same time, one management thread is responsible for adding or deleting nodes to the linked list, and one recovery thread releases memory resources of the deleted nodes. In the initial state, the reference count value is 1 to represent the initial reference count of the management thread to the linked list, and the addition of 2 to the reference count value before the access thread reads the data in the linked list is represented as 1 reference count to the linked list by the current access thread and the management thread. If the reference count value is not 0 before 2 is added, the reference count value is atomized and reduced by 2 after the process of reading the linked list data is completed, and the current access thread and the management thread simultaneously reduce the reference count by 1 to the linked list. If the reference count value is 0 before adding 2, the reference count value is atomized and reduced by 1 after completing the process of reading the linked list data, which means that only the reference of the access thread to the linked list is reduced and the initial reference of the management thread to the linked list is restored. When the reference count value is decremented to 0, the reclamation thread is notified that the memory resources of the deleted node are to be freed. After the recovery thread completes the process of releasing the memory resources, an atomic compare and swap (CAS) operation is used to determine whether the reference count value is 0, and if the reference count value is 0, the atomized replacement is replaced with an initial value of 1.
When the management thread deletes the nodes in the linked list, the link pointers of the nodes before the deleted nodes point to the nodes after the deleted nodes, and meanwhile, the link pointers of the deleted nodes are kept unchanged. If the access thread still reads the data of the deleted node at the moment, the access thread is not influenced to continue to access the next node through the link pointer of the node. Until the access thread finishes the process of reading the linked list data, the recovery thread is informed of releasing the memory resources of the deleted node when the reference count value is reduced to 0.
After deleting the nodes in the linked list, the management thread firstly caches the deleted nodes and wakes up the suspended recovery thread to release the memory of the nodes. The reclamation thread fetches the deleted node and then atomises the reference count value by 1. If the reference count value is 0 after 1 reduction, the data of the thread in the read chain table is not accessed, and the memory resource of the deleted node begins to be released. If the reference count value is not 0 after 1 reduction, the process that the recovery thread hangs up and waits for the access thread to finish reading the linked list data is described that the access thread is in the read linked list.
Compared with the prior art, the invention has the following beneficial effects: the invention discloses a linked list management method based on reference counting, which can ensure that other access threads can read data in a linked list without barriers while adding or deleting nodes to the linked list by a management thread under the condition of not using any lock, and can not cause the occurrence of invalid memory access, thereby improving the overall response capability of system operation.
The detailed structure of the present invention is further described below with reference to the accompanying drawings and detailed description.
Drawings
FIG. 1 is a diagram showing a first access thread starting to read linked list data and a management thread starting to delete nodes;
FIG. 2 is a diagram of a first access thread still accessing a deleted node;
FIG. 3 is a diagram of a reclamation thread preparing to free memory resources of deleted nodes;
FIG. 4 is a diagram showing a first access thread completing the process of reading linked list data, informing a reclamation thread to start releasing deleted nodes;
FIG. 5 is a diagram of a second access thread beginning to read linked list data;
FIG. 6 is a diagram illustrating the completion of reading linked list data by a second access thread.
Detailed Description
As shown in fig. 1: the linked list contains A, B, C, D four nodes. The first access thread, prior to reading the linked list data, increments the reference count value by 2. The initial value of the reference count value is 1, and becomes 3 after adding 2. The management thread starts deleting node B and the first access thread is accessing node a.
As shown in fig. 2: the node B is deleted from the linked list by the management thread such that the link pointer of node a points directly to node C, but the link pointer of node B remains to point to node C. At this point the first access thread is accessing node B, the access thread may still continue to access node C via node B's link pointer. After deleting node B, the management thread notifies the reclamation thread to start releasing memory resources of node B.
As shown in fig. 3: before the recovery thread releases the memory resource of the node B, the reference count value is firstly atomized and reduced by 1, and the fact that the reference count value is not 0 after the reference count value is reduced by 1 indicates that the recovery thread has access to the linked list data, and the recovery thread suspends the process of waiting for the access thread to finish reading the linked list data. The first access thread is now accessing node C.
As shown in fig. 4: after the first access thread finishes accessing the node D, ending the process of reading the linked list data, then reducing the reference count value by 2 in an atomization mode, and after the reference count value is reduced by 2, indicating that no other thread is accessing the linked list, and informing the recovery thread to start releasing the memory resource of the node B.
As shown in fig. 5: the reclamation thread is freeing up the memory resources of node B and the second access thread begins to read the linked list data and increment the reference count value by 2. Since the reference count value is 0 before 2 is added, the reference count value is atomized by 1 after the process of reading the linked list data is completed. Since the node B has been deleted before, the access thread will not access the node B any more.
As shown in fig. 6: after the recovery thread releases the memory resources of the node B, it is determined whether the reference count value is 0 using an atomic compare and swap operation, and the replacement that will not be atomized is 1 because the count value is not 0. After the second access thread finishes accessing the node D, the process of reading the linked list data is finished, and then the reference count value is atomized and reduced by 1, so that the initial reference count of the management thread to the linked list is restored.
The present invention relates to the concept of atomic operations in the field of computer technology, and it is well within the skill of those skilled in the art to implement such atomic operations on different processor platforms using the relevant machine instructions of the platform. For example, the atomic plus operations and atomic compare and swap (CAS) operations used in the present invention may be implemented on Intel x86 processor platforms with LOCK XADD instructions and LOCK CMPXCHG instructions, where the functionality of the LOCK XADD instructions may also be replaced with LOCK CMPXCHG instruction conditional loops, and the machine instructions implementing these atomic operations by different processor hardware platforms are different, and such differences cannot be interpreted as being beyond the scope of the present invention.
Noun interpretation:
locking mechanism: (1) Under the multi-thread competition, locking and unlocking can cause relatively more context switching and scheduling delay, and cause performance problems. (2) Holding a lock by one thread may cause all other threads that require the lock to be suspended. (3) If a high priority thread waits for a low priority thread to release the lock, this can lead to a priority reversal, causing performance risks.
Read-write lock: read-write locks are an improvement over conventional lock mechanisms in that they allow multiple threads to obtain a read lock in a read-only manner while concurrently performing read access to shared resource data. Only one thread is allowed to obtain the write lock in a write manner, and the data of the shared resource is modified, including modifying the data structure. Write accesses must acquire a write lock after all read accesses release the read lock, and read accesses must wait for a modify operation to release the write lock before acquiring the read lock.
CAS operation: the mechanism used for atomic comparison and exchange above is CAS, compare and Swap.
The CAS has 3 operands, a memory value V, an old expected value a, a new value B to be modified. If and only if the expected value A and the memory value V are the same, the memory value V is modified to B, otherwise nothing is done.
Claims (6)
1. A linked list management method based on reference counting is characterized in that: setting a reference count value and a linked list associated with the reference count value, wherein nodes in the linked list at least comprise a link pointer, the nodes are connected through the link pointer, the initial value of the reference count value is X, and X is not 0; the system also comprises a plurality of access threads, a management thread and a recycling thread; the access thread firstly increases the atomization of the reference count value before reading the data in the linked list, and then decreases the atomization of the reference count value after completing the process of reading the data from the linked list; when the reference count value is decremented to 0, the reclamation thread is notified that the memory resources of the deleted node are to be freed.
2. The reference count-based linked list management method of claim 1, wherein: after the management thread deletes the nodes from the linked list, the deleted nodes are cached, and then the recovery thread is notified to fetch the deleted nodes to release the memory resources.
3. The reference count-based linked list management method of claim 1, wherein: before releasing the memory resource of the deleted node, the recovery thread firstly atomizes the reference count value to subtract an initial value X, if X is subtracted, the recovery thread is suspended to wait for the access thread to finish the process of reading the linked list data, and if X is subtracted, the recovery thread is 0, and then the memory resource of the deleted node is started to be released; after the recovery thread finishes the process of releasing the memory resources, an atomic comparison and exchange operation is used for judging whether the reference count value is 0, and if the reference count value is 0, the atomized replacement is replaced by an initial value X.
4. A method of managing a linked list based on reference counts as claimed in any one of claims 1 to 3, wherein: the initial value of the reference count value is 1, the access thread firstly increases the value of the reference count by 2 in an atomization way before reading the data in the linked list, if the value of the reference count is not 0 before increasing the value of the reference count by 2 in an atomization way after completing the process of reading the linked list data, and if the value of the reference count is 0 before increasing the value of the reference count by 1 in an atomization way after completing the process of reading the linked list data; when the reference count value is decremented to 0, the reclamation thread is notified that the memory resources of the deleted node are to be freed.
5. The reference count-based linked list management method of claim 4, wherein: after the recovery thread takes out the deleted node, firstly reducing 1 of the reference count value in an atomization mode, suspending the recovery thread and waiting for the access thread to finish the process of reading the linked list data if the reference count value is not 0 after 1 is reduced, and starting to release the memory resource of the deleted node if the reference count value is 0 after 1 is reduced; after the recovery thread finishes the process of releasing the memory resources, an atomic comparison and exchange operation is used for judging whether the reference count value is 0, and if the reference count value is 0, the replacement of atomization is an initial value of 1.
6. The reference count-based linked list management method of claim 5, wherein: after the management thread finishes the process of adding or deleting the nodes to the linked list, the management thread is converted into a role of recovering the thread to release the memory resources of the deleted nodes.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010441419.6A CN111723250B (en) | 2020-05-22 | 2020-05-22 | Chain table management method based on reference counting |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010441419.6A CN111723250B (en) | 2020-05-22 | 2020-05-22 | Chain table management method based on reference counting |
Publications (2)
Publication Number | Publication Date |
---|---|
CN111723250A CN111723250A (en) | 2020-09-29 |
CN111723250B true CN111723250B (en) | 2024-03-08 |
Family
ID=72564941
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202010441419.6A Active CN111723250B (en) | 2020-05-22 | 2020-05-22 | Chain table management method based on reference counting |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111723250B (en) |
Families Citing this family (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN112650609B (en) * | 2020-12-29 | 2023-11-10 | 北京天融信网络安全技术有限公司 | Concurrent resource processing method and device and electronic equipment |
Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101046755A (en) * | 2006-03-28 | 2007-10-03 | 郭明南 | System and method of computer automatic memory management |
DE102012220267A1 (en) * | 2011-11-08 | 2013-05-08 | Nvidia Corporation | Computational work distribution reference counter |
CN104216767A (en) * | 2014-09-18 | 2014-12-17 | 东软集团股份有限公司 | Method and device for accessing shared data among multiple threads |
CN108874446A (en) * | 2018-04-12 | 2018-11-23 | 武汉斗鱼网络科技有限公司 | Multi-threaded Access Methods and device |
CN109144892A (en) * | 2018-08-27 | 2019-01-04 | 南京国电南自轨道交通工程有限公司 | A kind of buffering linked list data structure design method of managing internal memory medium-high frequency delta data |
CN109683984A (en) * | 2018-12-14 | 2019-04-26 | 拉扎斯网络科技(上海)有限公司 | The hot loading method of data, device, electronic equipment and computer readable storage medium |
CN110352406A (en) * | 2017-03-10 | 2019-10-18 | 华为技术有限公司 | Without lock reference count |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US7299242B2 (en) * | 2001-01-12 | 2007-11-20 | Sun Microsystems, Inc. | Single-word lock-free reference counting |
-
2020
- 2020-05-22 CN CN202010441419.6A patent/CN111723250B/en active Active
Patent Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101046755A (en) * | 2006-03-28 | 2007-10-03 | 郭明南 | System and method of computer automatic memory management |
DE102012220267A1 (en) * | 2011-11-08 | 2013-05-08 | Nvidia Corporation | Computational work distribution reference counter |
CN104216767A (en) * | 2014-09-18 | 2014-12-17 | 东软集团股份有限公司 | Method and device for accessing shared data among multiple threads |
CN110352406A (en) * | 2017-03-10 | 2019-10-18 | 华为技术有限公司 | Without lock reference count |
CN108874446A (en) * | 2018-04-12 | 2018-11-23 | 武汉斗鱼网络科技有限公司 | Multi-threaded Access Methods and device |
CN109144892A (en) * | 2018-08-27 | 2019-01-04 | 南京国电南自轨道交通工程有限公司 | A kind of buffering linked list data structure design method of managing internal memory medium-high frequency delta data |
CN109683984A (en) * | 2018-12-14 | 2019-04-26 | 拉扎斯网络科技(上海)有限公司 | The hot loading method of data, device, electronic equipment and computer readable storage medium |
Non-Patent Citations (1)
Title |
---|
基于智能指针的垃圾收集器的研究与实现;王立平等;电脑与电信;20090110(第01期);52-55 * |
Also Published As
Publication number | Publication date |
---|---|
CN111723250A (en) | 2020-09-29 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US10353749B2 (en) | Lock-free dual queue with condition synchronization and time-outs | |
US5287521A (en) | Method and apparatus for releasing and obtaining shared and exclusive locks | |
US5274809A (en) | Task execution control method for a multiprocessor system with enhanced post/wait procedure | |
Rossbach et al. | TxLinux: Using and managing hardware transactional memory in an operating system | |
US5440750A (en) | Information processing system capable of executing a single instruction for watching and waiting for writing of information for synchronization by another processor | |
DE112006001698T5 (en) | Basic features to improve thread-level speculation | |
US7506339B2 (en) | High performance synchronization of accesses by threads to shared resources | |
US6961865B1 (en) | Techniques for resuming a transaction after an error | |
DE112007000812B4 (en) | ONE MEMORY DEVICE WITH THREE LOGICS, METHOD FOR PERFORMING THE METHOD STEPS OF THE ONE MEMORY, ONE PROCESSOR DEVICE AND SYSTEM FOR PROVIDING AN EFFICIENT MECHANISM FOR TRANSACTIONAL MEMORY EXECUTIONS IN OUT-OF-ORDER PROCESSORS | |
US4847754A (en) | Extended atomic operations | |
US5875342A (en) | User programmable interrupt mask with timeout | |
US8239871B2 (en) | Managing timeout in a multithreaded system by instantiating a timer object having scheduled expiration time and set of timeout handling information | |
US8386720B2 (en) | Method for allowing exclusive access to shared data | |
KR100911796B1 (en) | Multi processor and multi thread safe message queue with hardware assistance | |
US20030070021A1 (en) | Locking of computer resources | |
JPH01303527A (en) | Control method for shared resources | |
US20130283282A1 (en) | Component-specific disclaimable locks | |
CN112306699B (en) | Method and device for accessing critical resources, computer equipment and readable storage medium | |
JP3737638B2 (en) | Object lock management method and apparatus, and object lock release method and apparatus | |
CN116438518A (en) | Processor architecture for micro-thread control by hardware accelerated kernel threads | |
CN111723250B (en) | Chain table management method based on reference counting | |
WO2000033195A1 (en) | Elimination of traps and atomicity in thread synchronization | |
JP3798726B2 (en) | MEMORY ACCESS ORDERING AND LOCK MANAGEMENT METHOD, DEVICE, PROGRAM, AND RECORDING MEDIUM | |
JPH04182858A (en) | Shared memory management system | |
CN111444002A (en) | Task scheduling method and device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |