Jetson Inference
DNN Vision Library
RingBuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __MULTITHREAD_RINGBUFFER_H_
24 #define __MULTITHREAD_RINGBUFFER_H_
25 
26 #include "Mutex.h"
27 
28 
34 {
35 public:
39  enum Flags
40  {
41  Read = (1 << 0),
42  ReadOnce = (1 << 1) | Read,
43  ReadLatest = (1 << 2) | Read,
44  ReadLatestOnce = (1 << 3) | ReadLatest,
45  Write = (1 << 4),
46  Threaded = (1 << 5),
47  ZeroCopy = (1 << 6),
48  };
49 
53  inline RingBuffer( uint32_t flags=Threaded );
54 
58  inline ~RingBuffer();
59 
70  inline bool Alloc( uint32_t numBuffers, size_t size, uint32_t flags=0 );
71 
75  inline void Free();
76 
80  inline void* Peek( uint32_t flags );
81 
85  inline void* Next( uint32_t flags );
86 
90  inline uint32_t GetFlags() const;
91 
95  inline void SetFlags( uint32_t flags );
96 
100  inline void SetThreaded( bool threaded );
101 
102 protected:
103 
104  uint32_t mNumBuffers;
105  uint32_t mLatestRead;
106  uint32_t mLatestWrite;
107  uint32_t mFlags;
108 
109  void** mBuffers;
110  size_t mBufferSize;
111  bool mReadOnce;
113 };
114 
115 // inline implementations
116 #include "RingBuffer.inl"
117 
118 #endif
119 
RingBuffer::~RingBuffer
~RingBuffer()
Destructor.
RingBuffer::mReadOnce
bool mReadOnce
Definition: RingBuffer.h:111
RingBuffer::Threaded
@ Threaded
Buffers should be thread-safe (enabled by default).
Definition: RingBuffer.h:46
RingBuffer::mNumBuffers
uint32_t mNumBuffers
Definition: RingBuffer.h:104
RingBuffer::RingBuffer
RingBuffer(uint32_t flags=Threaded)
Construct a new ring buffer.
RingBuffer::ReadOnce
@ ReadOnce
Read the next buffer, but only if it hasn't been read before.
Definition: RingBuffer.h:42
RingBuffer::mBufferSize
size_t mBufferSize
Definition: RingBuffer.h:110
RingBuffer::Next
void * Next(uint32_t flags)
Get the next read/write buffer and advance the position in the queue.
Mutex
A lightweight mutual exclusion lock.
Definition: Mutex.h:35
RingBuffer::mBuffers
void ** mBuffers
Definition: RingBuffer.h:109
RingBuffer::GetFlags
uint32_t GetFlags() const
Get the flags of the ring buffer.
RingBuffer::ReadLatest
@ ReadLatest
Read the latest buffer in the queue, skipping other buffers that may not have been read.
Definition: RingBuffer.h:43
RingBuffer::SetFlags
void SetFlags(uint32_t flags)
Set the ring buffer's flags.
RingBuffer::Write
@ Write
Write the next buffer.
Definition: RingBuffer.h:45
RingBuffer::Alloc
bool Alloc(uint32_t numBuffers, size_t size, uint32_t flags=0)
Allocate memory for a set of buffers, where each buffer has the specified size.
RingBuffer::mMutex
Mutex mMutex
Definition: RingBuffer.h:112
RingBuffer::Flags
Flags
Ring buffer flags.
Definition: RingBuffer.h:39
RingBuffer::SetThreaded
void SetThreaded(bool threaded)
Enable or disable multi-threading.
RingBuffer::Free
void Free()
Free the buffer allocations.
RingBuffer::Read
@ Read
Read the next buffer.
Definition: RingBuffer.h:41
Mutex.h
RingBuffer
Thread-safe circular ring buffer queue.
Definition: RingBuffer.h:33
RingBuffer::ZeroCopy
@ ZeroCopy
Buffers should be allocated in mapped CPU/GPU zeroCopy memory (otherwise GPU only)
Definition: RingBuffer.h:47
RingBuffer::mLatestWrite
uint32_t mLatestWrite
Definition: RingBuffer.h:106
RingBuffer::ReadLatestOnce
@ ReadLatestOnce
Combination of ReadOnce and ReadLatest flags.
Definition: RingBuffer.h:44
RingBuffer::mLatestRead
uint32_t mLatestRead
Definition: RingBuffer.h:105
RingBuffer::mFlags
uint32_t mFlags
Definition: RingBuffer.h:107
RingBuffer::Peek
void * Peek(uint32_t flags)
Get the next read/write buffer without advancing the position in the queue.