-
Notifications
You must be signed in to change notification settings - Fork 20k
feat: add DoubleBuffer implementation and initial tests #5885
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
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5885 +/- ##
============================================
+ Coverage 66.81% 66.83% +0.02%
- Complexity 4501 4515 +14
============================================
Files 611 612 +1
Lines 16958 16987 +29
Branches 3275 3281 +6
============================================
+ Hits 11330 11354 +24
- Misses 5181 5182 +1
- Partials 447 451 +4 ☔ View full report in Codecov by Sentry. |
* | ||
* @param <Item> The type of elements stored in the double buffer. | ||
*/ | ||
public class DoubleBuffer<Item> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about using T
instead of Item
. Because this is common practice
|
||
if (isPrimaryActive.get()) { | ||
if (primaryIndex.get() < capacity) { | ||
primaryBuffer[primaryIndex.getAndIncrement()] = item; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you want to make this code thread-safe, will be better to use ReentrantLock
for this.
private final ReentrantLock lock = new ReentrantLock();
public void switchBuffer() {
lock.lock();
try {
isPrimaryActive.set(!isPrimaryActive.get());
} finally {
lock.unlock();
}
}
public boolean put(Item item) {
if (item == null) {
throw new IllegalArgumentException("Null items are not allowed");
}
lock.lock();
try {
if (isPrimaryActive.get()) {
if (primaryIndex.get() < capacity) {
primaryBuffer[primaryIndex.getAndIncrement()] = item;
return true;
}
} else {
if (secondaryIndex.get() < capacity) {
secondaryBuffer[secondaryIndex.getAndIncrement()] = item;
return true;
}
}
return false; // Buffer is full
} finally {
lock.unlock();
}
}
public Item get() {
lock.lock();
try {
if (isPrimaryActive.get()) {
if (primaryIndex.get() == 0) {
return null;
}
return primaryBuffer[primaryIndex.decrementAndGet()];
} else {
if (secondaryIndex.get() == 0) {
return null;
}
return secondaryBuffer[secondaryIndex.decrementAndGet()];
}
} finally {
lock.unlock();
}
}
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution! |
Please reopen this pull request once you have made the required changes. If you need help, feel free to ask in our Discord server or ping one of the maintainers here. Thank you for your contribution! |
I have read and followed the guidelines outlined in CONTRIBUTING.md.
Yes, I have read and followed the guidelines provided in the CONTRIBUTING.md file.
This pull request contains only my own work; it has not been plagiarized.
Yes, this pull request contains only my original work and has not been plagiarized.
All filenames follow the PascalCase convention.
Yes, all filenames follow the PascalCase convention.
All function and variable names adhere to Java naming conventions.
Yes, all function and variable names adhere to Java naming conventions.
[] All new algorithms include a URL in their comments linking to a Wikipedia page or a similar source for reference.
All code is formatted using
clang-format -i --style=file /datastructures/buffers/DoubleBufferTest.java