• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace examples {
26 
27 /// Threaded task that performs several allocations.
28 class MyTask final {
29  public:
30   using Layout = pw::allocator::Layout;
31 
MyTask(pw::Allocator & allocator,const pw::thread::Options & options)32   MyTask(pw::Allocator& allocator, const pw::thread::Options& options)
33       : thread_core_(allocator) {
34     thread_ = pw::thread::Thread(options, thread_core_);
35   }
36 
join()37   void join() { thread_.join(); }
38 
39  private:
40   class MyThreadCore : public pw::thread::ThreadCore {
41    public:
MyThreadCore(pw::Allocator & allocator)42     MyThreadCore(pw::Allocator& allocator) : allocator_(allocator) {}
43 
44    private:
Run()45     void Run() override {
46       std::array<NamedU32*, 10> values = {};
47       uint32_t counter = 0;
48       for (auto& value : values) {
49         void* ptr = allocator_.Allocate(Layout::Of<NamedU32>());
50         PW_CHECK_NOTNULL(ptr);
51         value = new (ptr) NamedU32("test", ++counter);
52       }
53       counter = 0;
54       for (auto& value : values) {
55         PW_CHECK_INT_EQ(value->value(), ++counter);
56         allocator_.Deallocate(value);
57       }
58     }
59 
60     pw::Allocator& allocator_;
61   } thread_core_;
62 
63   pw::thread::Thread thread_;
64 };
65 
66 // DOCSTAG: [pw_allocator-examples-spin_lock]
RunTasks(pw::Allocator & allocator,const pw::thread::Options & options)67 void RunTasks(pw::Allocator& allocator, const pw::thread::Options& options) {
68   pw::allocator::SynchronizedAllocator<pw::sync::InterruptSpinLock> synced(
69       allocator);
70   MyTask task1(synced, options);
71   MyTask task2(synced, options);
72   task1.join();
73   task2.join();
74 }
75 // DOCSTAG: [pw_allocator-examples-spin_lock]
76 
77 }  // namespace examples
78 
79 namespace {
80 
81 using AllocatorForTest = ::pw::allocator::test::AllocatorForTest<2048>;
82 
TEST(SpinLockExample,RunTasks)83 TEST(SpinLockExample, RunTasks) {
84   // TODO: b/328831791 - This example did a nice job of uncovering some
85   // pathological fragmentation by `Block::AllocFirst`. Reduce the size of this
86   // allocator once that is addressed.
87   AllocatorForTest allocator;
88   pw::thread::test::TestThreadContext context;
89   examples::RunTasks(allocator, context.options());
90 }
91 
92 }  // namespace
93