aboutsummaryrefslogtreecommitdiff
path: root/pw_work_queue/work_queue.cc
blob: 3890d5c61747a51f0348e4a35e864847512be721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_work_queue/work_queue.h"

#include <mutex>

#include "pw_assert/check.h"

namespace pw::work_queue {

void WorkQueue::RequestStop() {
  {
    std::lock_guard lock(lock_);
    stop_requested_ = true;
  }  // Release lock before calling .release() on the semaphore.
  work_notification_.release();
}

void WorkQueue::Run() {
  while (true) {
    work_notification_.acquire();

    // Drain the work queue.
    bool stop_requested;
    bool work_remaining;
    do {
      std::optional<WorkItem> possible_work_item;
      {
        std::lock_guard lock(lock_);
        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()) {
        continue;  // No work item to process.
      }
      WorkItem& work_item = possible_work_item.value();
      PW_CHECK(work_item != nullptr);
      work_item();
    } while (work_remaining);

    // Queue was drained, return if we've been requested to stop.
    if (stop_requested) {
      return;
    }
  }
}

void WorkQueue::CheckPushWork(WorkItem&& work_item) {
  PW_CHECK_OK(InternalPushWork(std::move(work_item)),
              "Failed to push work item into the work queue");
}

Status WorkQueue::InternalPushWork(WorkItem&& work_item) {
  {
    std::lock_guard lock(lock_);

    if (stop_requested_) {
      // Entries are not permitted to be enqueued once stop has been requested.
      return Status::FailedPrecondition();
    }

    if (queue_.full()) {
      return Status::ResourceExhausted();
    }

    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();
}

}  // namespace pw::work_queue