-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement lock free ring buffer with throttling [HUMBLE/IRON] (#321)
* Implement lock free ring buffer with throttling - (cherry picked from commit ade5822) * Update CHANGELOG and package version
- Loading branch information
Showing
8 changed files
with
405 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> | ||
<package format="3"> | ||
<name>ouster_ros</name> | ||
<version>0.12.2</version> | ||
<version>0.12.3</version> | ||
<description>Ouster ROS2 driver</description> | ||
<maintainer email="[email protected]">ouster developers</maintainer> | ||
<license file="LICENSE">BSD</license> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Copyright (c) 2018-2024, Ouster, Inc. | ||
* All rights reserved. | ||
* | ||
* @file thread_safe_ring_buffer.h | ||
* @brief File contains thread safe implementation of a ring buffer | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <condition_variable> | ||
#include <mutex> | ||
#include <vector> | ||
#include <atomic> | ||
|
||
/** | ||
* @class LockFreeRingBuffer thread safe ring buffer. | ||
* | ||
* @remarks current implementation has effective (capacity-1) when writing elements | ||
*/ | ||
class LockFreeRingBuffer { | ||
public: | ||
LockFreeRingBuffer(size_t capacity) | ||
: capacity_(capacity), | ||
write_idx_(0), | ||
read_idx_(0) {} | ||
|
||
/** | ||
* Gets the maximum number of items that this ring buffer can hold. | ||
*/ | ||
size_t capacity() const { return capacity_; } | ||
|
||
/** | ||
* Gets the number of item that currently occupy the ring buffer. This | ||
* number would vary between 0 and the capacity(). | ||
* | ||
* @remarks | ||
* if returned value was 0 or the value was equal to the buffer capacity(), | ||
* this does not guarantee that a subsequent call to read() or write() | ||
* wouldn't cause the calling thread to be blocked. | ||
*/ | ||
size_t size() const { | ||
return write_idx_ >= read_idx_ ? | ||
write_idx_ - read_idx_ : | ||
write_idx_ + capacity_ - read_idx_; | ||
} | ||
|
||
size_t available() const { | ||
return capacity_ - 1 - size(); | ||
} | ||
|
||
/** | ||
* Checks if the ring buffer is empty. | ||
*/ | ||
bool empty() const { | ||
return read_idx_ == write_idx_; | ||
} | ||
|
||
/** | ||
* Checks if the ring buffer is full. | ||
*/ | ||
bool full() const { | ||
return read_idx_ == (write_idx_ + 1) % capacity_; | ||
} | ||
|
||
/** | ||
*/ | ||
bool write(size_t count = 1) { | ||
if (count > available()) return false; | ||
write_idx_ = (write_idx_ + count) % capacity_; | ||
return true; | ||
} | ||
|
||
/** | ||
*/ | ||
bool read(size_t count = 1) { | ||
if (count > size()) return false; | ||
read_idx_ = (read_idx_ + count) % capacity_; | ||
return true; | ||
} | ||
|
||
size_t write_head() const { return write_idx_; } | ||
size_t read_head() const { return read_idx_; } | ||
|
||
private: | ||
const size_t capacity_; | ||
std::atomic<size_t> write_idx_; | ||
std::atomic<size_t> read_idx_; | ||
}; |
Oops, something went wrong.