sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget,/translations/zh_CN/trace/ring-buffer-designmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget,/translations/zh_TW/trace/ring-buffer-designmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget,/translations/it_IT/trace/ring-buffer-designmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget,/translations/ja_JP/trace/ring-buffer-designmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget,/translations/ko_KR/trace/ring-buffer-designmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget,/translations/sp_SP/trace/ring-buffer-designmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhcomment)}(h?SPDX-License-Identifier: GPL-2.0 OR GFDL-1.2-no-invariants-onlyh]h?SPDX-License-Identifier: GPL-2.0 OR GFDL-1.2-no-invariants-only}hhsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhhhF/var/lib/git/docbuild/linux/Documentation/trace/ring-buffer-design.rsthKubhsection)}(hhh](htitle)}(hLockless Ring Buffer Designh]hLockless Ring Buffer Design}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh paragraph)}(hCopyright 2009 Red Hat Inc.h]hCopyright 2009 Red Hat Inc.}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh field_list)}(hhh](hfield)}(hhh](h field_name)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh field_body)}(h$Steven Rostedt h]h)}(hhh](hSteven Rostedt <}(hhhhhNhNubh reference)}(hsrostedt@redhat.comh]hsrostedt@redhat.com}(hjhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:srostedt@redhat.comuh1jhhubh>}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(hhh](h)}(hLicenseh]hLicense}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj(hhhKubh)}(hPThe GNU Free Documentation License, Version 1.2 (dual licensed under the GPL v2)h]h)}(hPThe GNU Free Documentation License, Version 1.2 (dual licensed under the GPL v2)h]hPThe GNU Free Documentation License, Version 1.2 (dual licensed under the GPL v2)}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hj9ubah}(h]h ]h"]h$]h&]uh1hhj(ubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(hhh](h)}(h Reviewersh]h Reviewers}(hjZhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjWhhhKubh)}(hIMathieu Desnoyers, Huang Ying, Hidetoshi Seto, and Frederic Weisbecker. h]h)}(hGMathieu Desnoyers, Huang Ying, Hidetoshi Seto, and Frederic Weisbecker.h]hGMathieu Desnoyers, Huang Ying, Hidetoshi Seto, and Frederic Weisbecker.}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hjhubah}(h]h ]h"]h$]h&]uh1hhjWubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubeh}(h]h ]h"]h$]h&]uh1hhhhhhhhK ubh)}(hWritten for: 2.6.31h]hWritten for: 2.6.31}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hhh](h)}(h!Terminology used in this Documenth]h!Terminology used in this Document}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubhdefinition_list)}(hhh](hdefinition_list_item)}(h3tail - where new writes happen in the ring buffer. h](hterm)}(htailh]htail}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubh definition)}(hhh]h bullet_list)}(hhh]h list_item)}(h,where new writes happen in the ring buffer. h]h)}(h+where new writes happen in the ring buffer.h]h+where new writes happen in the ring buffer.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]bullet-uh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(h2head - where new reads happen in the ring buffer. h](j)}(hheadh]hhead}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]j)}(hhh]j)}(h+where new reads happen in the ring buffer. h]h)}(h*where new reads happen in the ring buffer.h]h*where new reads happen in the ring buffer.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjhhubj)}(hFproducer - the task that writes into the ring buffer (same as writer) h](j)}(hproducerh]hproducer}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhj@ubj)}(hhh]j)}(hhh]j)}(h;the task that writes into the ring buffer (same as writer) h]h)}(h:the task that writes into the ring buffer (same as writer)h]h:the task that writes into the ring buffer (same as writer)}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjXubah}(h]h ]h"]h$]h&]uh1jhjUubah}(h]h ]h"]h$]h&]jjuh1jhhhKhjRubah}(h]h ]h"]h$]h&]uh1jhj@ubeh}(h]h ]h"]h$]h&]uh1jhhhKhjhhubj)}(hwriter - same as producer h](j)}(hwriterh]hwriter}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]j)}(hhh]j)}(hsame as producer h]h)}(hsame as producerh]hsame as producer}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjhhubj)}(h@consumer - the task that reads from the buffer (same as reader) h](j)}(hconsumerh]hconsumer}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK"hjubj)}(hhh]j)}(hhh]j)}(h5the task that reads from the buffer (same as reader) h]h)}(h4the task that reads from the buffer (same as reader)h]h4the task that reads from the buffer (same as reader)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK"hjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhK"hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK"hjhhubj)}(hreader - same as consumer. h](j)}(hreaderh]hreader}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK%hjubj)}(hhh]j)}(hhh]j)}(hsame as consumer. h]h)}(hsame as consumer.h]hsame as consumer.}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK%hjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhK%hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK%hjhhubj)}(h^reader_page - A page outside the ring buffer used solely (for the most part) by the reader. h](j)}(h reader_pageh]h reader_page}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK)hjHubj)}(hhh]j)}(hhh]j)}(hNA page outside the ring buffer used solely (for the most part) by the reader. h]h)}(hMA page outside the ring buffer used solely (for the most part) by the reader.h]hMA page outside the ring buffer used solely (for the most part) by the reader.}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK(hj`ubah}(h]h ]h"]h$]h&]uh1jhj]ubah}(h]h ]h"]h$]h&]jjuh1jhhhK(hjZubah}(h]h ]h"]h$]h&]uh1jhjHubeh}(h]h ]h"]h$]h&]uh1jhhhK)hjhhubj)}(h@head_page - a pointer to the page that the reader will use next h](j)}(h head_pageh]h head_page}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK,hjubj)}(hhh]j)}(hhh]j)}(h4a pointer to the page that the reader will use next h]h)}(h3a pointer to the page that the reader will use nexth]h3a pointer to the page that the reader will use next}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhK,hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK,hjhhubj)}(h?tail_page - a pointer to the page that will be written to next h](j)}(h tail_pageh]h tail_page}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK/hjubj)}(hhh]j)}(hhh]j)}(h3a pointer to the page that will be written to next h]h)}(h2a pointer to the page that will be written to nexth]h2a pointer to the page that will be written to next}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK/hjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhK/hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK/hjhhubj)}(hMcommit_page - a pointer to the page with the last finished non-nested write. h](j)}(h commit_pageh]h commit_page}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK2hjubj)}(hhh]j)}(hhh]j)}(h?a pointer to the page with the last finished non-nested write. h]h)}(h>a pointer to the page with the last finished non-nested write.h]h>a pointer to the page with the last finished non-nested write.}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK2hj&ubah}(h]h ]h"]h$]h&]uh1jhj#ubah}(h]h ]h"]h$]h&]jjuh1jhhhK2hj ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhK2hjhhubj)}(hXcmpxchg - hardware-assisted atomic transaction that performs the following:: A = B if previous A == C R = cmpxchg(A, C, B) is saying that we replace A with B if and only if current A is equal to C, and we put the old (current) A into R R gets the previous A regardless if A is updated with B or not. To see if the update was successful a compare of ``R == C`` may be used. h](j)}(hcmpxchgh]hcmpxchg}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhK@hjPubj)}(hhh]j)}(hhh]j)}(hXhardware-assisted atomic transaction that performs the following:: A = B if previous A == C R = cmpxchg(A, C, B) is saying that we replace A with B if and only if current A is equal to C, and we put the old (current) A into R R gets the previous A regardless if A is updated with B or not. To see if the update was successful a compare of ``R == C`` may be used. h](h)}(hBhardware-assisted atomic transaction that performs the following::h]hAhardware-assisted atomic transaction that performs the following:}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK5hjhubh literal_block)}(hA = B if previous A == C R = cmpxchg(A, C, B) is saying that we replace A with B if and only if current A is equal to C, and we put the old (current) A into R R gets the previous A regardless if A is updated with B or not.h]hA = B if previous A == C R = cmpxchg(A, C, B) is saying that we replace A with B if and only if current A is equal to C, and we put the old (current) A into R R gets the previous A regardless if A is updated with B or not.}hj|sbah}(h]h ]h"]h$]h&]hhuh1jzhhhK7hjhubh)}(hHTo see if the update was successful a compare of ``R == C`` may be used.h](h1To see if the update was successful a compare of }(hjhhhNhNubhliteral)}(h ``R == C``h]hR == C}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh may be used.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK?hjhubeh}(h]h ]h"]h$]h&]uh1jhjeubah}(h]h ]h"]h$]h&]jjuh1jhhhK5hjbubah}(h]h ]h"]h$]h&]uh1jhjPubeh}(h]h ]h"]h$]h&]uh1jhhhK@hjhhubeh}(h]h ]h"]h$]h&]uh1jhjhhhNhNubeh}(h]!terminology-used-in-this-documentah ]h"]!terminology used in this documentah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hThe Generic Ring Bufferh]hThe Generic Ring Buffer}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKCubh)}(hUThe ring buffer can be used in either an overwrite mode or in producer/consumer mode.h]hUThe ring buffer can be used in either an overwrite mode or in producer/consumer mode.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKEhjhhubh)}(hProducer/consumer mode is where if the producer were to fill up the buffer before the consumer could free up anything, the producer will stop writing to the buffer. This will lose most recent events.h]hProducer/consumer mode is where if the producer were to fill up the buffer before the consumer could free up anything, the producer will stop writing to the buffer. This will lose most recent events.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKHhjhhubh)}(hOverwrite mode is where if the producer were to fill up the buffer before the consumer could free up anything, the producer will overwrite the older data. This will lose the oldest events.h]hOverwrite mode is where if the producer were to fill up the buffer before the consumer could free up anything, the producer will overwrite the older data. This will lose the oldest events.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKLhjhhubh)}(hX1No two writers can write at the same time (on the same per-cpu buffer), but a writer may interrupt another writer, but it must finish writing before the previous writer may continue. This is very important to the algorithm. The writers act like a "stack". The way interrupts works enforces this behavior::h]hX4No two writers can write at the same time (on the same per-cpu buffer), but a writer may interrupt another writer, but it must finish writing before the previous writer may continue. This is very important to the algorithm. The writers act like a “stack”. The way interrupts works enforces this behavior:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKPhjhhubj{)}(hwriter1 start writer2 start writer3 start writer3 finishes writer2 finishes writer1 finishesh]hwriter1 start writer2 start writer3 start writer3 finishes writer2 finishes writer1 finishes}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhKWhjhhubh)}(hhThis is very much like a writer being preempted by an interrupt and the interrupt doing a write as well.h]hhThis is very much like a writer being preempted by an interrupt and the interrupt doing a write as well.}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK^hjhhubh)}(hX{Readers can happen at any time. But no two readers may run at the same time, nor can a reader preempt/interrupt another reader. A reader cannot preempt/interrupt a writer, but it may read/consume from the buffer at the same time as a writer is writing, but the reader must be on another processor to do so. A reader may read on its own processor and can be preempted by a writer.h]hX{Readers can happen at any time. But no two readers may run at the same time, nor can a reader preempt/interrupt another reader. A reader cannot preempt/interrupt a writer, but it may read/consume from the buffer at the same time as a writer is writing, but the reader must be on another processor to do so. A reader may read on its own processor and can be preempted by a writer.}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKahjhhubh)}(hA writer can preempt a reader, but a reader cannot preempt a writer. But a reader can read the buffer at the same time (on another processor) as a writer.h]hA writer can preempt a reader, but a reader cannot preempt a writer. But a reader can read the buffer at the same time (on another processor) as a writer.}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhjhhubh)}(hMThe ring buffer is made up of a list of pages held together by a linked list.h]hMThe ring buffer is made up of a list of pages held together by a linked list.}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKlhjhhubh)}(h`At initialization a reader page is allocated for the reader that is not part of the ring buffer.h]h`At initialization a reader page is allocated for the reader that is not part of the ring buffer.}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKnhjhhubh)}(hWThe head_page, tail_page and commit_page are all initialized to point to the same page.h]hWThe head_page, tail_page and commit_page are all initialized to point to the same page.}(hjohhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKqhjhhubh)}(hThe reader page is initialized to have its next pointer pointing to the head page, and its previous pointer pointing to a page before the head page.h]hThe reader page is initialized to have its next pointer pointing to the head page, and its previous pointer pointing to a page before the head page.}(hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKthjhhubh)}(hXThe reader has its own page to use. At start up time, this page is allocated but is not attached to the list. When the reader wants to read from the buffer, if its page is empty (like it is on start-up), it will swap its page with the head_page. The old reader page will become part of the ring buffer and the head_page will be removed. The page after the inserted page (old reader_page) will become the new head page.h]hXThe reader has its own page to use. At start up time, this page is allocated but is not attached to the list. When the reader wants to read from the buffer, if its page is empty (like it is on start-up), it will swap its page with the head_page. The old reader page will become part of the ring buffer and the head_page will be removed. The page after the inserted page (old reader_page) will become the new head page.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKxhjhhubh)}(h|Once the new page is given to the reader, the reader could do what it wants with it, as long as a writer has left that page.h]h|Once the new page is given to the reader, the reader could do what it wants with it, as long as a writer has left that page.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hA sample of how the reader page is swapped: Note this does not show the head page in the buffer, it is for demonstrating a swap only.h]hA sample of how the reader page is swapped: Note this does not show the head page in the buffer, it is for demonstrating a swap only.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj{)}(hX,+------+ |reader| RING BUFFER |page | +------+ +---+ +---+ +---+ | |-->| |-->| | | |<--| |<--| | +---+ +---+ +---+ ^ | ^ | | +-------------+ | +-----------------+ +------+ |reader| RING BUFFER |page |-------------------+ +------+ v | +---+ +---+ +---+ | | |-->| |-->| | | | |<--| |<--| |<-+ | +---+ +---+ +---+ | | ^ | ^ | | | | +-------------+ | | | +-----------------+ | +------------------------------------+ +------+ |reader| RING BUFFER |page |-------------------+ +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | |-->| |-->| | | | | | | |<--| |<-+ | | +---+ +---+ +---+ | | | | ^ | | | | +-------------+ | | | +-----------------------------+ | +------------------------------------+ +------+ |buffer| RING BUFFER |page |-------------------+ +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | | | |-->| | | | New | | | |<--| |<-+ | | Reader +---+ +---+ +---+ | | | page ----^ | | | | | | | +-----------------------------+ | +------------------------------------+h]hX,+------+ |reader| RING BUFFER |page | +------+ +---+ +---+ +---+ | |-->| |-->| | | |<--| |<--| | +---+ +---+ +---+ ^ | ^ | | +-------------+ | +-----------------+ +------+ |reader| RING BUFFER |page |-------------------+ +------+ v | +---+ +---+ +---+ | | |-->| |-->| | | | |<--| |<--| |<-+ | +---+ +---+ +---+ | | ^ | ^ | | | | +-------------+ | | | +-----------------+ | +------------------------------------+ +------+ |reader| RING BUFFER |page |-------------------+ +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | |-->| |-->| | | | | | | |<--| |<-+ | | +---+ +---+ +---+ | | | | ^ | | | | +-------------+ | | | +-----------------------------+ | +------------------------------------+ +------+ |buffer| RING BUFFER |page |-------------------+ +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | | | |-->| | | | New | | | |<--| |<-+ | | Reader +---+ +---+ +---+ | | | page ----^ | | | | | | | +-----------------------------+ | +------------------------------------+}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhKhjhhubh)}(hIt is possible that the page swapped is the commit page and the tail page, if what is in the ring buffer is less than what is held in a buffer page.h]hIt is possible that the page swapped is the commit page and the tail page, if what is in the ring buffer is less than what is held in a buffer page.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj{)}(hX reader page commit page tail page | | | v | | +---+ | | | |<----------+ | | |<------------------------+ | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX reader page commit page tail page | | | v | | +---+ | | | |<----------+ | | |<------------------------+ | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhKhjhhubh)}(hThis case is still valid for this algorithm. When the writer leaves the page, it simply goes into the ring buffer since the reader page still points to the next location in the ring buffer.h]hThis case is still valid for this algorithm. When the writer leaves the page, it simply goes into the ring buffer since the reader page still points to the next location in the ring buffer.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hThe main pointers:h]hThe main pointers:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh block_quote)}(hXqreader page - The page used solely by the reader and is not part of the ring buffer (may be swapped in) head page - the next page in the ring buffer that will be swapped with the reader page. tail page - the page where the next write will take place. commit page - the page that last finished a write. h]j)}(hhh](j)}(hjreader page - The page used solely by the reader and is not part of the ring buffer (may be swapped in) h](j)}(h reader pageh]h reader page}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]j)}(hhh]j)}(hZThe page used solely by the reader and is not part of the ring buffer (may be swapped in) h]h)}(hYThe page used solely by the reader and is not part of the ring buffer (may be swapped in)h]hYThe page used solely by the reader and is not part of the ring buffer (may be swapped in)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hZhead page - the next page in the ring buffer that will be swapped with the reader page. h](j)}(h head pageh]h head page}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjFubj)}(hhh]j)}(hhh]j)}(hLthe next page in the ring buffer that will be swapped with the reader page. h]h)}(hKthe next page in the ring buffer that will be swapped with the reader page.h]hKthe next page in the ring buffer that will be swapped with the reader page.}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj^ubah}(h]h ]h"]h$]h&]uh1jhj[ubah}(h]h ]h"]h$]h&]jjuh1jhhhKhjXubah}(h]h ]h"]h$]h&]uh1jhjFubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(h;tail page - the page where the next write will take place. h](j)}(h tail pageh]h tail page}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]j)}(hhh]j)}(h/the page where the next write will take place. h]h)}(h.the page where the next write will take place.h]h.the page where the next write will take place.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(h3commit page - the page that last finished a write. h](j)}(h commit pageh]h commit page}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]j)}(hhh]j)}(h%the page that last finished a write. h]h)}(h$the page that last finished a write.h]h$the page that last finished a write.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubeh}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhKhjhhubh)}(hThe commit page only is updated by the outermost writer in the writer stack. A writer that preempts another writer will not move the commit page.h]hThe commit page only is updated by the outermost writer in the writer stack. A writer that preempts another writer will not move the commit page.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hWhen data is written into the ring buffer, a position is reserved in the ring buffer and passed back to the writer. When the writer is finished writing data into that position, it commits the write.h]hWhen data is written into the ring buffer, a position is reserved in the ring buffer and passed back to the writer. When the writer is finished writing data into that position, it commits the write.}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hAnother write (or a read) may take place at anytime during this transaction. If another write happens it must finish before continuing with the previous write.h]hAnother write (or a read) may take place at anytime during this transaction. If another write happens it must finish before continuing with the previous write.}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hX` Write reserve:: Buffer page +---------+ |written | +---------+ <--- given back to writer (current commit) |reserved | +---------+ <--- tail pointer | empty | +---------+ Write commit:: Buffer page +---------+ |written | +---------+ |written | +---------+ <--- next position for write (current commit) | empty | +---------+ If a write happens after the first reserve:: Buffer page +---------+ |written | +---------+ <-- current commit |reserved | +---------+ <--- given back to second writer |reserved | +---------+ <--- tail pointer After second writer commits:: Buffer page +---------+ |written | +---------+ <--(last full commit) |reserved | +---------+ |pending | |commit | +---------+ <--- tail pointer When the first writer commits:: Buffer page +---------+ |written | +---------+ |written | +---------+ |written | +---------+ <--(last full commit and tail pointer) h](j)}(hXWrite reserve:: Buffer page +---------+ |written | +---------+ <--- given back to writer (current commit) |reserved | +---------+ <--- tail pointer | empty | +---------+ Write commit:: Buffer page +---------+ |written | +---------+ |written | +---------+ <--- next position for write (current commit) | empty | +---------+ h](h)}(hWrite reserve::h]hWrite reserve:}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjFubj{)}(h Buffer page +---------+ |written | +---------+ <--- given back to writer (current commit) |reserved | +---------+ <--- tail pointer | empty | +---------+h]h Buffer page +---------+ |written | +---------+ <--- given back to writer (current commit) |reserved | +---------+ <--- tail pointer | empty | +---------+}hjXsbah}(h]h ]h"]h$]h&]hhuh1jzhhhKhjFubh)}(hWrite commit::h]h Write commit:}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjFubj{)}(h Buffer page +---------+ |written | +---------+ |written | +---------+ <--- next position for write (current commit) | empty | +---------+h]h Buffer page +---------+ |written | +---------+ |written | +---------+ <--- next position for write (current commit) | empty | +---------+}hjtsbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjFubeh}(h]h ]h"]h$]h&]uh1jhhhKhjBubh)}(h,If a write happens after the first reserve::h]h+If a write happens after the first reserve:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjBubj{)}(hXn Buffer page +---------+ |written | +---------+ <-- current commit |reserved | +---------+ <--- given back to second writer |reserved | +---------+ <--- tail pointer After second writer commits:: Buffer page +---------+ |written | +---------+ <--(last full commit) |reserved | +---------+ |pending | |commit | +---------+ <--- tail pointer When the first writer commits:: Buffer page +---------+ |written | +---------+ |written | +---------+ |written | +---------+ <--(last full commit and tail pointer)h]hXn Buffer page +---------+ |written | +---------+ <-- current commit |reserved | +---------+ <--- given back to second writer |reserved | +---------+ <--- tail pointer After second writer commits:: Buffer page +---------+ |written | +---------+ <--(last full commit) |reserved | +---------+ |pending | |commit | +---------+ <--- tail pointer When the first writer commits:: Buffer page +---------+ |written | +---------+ |written | +---------+ |written | +---------+ <--(last full commit and tail pointer)}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjBubeh}(h]h ]h"]h$]h&]uh1jhhhKhjhhubh)}(hXThe commit pointer points to the last write location that was committed without preempting another write. When a write that preempted another write is committed, it only becomes a pending commit and will not be a full commit until all writes have been committed.h]hXThe commit pointer points to the last write location that was committed without preempting another write. When a write that preempted another write is committed, it only becomes a pending commit and will not be a full commit until all writes have been committed.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM1hjhhubh)}(hThe commit page points to the page that has the last full commit. The tail page points to the page with the last write (before committing).h]hThe commit page points to the page that has the last full commit. The tail page points to the page with the last write (before committing).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM6hjhhubh)}(hThe tail page is always equal to or after the commit page. It may be several pages ahead. If the tail page catches up to the commit page then no more writes may take place (regardless of the mode of the ring buffer: overwrite and produce/consumer).h]hThe tail page is always equal to or after the commit page. It may be several pages ahead. If the tail page catches up to the commit page then no more writes may take place (regardless of the mode of the ring buffer: overwrite and produce/consumer).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM:hjhhubh)}(hThe order of pages is::h]hThe order of pages is:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM?hjhhubj{)}(hhead page commit page tail pageh]hhead page commit page tail page}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhMAhjhhubh)}(hPossible scenario::h]hPossible scenario:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMEhjhhubj{)}(hX+ tail page head page commit page | | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX+ tail page head page commit page | | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhMGhjhhubh)}(hXThere is a special case that the head page is after either the commit page and possibly the tail page. That is when the commit (and tail) page has been swapped with the reader page. This is because the head page is always part of the ring buffer, but the reader page is not. Whenever there has been less than a full page that has been committed inside the ring buffer, and a reader swaps out a page, it will be swapping out the commit page.h]hXThere is a special case that the head page is after either the commit page and possibly the tail page. That is when the commit (and tail) page has been swapped with the reader page. This is because the head page is always part of the ring buffer, but the reader page is not. Whenever there has been less than a full page that has been committed inside the ring buffer, and a reader swaps out a page, it will be swapping out the commit page.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMPhjhhubj{)}(hXg reader page commit page tail page | | | v | | +---+ | | | |<----------+ | | |<------------------------+ | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head pageh]hXg reader page commit page tail page | | | v | | +---+ | | | |<----------+ | | |<------------------------+ | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head page}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhMYhjhhubh)}(hbIn this case, the head page will not move when the tail and commit move back into the ring buffer.h]hbIn this case, the head page will not move when the tail and commit move back into the ring buffer.}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMlhjhhubh)}(hX The reader cannot swap a page into the ring buffer if the commit page is still on that page. If the read meets the last commit (real commit not pending or reserved), then there is nothing more to read. The buffer is considered empty until another full commit finishes.h]hX The reader cannot swap a page into the ring buffer if the commit page is still on that page. If the read meets the last commit (real commit not pending or reserved), then there is nothing more to read. The buffer is considered empty until another full commit finishes.}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMohjhhubh)}(hWhen the tail meets the head page, if the buffer is in overwrite mode, the head page will be pushed ahead one. If the buffer is in producer/consumer mode, the write will fail.h]hWhen the tail meets the head page, if the buffer is in overwrite mode, the head page will be pushed ahead one. If the buffer is in producer/consumer mode, the write will fail.}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMthjhhubh)}(hOverwrite mode::h]hOverwrite mode:}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMxhjhhubj{)}(hX tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head page tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head page tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head pageh]hX tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head page tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head page tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | head page}hj`sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMzhjhhubh)}(hNote, the reader page will still point to the previous head page. But when a swap takes place, it will use the most recent head page.h]hNote, the reader page will still point to the previous head page. But when a swap takes place, it will use the most recent head page.}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]the-generic-ring-bufferah ]h"]the generic ring bufferah$]h&]uh1hhhhhhhhKCubh)}(hhh](h)}(h Making the Ring Buffer Lockless:h]h Making the Ring Buffer Lockless:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hXThe main idea behind the lockless algorithm is to combine the moving of the head_page pointer with the swapping of pages with the reader. State flags are placed inside the pointer to the page. To do this, each page must be aligned in memory by 4 bytes. This will allow the 2 least significant bits of the address to be used as flags, since they will always be zero for the address. To get the address, simply mask out the flags::h]hXThe main idea behind the lockless algorithm is to combine the moving of the head_page pointer with the swapping of pages with the reader. State flags are placed inside the pointer to the page. To do this, each page must be aligned in memory by 4 bytes. This will allow the 2 least significant bits of the address to be used as flags, since they will always be zero for the address. To get the address, simply mask out the flags:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj{)}(hMASK = ~3 address & MASKh]hMASK = ~3 address & MASK}hjsbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(h)Two flags will be kept by these two bits:h]h)Two flags will be kept by these two bits:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hHEADER - the page being pointed to is a head page UPDATE - the page being pointed to is being updated by a writer and was or is about to be a head page. h]j)}(hhh](j)}(h2HEADER - the page being pointed to is a head page h](j)}(hHEADERh]hHEADER}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhMhjubj)}(hhh]j)}(hhh]j)}(h)the page being pointed to is a head page h]h)}(h(the page being pointed to is a head pageh]h(the page being pointed to is a head page}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]jjuh1jhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhhhMhjubj)}(hiUPDATE - the page being pointed to is being updated by a writer and was or is about to be a head page. h](j)}(hUPDATEh]hUPDATE}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhMhj ubj)}(hhh]j)}(hhh]j)}(h^the page being pointed to is being updated by a writer and was or is about to be a head page. h]h)}(h]the page being pointed to is being updated by a writer and was or is about to be a head page.h]h]the page being pointed to is being updated by a writer and was or is about to be a head page.}(hj$ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]jjuh1jhhhMhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhhhMhjubeh}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhMhjhhubj{)}(hXI reader page | v +---+ | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hXI reader page | v +---+ | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hjV sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(hThe above pointer "-H->" would have the HEADER flag set. That is the next page is the next page to be swapped out by the reader. This pointer means the next page is the head page.h]hThe above pointer “-H->” would have the HEADER flag set. That is the next page is the next page to be swapped out by the reader. This pointer means the next page is the head page.}(hjd hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hjWhen the tail page meets the head pointer, it will use cmpxchg to change the pointer to the UPDATE state::h]hiWhen the tail page meets the head pointer, it will use cmpxchg to change the pointer to the UPDATE state:}(hjr hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj{)}(hX tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(h0"-U->" represents a pointer in the UPDATE state.h]h4“-U->” represents a pointer in the UPDATE state.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXAny access to the reader will need to take some sort of lock to serialize the readers. But the writers will never take a lock to write to the ring buffer. This means we only need to worry about a single reader, and writes only preempt in "stack" formation.h]hXAny access to the reader will need to take some sort of lock to serialize the readers. But the writers will never take a lock to write to the ring buffer. This means we only need to worry about a single reader, and writes only preempt in “stack” formation.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXGWhen the reader tries to swap the page with the ring buffer, it will also use cmpxchg. If the flag bit in the pointer to the head page does not have the HEADER flag set, the compare will fail and the reader will need to look for the new head page and try again. Note, the flags UPDATE and HEADER are never set at the same time.h]hXGWhen the reader tries to swap the page with the ring buffer, it will also use cmpxchg. If the flag bit in the pointer to the head page does not have the HEADER flag set, the compare will fail and the reader will need to look for the new head page and try again. Note, the flags UPDATE and HEADER are never set at the same time.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h-The reader swaps the reader page as follows::h]h,The reader swaps the reader page as follows:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj{)}(hXM+------+ |reader| RING BUFFER |page | +------+ +---+ +---+ +---+ | |--->| |--->| | | |<---| |<---| | +---+ +---+ +---+ ^ | ^ | | +---------------+ | +-----H-------------+h]hXM+------+ |reader| RING BUFFER |page | +------+ +---+ +---+ +---+ | |--->| |--->| | | |<---| |<---| | +---+ +---+ +---+ ^ | ^ | | +---------------+ | +-----H-------------+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(hXThe reader sets the reader page next pointer as HEADER to the page after the head page::h]hWThe reader sets the reader page next pointer as HEADER to the page after the head page:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj{)}(hX+------+ |reader| RING BUFFER |page |-------H-----------+ +------+ v | +---+ +---+ +---+ | | |--->| |--->| | | | |<---| |<---| |<-+ | +---+ +---+ +---+ | | ^ | ^ | | | | +---------------+ | | | +-----H-------------+ | +--------------------------------------+h]hX+------+ |reader| RING BUFFER |page |-------H-----------+ +------+ v | +---+ +---+ +---+ | | |--->| |--->| | | | |<---| |<---| |<-+ | +---+ +---+ +---+ | | ^ | ^ | | | | +---------------+ | | | +-----H-------------+ | +--------------------------------------+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(hIt does a cmpxchg with the pointer to the previous head page to make it point to the reader page. Note that the new pointer does not have the HEADER flag set. This action atomically moves the head page forward::h]hIt does a cmpxchg with the pointer to the previous head page to make it point to the reader page. Note that the new pointer does not have the HEADER flag set. This action atomically moves the head page forward:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubj{)}(hX+------+ |reader| RING BUFFER |page |-------H-----------+ +------+ v | ^ +---+ +---+ +---+ | | | |-->| |-->| | | | | |<--| |<--| |<-+ | | +---+ +---+ +---+ | | | | ^ | | | | +-------------+ | | | +-----------------------------+ | +------------------------------------+h]hX+------+ |reader| RING BUFFER |page |-------H-----------+ +------+ v | ^ +---+ +---+ +---+ | | | |-->| |-->| | | | | |<--| |<--| |<-+ | | +---+ +---+ +---+ | | | | ^ | | | | +-------------+ | | | +-----------------------------+ | +------------------------------------+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(heAfter the new head page is set, the previous pointer of the head page is updated to the reader page::h]hdAfter the new head page is set, the previous pointer of the head page is updated to the reader page:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj{)}(hXZ+------+ |reader| RING BUFFER |page |-------H-----------+ +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | |-->| |-->| | | | | | | |<--| |<-+ | | +---+ +---+ +---+ | | | | ^ | | | | +-------------+ | | | +-----------------------------+ | +------------------------------------+ +------+ |buffer| RING BUFFER |page |-------H-----------+ <--- New head page +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | | | |-->| | | | New | | | |<--| |<-+ | | Reader +---+ +---+ +---+ | | | page ----^ | | | | | | | +-----------------------------+ | +------------------------------------+h]hXZ+------+ |reader| RING BUFFER |page |-------H-----------+ +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | |-->| |-->| | | | | | | |<--| |<-+ | | +---+ +---+ +---+ | | | | ^ | | | | +-------------+ | | | +-----------------------------+ | +------------------------------------+ +------+ |buffer| RING BUFFER |page |-------H-----------+ <--- New head page +------+ <---------------+ v | ^ +---+ +---+ +---+ | | | | | |-->| | | | New | | | |<--| |<-+ | | Reader +---+ +---+ +---+ | | | page ----^ | | | | | | | +-----------------------------+ | +------------------------------------+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhM!hjhhubh)}(hXAnother important point: The page that the reader page points back to by its previous pointer (the one that now points to the new head page) never points back to the reader page. That is because the reader page is not part of the ring buffer. Traversing the ring buffer via the next pointers will always stay in the ring buffer. Traversing the ring buffer via the prev pointers may not.h]hXAnother important point: The page that the reader page points back to by its previous pointer (the one that now points to the new head page) never points back to the reader page. That is because the reader page is not part of the ring buffer. Traversing the ring buffer via the next pointers will always stay in the ring buffer. Traversing the ring buffer via the prev pointers may not.}(hj( hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM;hjhhubh)}(hNote, the way to determine a reader page is simply by examining the previous pointer of the page. If the next pointer of the previous page does not point back to the original page, then the original page is a reader page::h]hNote, the way to determine a reader page is simply by examining the previous pointer of the page. If the next pointer of the previous page does not point back to the original page, then the original page is a reader page:}(hj6 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMBhjhhubj{)}(h +--------+ | reader | next +----+ | page |-------->| |<====== (buffer page) +--------+ +----+ | | ^ | v | next prev | +----+ +------------->| | +----+h]h +--------+ | reader | next +----+ | page |-------->| |<====== (buffer page) +--------+ +----+ | | ^ | v | next prev | +----+ +------------->| | +----+}hjD sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMGhjhhubh)}(h$The way the head page moves forward:h]h$The way the head page moves forward:}(hjR hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMQhjhhubh)}(hXWhen the tail page meets the head page and the buffer is in overwrite mode and more writes take place, the head page must be moved forward before the writer may move the tail page. The way this is done is that the writer performs a cmpxchg to convert the pointer to the head page from the HEADER flag to have the UPDATE flag set. Once this is done, the reader will not be able to swap the head page from the buffer, nor will it be able to move the head page, until the writer is finished with the move.h]hXWhen the tail page meets the head page and the buffer is in overwrite mode and more writes take place, the head page must be moved forward before the writer may move the tail page. The way this is done is that the writer performs a cmpxchg to convert the pointer to the head page from the HEADER flag to have the UPDATE flag set. Once this is done, the reader will not be able to swap the head page from the buffer, nor will it be able to move the head page, until the writer is finished with the move.}(hj` hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMShjhhubh)}(hThis eliminates any races that the reader can have on the writer. The reader must spin, and this is why the reader cannot preempt the writer::h]hThis eliminates any races that the reader can have on the writer. The reader must spin, and this is why the reader cannot preempt the writer:}(hjn hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM[hjhhubj{)}(hX tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj| sbah}(h]h ]h"]h$]h&]hhuh1jzhhhM^hjhhubh)}(h8The following page will be made into the new head page::h]h7The following page will be made into the new head page:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMnhjhhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMphjhhubh)}(h[After the new head page has been set, we can set the old head page pointer back to NORMAL::h]hZAfter the new head page has been set, we can set the old head page pointer back to NORMAL:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMxhjhhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhM{hjhhubh)}(hHAfter the head page has been moved, the tail page may now move forward::h]hGAfter the head page has been moved, the tail page may now move forward:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(hFThe above are the trivial updates. Now for the more complex scenarios.h]hFThe above are the trivial updates. Now for the more complex scenarios.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hX}As stated before, if enough writes preempt the first write, the tail page may make it all the way around the buffer and meet the commit page. At this time, we must start dropping writes (usually with some kind of warning to the user). But what happens if the commit was still on the reader page? The commit page is not part of the ring buffer. The tail page must account for this::h]hX|As stated before, if enough writes preempt the first write, the tail page may make it all the way around the buffer and meet the commit page. At this time, we must start dropping writes (usually with some kind of warning to the user). But what happens if the commit was still on the reader page? The commit page is not part of the ring buffer. The tail page must account for this: h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj{)}(hX reader page commit page | | v | +---+ | | |<----------+ | | | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | tail pageh]hX reader page commit page | | v | +---+ | | |<----------+ | | | |------+ +---+ | | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+ ^ | tail page}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhjhhubh)}(hIf the tail page were to simply push the head page forward, the commit when leaving the reader page would not be pointing to the correct page.h]hIf the tail page were to simply push the head page forward, the commit when leaving the reader page would not be pointing to the correct page.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hThe solution to this is to test if the commit page is on the reader page before pushing the head page. If it is, then it can be assumed that the tail page wrapped the buffer, and we must drop new writes.h]hThe solution to this is to test if the commit page is on the reader page before pushing the head page. If it is, then it can be assumed that the tail page wrapped the buffer, and we must drop new writes.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXThis is not a race condition, because the commit page can only be moved by the outermost writer (the writer that was preempted). This means that the commit will not move while a writer is moving the tail page. The reader cannot swap the reader page if it is also being used as the commit page. The reader can simply check that the commit is off the reader page. Once the commit page leaves the reader page it will never go back on it unless a reader does another swap with the buffer page that is also the commit page.h]hXThis is not a race condition, because the commit page can only be moved by the outermost writer (the writer that was preempted). This means that the commit will not move while a writer is moving the tail page. The reader cannot swap the reader page if it is also being used as the commit page. The reader can simply check that the commit is off the reader page. Once the commit page leaves the reader page it will never go back on it unless a reader does another swap with the buffer page that is also the commit page.}(hj$ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]making-the-ring-buffer-locklessah ]h"] making the ring buffer lockless:ah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(h Nested writesh]h Nested writes}(hj= hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj: hhhhhMubh)}(hIn the pushing forward of the tail page we must first push forward the head page if the head page is the next page. If the head page is not the next page, the tail page is simply updated with a cmpxchg.h]hIn the pushing forward of the tail page we must first push forward the head page if the head page is the next page. If the head page is not the next page, the tail page is simply updated with a cmpxchg.}(hjK hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubh)}(haOnly writers move the tail page. This must be done atomically to protect against nested writers::h]h`Only writers move the tail page. This must be done atomically to protect against nested writers:}(hjY hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hZtemp_page = tail_page next_page = temp_page->next cmpxchg(tail_page, temp_page, next_page)h]hZtemp_page = tail_page next_page = temp_page->next cmpxchg(tail_page, temp_page, next_page)}hjg sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hThe above will update the tail page if it is still pointing to the expected page. If this fails, a nested write pushed it forward, the current write does not need to push it::h]hThe above will update the tail page if it is still pointing to the expected page. If this fails, a nested write pushed it forward, the current write does not need to push it:}(hju hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX  temp page | v tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX  temp page | v tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(h7Nested write comes in and moves the tail page forward::h]h6Nested write comes in and moves the tail page forward:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX  tail page (moved by nested writer) temp page | | | v v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX  tail page (moved by nested writer) temp page | | | v v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hThe above would fail the cmpxchg, but since the tail page has already been moved forward, the writer will just try again to reserve storage on the new tail page.h]hThe above would fail the cmpxchg, but since the tail page has already been moved forward, the writer will just try again to reserve storage on the new tail page.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubh)}(h7But the moving of the head page is a bit more complex::h]h6But the moving of the head page is a bit more complex:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(h4The write converts the head page pointer to UPDATE::h]h3The write converts the head page pointer to UPDATE:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hX But if a nested writer preempts here, it will see that the next page is a head page, but it is also nested. It will detect that it is nested and will save that information. The detection is the fact that it sees the UPDATE flag instead of a HEADER or NORMAL pointer.h]hX But if a nested writer preempts here, it will see that the next page is a head page, but it is also nested. It will detect that it is nested and will save that information. The detection is the fact that it sees the UPDATE flag instead of a HEADER or NORMAL pointer.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubh)}(h6The nested writer will set the new head page pointer::h]h5The nested writer will set the new head page pointer:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hBut it will not reset the update back to normal. Only the writer that converted a pointer from HEAD to UPDATE will convert it back to NORMAL::h]hBut it will not reset the update back to normal. Only the writer that converted a pointer from HEAD to UPDATE will convert it back to NORMAL:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj: hhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj+ sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hbAfter the nested writer finishes, the outermost writer will convert the UPDATE pointer to NORMAL::h]haAfter the nested writer finishes, the outermost writer will convert the UPDATE pointer to NORMAL:}(hj9 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |--->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hjG sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hjIt can be even more complex if several nested writes came in and moved the tail page ahead several pages::h]hiIt can be even more complex if several nested writes came in and moved the tail page ahead several pages:}(hjU hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM&hj: hhubj{)}(h(first writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h(first writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-H->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hjc sbah}(h]h ]h"]h$]h&]hhuh1jzhhhM*hj: hhubh)}(h4The write converts the head page pointer to UPDATE::h]h3The write converts the head page pointer to UPDATE:}(hjq hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM4hj: hhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhM6hj: hhubh)}(hINext writer comes in, and sees the update and sets up the new head page::h]hHNext writer comes in, and sees the update and sets up the new head page:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM>hj: hhubj{)}(h(second writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h(second writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMAhj: hhubh)}(hThe nested writer moves the tail page forward. But does not set the old update page to NORMAL because it is not the outermost writer::h]hThe nested writer moves the tail page forward. But does not set the old update page to NORMAL because it is not the outermost writer:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMKhj: hhubj{)}(h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMNhj: hhubh)}(hqAnother writer preempts and sees the page after the tail page is a head page. It changes it from HEAD to UPDATE::h]hpAnother writer preempts and sees the page after the tail page is a head page. It changes it from HEAD to UPDATE:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMVhj: hhubj{)}(h(third writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-U->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h(third writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-U->| |---> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMYhj: hhubh)}(h,The writer will move the head page forward::h]h+The writer will move the head page forward:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMchj: hhubj{)}(h(third writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-U->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h(third writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-U->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMfhj: hhubh)}(h_But now that the third writer did change the HEAD flag to UPDATE it will convert it to normal::h]h^But now that the third writer did change the HEAD flag to UPDATE it will convert it to normal:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMphj: hhubj{)}(h(third writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]h(third writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMthj: hhubh)}(hGThen it will move the tail page, and return back to the second writer::h]hFThen it will move the tail page, and return back to the second writer:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX(second writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX(second writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj' sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hThe second writer will fail to move the tail page because it was already moved, so it will try again and add its data to the new tail page. It will return to the first writer::h]hThe second writer will fail to move the tail page because it was already moved, so it will try again and add its data to the new tail page. It will return to the first writer:}(hj5 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX(first writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX(first writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hjC sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hThe first writer cannot know atomically if the tail page moved while it updates the HEAD page. It will then update the head page to what it thinks is the new head page::h]hThe first writer cannot know atomically if the tail page moved while it updates the HEAD page. It will then update the head page to what it thinks is the new head page:}(hjQ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX(first writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX(first writer) tail page | v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj_ sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hXSince the cmpxchg returns the old value of the pointer the first writer will see it succeeded in updating the pointer from NORMAL to HEAD. But as we can see, this is not good enough. It must also check to see if the tail page is either where it use to be or on the next page::h]hXSince the cmpxchg returns the old value of the pointer the first writer will see it succeeded in updating the pointer from NORMAL to HEAD. But as we can see, this is not good enough. It must also check to see if the tail page is either where it use to be or on the next page:}(hjm hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX(first writer) A B tail page | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX(first writer) A B tail page | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |-H->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj{ sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hIf tail page != A and tail page != B, then it must reset the pointer back to NORMAL. The fact that it only needs to worry about nested writers means that it only needs to check this after setting the HEAD page::h]hIf tail page != A and tail page != B, then it must reset the pointer back to NORMAL. The fact that it only needs to worry about nested writers means that it only needs to check this after setting the HEAD page:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX(first writer) A B tail page | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX(first writer) A B tail page | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |-U->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubh)}(hNow the writer can update the head page. This is also why the head page must remain in UPDATE and only reset by the outermost writer. This prevents the reader from seeing the incorrect head page::h]hNow the writer can update the head page. This is also why the head page must remain in UPDATE and only reset by the outermost writer. This prevents the reader from seeing the incorrect head page:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj: hhubj{)}(hX(first writer) A B tail page | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+h]hX(first writer) A B tail page | | | v v v +---+ +---+ +---+ +---+ <---| |--->| |--->| |--->| |-H-> --->| |<---| |<---| |<---| |<--- +---+ +---+ +---+ +---+}hj sbah}(h]h ]h"]h$]h&]hhuh1jzhhhMhj: hhubeh}(h] nested-writesah ]h"] nested writesah$]h&]uh1hhhhhhhhMubeh}(h]lockless-ring-buffer-designah ]h"]lockless ring buffer designah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerj error_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j j jjjj~j7 j4 j j u nametypes}(j jjj7 j uh}(j hjjj~jj4 jj j: u footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.