blob: cc06902505cfcb6d59acdf42183d2e7611d224a9 [file] [log] [blame]
Andrew Top2a796462018-06-29 09:04:04 -07001// Copyright 2015 The Cobalt Authors. All Rights Reserved.
David Ghandehari9e5b5872016-07-28 09:50:04 -07002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://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,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Kaido Kert25902c62024-06-17 17:10:28 -070015#if SB_API_VERSION < 16
16
Kaido Kert612c0202020-01-22 10:28:42 -080017#include "starboard/configuration_constants.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070018#include "starboard/nplb/thread_helpers.h"
19#include "starboard/thread.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22namespace starboard {
23namespace nplb {
24namespace {
25
Chad Duffinac9ac062019-07-23 10:06:45 -070026const SbThreadPriority kAllThreadPriorities[] = {
27 kSbThreadPriorityLowest, kSbThreadPriorityLow,
28 kSbThreadPriorityNormal, kSbThreadPriorityHigh,
29 kSbThreadPriorityHighest, kSbThreadPriorityRealTime,
30 kSbThreadNoPriority,
31};
32
David Ghandehari9e5b5872016-07-28 09:50:04 -070033TEST(SbThreadCreateTest, SunnyDay) {
34 const int kTrials = 64;
35 for (int i = 0; i < kTrials; ++i) {
36 SbThread thread = SbThreadCreate(
37 0, kSbThreadNoPriority, kSbThreadNoAffinity, true, nplb::kThreadName,
38 nplb::AddOneEntryPoint, nplb::kSomeContext);
39 EXPECT_TRUE(SbThreadIsValid(thread));
40 void* result = NULL;
41 EXPECT_TRUE(SbThreadJoin(thread, &result));
42 EXPECT_EQ(nplb::kSomeContextPlusOne, result);
43 }
44}
45
46TEST(SbThreadCreateTest, SunnyDayWithPriorities) {
47 const int kTrials = 64;
48 for (int i = 0; i < kTrials; ++i) {
49 SbThreadPriority priority;
50 switch (i % 7) {
51 case 0:
52 priority = kSbThreadPriorityLowest;
53 break;
54 case 1:
55 priority = kSbThreadPriorityLow;
56 break;
57 case 2:
58 priority = kSbThreadPriorityNormal;
59 break;
60 case 3:
61 priority = kSbThreadPriorityHigh;
62 break;
63 case 4:
64 priority = kSbThreadPriorityHighest;
65 break;
66 case 5:
67 priority = kSbThreadPriorityRealTime;
68 break;
69 case 6:
70 priority = kSbThreadNoPriority;
71 break;
72 }
73 SbThread thread = SbThreadCreate(0, priority, kSbThreadNoAffinity, true,
74 nplb::kThreadName, nplb::AddOneEntryPoint,
75 nplb::kSomeContext);
76 EXPECT_TRUE(SbThreadIsValid(thread));
77 void* result = NULL;
78 EXPECT_TRUE(SbThreadJoin(thread, &result));
79 EXPECT_EQ(nplb::kSomeContextPlusOne, result);
80 }
81}
82
Chad Duffinac9ac062019-07-23 10:06:45 -070083void* CreateNestedThreadFunc(void* context) {
84 for (auto thread_priority : kAllThreadPriorities) {
85 SbThread thread = SbThreadCreate(
86 0, thread_priority, kSbThreadNoAffinity, true, nplb::kThreadName,
87 nplb::AddOneEntryPoint, nplb::kSomeContext);
88 EXPECT_TRUE(SbThreadIsValid(thread));
89 void* result = NULL;
90 EXPECT_TRUE(SbThreadJoin(thread, &result));
91 EXPECT_EQ(nplb::kSomeContextPlusOne, result);
92 }
93 return NULL;
94}
95
96TEST(SbThreadCreateTest, SunnyDayWithNestedPriorities) {
97 for (auto thread_priority : kAllThreadPriorities) {
98 SbThread thread = SbThreadCreate(
99 0, thread_priority, kSbThreadNoAffinity, true, nplb::kThreadName,
100 CreateNestedThreadFunc, nplb::kSomeContext);
101 EXPECT_TRUE(SbThreadIsValid(thread));
102 void* result = NULL;
103 EXPECT_TRUE(SbThreadJoin(thread, &result));
104 }
105}
106
David Ghandehari9e5b5872016-07-28 09:50:04 -0700107TEST(SbThreadCreateTest, SunnyDayNoName) {
108 SbThread thread =
109 SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true, NULL,
110 nplb::AddOneEntryPoint, nplb::kSomeContext);
111 EXPECT_TRUE(SbThreadIsValid(thread));
112 void* result = NULL;
113 EXPECT_TRUE(SbThreadJoin(thread, &result));
114 EXPECT_EQ(nplb::kSomeContextPlusOne, result);
115}
116
117TEST(SbThreadCreateTest, SunnyDayNoContext) {
118 SbThread thread =
119 SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true,
120 nplb::kThreadName, nplb::AddOneEntryPoint, NULL);
121 EXPECT_TRUE(SbThreadIsValid(thread));
122 void* result = NULL;
123 EXPECT_TRUE(SbThreadJoin(thread, &result));
124 EXPECT_EQ(nplb::ToVoid(1), result);
125}
126
127TEST(SbThreadCreateTest, SunnyDayWithAffinity) {
128 SbThread thread =
129 SbThreadCreate(0, kSbThreadNoPriority, 0, true, nplb::kThreadName,
130 nplb::AddOneEntryPoint, nplb::kSomeContext);
131 EXPECT_TRUE(SbThreadIsValid(thread));
132 void* result = NULL;
133 EXPECT_TRUE(SbThreadJoin(thread, &result));
134 EXPECT_EQ(nplb::kSomeContextPlusOne, result);
135}
136
137TEST(SbThreadCreateTest, SunnyDayDetached) {
138 SbThread thread = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity,
139 false, nplb::kThreadName,
140 nplb::AddOneEntryPoint, nplb::kSomeContext);
141 EXPECT_TRUE(SbThreadIsValid(thread));
142 void* result = NULL;
143 EXPECT_FALSE(SbThreadJoin(thread, &result));
144}
145
146TEST(SbThreadCreateTest, Summertime) {
Kaido Kert612c0202020-01-22 10:28:42 -0800147 const int kMany = kSbMaxThreads;
Andrew Topa7b1cfa2019-12-18 19:15:07 -0800148 std::vector<SbThread> threads(kMany);
David Ghandehari9e5b5872016-07-28 09:50:04 -0700149 for (int i = 0; i < kMany; ++i) {
150 threads[i] = SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity,
151 true, nplb::kThreadName, nplb::AddOneEntryPoint,
152 nplb::ToVoid(i));
153 EXPECT_TRUE(SbThreadIsValid(threads[i]));
154 }
155
156 for (int i = 0; i < kMany; ++i) {
157 void* result = NULL;
158 void* const kExpected = nplb::ToVoid(i + 1);
159 EXPECT_TRUE(SbThreadJoin(threads[i], &result));
160 EXPECT_EQ(kExpected, result);
161 }
162}
163
164TEST(SbThreadCreateTest, RainyDayNoEntryPoint) {
165 SbThread thread =
166 SbThreadCreate(0, kSbThreadNoPriority, kSbThreadNoAffinity, true,
167 nplb::kThreadName, NULL, nplb::kSomeContext);
168 EXPECT_FALSE(SbThreadIsValid(thread));
169}
170
171} // namespace
172} // namespace nplb
173} // namespace starboard
Kaido Kert25902c62024-06-17 17:10:28 -0700174
175#endif // SB_API_VERSION < 16