Data writing, reading, and communication methods based on lock-free ring shared memory
By obtaining the buffer capacity and write pointer address in a lock-free circular shared memory, and directly writing and offsetting data, the problem of the writer being dependent on the reader is solved, and the timeliness of writing and the continuity of reading are achieved in a multi-core environment.
Patent Information
- Application Number
- CN202210129905.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-11
- Publication Date
- 2026-03-06
- Estimated Expiration
- 2042-02-11
AI Technical Summary
In existing technologies, lock-free circular buffers cannot support multi-core technology. Writers are dependent on readers, and when readers fail to respond in time, resulting in insufficient remaining space, it can lead to write failures or data loss.
By obtaining the buffer capacity and write pointer address of the lock-free circular shared memory, data is directly written to the buffer, and offset processing is performed during writing, avoiding logical judgments related to the read pointer and ensuring the timeliness and integrity of the write.
It achieves timely and complete data writing in a multi-core environment, avoids data loss, supports lock-free reading of continuous data by multiple readers, and ensures timely reading.
Smart Images

Figure CN114490439B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of communication of co-existing virtual machines in network virtualization technology, specifically to a method for data writing, reading, and communication based on lock-free circular shared memory. Background Art
[0002] In a program (for example, a communication program), a lock-free circular buffer is often used as a data structure to store the data sent and received in communication. A lock-free circular buffer is a first-in-first-out circular buffer that can provide mutually exclusive access to the buffer for the communication program. A classic lock-free circular buffer is set to a fixed size, and a head pointer (front pointer) is set to indicate the writer position, and a tail pointer (back pointer) is set to indicate the reader position. When the writer writes data, it must ensure that back < read + BUFFER_SIZE; when the reader reads data, it must ensure that read < back.
[0003] However, the classic lock-free circular buffer only supports synchronous reading and writing by one writer and one reader, resulting in its inability to support multi-core technology. To solve this technical problem, Patent CN106790599A proposes a method for communication of co-existing virtual machines based on a multi-core lock-free circular buffer, which supports efficient concurrent reading by the receiver, has good concurrency in buffer reading and writing, high communication efficiency of co-existing virtual machines, and can fully utilize the multi-core processing performance of the processor.
[0004] However, Patent CN106790599A still has the following technical problems: the writer is dependent on the reader. When the reader is not timely and the remaining space is insufficient, it will cause the write data to fail, resulting in data loss or untimely writing of data. Summary of the Invention
[0005] In view of this, it is necessary to provide a method for data writing, reading, and communication based on lock-free circular shared memory to solve the technical problem in the prior art that when the writer is dependent on the reader and the reader is not timely, the remaining space is insufficient, which will cause the write data to fail, resulting in data loss or untimely writing of data.
[0006] To solve the above technical problem, the present invention provides a data writing method based on lock-free circular shared memory, including:
[0007] Step 1: Obtain the buffer capacity of the buffer in the lock-free circular shared memory and the current write pointer address;
[0008] Step 2: Obtain the data to be written and the data length of the data to be written;
[0009] Step 3: Write the data to be written into the buffer based on the current write pointer address and the buffer capacity;
[0010] Step 4: Based on the data to be written, offset the current write pointer address to obtain the offset write pointer address, use the offset write pointer address as the current write pointer address, and return to Step 2.
[0011] In some possible implementations, step three includes:
[0012] Determine whether the sum of the current write pointer address and the data length of the data to be written is less than or equal to the buffer capacity;
[0013] If the sum of the current write pointer address and the data length of the data to be written is less than or equal to the buffer capacity, then the data to be written is written to the region in the buffer whose starting position is the current write pointer address;
[0014] If the sum of the current write pointer address and the data length of the data to be written is greater than the buffer capacity, then the data to be written is divided into data lengths of... The first sub-data to be written and its length are The second data to be written is written to the region in the buffer whose starting position is the current write pointer address and whose ending position is the tail address of the buffer. The second data to be written is written to the region whose starting position is the head address of the buffer, and the unlocked circular shared memory is marked as full.
[0015] Where capacity is the buffer capacity; writer c The current write pointer address; datalen w The length of the data to be written is denoted as _____.
[0016] In some possible implementations, the step of offsetting the current write pointer address based on the data to be written to obtain the offset write pointer address includes:
[0017] When the sum of the current write pointer address and the length of the data to be written is less than or equal to the buffer capacity, the offset write pointer address is:
[0018]
[0019] When the sum of the current write pointer address and the length of the data to be written is greater than the buffer capacity, the offset write pointer address is:
[0020]
[0021] In the formula, writer oWrite the pointer address to the offset; writer c The current write pointer address; datalen w The length of the data to be written is denoted as ; capacity is denoted as Buffer capacity.
[0022] On the other hand, the present invention also provides a data reading method based on lock-free ring shared memory, comprising:
[0023] Step 4: Obtain the current read pointer address, the current write pointer address, the length of the data to be read, and the buffer capacity;
[0024] Step 5: Determine whether the sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address;
[0025] Step 6: If the sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address, then read the data to be read from the region in the buffer whose starting position is the current read pointer address;
[0026] Step 7: If the sum of the current read pointer address and the data length of the data to be read is greater than the current write pointer address, then determine whether the current read pointer address is less than or equal to the current write pointer address, and whether the lock-free circular shared memory is full of data;
[0027] Step 8: If the current read pointer address is greater than the current write pointer address, and the lock-free circular shared memory is full of data, then the data to be read is divided into a first sub-data to be read and a second sub-data to be read. The first sub-data to be read is read from the region in the buffer whose starting position is the current read pointer address, and the second sub-data to be read is read from the region in the buffer whose starting position is the head of the buffer.
[0028] Step 9: Offset the current read pointer address based on the data to be read to obtain the offset read pointer address, use the offset read pointer address as the current read pointer address, and return to Step 4.
[0029] In some possible implementations, prior to step six, the following is included:
[0030] Step 10: Based on the current read pointer address, the current write pointer address, and the data length of the data to be read, determine whether the first time interval between writing data to the buffer and reading data from the buffer is greater than a threshold time interval;
[0031] Step 11: If the first time interval is less than or equal to the threshold time interval, proceed to step 6; if the first time interval is greater than the threshold time interval, proceed to step 12.
[0032] Step 12: Reset the current read pointer address and offset the current read pointer address relative to the current write pointer address by the data length of the data to be read;
[0033] Step 13: Determine whether the current write pointer address is greater than the data length of the data to be read; if the current write pointer address is greater than the data length of the data to be read, then proceed to step 6.
[0034] Step 14: If the current write pointer address is less than or equal to the data length of the data to be read, determine whether the lock-free circular shared memory is full. If the lock-free circular shared memory is full, divide the data to be read into a third sub-data to be read and a fourth sub-data to be read, and read the third sub-data to be read from the region of the target start address in the buffer, and read the fourth sub-data to be read from the region of the buffer whose start position is the head address of the buffer.
[0035] In some possible implementations, prior to step eight, the following is also included:
[0036] Step 15: Based on the current read pointer address, the current write pointer address, the buffer capacity, and the data length of the data to be read, determine whether the second time interval between writing data to the buffer and reading data from the buffer is greater than a threshold time interval;
[0037] Step 16: If the second time interval is less than or equal to the threshold time interval, proceed to step 8;
[0038] Step 17: If the second time interval is greater than the threshold time interval, proceed to Step 12.
[0039] In some possible implementations, the length of the first sub-data to be read is:
[0040]
[0041] The length of the second sub-data to be read is:
[0042]
[0043] The length of the third sub-data to be read is:
[0044]
[0045] The length of the fourth sub-data to be read is:
[0046]
[0047] The target starting address is:
[0048]
[0049] In the formula, This refers to the first sub-data to be read; This is the second sub-data to be read; The third sub-data to be read; This refers to the fourth sub-data to be read; The target starting address; The buffer capacity is [value]. The length of the data to be read; The current read pointer address is denoted as ; capacity is the buffer capacity.
[0050] In some possible implementations, the step of offsetting the current read pointer address based on the data to be read to obtain the offset read pointer address includes:
[0051] When the sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address, and the first time interval is less than or equal to the threshold time interval; or, when the current write pointer address is greater than the data length of the data to be read, the offset read pointer address is:
[0052]
[0053] When the current write pointer address is less than the length of the data to be read, and the lock-free circular shared memory is full of data, the offset read pointer address is:
[0054]
[0055] When the current read pointer address is greater than the current write pointer address, the lock-free circular shared memory is full of data, and the second time interval is less than or equal to the threshold time interval, the offset read pointer address is:
[0056]
[0057] In the formula, reader o The offset read pointer address; reader c The current read pointer address; writer c The address of the current write pointer.
[0058] In some possible implementations, obtaining the current read pointer address includes:
[0059] Initialize the read pointer to obtain the initial read pointer address, and offset the initial read pointer by a specified length to obtain the pointer offset address;
[0060] Determine whether the difference between the initial read pointer address and the pointer offset address is less than 0;
[0061] If the difference between the initial read pointer address and the pointer offset address is less than 0, then it is determined whether the lock-free circular shared memory is full. If the lock-free shared memory is full, then the current read pointer address is: If the lock-free shared memory is not full, then the current read pointer address is the head address of the buffer.
[0062] If the difference between the current read pointer address and the pointer offset address is greater than or equal to 0, then the current read pointer address is: ;
[0063] in, The current read pointer address; capacity is the buffer capacity; writer c The current write pointer address is denoted as ; offset is the specified offset length.
[0064] On the other hand, the present invention also provides a communication method based on lock-free ring shared memory, comprising:
[0065] Data is written to the buffer of the lock-free circular shared memory using a data writing method based on lock-free circular shared memory;
[0066] Data is read from the buffer of the lockless circular shared memory using a data reading method based on lockless circular shared memory;
[0067] The data writing method based on lock-free circular shared memory is any possible implementation of the data writing method based on lock-free circular shared memory described above, and the data reading method based on lock-free circular shared memory is any possible implementation of the data reading method based on lock-free circular shared memory described above.
[0068] The beneficial effects of the above embodiments are: the data writing method based on lock-free circular shared memory provided by the present invention can write the data to be written to the buffer according to the current write pointer address without performing logical judgments related to the read pointer. That is, the writer is not dependent on the reader, thereby avoiding the loss of the data to be written or the untimely writing, and ensuring the timeliness and integrity of the data to be written. Attached Figure Description
[0069] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0070] Figure 1 A schematic flowchart of an embodiment of the data writing method based on lock-free ring shared memory provided by the present invention;
[0071] Figure 2 A schematic diagram of an overall embodiment of the data writing method based on lock-free circular shared memory provided by the present invention;
[0072] Figure 3 For the present invention Figure 1 A schematic diagram of an embodiment of S103;
[0073] Figure 4 This is a schematic flowchart of an embodiment of the data reading method based on lock-free ring shared memory provided by the present invention;
[0074] Figure 5 A schematic diagram of an overall embodiment of the data reading method based on lock-free ring shared memory provided by the present invention;
[0075] Figure 6 This is a schematic flowchart of an embodiment of the present invention that performs a delay judgment on the data to be read before S403;
[0076] Figure 7 This is a schematic flowchart of an embodiment of the present invention that performs a delay determination on the data to be read before S405;
[0077] Figure 8 For the present invention Figure 4 A schematic diagram of an embodiment of S401;
[0078] Figure 9 This is a schematic flowchart of an embodiment of the communication method based on lock-free ring shared memory provided by the present invention. Detailed Implementation
[0079] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative effort are within the scope of protection of the present invention.
[0080] In the description of the embodiments of this application, unless otherwise stated, "a plurality of" means two or more.
[0081] Some of the block diagrams shown in the accompanying drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities can be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor systems and / or microcontroller systems.
[0082] In this document, the term "embodiment" means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of the invention. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0083] This invention provides a method for data writing, reading, and communication based on lock-free ring shared memory, which will be described below.
[0084] Figure 1 This is a schematic flowchart of an embodiment of the data writing method based on lock-free circular shared memory provided by the present invention. Figure 2 This is a schematic diagram of an overall embodiment of the data writing method based on lock-free circular shared memory provided by the present invention, as shown below. Figure 1 and Figure 2 As shown, the data writing method based on lock-free ring shared memory includes:
[0085] S101. Obtain the buffer capacity and current write pointer address (writer) of the lock-free circular shared memory. c ;
[0086] S102. Obtain the data to be written and its length (datalen). w ;
[0087] S103. Write the data to be written to the buffer based on the current write pointer address and the buffer capacity;
[0088] S104. Offset the current write pointer address based on the data to be written to obtain the offset write pointer address (writer). o The offset write pointer address is used as the current write pointer address, and the process returns to step S102.
[0089] Compared with the prior art, the data writing method based on lock-free circular shared memory provided by the embodiments of the present invention can write the data to be written to the buffer according to the current write pointer address, without performing logical judgments related to the read pointer when writing data. That is, the writer is not dependent on the reader, thereby avoiding the loss of the data to be written or the untimely writing, and ensuring the timeliness and integrity of the data to be written.
[0090] It should be understood that a lock-free circular shared memory, including a buffer, should be requested before step S101.
[0091] It should also be understood that the data to be written is not limited to waveform data; it can be any type of data.
[0092] In some embodiments of the present invention, such as Figure 2 and Figure 3 As shown, step S103 includes:
[0093] S301. Determine whether the sum of the current write pointer address and the data length of the data to be written is less than or equal to the buffer capacity;
[0094] S302. If the sum of the current write pointer address and the data length of the data to be written is less than or equal to the buffer capacity, then the data to be written is written to the area in the buffer whose starting position is the current write pointer address.
[0095] S303. If the sum of the current write pointer address and the data length of the data to be written is greater than the buffer capacity, the data to be written is divided into a first sub-data to be written and a second sub-data to be written. The first sub-data to be written is written to the region in the buffer whose starting position is the current write pointer address and whose ending position is the end address of the buffer. The second sub-data to be written is written to the region whose starting position is the head address of the buffer. The lock-free circular shared memory is marked as full.
[0096] Specifically: the length of the first sub-data to be written is:
[0097]
[0098] The length of the second sub-data to be written is:
[0099]
[0100] In the formula, L1 is the first sub-data to be written; L2 is the second sub-data to be written; capacity is the buffer capacity; writer c The current write pointer address; datalen w The length of the data to be written.
[0101] In some embodiments of the present invention, step S104, which involves offsetting the current write pointer address based on the data to be written to obtain the offset write pointer address, includes:
[0102] When the sum of the current write pointer address and the length of the data to be written is less than or equal to the buffer capacity, the offset write pointer address is:
[0103]
[0104] When the sum of the current write pointer address and the length of the data to be written is greater than the buffer capacity, the offset write pointer address is:
[0105]
[0106] In the formula, writer o Write the pointer address as the offset.
[0107] Existing data reading methods based on lock-free circular shared memory cannot achieve continuous data reading from the buffer for a single reader when multiple readers access the data.
[0108] To address the technical problem in existing lock-free circular shared memory-based data reading methods where multiple readers cannot read consecutive data from the buffer, this invention also provides a lock-free circular shared memory-based data reading method. Figure 4 This is a schematic flowchart of an embodiment of the data reading method based on lock-free circular shared memory provided by the present invention. Figure 5 This is a schematic diagram of an overall embodiment of the data reading method based on lock-free circular shared memory provided by the present invention, as shown below. Figure 4 and Figure 5 As shown, the data reading method based on lock-free ring shared memory includes:
[0109] S401. Obtain the current read pointer address, the current write pointer address, the length of the data to be read, and the buffer capacity;
[0110] S402. Determine whether the sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address;
[0111] S403. If the sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address, then the data to be read is read from the region in the buffer whose starting position is the current read pointer address.
[0112] S404. If the sum of the current read pointer address and the length of the data to be read is greater than the current write pointer address, then determine whether the current read pointer address is less than or equal to the current write pointer address, and whether the lock-free circular shared memory is full of data.
[0113] S405. If the current read pointer address is greater than the current write pointer address, and the lock-free circular shared memory is full of data, then the data to be read is divided into the first sub-data to be read and the second sub-data to be read. The first sub-data to be read is read from the region in the buffer whose starting position is the current read pointer address, and the second sub-data to be read is read from the region in the buffer whose starting position is the head of the buffer.
[0114] S406. Based on the data to be read, offset the current read pointer address to obtain the offset read pointer address, use the offset read pointer address as the current read pointer address, and return to step S401.
[0115] The data reading method based on lock-free circular shared memory provided in this invention sets each reader to have its own current reader address, so that they do not interfere with each other. This allows a single reader to start reading continuous data from any position without affecting other readers, achieving the technical effect of lock-free reading of continuous data by multiple readers.
[0116] It should be understood that when multiple readers read the data to be read from the buffer, multiple readers can be multi-threaded, multi-processed, multi-CPU, etc., without being specifically limited here.
[0117] It should also be understood that the data to be read is not limited to waveform data; it can be any type of data.
[0118] Furthermore, in practical applications, abnormal delays in data retrieval may occur due to special circumstances such as CPU strain or network lag. Some application scenarios require highly timely real-time data, such as doctors needing to observe real-time waveform data of a patient's ECG, respiration, and blood oxygenation at the device or central service station to take timely emergency measures. To ensure the timeliness of the retrieved data, in some embodiments of this invention, such as... Figure 5 and Figure 6 As shown, before step S403, the following steps are also included:
[0119] S601. Based on the current read pointer address, the current write pointer address, and the data length of the data to be read, determine whether the first time interval between writing data to the buffer and reading data from the buffer is greater than the threshold time interval.
[0120] S602. If the first time interval is less than or equal to the threshold time interval, proceed to step S403; if the first time interval is greater than the threshold time interval, proceed to step S603.
[0121] S603. Reset the current read pointer address and offset the current read pointer address relative to the current write pointer address by the data length of the data to be read;
[0122] S604. Determine whether the current write pointer address is greater than the data length of the data to be read; if the current write pointer address is greater than the data length of the data to be read, proceed to step S403.
[0123] S605. If the current write pointer address is less than or equal to the length of the data to be read, determine whether the lock-free circular shared memory is full. If the lock-free circular shared memory is full, divide the data to be read into a third sub-data to be read and a fourth sub-data to be read. Read the third sub-data to be read from the region of the target start address in the buffer and read the fourth sub-data to be read from the region of the buffer whose start position is the buffer head address.
[0124] In this embodiment of the invention, when reading data, the read data is subject to delay and validity checks to ensure the timeliness of data reading.
[0125] In some embodiments of the present invention, such as Figure 5 and Figure 7 As shown, before step S405, the procedure further includes:
[0126] S701. Based on the current read pointer address, the current write pointer address, the buffer capacity, and the data length of the data to be read, determine whether the second time interval between writing data to the buffer and reading data from the buffer is greater than the threshold time interval.
[0127] S702. If the second time interval is less than or equal to the threshold time interval, proceed to step S405.
[0128] S703. If the second time interval is greater than the threshold time interval, then proceed to step S603.
[0129] This invention ensures the timeliness of the entire data reading process by judging the timeliness of data reading even when the sum of the current read pointer address and the data length to be read is greater than the current write pointer address.
[0130] It should be noted that the threshold time interval should be adjusted according to actual needs.
[0131] Specifically: the length of the first sub-data to be read is:
[0132]
[0133] The length of the second sub-data to be read is:
[0134]
[0135] The length of the third sub-data to be read is:
[0136]
[0137] The length of the fourth sub-data to be read is:
[0138]
[0139] The target starting address is:
[0140]
[0141] In the formula, This is the first sub-data to be read; This is the second piece of data to be read; This is the third sub-data to be read; This is the fourth piece of data to be read; The starting address of the target; For buffer capacity; The length of the data to be read; The current read pointer address; capacity is the buffer capacity.
[0142] In some embodiments of the present invention, such as Figure 5 As shown, step S406, which involves offsetting the current read pointer address based on the data to be read to obtain the offset read pointer address, includes:
[0143] When the sum of the current read pointer address and the length of the data to be read is less than or equal to the current write pointer address, and the first time interval is less than or equal to the threshold time interval; or, when the current write pointer address is greater than the length of the data to be read, the offset read pointer address is:
[0144]
[0145] When the current write pointer address is less than the length of the data to be read, and the lock-free circular shared memory is full, the offset read pointer address is:
[0146]
[0147] When the current read pointer address is greater than the current write pointer address, the lock-free circular shared memory is full of data, and the second time interval is less than or equal to the threshold time interval, the offset read pointer address is:
[0148]
[0149] In the formula, reader o This is the offset address of the read pointer; reader c The current read pointer address; writer c This is the address of the current write pointer.
[0150] In some embodiments of the present invention, such as Figure 5 and Figure 8 As shown, obtaining the current read pointer address in step S401 includes:
[0151] S801. Initialize the read pointer, obtain the initial read pointer address, and offset the initial read pointer by a specified length to obtain the pointer offset address;
[0152] S802. Determine whether the difference between the initial read pointer address and the pointer offset address is less than 0;
[0153] S803. If the difference between the initial read pointer address and the pointer offset address is less than 0, then determine whether the lock-free circular shared memory is full. If the lock-free shared memory is full, then the current read pointer address is: If the lock-free shared memory is not full, the current read pointer address is the head address of the buffer.
[0154] S804. If the difference between the current read pointer address and the pointer offset address is greater than or equal to 0, then the current read pointer address is: ;
[0155] in, The current read pointer address; capacity is the buffer capacity; writer c The current write pointer address is specified; offset is the specified offset length.
[0156] On the other hand, embodiments of the present invention also provide a communication method based on lock-free ring shared memory, such as... Figure 9 As shown, the communication method based on lock-free ring shared memory includes:
[0157] S901, Data is written to the buffer of lock-free circular shared memory using a data writing method based on lock-free circular shared memory;
[0158] S902, Read data from the buffer of the lockless ring shared memory using a data reading method based on lockless ring shared memory;
[0159] The data writing method based on lock-free circular shared memory is any one of the lock-free circular shared memory data writing methods in the above embodiments, and the data reading method based on lock-free circular shared memory is any one of the lock-free circular shared memory data reading methods in the above embodiments.
[0160] The data writing, reading, and communication method based on lock-free circular shared memory provided in this invention ensures the timeliness and integrity of data writing by not performing logical judgments related to the read pointer when writing data, but simply copying the data and moving the write pointer. When reading data, the relative distance between the read and write pointers determines the effective data length to be read, and the validity and latency of the read data are judged to ensure timely data reading and ultimately achieve real-time read and write performance. Furthermore, a single reader can start reading continuous data from any reasonable position without affecting other readers, achieving a truly lock-free multi-reader reading effect.
[0161] Those skilled in the art will understand that all or part of the processes of the methods described in the above embodiments can be implemented by a computer program instructing related hardware, and the computer program can be stored in a computer-readable storage medium. The computer-readable storage medium may be a disk, optical disk, read-only memory, or random access memory, etc.
[0162] The above provides a detailed description of the data writing, reading, and communication method based on lock-free ring shared memory provided by this invention. Specific examples have been used to illustrate the principles and implementation methods of this invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of this invention. At the same time, those skilled in the art will recognize that there will be changes in the specific implementation methods and application scope based on the ideas of this invention. Therefore, the content of this specification should not be construed as a limitation of this invention.
Claims
1. A data reading method based on a lock-free ring-shaped shared memory, characterized in that, Comprising: Step four, obtaining a current read pointer address, a current write pointer address, a data length of data to be read, and a buffer capacity of a buffer; Step five, judging whether a sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address; Step six, if the sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address, reading the data to be read from a region with the current read pointer address as a starting position in the buffer; Step seven, if the sum of the current read pointer address and the data length of the data to be read is greater than the current write pointer address, judging whether the current read pointer address is less than or equal to the current write pointer address, and whether the lock-free ring-shaped shared memory is full of data; Step eight, if the current read pointer address is greater than the current write pointer address and the lock-free ring-shaped shared memory is full of data, dividing the data to be read into a first data to be read and a second data to be read, reading the first data to be read from a region with the current read pointer address as a starting position in the buffer, and reading the second data to be read from a region with a head of the buffer as a starting position in the buffer; Step nine, offsetting the current read pointer address based on the data to be read to obtain an offset read pointer address, taking the offset read pointer address as the current read pointer address, and returning to the step four; Before the step six, further comprising: Step ten, judging whether a first time interval of writing data into the buffer and reading data in the buffer based on the current read pointer address, the current write pointer address, and the data length of the data to be read is greater than a threshold time interval; Step eleven, if the first time interval is less than or equal to the threshold time interval, performing the step six; if the first time interval is greater than the threshold time interval, performing step twelve; Step twelve, resetting the current read pointer address, and offsetting the current read pointer address relative to the current write pointer address by the data length of the data to be read; Step thirteen, judging whether the current write pointer address is greater than the data length of the data to be read; if the current write pointer address is greater than the data length of the data to be read, performing the step six; Step fourteen, if the current write pointer address is less than or equal to the data length of the data to be read, judging whether the lock-free ring-shaped shared memory is full; if the lock-free ring-shaped shared memory is full, dividing the data to be read into a third data to be read and a fourth data to be read, reading the third data to be read from a region with a target starting address in the buffer, and reading the fourth data to be read from a region with the head of the buffer as a starting position in the buffer.
2. The data reading method based on the lock-free ring-shaped shared memory according to claim 1, wherein, Before the step eight, further comprising: Step fifteen, judging whether a second time interval of writing data into the buffer and reading data in the buffer is greater than a threshold time interval based on the current read pointer address, the current write pointer address, the buffer capacity and the data length of the data to be read; Step sixteen, if the second time interval is less than or equal to the threshold time interval, proceeding to step eight; Step seventeen, if the second time interval is greater than the threshold time interval, proceeding to step twelve.
3. The data reading method based on the lock-free ring-shaped shared memory according to claim 2, characterized in that, The length of the first sub-data to be read is: The length of the second sub-data to be read is: The length of the third sub-data to be read is: The length of the fourth sub-data to be read is: The target start address is: wherein, is the first to-be-read sub-data; is the second to-be-read sub-data; is the third to-be-read sub-data; is the fourth to-be-read sub-data; is the target start address; is the buffer capacity; is the data length of the to-be-read data; is the current read pointer address; and capacity is the buffer capacity.
4. The data reading method based on the lock-free ring-shaped shared memory according to claim 3, characterized in that, The offset read pointer address is obtained by offsetting the current read pointer address based on the data to be read, comprising: When the sum of the current read pointer address and the data length of the data to be read is less than or equal to the current write pointer address, and the first time interval is less than or equal to the threshold time interval; or, when the current write pointer address is greater than the data length of the data to be read, the offset read pointer address is: When the current write pointer address is less than the data length of the data to be read, and the lock-free ring-shaped shared memory is full of data, the offset read pointer address is: When the current read pointer address is greater than the current write pointer address, the lock-free ring-shaped shared memory is full of data, and the second time interval is less than or equal to the threshold time interval, the offset write pointer address is: wherein reader o is the offset read pointer address; reader c is the current read pointer address; writer c is the current write pointer address.
5. The lockless ring buffer shared memory based data reading method according to claim 1, wherein, The current read pointer address is obtained by: Initializing the read pointer to obtain an initial read pointer address, and offsetting the initial read pointer by a specified length to obtain a pointer offset address; Judging whether the difference between the initial read pointer address and the pointer offset address is less than 0; If the difference between the initial read pointer address and the pointer offset address is less than 0, it is determined whether the lock-free ring shared memory is full, and if the lock-free shared memory is full, the current read pointer address is: ; and if the lock-free shared memory is not full, the current read pointer address is the head address of the buffer. if a difference between the current read pointer address and the pointer offset address is greater than or equal to zero, then the current read pointer address is: ; wherein, is the current read pointer address; capacity is the buffer capacity; writer c is the current write pointer address; offset is the specified offset length.
6. A communication method based on a lock-free ring-shaped shared memory, characterized by, Comprising: Writing data into the buffer of the lock-free ring-shaped shared memory based on the data writing method based on the lock-free ring-shaped shared memory; Reading data from the buffer of the lock-free ring-shaped shared memory based on the data reading method based on the lock-free ring-shaped shared memory; The data reading method based on the lock-free ring-shaped shared memory is any one of the data reading methods based on the lock-free ring-shaped shared memory in claims 1-5.
7. The communication method based on the lock-free ring-shaped shared memory according to claim 6, characterized in that, The data writing method based on the lock-free ring-shaped shared memory, comprising: Step one, obtaining the buffer capacity of the buffer in the lock-free ring-shaped shared memory and the current write pointer address; Step two, obtaining the data to be written and the data length of the data to be written; Step three, writing the data to be written into the buffer based on the current write pointer address and the buffer capacity; Step four, offsetting the current write pointer address based on the data to be written to obtain an offset write pointer address, taking the offset write pointer address as the current write pointer address, and returning to step two.
8. The communication method based on the lock-free ring-shaped shared memory according to claim 7, characterized in that, The step three comprises: Judging whether the sum of the current write pointer address and the data length of the data to be written is less than or equal to the buffer capacity; if the sum of the current write pointer address and the data length of the data to be written is less than or equal to the buffer capacity, writing the data to be written into a region in the buffer whose starting position is the current write pointer address; If the sum of the current write pointer address and the data length of the data to be written is greater than the buffer capacity, the data to be written is divided into a first sub-data to be written with a data length of and a second sub-data to be written with a data length of The first sub-data to be written is written into the region of the buffer with a starting position of the current write pointer address and an ending position of the tail address of the buffer, the second sub-data to be written is written into the region with a starting position of the head address of the buffer, and the lock-free ring-shaped shared memory is marked as full. wherein, capacity is the buffer capacity; writer c is the current write pointer address; datalen w is the data length of the data to be written.
9. The communication method based on the lock-free ring-shaped shared memory according to claim 8, characterized in that, the offsetting the current write pointer address based on the data to be written to obtain an offset write pointer address comprises: when the sum of the current write pointer address and the data length of the data to be written is less than or equal to the buffer capacity, the offset write pointer address is: when the sum of the current write pointer address and the data length of the data to be written is greater than the buffer capacity, the offset write pointer address is: when the sum of the current write pointer address and the data length of the data to be written is greater than the buffer capacity, the offset write pointer address is: In the formula, writer o is the offset write pointer address; writer c is the current write pointer address; datalen w is the data length of the data to be written; and capacity is the buffer capacity.
Citation Information
Patent Citations
Communication method of symbiotic virtual machine based on multi-core lockless ring buffer area
CN106790599A
Data reading and writing method and device, and circle queue
CN108733344A
Data reading and writing method and device based on asynchronous annular buffer area
CN110704335A