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
|
|
|
|
2024-08-29 13:00:40 +00:00
|
|
|
#include <optional>
|
|
|
|
|
|
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 {
|
2012-05-22 15:57:34 +00:00
|
|
|
|
2021-05-07 15:02:36 +02:00
|
|
|
TEST(PlatformThreadTest, DefaultConstructedIsEmpty) {
|
|
|
|
|
PlatformThread thread;
|
2024-08-29 13:00:40 +00:00
|
|
|
EXPECT_EQ(thread.GetHandle(), std::nullopt);
|
2021-05-07 15:02:36 +02:00
|
|
|
EXPECT_TRUE(thread.empty());
|
2021-04-20 17:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-07 15:02:36 +02:00
|
|
|
TEST(PlatformThreadTest, StartFinalize) {
|
|
|
|
|
PlatformThread thread = PlatformThread::SpawnJoinable([] {}, "1");
|
2024-08-29 13:00:40 +00:00
|
|
|
EXPECT_NE(thread.GetHandle(), std::nullopt);
|
2021-05-07 15:02:36 +02:00
|
|
|
EXPECT_FALSE(thread.empty());
|
|
|
|
|
thread.Finalize();
|
|
|
|
|
EXPECT_TRUE(thread.empty());
|
2021-06-28 10:29:15 +02:00
|
|
|
rtc::Event done;
|
|
|
|
|
thread = PlatformThread::SpawnDetached([&] { done.Set(); }, "2");
|
2021-05-07 15:02:36 +02:00
|
|
|
EXPECT_FALSE(thread.empty());
|
|
|
|
|
thread.Finalize();
|
|
|
|
|
EXPECT_TRUE(thread.empty());
|
2022-08-19 08:16:48 +00:00
|
|
|
done.Wait(webrtc::TimeDelta::Seconds(30));
|
2021-05-05 10:42:04 +02:00
|
|
|
}
|
2016-04-22 09:08:44 -07:00
|
|
|
|
2021-05-07 15:02:36 +02:00
|
|
|
TEST(PlatformThreadTest, MovesEmpty) {
|
|
|
|
|
PlatformThread thread1;
|
|
|
|
|
PlatformThread thread2 = std::move(thread1);
|
|
|
|
|
EXPECT_TRUE(thread1.empty());
|
|
|
|
|
EXPECT_TRUE(thread2.empty());
|
|
|
|
|
}
|
2021-05-06 13:12:47 +00:00
|
|
|
|
2021-05-07 15:02:36 +02:00
|
|
|
TEST(PlatformThreadTest, MovesHandles) {
|
|
|
|
|
PlatformThread thread1 = PlatformThread::SpawnJoinable([] {}, "1");
|
|
|
|
|
PlatformThread thread2 = std::move(thread1);
|
|
|
|
|
EXPECT_TRUE(thread1.empty());
|
|
|
|
|
EXPECT_FALSE(thread2.empty());
|
2021-06-28 10:29:15 +02:00
|
|
|
rtc::Event done;
|
|
|
|
|
thread1 = PlatformThread::SpawnDetached([&] { done.Set(); }, "2");
|
2021-05-07 15:02:36 +02:00
|
|
|
thread2 = std::move(thread1);
|
|
|
|
|
EXPECT_TRUE(thread1.empty());
|
|
|
|
|
EXPECT_FALSE(thread2.empty());
|
2022-08-19 08:16:48 +00:00
|
|
|
done.Wait(webrtc::TimeDelta::Seconds(30));
|
2016-04-22 09:08:44 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-07 15:02:36 +02:00
|
|
|
TEST(PlatformThreadTest,
|
|
|
|
|
TwoThreadHandlesAreDifferentWhenStartedAndEqualWhenJoined) {
|
|
|
|
|
PlatformThread thread1 = PlatformThread();
|
|
|
|
|
PlatformThread thread2 = PlatformThread();
|
|
|
|
|
EXPECT_EQ(thread1.GetHandle(), thread2.GetHandle());
|
|
|
|
|
thread1 = PlatformThread::SpawnJoinable([] {}, "1");
|
|
|
|
|
thread2 = PlatformThread::SpawnJoinable([] {}, "2");
|
|
|
|
|
EXPECT_NE(thread1.GetHandle(), thread2.GetHandle());
|
|
|
|
|
thread1.Finalize();
|
|
|
|
|
EXPECT_NE(thread1.GetHandle(), thread2.GetHandle());
|
|
|
|
|
thread2.Finalize();
|
|
|
|
|
EXPECT_EQ(thread1.GetHandle(), thread2.GetHandle());
|
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-07 15:02:36 +02:00
|
|
|
PlatformThread::SpawnJoinable([&] { flag = true; }, "T");
|
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.
|
|
|
|
|
rtc::Event event;
|
2021-05-07 15:02:36 +02:00
|
|
|
PlatformThread::SpawnJoinable([&] { event.Set(); }, "T");
|
2022-08-19 08:16:48 +00:00
|
|
|
EXPECT_TRUE(event.Wait(/*give_up_after=*/webrtc::TimeDelta::Zero()));
|
2021-04-20 17:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07 15:02:36 +02:00
|
|
|
PlatformThread::SpawnDetached(
|
|
|
|
|
[&] {
|
|
|
|
|
thread_started.Set();
|
|
|
|
|
thread_continue.Wait(Event::kForever);
|
|
|
|
|
flag = true;
|
|
|
|
|
thread_exiting.Set();
|
|
|
|
|
},
|
|
|
|
|
"T");
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 15:03:05 +02:00
|
|
|
} // namespace rtc
|