1 // Copyright 2024 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 #include "examples/named_u32.h"
16 #include "pw_allocator/synchronized_allocator.h"
17 #include "pw_allocator/testing.h"
18 #include "pw_assert/check.h"
19 #include "pw_sync/interrupt_spin_lock.h"
20 #include "pw_thread/test_thread_context.h"
21 #include "pw_thread/thread.h"
22 #include "pw_thread/thread_core.h"
23 #include "pw_unit_test/framework.h"
24
25 // TODO: https://pwbug.dev/365161669 - Express joinability as a build-system
26 // constraint.
27 #if PW_THREAD_JOINING_ENABLED
28
29 namespace examples {
30
31 /// Threaded task that performs several allocations.
32 class MyTask final {
33 public:
34 using Layout = pw::allocator::Layout;
35
MyTask(pw::Allocator & allocator,const pw::thread::Options & options)36 MyTask(pw::Allocator& allocator, const pw::thread::Options& options)
37 : thread_core_(allocator) {
38 thread_ = pw::Thread(options, thread_core_);
39 }
40
join()41 void join() { thread_.join(); }
42
43 private:
44 class MyThreadCore : public pw::thread::ThreadCore {
45 public:
MyThreadCore(pw::Allocator & allocator)46 MyThreadCore(pw::Allocator& allocator) : allocator_(allocator) {}
47
48 private:
Run()49 void Run() override {
50 std::array<NamedU32*, 10> values = {};
51 uint32_t counter = 0;
52 for (auto& value : values) {
53 void* ptr = allocator_.Allocate(Layout::Of<NamedU32>());
54 PW_CHECK_NOTNULL(ptr);
55 value = new (ptr) NamedU32("test", ++counter);
56 }
57 counter = 0;
58 for (auto& value : values) {
59 PW_CHECK_INT_EQ(value->value(), ++counter);
60 allocator_.Deallocate(value);
61 }
62 }
63
64 pw::Allocator& allocator_;
65 } thread_core_;
66
67 pw::Thread thread_;
68 };
69
70 // DOCSTAG: [pw_allocator-examples-spin_lock]
RunTasks(pw::Allocator & allocator,const pw::thread::Options & options)71 void RunTasks(pw::Allocator& allocator, const pw::thread::Options& options) {
72 pw::allocator::SynchronizedAllocator<pw::sync::InterruptSpinLock> synced(
73 allocator);
74 MyTask task1(synced, options);
75 MyTask task2(synced, options);
76 task1.join();
77 task2.join();
78 }
79 // DOCSTAG: [pw_allocator-examples-spin_lock]
80
81 } // namespace examples
82
83 namespace {
84
85 using AllocatorForTest = ::pw::allocator::test::AllocatorForTest<2048>;
86
TEST(SpinLockExample,RunTasks)87 TEST(SpinLockExample, RunTasks) {
88 // TODO: b/328831791 - This example did a nice job of uncovering some
89 // pathological fragmentation by `Block::AllocFirst`. Reduce the size of this
90 // allocator once that is addressed.
91 AllocatorForTest allocator;
92 pw::thread::test::TestThreadContext context;
93 examples::RunTasks(allocator, context.options());
94 }
95
96 } // namespace
97
98 #endif // PW_THREAD_JOINING_ENABLED
99