1 // Copyright 2020 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 <chrono>
16 
17 #include "pw_chrono/system_clock.h"
18 #include "pw_thread/sleep.h"
19 #include "pw_thread/test_threads.h"
20 #include "pw_thread_freertos/context.h"
21 #include "pw_thread_freertos/options.h"
22 
23 namespace pw::thread::test {
24 
TestOptionsThread0()25 const Options& TestOptionsThread0() {
26   static constexpr freertos::Options thread_0_options =
27       freertos::Options().set_name("pw::TestThread0");
28   return thread_0_options;
29 }
30 
TestOptionsThread1()31 const Options& TestOptionsThread1() {
32   static constexpr freertos::Options thread_1_options =
33       freertos::Options().set_name("pw::TestThread0");
34   return thread_1_options;
35 }
36 
37 // Although there's no risk of dynamic context re-use, there is a risk
38 // running out of heap. The way the FreeRTOS kernel works is that dynamic thread
39 // allocations are cleaned up during idle. There is no clean way to cleanly
40 // sleep until idle, ergo we simply sleep for a long period in the hope that
41 // the application will on average not be able to starve the heap if they
42 // execute this test over and over again.
WaitUntilDetachedThreadsCleanedUp()43 void WaitUntilDetachedThreadsCleanedUp() {
44   this_thread::sleep_for(
45       chrono::SystemClock::for_at_least(std::chrono::milliseconds(50)));
46 }
47 
48 }  // namespace pw::thread::test
49