Pigweed
 
Loading...
Searching...
No Matches
work_queue.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#pragma once
16
17#include <array>
18#include <cstdint>
19
20#include "pw_containers/inline_queue.h"
21#include "pw_function/function.h"
22#include "pw_metric/metric.h"
23#include "pw_span/span.h"
24#include "pw_status/status.h"
25#include "pw_sync/interrupt_spin_lock.h"
26#include "pw_sync/lock_annotations.h"
27#include "pw_sync/thread_notification.h"
28#include "pw_thread/thread_core.h"
29
30namespace pw::work_queue {
31
32using WorkItem = Function<void()>;
33
50class WorkQueue : public thread::ThreadCore {
51 public:
58 WorkQueue(InlineQueue<WorkItem>& queue, size_t queue_capacity)
59 : stop_requested_(false), queue_(queue) {
60 min_queue_remaining_.Set(static_cast<uint32_t>(queue_capacity));
61 }
62
80 Status PushWork(WorkItem&& work_item) PW_LOCKS_EXCLUDED(lock_) {
81 return InternalPushWork(std::move(work_item));
82 }
83
97 void CheckPushWork(WorkItem&& work_item) PW_LOCKS_EXCLUDED(lock_);
98
105 void RequestStop() PW_LOCKS_EXCLUDED(lock_);
106
107 private:
108 void Run() override PW_LOCKS_EXCLUDED(lock_);
109 Status InternalPushWork(WorkItem&& work_item) PW_LOCKS_EXCLUDED(lock_);
110
111 sync::InterruptSpinLock lock_;
112 bool stop_requested_ PW_GUARDED_BY(lock_);
113 InlineQueue<WorkItem>& queue_ PW_GUARDED_BY(lock_);
114 sync::ThreadNotification work_notification_;
115
116 // TODO(ewout): The group and/or its name token should be passed as a ctor
117 // arg instead. Depending on the approach here the group should be exposed
118 // While doing this evaluate whether perhaps we should instead construct
119 // TypedMetric<uint32_t>s directly, avoiding the macro usage given the
120 // min_queue_remaining_ initial value requires dependency injection.
121 // And lastly when the restructure is finalized add unit tests to ensure these
122 // metrics work as intended.
123 PW_METRIC_GROUP(metrics_, "pw::work_queue::WorkQueue");
124 PW_METRIC(metrics_, max_queue_used_, "max_queue_used", 0u);
125 PW_METRIC(metrics_, min_queue_remaining_, "min_queue_remaining", 0u);
126};
127
128template <size_t kWorkQueueEntries>
130 public:
131 constexpr WorkQueueWithBuffer() : WorkQueue(queue_, kWorkQueueEntries) {}
132
133 private:
135};
136
137} // namespace pw::work_queue
Definition: inline_queue.h:43
Definition: status.h:85
Definition: work_queue.h:50
Status PushWork(WorkItem &&work_item)
Definition: work_queue.h:80
WorkQueue(InlineQueue< WorkItem > &queue, size_t queue_capacity)
Definition: work_queue.h:58
void CheckPushWork(WorkItem &&work_item)
Definition: work_queue.h:129
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27