Lock-free ring buffer

Saved in:
Bibliographic Details
Title: Lock-free ring buffer
Patent Number: 11886,343
Publication Date: January 30, 2024
Appl. No: 17/454475
Application Filed: November 10, 2021
Abstract: According to at least one embodiment, a method for writing, by a computing thread, data to a ring buffer is disclosed. The method includes determining whether the ring buffer is full. If the ring buffer is not full, the method further includes: reserving an element of the ring buffer for writing the data, wherein reserving the element includes incrementing a size variable corresponding to a number of stored elements in the ring buffer; reserving a portion of the ring buffer at which the data is to be written; and determining whether a state of the portion of the ring buffer is in change by at least one other computing thread. If the state is not in change, the method further includes: marking the state of the portion of the ring buffer as being in change by the computing thread; and writing the data to the portion of the ring buffer.
Inventors: DreamWorks Animation LLC (Glendale, CA, US)
Assignees: DREAMWORKS ANIMATION LLC (Glendale, CA, US)
Claim: 1. A method for writing, by a computing thread, data to a ring buffer, the method comprising: determining whether the ring buffer is full, based on an atomic variable that represents a number of stored elements in the ring buffer; and in response to determining that the ring buffer is not full: incrementing a size variable corresponding to the number of stored elements in the ring buffer; reserving a portion of the ring buffer at which the data is to be written; determining whether a state of the portion of the ring buffer is in change by at least one other computing thread; and in response to determining that the state of the portion of the ring buffer is not in change by the at least one other computing thread: marking the state of the portion of the ring buffer as being in change by the computing thread; and writing the data to the portion of the ring buffer.
Claim: 2. The method of claim 1 , wherein: the portion of the ring buffer comprises a plurality of elements; and reserving the portion of the ring buffer comprises reserving each element of the plurality of elements, each element corresponding to a different index of the ring buffer.
Claim: 3. The method of claim 2 , wherein determining whether the state of the portion is in change by the at least one other computing thread comprises determining whether a state of each element of the plurality of elements is in change by at least one other computer thread.
Claim: 4. The method of claim 1 , wherein determining whether the state of the portion of the ring buffer is in change by the at least one other computing thread is based on a state variable having one of at least three values.
Claim: 5. The method of claim 4 , wherein: a first value of the state variable corresponds to an unoccupied state; a second value of the state variable corresponds to an in-transition state; and a third value of the state variable corresponds to an occupied state.
Claim: 6. The method of claim 5 , wherein marking the state of the portion of the ring buffer as being in change by the computing thread comprises marking (updating) the state variable to have the second value.
Claim: 7. The method of claim 5 , wherein: the portion of the ring buffer comprises a plurality of elements; and marking the state of the portion of the ring buffer as being in change by the computing thread comprises marking (updating) the state variable of a corresponding at least one element of the plurality of elements to have the second value.
Claim: 8. The method of claim 7 , wherein writing the data to the portion of the ring buffer comprises writing a respective subset of the data to the corresponding at least one element of the plurality of elements.
Claim: 9. The method of claim 5 , further comprising: in response to writing the data to the portion of the ring buffer, marking (updating) the state variable to have the third value.
Claim: 10. A machine-readable non-transitory medium having stored thereon machine-executable instructions for writing, by a computing thread, data to a ring buffer, the instructions comprising: determining whether the ring buffer is full, based on an atomic variable that represents a number of stored elements in the ring buffer; and in response to determining that the ring buffer is not full: incrementing a size variable corresponding to the number of stored elements in the ring buffer; reserving a portion of the ring buffer at which the data is to be written; determining whether a state of the portion of the ring buffer is in change by at least one other computing thread; and in response to determining that the state of the portion of the ring buffer is not in change by the at least one other computing thread: marking the state of the portion of the ring buffer as being in change by the computing thread; and writing the data to the portion of the ring buffer.
Claim: 11. The machine-readable non-transitory medium of claim 10 , wherein: the portion of the ring buffer comprises a plurality of elements; and reserving the portion of the ring buffer comprises reserving each element of the plurality of elements, each element corresponding to a different index of the ring buffer.
Claim: 12. The machine-readable non-transitory medium of claim 11 , wherein determining whether the state of the portion is in change by the at least one other computing thread comprises determining whether a state of each element of the plurality of elements is in change by at least one other computer thread.
Claim: 13. The machine-readable non-transitory medium of claim 10 , wherein determining whether the state of the portion of the ring buffer is in change by the at least one other computing thread is based on a state variable having one of at least three values.
Claim: 14. The machine-readable non-transitory medium of claim 13 , wherein: a first value of the state variable corresponds to an unoccupied state; a second value of the state variable corresponds to an in-transition state; and a third value of the state variable corresponds to an occupied state.
Claim: 15. The machine-readable non-transitory medium of claim 14 , wherein marking the state of the portion of the ring buffer as being in change by the computing thread comprises marking (updating) the state variable to have the second value.
Claim: 16. The machine-readable non-transitory medium of claim 14 , wherein: the portion of the ring buffer comprises a plurality of elements; marking the state of the portion of the ring buffer as being in change by the computing thread comprises marking (updating) the state variable of a corresponding at least one element of the plurality of elements to have the second value; and writing the data to the portion of the ring buffer comprises writing a respective subset of the data to the corresponding at least one element of the plurality of elements.
Claim: 17. The machine-readable non-transitory medium of claim 14 , further comprising: in response to writing the data to the portion of the ring buffer, marking (updating) the state variable to have the third value.
Claim: 18. A method for reading data from a ring buffer by a computing thread, the method comprising: determining whether the ring buffer is empty, based on an atomic variable that represents a number of stored elements in the ring buffer; in response to determining that the ring buffer is not empty: clearing an element of the ring buffer storing the data, wherein clearing the element comprises decrementing a size variable corresponding to the number of stored elements in the ring buffer; identifying a portion of the ring buffer from which the data is to be read; determining whether a state of the portion of the ring buffer is in change by at least one other computing thread; and in response to determining that the state of the portion is not in change by the at least one other computing thread: marking the state of the portion of the ring buffer as being in change by the computing thread; reading the data from the portion of the ring buffer; and destroying the data in the portion of the ring buffer.
Patent References Cited: 8611173 December 2013 Zhang
11314566 April 2022 Klee
20040181635 September 2004 Huras
20090204755 August 2009 Rushworth et al.
20090249356 October 2009 He et al.
20100332755 December 2010 Bu et al.
20110078249 March 2011 Blocksome
20130198419 August 2013 Jones et al.
20130246672 September 2013 Saputra
20140013021 January 2014 Matsumoto
20190087123 March 2019 Liljedahl
20190236749 August 2019 Gould et al.
20210303375 September 2021 Stevens
20210303551 September 2021 Stevens
Other References: European Patent Office Application Serial No. 22165682.0, Extended Search Report dated Aug. 4, 2022, 10 pages. cited by applicant
Primary Examiner: Patel, Kaushikkumar M
Attorney, Agent or Firm: LEE, HONG, DEGERMAN, KANG & WAIMEY
Accession Number: edspgr.11886343
Database: USPTO Patent Grants
Description
Abstract:According to at least one embodiment, a method for writing, by a computing thread, data to a ring buffer is disclosed. The method includes determining whether the ring buffer is full. If the ring buffer is not full, the method further includes: reserving an element of the ring buffer for writing the data, wherein reserving the element includes incrementing a size variable corresponding to a number of stored elements in the ring buffer; reserving a portion of the ring buffer at which the data is to be written; and determining whether a state of the portion of the ring buffer is in change by at least one other computing thread. If the state is not in change, the method further includes: marking the state of the portion of the ring buffer as being in change by the computing thread; and writing the data to the portion of the ring buffer.