aboutsummaryrefslogtreecommitdiff
path: root/pw_work_queue/work_queue.cc
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-03-12 23:07:32 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-03-12 23:07:32 +0000
commit47562fa92998f8f4289ae9a8048349067754d52e (patch)
treec1643be8ab17fc607cea748a8bb1d621a5964873 /pw_work_queue/work_queue.cc
parenteeec55b65fe2c3c7647bb70ea44b3c839eb1267c (diff)
parent646563934a3e2ee26f50171f94d95173a1662e2c (diff)
downloadpigweed-47562fa92998f8f4289ae9a8048349067754d52e.tar.gz
Snap for 11566117 from 646563934a3e2ee26f50171f94d95173a1662e2c to sdk-releaseplatform-tools-35.0.1
Change-Id: Iec629b181a2c6905754a4c340e334884e13fd3b4
Diffstat (limited to 'pw_work_queue/work_queue.cc')
-rw-r--r--pw_work_queue/work_queue.cc52
1 files changed, 29 insertions, 23 deletions
diff --git a/pw_work_queue/work_queue.cc b/pw_work_queue/work_queue.cc
index 62be12381..3890d5c61 100644
--- a/pw_work_queue/work_queue.cc
+++ b/pw_work_queue/work_queue.cc
@@ -21,8 +21,10 @@
namespace pw::work_queue {
void WorkQueue::RequestStop() {
- std::lock_guard lock(lock_);
- stop_requested_ = true;
+ {
+ std::lock_guard lock(lock_);
+ stop_requested_ = true;
+ } // Release lock before calling .release() on the semaphore.
work_notification_.release();
}
@@ -37,8 +39,11 @@ void WorkQueue::Run() {
std::optional<WorkItem> possible_work_item;
{
std::lock_guard lock(lock_);
- possible_work_item = circular_buffer_.Pop();
- work_remaining = !circular_buffer_.empty();
+ if (!queue_.empty()) {
+ possible_work_item.emplace(std::move(queue_.front()));
+ queue_.pop();
+ }
+ work_remaining = !queue_.empty();
stop_requested = stop_requested_;
}
if (!possible_work_item.has_value()) {
@@ -62,29 +67,30 @@ void WorkQueue::CheckPushWork(WorkItem&& work_item) {
}
Status WorkQueue::InternalPushWork(WorkItem&& work_item) {
- std::lock_guard lock(lock_);
+ {
+ std::lock_guard lock(lock_);
- if (stop_requested_) {
- // Entries are not permitted to be enqueued once stop has been requested.
- return Status::FailedPrecondition();
- }
+ if (stop_requested_) {
+ // Entries are not permitted to be enqueued once stop has been requested.
+ return Status::FailedPrecondition();
+ }
- if (circular_buffer_.full()) {
- return Status::ResourceExhausted();
- }
+ if (queue_.full()) {
+ return Status::ResourceExhausted();
+ }
- circular_buffer_.Push(std::move(work_item));
-
- // Update the watermarks for the queue.
- const uint32_t queue_entries = circular_buffer_.size();
- if (queue_entries > max_queue_used_.value()) {
- max_queue_used_.Set(queue_entries);
- }
- const uint32_t queue_remaining = circular_buffer_.capacity() - queue_entries;
- if (queue_remaining < min_queue_remaining_.value()) {
- min_queue_remaining_.Set(queue_entries);
- }
+ queue_.emplace(std::move(work_item));
+ // Update the watermarks for the queue.
+ const uint32_t queue_entries = queue_.size();
+ if (queue_entries > max_queue_used_.value()) {
+ max_queue_used_.Set(queue_entries);
+ }
+ const uint32_t queue_remaining = queue_.capacity() - queue_entries;
+ if (queue_remaining < min_queue_remaining_.value()) {
+ min_queue_remaining_.Set(queue_entries);
+ }
+ } // Release lock before calling .release() on the semaphore.
work_notification_.release();
return OkStatus();
}