-
Notifications
You must be signed in to change notification settings - Fork 107
/
posix_once_test.cc
156 lines (127 loc) · 4.46 KB
/
posix_once_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <pthread.h>
#include <sched.h>
#include "starboard/configuration_constants.h"
#include "starboard/nplb/posix_compliance/posix_thread_helpers.h"
#include "starboard/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {
int s_global_value;
void IncrementGlobalValue() {
++s_global_value;
}
TEST(PosixOnceTest, SunnyDaySingleInit) {
pthread_once_t once_control = PTHREAD_ONCE_INIT;
s_global_value = 0;
EXPECT_EQ(pthread_once(&once_control, &IncrementGlobalValue), 0);
EXPECT_EQ(s_global_value, 1);
}
TEST(PosixOnceTest, SunnyDayMultipleInit) {
pthread_once_t once_control = PTHREAD_ONCE_INIT;
s_global_value = 0;
EXPECT_EQ(pthread_once(&once_control, &IncrementGlobalValue), 0);
EXPECT_EQ(s_global_value, 1);
s_global_value = 0;
EXPECT_EQ(pthread_once(&once_control, &IncrementGlobalValue), 0);
EXPECT_EQ(s_global_value, 0);
s_global_value = 0;
EXPECT_EQ(pthread_once(&once_control, &IncrementGlobalValue), 0);
EXPECT_EQ(s_global_value, 0);
}
struct RunPosixOnceContext {
RunPosixOnceContext() : once_control(PTHREAD_ONCE_INIT) {
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condition, NULL);
}
~RunPosixOnceContext() {
pthread_cond_destroy(&condition);
pthread_mutex_destroy(&mutex);
}
posix::TestSemaphore semaphore;
pthread_mutex_t mutex;
pthread_cond_t condition;
pthread_once_t once_control;
};
void* RunPosixOnceEntryPoint(void* context) {
pthread_setname_np(pthread_self(), posix::kThreadName);
RunPosixOnceContext* run_sbonce_context =
reinterpret_cast<RunPosixOnceContext*>(context);
{
pthread_mutex_lock(&run_sbonce_context->mutex);
run_sbonce_context->semaphore.Put();
pthread_cond_wait(&run_sbonce_context->condition,
&run_sbonce_context->mutex);
pthread_mutex_unlock(&run_sbonce_context->mutex);
}
sched_yield();
static const int kIterationCount = 3;
for (int i = 0; i < kIterationCount; ++i) {
pthread_once(&run_sbonce_context->once_control, &IncrementGlobalValue);
}
return NULL;
}
// Here we spawn many threads each of which will call pthread_once multiple
// times using a shared pthread_once_t object. We then test that the
// initialization routine got called exactly one time.
TEST(PosixOnceTest, SunnyDayMultipleThreadsInit) {
const int kMany = kSbMaxThreads;
std::vector<pthread_t> threads(kMany);
const int kIterationCount = 10;
for (int i = 0; i < kIterationCount; ++i) {
pthread_once_t once_control = PTHREAD_ONCE_INIT;
RunPosixOnceContext context;
s_global_value = 0;
for (int j = 0; j < kMany; ++j) {
pthread_create(&threads[j], NULL, RunPosixOnceEntryPoint, &context);
}
// Wait for all threads to finish initializing and become ready, then
// broadcast the signal to begin. We do this to increase the chances that
// threads will call pthread_once() at the same time as each other.
for (int j = 0; j < kMany; ++j) {
context.semaphore.Take();
}
{
pthread_mutex_lock(&context.mutex);
pthread_cond_broadcast(&context.condition);
pthread_mutex_unlock(&context.mutex);
}
// Signal threads to beginWait for all threads to complete.
for (int i = 0; i < kMany; ++i) {
void* result;
pthread_join(threads[i], &result);
}
EXPECT_EQ(s_global_value, 1);
}
}
int* GetIntSingleton() {
static pthread_once_t s_once_flag = PTHREAD_ONCE_INIT;
static int* s_singleton = NULL;
struct Local {
static void Init() { s_singleton = new int(); }
};
pthread_once(&s_once_flag, Local::Init);
return s_singleton;
}
TEST(PosixOnceTest, InitializeOnceMacroFunction) {
int* int_singelton = GetIntSingleton();
ASSERT_TRUE(int_singelton);
EXPECT_EQ(*int_singelton, 0)
<< "Singleton Macro does not default initialize.";
}
} // namespace.
} // namespace nplb.
} // namespace starboard.