blob: 510fe037ff6177e2105da82004523e2e6c730286 [file] [log] [blame]
Ewout van Bekkum0f3901e2020-12-22 12:00:18 -08001// 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 <atomic>
16
17#include "gtest/gtest.h"
18#include "pw_thread/id.h"
19#include "pw_thread/test_threads.h"
20#include "pw_thread/thread.h"
21
22using pw::thread::test::TestOptionsThread0;
23using pw::thread::test::TestOptionsThread1;
24
25namespace pw::thread {
26namespace {
27
28TEST(Thread, DefaultIds) {
29 Thread not_executing_thread;
30 EXPECT_EQ(not_executing_thread.get_id(), Id());
31}
32
33static void SetBoolTrue(void* arg) {
34 *reinterpret_cast<std::atomic<bool>*>(arg) = true;
35}
36
37// TODO(ewout): this currently doesn't work on backends with dynamic context
38// allocation disabled. Perhaps we could require the backend to provide
39// thread options for the facade unit tests to use?
40
41#if PW_THREAD_JOINING_ENABLED
42TEST(Thread, Join) {
43 Thread thread;
44 EXPECT_FALSE(thread.joinable());
45 std::atomic<bool> thread_ran = false;
46 thread = Thread(TestOptionsThread0(), SetBoolTrue, &thread_ran);
47 EXPECT_TRUE(thread.joinable());
48 thread.join();
49 EXPECT_EQ(thread.get_id(), Id());
50 EXPECT_TRUE(thread_ran);
51}
52#endif // PW_THREAD_JOINING_ENABLED
53
54TEST(Thread, Detach) {
55 Thread thread;
56 std::atomic<bool> thread_ran = false;
57 thread = Thread(TestOptionsThread0(), SetBoolTrue, &thread_ran);
58 EXPECT_NE(thread.get_id(), Id());
59#if PW_THREAD_JOINING_ENABLED
60 EXPECT_TRUE(thread.joinable());
61#endif // PW_THREAD_JOINING_ENABLED
62 thread.detach();
63 EXPECT_EQ(thread.get_id(), Id());
64#if PW_THREAD_JOINING_ENABLED
65 EXPECT_FALSE(thread.joinable());
66#endif // PW_THREAD_JOINING_ENABLED
67 // We could use a synchronization primitive here to wait until the thread
68 // finishes running to check that thread_ran is true, but that's covered by
69 // pw_sync instead. Instead we use an idiotic busy loop.
70 // - Assume our clock is < 6Ghz
71 // - Assume we can check the clock in a single cycle
72 // - Wait for up to 1/10th of a second @ 6Ghz, this may be a long period on a
73 // slower (i.e. real) machine.
74 constexpr uint64_t kMaxIterations = 6'000'000'000 / 10;
75 for (uint64_t i = 0; i < kMaxIterations; ++i) {
76 if (thread_ran)
77 break;
78 }
79 EXPECT_TRUE(thread_ran);
80}
81
82TEST(Thread, SwapWithoutExecution) {
83 Thread thread_0;
84 Thread thread_1;
85
86 // Make sure we can swap threads which are not associated with any execution.
87 thread_0.swap(thread_1);
88}
89
90TEST(Thread, SwapWithOneExecuting) {
91 Thread thread_0;
92 EXPECT_EQ(thread_0.get_id(), Id());
93
94 static std::atomic<bool> thread_ran = false;
95 Thread thread_1(TestOptionsThread1(), SetBoolTrue, &thread_ran);
96 EXPECT_NE(thread_1.get_id(), Id());
97
98 thread_0.swap(thread_1);
99 EXPECT_NE(thread_0.get_id(), Id());
100 EXPECT_EQ(thread_1.get_id(), Id());
101
102 thread_0.detach();
103 EXPECT_EQ(thread_0.get_id(), Id());
104}
105
106TEST(Thread, SwapWithTwoExecuting) {
107 static std::atomic<bool> thread_a_ran = false;
108 Thread thread_0(TestOptionsThread0(), SetBoolTrue, &thread_a_ran);
109 static std::atomic<bool> thread_b_ran = false;
110 Thread thread_1(TestOptionsThread1(), SetBoolTrue, &thread_b_ran);
111 const Id thread_a_id = thread_0.get_id();
112 EXPECT_NE(thread_a_id, Id());
113 const Id thread_b_id = thread_1.get_id();
114 EXPECT_NE(thread_b_id, Id());
115 EXPECT_NE(thread_a_id, thread_b_id);
116
117 thread_0.swap(thread_1);
118 EXPECT_EQ(thread_1.get_id(), thread_a_id);
119 EXPECT_EQ(thread_0.get_id(), thread_b_id);
120
121 thread_0.detach();
122 EXPECT_EQ(thread_0.get_id(), Id());
123 thread_1.detach();
124 EXPECT_EQ(thread_1.get_id(), Id());
125}
126
127TEST(Thread, MoveOperator) {
128 Thread thread_0;
129 EXPECT_EQ(thread_0.get_id(), Id());
130
131 std::atomic<bool> thread_ran = false;
132 Thread thread_1(TestOptionsThread1(), SetBoolTrue, &thread_ran);
133 EXPECT_NE(thread_1.get_id(), Id());
134
135 thread_0 = std::move(thread_1);
136 EXPECT_NE(thread_0.get_id(), Id());
137 EXPECT_EQ(thread_1.get_id(), Id());
138
139 thread_0.detach();
140 EXPECT_EQ(thread_0.get_id(), Id());
141}
142
143} // namespace
144} // namespace pw::thread