2012-05-22 15:57:34 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
2012-05-23 15:49:48 +00:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/platform_thread.h"
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2021-04-20 17:41:54 +02:00
|
|
|
#include "rtc_base/event.h"
|
|
|
|
|
#include "system_wrappers/include/sleep.h"
|
|
|
|
|
#include "test/gmock.h"
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2016-04-22 09:08:44 -07:00
|
|
|
namespace rtc {
|
2021-05-06 13:12:47 +00:00
|
|
|
namespace {
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2021-05-06 13:12:47 +00:00
|
|
|
void NullRunFunction(void* obj) {}
|
2017-02-22 11:22:05 -08:00
|
|
|
|
2021-05-06 13:12:47 +00:00
|
|
|
// Function that sets a boolean.
|
|
|
|
|
void SetFlagRunFunction(void* obj) {
|
|
|
|
|
bool* obj_as_bool = static_cast<bool*>(obj);
|
|
|
|
|
*obj_as_bool = true;
|
2021-04-20 17:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-06 13:12:47 +00:00
|
|
|
void StdFunctionRunFunction(void* obj) {
|
|
|
|
|
std::function<void()>* fun = static_cast<std::function<void()>*>(obj);
|
|
|
|
|
(*fun)();
|
2021-05-05 10:42:04 +02:00
|
|
|
}
|
2016-04-22 09:08:44 -07:00
|
|
|
|
2021-05-06 13:12:47 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
TEST(PlatformThreadTest, StartStop) {
|
|
|
|
|
PlatformThread thread(&NullRunFunction, nullptr, "PlatformThreadTest");
|
|
|
|
|
EXPECT_TRUE(thread.name() == "PlatformThreadTest");
|
|
|
|
|
EXPECT_TRUE(thread.GetThreadRef() == 0);
|
|
|
|
|
thread.Start();
|
|
|
|
|
EXPECT_TRUE(thread.GetThreadRef() != 0);
|
|
|
|
|
thread.Stop();
|
|
|
|
|
EXPECT_TRUE(thread.GetThreadRef() == 0);
|
2016-04-22 09:08:44 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-06 13:12:47 +00:00
|
|
|
TEST(PlatformThreadTest, StartStop2) {
|
|
|
|
|
PlatformThread thread1(&NullRunFunction, nullptr, "PlatformThreadTest1");
|
|
|
|
|
PlatformThread thread2(&NullRunFunction, nullptr, "PlatformThreadTest2");
|
|
|
|
|
EXPECT_TRUE(thread1.GetThreadRef() == thread2.GetThreadRef());
|
|
|
|
|
thread1.Start();
|
|
|
|
|
thread2.Start();
|
|
|
|
|
EXPECT_TRUE(thread1.GetThreadRef() != thread2.GetThreadRef());
|
|
|
|
|
thread2.Stop();
|
|
|
|
|
thread1.Stop();
|
2016-04-22 09:08:44 -07:00
|
|
|
}
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2015-11-23 14:47:56 -08:00
|
|
|
TEST(PlatformThreadTest, RunFunctionIsCalled) {
|
2012-05-22 15:57:34 +00:00
|
|
|
bool flag = false;
|
2021-05-06 13:12:47 +00:00
|
|
|
PlatformThread thread(&SetFlagRunFunction, &flag, "RunFunctionIsCalled");
|
|
|
|
|
thread.Start();
|
|
|
|
|
|
|
|
|
|
// At this point, the flag may be either true or false.
|
|
|
|
|
thread.Stop();
|
|
|
|
|
|
|
|
|
|
// We expect the thread to have run at least once.
|
2012-05-22 15:57:34 +00:00
|
|
|
EXPECT_TRUE(flag);
|
|
|
|
|
}
|
2017-02-22 11:22:05 -08:00
|
|
|
|
2021-04-20 17:41:54 +02:00
|
|
|
TEST(PlatformThreadTest, JoinsThread) {
|
|
|
|
|
// This test flakes if there are problems with the join implementation.
|
2021-05-06 13:12:47 +00:00
|
|
|
EXPECT_TRUE(ThreadAttributes().joinable);
|
2021-04-20 17:41:54 +02:00
|
|
|
rtc::Event event;
|
2021-05-06 13:12:47 +00:00
|
|
|
std::function<void()> thread_function = [&] { event.Set(); };
|
|
|
|
|
PlatformThread thread(&StdFunctionRunFunction, &thread_function, "T");
|
|
|
|
|
thread.Start();
|
|
|
|
|
thread.Stop();
|
2021-04-20 17:41:54 +02:00
|
|
|
EXPECT_TRUE(event.Wait(/*give_up_after_ms=*/0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(PlatformThreadTest, StopsBeforeDetachedThreadExits) {
|
|
|
|
|
// This test flakes if there are problems with the detached thread
|
|
|
|
|
// implementation.
|
|
|
|
|
bool flag = false;
|
|
|
|
|
rtc::Event thread_started;
|
|
|
|
|
rtc::Event thread_continue;
|
|
|
|
|
rtc::Event thread_exiting;
|
2021-05-06 13:12:47 +00:00
|
|
|
std::function<void()> thread_function = [&] {
|
|
|
|
|
thread_started.Set();
|
|
|
|
|
thread_continue.Wait(Event::kForever);
|
|
|
|
|
flag = true;
|
|
|
|
|
thread_exiting.Set();
|
|
|
|
|
};
|
|
|
|
|
{
|
|
|
|
|
PlatformThread thread(&StdFunctionRunFunction, &thread_function, "T",
|
|
|
|
|
ThreadAttributes().SetDetached());
|
|
|
|
|
thread.Start();
|
|
|
|
|
thread.Stop();
|
|
|
|
|
}
|
2021-04-20 17:41:54 +02:00
|
|
|
thread_started.Wait(Event::kForever);
|
|
|
|
|
EXPECT_FALSE(flag);
|
|
|
|
|
thread_continue.Set();
|
|
|
|
|
thread_exiting.Wait(Event::kForever);
|
|
|
|
|
EXPECT_TRUE(flag);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-22 09:08:44 -07:00
|
|
|
} // namespace rtc
|