xref: /aosp_15_r20/external/pigweed/pw_thread/test_thread_context_facade_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 "pw_sync/binary_semaphore.h"
16 #include "pw_thread/test_thread_context.h"
17 #include "pw_thread/thread.h"
18 #include "pw_unit_test/framework.h"
19 
20 using pw::thread::test::TestThreadContext;
21 
22 namespace pw::thread {
23 namespace {
24 
TEST(Thread,TestThreadContext)25 TEST(Thread, TestThreadContext) {
26   TestThreadContext context_0;
27   TestThreadContext context_1;
28   Thread thread_0;
29   Thread thread_1;
30   sync::BinarySemaphore thread_ran_sem_0;
31   sync::BinarySemaphore thread_ran_sem_1;
32 
33   thread_0 = Thread(context_0.options(),
34                     [&thread_ran_sem_0] { thread_ran_sem_0.release(); });
35   thread_1 = Thread(context_1.options(),
36                     [&thread_ran_sem_1] { thread_ran_sem_1.release(); });
37 
38   EXPECT_NE(thread_0.get_id(), Thread::id());
39   EXPECT_TRUE(thread_0.joinable());
40 
41   EXPECT_NE(thread_1.get_id(), Thread::id());
42   EXPECT_TRUE(thread_1.joinable());
43 
44   thread_0.detach();
45   thread_1.detach();
46 
47   EXPECT_EQ(thread_0.get_id(), Thread::id());
48   EXPECT_FALSE(thread_0.joinable());
49 
50   EXPECT_EQ(thread_1.get_id(), Thread::id());
51   EXPECT_FALSE(thread_1.joinable());
52 
53   thread_ran_sem_0.acquire();
54   thread_ran_sem_1.acquire();
55 }
56 
57 }  // namespace
58 }  // namespace pw::thread
59