Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atomic increment to FifoControllers #575

Merged
merged 3 commits into from Jul 17, 2019

Conversation

tguilbert-google
Copy link
Member

This CL adds an increment counter methods that allow for atomically
incrementing the read/write indexes, rather than geting the current
index, adding to it and setting it.

Also, getFullFramesAvailable() and getEmptyFramesAvailable() now clamp
negative values to 0.

This CL adds an increment counter methods that allow for atomically
incrementing the read/write indexes, rather than geting the current
index, adding to it and setting it.

Also, getFullFramesAvailable() and getEmptyFramesAvailable() now clamp
negative values to 0.
@@ -32,19 +32,24 @@ class FifoController : public FifoControllerBase
FifoController(uint32_t bufferSize, uint32_t threshold);
virtual ~FifoController() = default;

// TODO review use atomics or memory barriers
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I could tell, the current use of memory barriers is adequate to keep mWriteCounter and mReadCounter consistent. However, I don't know if there could be problems in FifoControllerBase::getFullFramesAvailable(), when we access both variables. I can't tell if there is a scenario where two threads would get a different number of frames, that could be prevented by a use of std::memory_order_seq_cst instead. From here (https://en.cppreference.com/w/cpp/atomic/memory_order#Sequentially-consistent_ordering) :
"Sequential ordering may be necessary for multiple producer-multiple consumer situations where all consumers must observe the actions of all producers occurring in the same order."

How many producers/consumers can FifoBuffer have?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This FIFO has only one reader and one writer.

src/fifo/FifoBuffer.cpp Outdated Show resolved Hide resolved
src/fifo/FifoControllerBase.cpp Outdated Show resolved Hide resolved
@@ -38,8 +38,8 @@ uint32_t FifoControllerBase::getReadIndex() const {
return static_cast<uint32_t>(getReadCounter() % mTotalFrames);
}

void FifoControllerBase::advanceReadIndex(int numFrames) {
setReadCounter(getReadCounter() + numFrames);
void FifoControllerBase::advanceReadIndex(uint32_t numFrames) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the int --> uint32_t conversion is invalid here as well. If the int is there for a reason, does the write index also need an int?

Copy link
Collaborator

@philburk philburk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except for assert.h

@tguilbert-google tguilbert-google merged commit d58c156 into google:master Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants