henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "rtc_base/thread.h" |
| 12 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame^] | 15 | #include "api/task_queue/task_queue_factory.h" |
| 16 | #include "api/task_queue/task_queue_test.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "rtc_base/async_invoker.h" |
| 18 | #include "rtc_base/async_udp_socket.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/event.h" |
| 20 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/null_socket_server.h" |
| 22 | #include "rtc_base/physical_socket_server.h" |
| 23 | #include "rtc_base/socket_address.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 24 | #include "rtc_base/third_party/sigslot/sigslot.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
| 26 | #if defined(WEBRTC_WIN) |
| 27 | #include <comdef.h> // NOLINT |
| 28 | #endif |
| 29 | |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 30 | namespace rtc { |
| 31 | namespace { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | |
| 33 | // Generates a sequence of numbers (collaboratively). |
| 34 | class TestGenerator { |
| 35 | public: |
| 36 | TestGenerator() : last(0), count(0) {} |
| 37 | |
| 38 | int Next(int prev) { |
| 39 | int result = prev + last; |
| 40 | last = result; |
| 41 | count += 1; |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | int last; |
| 46 | int count; |
| 47 | }; |
| 48 | |
| 49 | struct TestMessage : public MessageData { |
| 50 | explicit TestMessage(int v) : value(v) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | |
| 52 | int value; |
| 53 | }; |
| 54 | |
| 55 | // Receives on a socket and sends by posting messages. |
| 56 | class SocketClient : public TestGenerator, public sigslot::has_slots<> { |
| 57 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | SocketClient(AsyncSocket* socket, |
| 59 | const SocketAddress& addr, |
| 60 | Thread* post_thread, |
| 61 | MessageHandler* phandler) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | : socket_(AsyncUDPSocket::Create(socket, addr)), |
| 63 | post_thread_(post_thread), |
| 64 | post_handler_(phandler) { |
| 65 | socket_->SignalReadPacket.connect(this, &SocketClient::OnPacket); |
| 66 | } |
| 67 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 68 | ~SocketClient() override { delete socket_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | |
| 70 | SocketAddress address() const { return socket_->GetLocalAddress(); } |
| 71 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 72 | void OnPacket(AsyncPacketSocket* socket, |
| 73 | const char* buf, |
| 74 | size_t size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | const SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 76 | const int64_t& packet_time_us) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 77 | EXPECT_EQ(size, sizeof(uint32_t)); |
| 78 | uint32_t prev = reinterpret_cast<const uint32_t*>(buf)[0]; |
| 79 | uint32_t result = Next(prev); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 81 | post_thread_->PostDelayed(RTC_FROM_HERE, 200, post_handler_, 0, |
| 82 | new TestMessage(result)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | private: |
| 86 | AsyncUDPSocket* socket_; |
| 87 | Thread* post_thread_; |
| 88 | MessageHandler* post_handler_; |
| 89 | }; |
| 90 | |
| 91 | // Receives messages and sends on a socket. |
| 92 | class MessageClient : public MessageHandler, public TestGenerator { |
| 93 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | MessageClient(Thread* pth, Socket* socket) : socket_(socket) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 96 | ~MessageClient() override { delete socket_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 98 | void OnMessage(Message* pmsg) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | TestMessage* msg = static_cast<TestMessage*>(pmsg->pdata); |
| 100 | int result = Next(msg->value); |
| 101 | EXPECT_GE(socket_->Send(&result, sizeof(result)), 0); |
| 102 | delete msg; |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | Socket* socket_; |
| 107 | }; |
| 108 | |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 109 | class CustomThread : public rtc::Thread { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 110 | public: |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 111 | CustomThread() |
| 112 | : Thread(std::unique_ptr<SocketServer>(new rtc::NullSocketServer())) {} |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 113 | ~CustomThread() override { Stop(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | bool Start() { return false; } |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 115 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 116 | bool WrapCurrent() { return Thread::WrapCurrent(); } |
| 117 | void UnwrapCurrent() { Thread::UnwrapCurrent(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | // A thread that does nothing when it runs and signals an event |
| 121 | // when it is destroyed. |
| 122 | class SignalWhenDestroyedThread : public Thread { |
| 123 | public: |
| 124 | SignalWhenDestroyedThread(Event* event) |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 125 | : Thread(std::unique_ptr<SocketServer>(new NullSocketServer())), |
| 126 | event_(event) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 128 | ~SignalWhenDestroyedThread() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | Stop(); |
| 130 | event_->Set(); |
| 131 | } |
| 132 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 133 | void Run() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 134 | // Do nothing. |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | Event* event_; |
| 139 | }; |
| 140 | |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 141 | // A bool wrapped in a mutex, to avoid data races. Using a volatile |
| 142 | // bool should be sufficient for correct code ("eventual consistency" |
| 143 | // between caches is sufficient), but we can't tell the compiler about |
| 144 | // that, and then tsan complains about a data race. |
| 145 | |
| 146 | // See also discussion at |
| 147 | // http://stackoverflow.com/questions/7223164/is-mutex-needed-to-synchronize-a-simple-flag-between-pthreads |
| 148 | |
| 149 | // Using std::atomic<bool> or std::atomic_flag in C++11 is probably |
| 150 | // the right thing to do, but those features are not yet allowed. Or |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 151 | // rtc::AtomicInt, if/when that is added. Since the use isn't |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 152 | // performance critical, use a plain critical section for the time |
| 153 | // being. |
| 154 | |
| 155 | class AtomicBool { |
| 156 | public: |
| 157 | explicit AtomicBool(bool value = false) : flag_(value) {} |
| 158 | AtomicBool& operator=(bool value) { |
| 159 | CritScope scoped_lock(&cs_); |
| 160 | flag_ = value; |
| 161 | return *this; |
| 162 | } |
| 163 | bool get() const { |
| 164 | CritScope scoped_lock(&cs_); |
| 165 | return flag_; |
| 166 | } |
| 167 | |
| 168 | private: |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 169 | CriticalSection cs_; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 170 | bool flag_; |
| 171 | }; |
| 172 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | // Function objects to test Thread::Invoke. |
| 174 | struct FunctorA { |
| 175 | int operator()() { return 42; } |
| 176 | }; |
| 177 | class FunctorB { |
| 178 | public: |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 179 | explicit FunctorB(AtomicBool* flag) : flag_(flag) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 180 | void operator()() { |
| 181 | if (flag_) |
| 182 | *flag_ = true; |
| 183 | } |
| 184 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | private: |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 186 | AtomicBool* flag_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | }; |
| 188 | struct FunctorC { |
| 189 | int operator()() { |
| 190 | Thread::Current()->ProcessMessages(50); |
| 191 | return 24; |
| 192 | } |
| 193 | }; |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 194 | struct FunctorD { |
| 195 | public: |
| 196 | explicit FunctorD(AtomicBool* flag) : flag_(flag) {} |
| 197 | FunctorD(FunctorD&&) = default; |
| 198 | FunctorD& operator=(FunctorD&&) = default; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 199 | void operator()() { |
| 200 | if (flag_) |
| 201 | *flag_ = true; |
| 202 | } |
| 203 | |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 204 | private: |
| 205 | AtomicBool* flag_; |
| 206 | RTC_DISALLOW_COPY_AND_ASSIGN(FunctorD); |
| 207 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | |
| 209 | // See: https://code.google.com/p/webrtc/issues/detail?id=2409 |
| 210 | TEST(ThreadTest, DISABLED_Main) { |
| 211 | const SocketAddress addr("127.0.0.1", 0); |
| 212 | |
| 213 | // Create the messaging client on its own thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 214 | auto th1 = Thread::CreateWithSocketServer(); |
| 215 | Socket* socket = |
| 216 | th1->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); |
| 217 | MessageClient msg_client(th1.get(), socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 218 | |
| 219 | // Create the socket client on its own thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 220 | auto th2 = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | AsyncSocket* asocket = |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 222 | th2->socketserver()->CreateAsyncSocket(addr.family(), SOCK_DGRAM); |
| 223 | SocketClient sock_client(asocket, addr, th1.get(), &msg_client); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | |
| 225 | socket->Connect(sock_client.address()); |
| 226 | |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 227 | th1->Start(); |
| 228 | th2->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 229 | |
| 230 | // Get the messages started. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 231 | th1->PostDelayed(RTC_FROM_HERE, 100, &msg_client, 0, new TestMessage(1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | |
| 233 | // Give the clients a little while to run. |
| 234 | // Messages will be processed at 100, 300, 500, 700, 900. |
| 235 | Thread* th_main = Thread::Current(); |
| 236 | th_main->ProcessMessages(1000); |
| 237 | |
| 238 | // Stop the sending client. Give the receiver a bit longer to run, in case |
| 239 | // it is running on a machine that is under load (e.g. the build machine). |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 240 | th1->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 241 | th_main->ProcessMessages(200); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 242 | th2->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 243 | |
| 244 | // Make sure the results were correct |
| 245 | EXPECT_EQ(5, msg_client.count); |
| 246 | EXPECT_EQ(34, msg_client.last); |
| 247 | EXPECT_EQ(5, sock_client.count); |
| 248 | EXPECT_EQ(55, sock_client.last); |
| 249 | } |
| 250 | |
| 251 | // Test that setting thread names doesn't cause a malfunction. |
| 252 | // There's no easy way to verify the name was set properly at this time. |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 253 | TEST(ThreadTest, Names) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | // Default name |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 255 | auto thread = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | EXPECT_TRUE(thread->Start()); |
| 257 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 258 | // Name with no object parameter |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 259 | thread = Thread::CreateWithSocketServer(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 260 | EXPECT_TRUE(thread->SetName("No object", nullptr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 261 | EXPECT_TRUE(thread->Start()); |
| 262 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 263 | // Really long name |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 264 | thread = Thread::CreateWithSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 265 | EXPECT_TRUE(thread->SetName("Abcdefghijklmnopqrstuvwxyz1234567890", this)); |
| 266 | EXPECT_TRUE(thread->Start()); |
| 267 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 268 | } |
| 269 | |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 270 | TEST(ThreadTest, Wrap) { |
| 271 | Thread* current_thread = Thread::Current(); |
Niels Möller | 5a8f860 | 2019-06-12 11:30:59 +0200 | [diff] [blame] | 272 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
| 273 | |
| 274 | { |
| 275 | CustomThread cthread; |
| 276 | EXPECT_TRUE(cthread.WrapCurrent()); |
| 277 | EXPECT_EQ(&cthread, Thread::Current()); |
| 278 | EXPECT_TRUE(cthread.RunningForTest()); |
| 279 | EXPECT_FALSE(cthread.IsOwned()); |
| 280 | cthread.UnwrapCurrent(); |
| 281 | EXPECT_FALSE(cthread.RunningForTest()); |
| 282 | } |
| 283 | ThreadManager::Instance()->SetCurrentThread(current_thread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 284 | } |
| 285 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 286 | TEST(ThreadTest, Invoke) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 287 | // Create and start the thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 288 | auto thread = Thread::CreateWithSocketServer(); |
| 289 | thread->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 290 | // Try calling functors. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 291 | EXPECT_EQ(42, thread->Invoke<int>(RTC_FROM_HERE, FunctorA())); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 292 | AtomicBool called; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 293 | FunctorB f2(&called); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 294 | thread->Invoke<void>(RTC_FROM_HERE, f2); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 295 | EXPECT_TRUE(called.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 296 | // Try calling bare functions. |
| 297 | struct LocalFuncs { |
| 298 | static int Func1() { return 999; } |
| 299 | static void Func2() {} |
| 300 | }; |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 301 | EXPECT_EQ(999, thread->Invoke<int>(RTC_FROM_HERE, &LocalFuncs::Func1)); |
| 302 | thread->Invoke<void>(RTC_FROM_HERE, &LocalFuncs::Func2); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 303 | } |
| 304 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 305 | // Verifies that two threads calling Invoke on each other at the same time does |
| 306 | // not deadlock. |
| 307 | TEST(ThreadTest, TwoThreadsInvokeNoDeadlock) { |
| 308 | AutoThread thread; |
| 309 | Thread* current_thread = Thread::Current(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 310 | ASSERT_TRUE(current_thread != nullptr); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 311 | |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 312 | auto other_thread = Thread::CreateWithSocketServer(); |
| 313 | other_thread->Start(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 314 | |
| 315 | struct LocalFuncs { |
| 316 | static void Set(bool* out) { *out = true; } |
| 317 | static void InvokeSet(Thread* thread, bool* out) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 318 | thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 319 | } |
| 320 | }; |
| 321 | |
| 322 | bool called = false; |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 323 | other_thread->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 324 | RTC_FROM_HERE, Bind(&LocalFuncs::InvokeSet, current_thread, &called)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 325 | |
| 326 | EXPECT_TRUE(called); |
| 327 | } |
| 328 | |
| 329 | // Verifies that if thread A invokes a call on thread B and thread C is trying |
| 330 | // to invoke A at the same time, thread A does not handle C's invoke while |
| 331 | // invoking B. |
| 332 | TEST(ThreadTest, ThreeThreadsInvoke) { |
| 333 | AutoThread thread; |
| 334 | Thread* thread_a = Thread::Current(); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 335 | auto thread_b = Thread::CreateWithSocketServer(); |
| 336 | auto thread_c = Thread::CreateWithSocketServer(); |
| 337 | thread_b->Start(); |
| 338 | thread_c->Start(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 339 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 340 | class LockedBool { |
| 341 | public: |
| 342 | explicit LockedBool(bool value) : value_(value) {} |
| 343 | |
| 344 | void Set(bool value) { |
| 345 | CritScope lock(&crit_); |
| 346 | value_ = value; |
| 347 | } |
| 348 | |
| 349 | bool Get() { |
| 350 | CritScope lock(&crit_); |
| 351 | return value_; |
| 352 | } |
| 353 | |
| 354 | private: |
| 355 | CriticalSection crit_; |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 356 | bool value_ RTC_GUARDED_BY(crit_); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 359 | struct LocalFuncs { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 360 | static void Set(LockedBool* out) { out->Set(true); } |
| 361 | static void InvokeSet(Thread* thread, LockedBool* out) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 362 | thread->Invoke<void>(RTC_FROM_HERE, Bind(&Set, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // Set |out| true and call InvokeSet on |thread|. |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 366 | static void SetAndInvokeSet(LockedBool* out, |
| 367 | Thread* thread, |
| 368 | LockedBool* out_inner) { |
| 369 | out->Set(true); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 370 | InvokeSet(thread, out_inner); |
| 371 | } |
| 372 | |
| 373 | // Asynchronously invoke SetAndInvokeSet on |thread1| and wait until |
| 374 | // |thread1| starts the call. |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 375 | static void AsyncInvokeSetAndWait(AsyncInvoker* invoker, |
| 376 | Thread* thread1, |
| 377 | Thread* thread2, |
| 378 | LockedBool* out) { |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 379 | CriticalSection crit; |
| 380 | LockedBool async_invoked(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 381 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 382 | invoker->AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 383 | RTC_FROM_HERE, thread1, |
| 384 | Bind(&SetAndInvokeSet, &async_invoked, thread2, out)); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 385 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 386 | EXPECT_TRUE_WAIT(async_invoked.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 387 | } |
| 388 | }; |
| 389 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 390 | AsyncInvoker invoker; |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 391 | LockedBool thread_a_called(false); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 392 | |
| 393 | // Start the sequence A --(invoke)--> B --(async invoke)--> C --(invoke)--> A. |
| 394 | // Thread B returns when C receives the call and C should be blocked until A |
| 395 | // starts to process messages. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 396 | thread_b->Invoke<void>(RTC_FROM_HERE, |
| 397 | Bind(&LocalFuncs::AsyncInvokeSetAndWait, &invoker, |
| 398 | thread_c.get(), thread_a, &thread_a_called)); |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 399 | EXPECT_FALSE(thread_a_called.Get()); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 400 | |
pbos@webrtc.org | e93cbd1 | 2014-10-15 14:54:56 +0000 | [diff] [blame] | 401 | EXPECT_TRUE_WAIT(thread_a_called.Get(), 2000); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 402 | } |
| 403 | |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 404 | // Set the name on a thread when the underlying QueueDestroyed signal is |
| 405 | // triggered. This causes an error if the object is already partially |
| 406 | // destroyed. |
| 407 | class SetNameOnSignalQueueDestroyedTester : public sigslot::has_slots<> { |
| 408 | public: |
| 409 | SetNameOnSignalQueueDestroyedTester(Thread* thread) : thread_(thread) { |
| 410 | thread->SignalQueueDestroyed.connect( |
| 411 | this, &SetNameOnSignalQueueDestroyedTester::OnQueueDestroyed); |
| 412 | } |
| 413 | |
| 414 | void OnQueueDestroyed() { |
| 415 | // Makes sure that if we access the Thread while it's being destroyed, that |
| 416 | // it doesn't cause a problem because the vtable has been modified. |
| 417 | thread_->SetName("foo", nullptr); |
| 418 | } |
| 419 | |
| 420 | private: |
| 421 | Thread* thread_; |
| 422 | }; |
| 423 | |
| 424 | TEST(ThreadTest, SetNameOnSignalQueueDestroyed) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 425 | auto thread1 = Thread::CreateWithSocketServer(); |
| 426 | SetNameOnSignalQueueDestroyedTester tester1(thread1.get()); |
| 427 | thread1.reset(); |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 428 | |
| 429 | Thread* thread2 = new AutoThread(); |
| 430 | SetNameOnSignalQueueDestroyedTester tester2(thread2); |
| 431 | delete thread2; |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 434 | class AsyncInvokeTest : public ::testing::Test { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 435 | public: |
| 436 | void IntCallback(int value) { |
| 437 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 438 | int_value_ = value; |
| 439 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 440 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 441 | expected_thread_ = thread; |
| 442 | } |
| 443 | |
| 444 | protected: |
| 445 | enum { kWaitTimeout = 1000 }; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 446 | AsyncInvokeTest() : int_value_(0), expected_thread_(nullptr) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 447 | |
| 448 | int int_value_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 449 | Thread* expected_thread_; |
| 450 | }; |
| 451 | |
henrike@webrtc.org | e30dab7 | 2014-10-09 15:41:40 +0000 | [diff] [blame] | 452 | TEST_F(AsyncInvokeTest, FireAndForget) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 453 | AsyncInvoker invoker; |
| 454 | // Create and start the thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 455 | auto thread = Thread::CreateWithSocketServer(); |
| 456 | thread->Start(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 457 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 458 | AtomicBool called; |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 459 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorB(&called)); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 460 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 461 | thread->Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 464 | TEST_F(AsyncInvokeTest, NonCopyableFunctor) { |
| 465 | AsyncInvoker invoker; |
| 466 | // Create and start the thread. |
| 467 | auto thread = Thread::CreateWithSocketServer(); |
| 468 | thread->Start(); |
| 469 | // Try calling functor. |
| 470 | AtomicBool called; |
| 471 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), FunctorD(&called)); |
| 472 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
| 473 | thread->Stop(); |
| 474 | } |
| 475 | |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 476 | TEST_F(AsyncInvokeTest, KillInvokerDuringExecute) { |
| 477 | // Use these events to get in a state where the functor is in the middle of |
| 478 | // executing, and then to wait for it to finish, ensuring the "EXPECT_FALSE" |
| 479 | // is run. |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 480 | Event functor_started; |
| 481 | Event functor_continue; |
| 482 | Event functor_finished; |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 483 | |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 484 | auto thread = Thread::CreateWithSocketServer(); |
| 485 | thread->Start(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 486 | volatile bool invoker_destroyed = false; |
| 487 | { |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 488 | auto functor = [&functor_started, &functor_continue, &functor_finished, |
| 489 | &invoker_destroyed] { |
| 490 | functor_started.Set(); |
| 491 | functor_continue.Wait(Event::kForever); |
| 492 | rtc::Thread::Current()->SleepMs(kWaitTimeout); |
| 493 | EXPECT_FALSE(invoker_destroyed); |
| 494 | functor_finished.Set(); |
| 495 | }; |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 496 | AsyncInvoker invoker; |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 497 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, thread.get(), functor); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 498 | functor_started.Wait(Event::kForever); |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 499 | |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 500 | // Destroy the invoker while the functor is still executing (doing |
| 501 | // SleepMs). |
deadbeef | aea9293 | 2017-05-23 12:55:03 -0700 | [diff] [blame] | 502 | functor_continue.Set(); |
deadbeef | 162cb53 | 2017-02-23 17:10:07 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // If the destructor DIDN'T wait for the functor to finish executing, it will |
| 506 | // hit the EXPECT_FALSE(invoker_destroyed) after it finishes sleeping for a |
| 507 | // second. |
| 508 | invoker_destroyed = true; |
| 509 | functor_finished.Wait(Event::kForever); |
| 510 | } |
| 511 | |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 512 | // Variant of the above test where the async-invoked task calls AsyncInvoke |
| 513 | // *again*, for the thread on which the AsyncInvoker is currently being |
| 514 | // destroyed. This shouldn't deadlock or crash; this second invocation should |
| 515 | // just be ignored. |
| 516 | TEST_F(AsyncInvokeTest, KillInvokerDuringExecuteWithReentrantInvoke) { |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 517 | Event functor_started; |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 518 | // Flag used to verify that the recursively invoked task never actually runs. |
| 519 | bool reentrant_functor_run = false; |
| 520 | |
| 521 | Thread* main = Thread::Current(); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 522 | Thread thread(std::make_unique<NullSocketServer>()); |
deadbeef | 3af63b0 | 2017-08-08 17:59:47 -0700 | [diff] [blame] | 523 | thread.Start(); |
| 524 | { |
| 525 | AsyncInvoker invoker; |
| 526 | auto reentrant_functor = [&reentrant_functor_run] { |
| 527 | reentrant_functor_run = true; |
| 528 | }; |
| 529 | auto functor = [&functor_started, &invoker, main, reentrant_functor] { |
| 530 | functor_started.Set(); |
| 531 | Thread::Current()->SleepMs(kWaitTimeout); |
| 532 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, main, reentrant_functor); |
| 533 | }; |
| 534 | // This queues a task on |thread| to sleep for |kWaitTimeout| then queue a |
| 535 | // task on |main|. But this second queued task should never run, since the |
| 536 | // destructor will be entered before it's even invoked. |
| 537 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, &thread, functor); |
| 538 | functor_started.Wait(Event::kForever); |
| 539 | } |
| 540 | EXPECT_FALSE(reentrant_functor_run); |
| 541 | } |
| 542 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 543 | TEST_F(AsyncInvokeTest, Flush) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 544 | AsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 545 | AtomicBool flag1; |
| 546 | AtomicBool flag2; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 547 | // Queue two async calls to the current thread. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 548 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1)); |
| 549 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 550 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 551 | EXPECT_FALSE(flag1.get()); |
| 552 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 553 | // Force them to run now. |
| 554 | invoker.Flush(Thread::Current()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 555 | EXPECT_TRUE(flag1.get()); |
| 556 | EXPECT_TRUE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 557 | } |
| 558 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 559 | TEST_F(AsyncInvokeTest, FlushWithIds) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 560 | AsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 561 | AtomicBool flag1; |
| 562 | AtomicBool flag2; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 563 | // Queue two async calls to the current thread, one with a message id. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 564 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag1), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 565 | 5); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 566 | invoker.AsyncInvoke<void>(RTC_FROM_HERE, Thread::Current(), FunctorB(&flag2)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 567 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 568 | EXPECT_FALSE(flag1.get()); |
| 569 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 570 | // Execute pending calls with id == 5. |
| 571 | invoker.Flush(Thread::Current(), 5); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 572 | EXPECT_TRUE(flag1.get()); |
| 573 | EXPECT_FALSE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 574 | flag1 = false; |
| 575 | // Execute all pending calls. The id == 5 call should not execute again. |
| 576 | invoker.Flush(Thread::Current()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 577 | EXPECT_FALSE(flag1.get()); |
| 578 | EXPECT_TRUE(flag2.get()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 581 | class GuardedAsyncInvokeTest : public ::testing::Test { |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 582 | public: |
| 583 | void IntCallback(int value) { |
| 584 | EXPECT_EQ(expected_thread_, Thread::Current()); |
| 585 | int_value_ = value; |
| 586 | } |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 587 | void SetExpectedThreadForIntCallback(Thread* thread) { |
| 588 | expected_thread_ = thread; |
| 589 | } |
| 590 | |
| 591 | protected: |
| 592 | const static int kWaitTimeout = 1000; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 593 | GuardedAsyncInvokeTest() : int_value_(0), expected_thread_(nullptr) {} |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 594 | |
| 595 | int int_value_; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 596 | Thread* expected_thread_; |
| 597 | }; |
| 598 | |
| 599 | // Functor for creating an invoker. |
| 600 | struct CreateInvoker { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 601 | CreateInvoker(std::unique_ptr<GuardedAsyncInvoker>* invoker) |
| 602 | : invoker_(invoker) {} |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 603 | void operator()() { invoker_->reset(new GuardedAsyncInvoker()); } |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 604 | std::unique_ptr<GuardedAsyncInvoker>* invoker_; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | // Test that we can call AsyncInvoke<void>() after the thread died. |
| 608 | TEST_F(GuardedAsyncInvokeTest, KillThreadFireAndForget) { |
| 609 | // Create and start the thread. |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 610 | std::unique_ptr<Thread> thread(Thread::Create()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 611 | thread->Start(); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 612 | std::unique_ptr<GuardedAsyncInvoker> invoker; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 613 | // Create the invoker on |thread|. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 614 | thread->Invoke<void>(RTC_FROM_HERE, CreateInvoker(&invoker)); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 615 | // Kill |thread|. |
| 616 | thread = nullptr; |
| 617 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 618 | AtomicBool called; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 619 | EXPECT_FALSE(invoker->AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 620 | // With thread gone, nothing should happen. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 621 | WAIT(called.get(), kWaitTimeout); |
| 622 | EXPECT_FALSE(called.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 625 | // The remaining tests check that GuardedAsyncInvoker behaves as AsyncInvoker |
| 626 | // when Thread is still alive. |
| 627 | TEST_F(GuardedAsyncInvokeTest, FireAndForget) { |
| 628 | GuardedAsyncInvoker invoker; |
| 629 | // Try calling functor. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 630 | AtomicBool called; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 631 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&called))); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 632 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 633 | } |
| 634 | |
Cameron Pickett | d132ce1 | 2018-03-12 16:07:37 -0700 | [diff] [blame] | 635 | TEST_F(GuardedAsyncInvokeTest, NonCopyableFunctor) { |
| 636 | GuardedAsyncInvoker invoker; |
| 637 | // Try calling functor. |
| 638 | AtomicBool called; |
| 639 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorD(&called))); |
| 640 | EXPECT_TRUE_WAIT(called.get(), kWaitTimeout); |
| 641 | } |
| 642 | |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 643 | TEST_F(GuardedAsyncInvokeTest, Flush) { |
| 644 | GuardedAsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 645 | AtomicBool flag1; |
| 646 | AtomicBool flag2; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 647 | // Queue two async calls to the current thread. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 648 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1))); |
| 649 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 650 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 651 | EXPECT_FALSE(flag1.get()); |
| 652 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 653 | // Force them to run now. |
| 654 | EXPECT_TRUE(invoker.Flush()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 655 | EXPECT_TRUE(flag1.get()); |
| 656 | EXPECT_TRUE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | TEST_F(GuardedAsyncInvokeTest, FlushWithIds) { |
| 660 | GuardedAsyncInvoker invoker; |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 661 | AtomicBool flag1; |
| 662 | AtomicBool flag2; |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 663 | // Queue two async calls to the current thread, one with a message id. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 664 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag1), 5)); |
| 665 | EXPECT_TRUE(invoker.AsyncInvoke<void>(RTC_FROM_HERE, FunctorB(&flag2))); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 666 | // Because we haven't pumped messages, these should not have run yet. |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 667 | EXPECT_FALSE(flag1.get()); |
| 668 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 669 | // Execute pending calls with id == 5. |
| 670 | EXPECT_TRUE(invoker.Flush(5)); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 671 | EXPECT_TRUE(flag1.get()); |
| 672 | EXPECT_FALSE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 673 | flag1 = false; |
| 674 | // Execute all pending calls. The id == 5 call should not execute again. |
| 675 | EXPECT_TRUE(invoker.Flush()); |
nisse | d9b75be | 2015-11-16 00:54:07 -0800 | [diff] [blame] | 676 | EXPECT_FALSE(flag1.get()); |
| 677 | EXPECT_TRUE(flag2.get()); |
Magnus Jedvert | a1f590f | 2015-08-20 16:42:42 +0200 | [diff] [blame] | 678 | } |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 679 | |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 680 | void ThreadIsCurrent(Thread* thread, bool* result, Event* event) { |
| 681 | *result = thread->IsCurrent(); |
| 682 | event->Set(); |
| 683 | } |
| 684 | |
| 685 | void WaitAndSetEvent(Event* wait_event, Event* set_event) { |
| 686 | wait_event->Wait(Event::kForever); |
| 687 | set_event->Set(); |
| 688 | } |
| 689 | |
| 690 | // A functor that keeps track of the number of copies and moves. |
| 691 | class LifeCycleFunctor { |
| 692 | public: |
| 693 | struct Stats { |
| 694 | size_t copy_count = 0; |
| 695 | size_t move_count = 0; |
| 696 | }; |
| 697 | |
| 698 | LifeCycleFunctor(Stats* stats, Event* event) : stats_(stats), event_(event) {} |
| 699 | LifeCycleFunctor(const LifeCycleFunctor& other) { *this = other; } |
| 700 | LifeCycleFunctor(LifeCycleFunctor&& other) { *this = std::move(other); } |
| 701 | |
| 702 | LifeCycleFunctor& operator=(const LifeCycleFunctor& other) { |
| 703 | stats_ = other.stats_; |
| 704 | event_ = other.event_; |
| 705 | ++stats_->copy_count; |
| 706 | return *this; |
| 707 | } |
| 708 | |
| 709 | LifeCycleFunctor& operator=(LifeCycleFunctor&& other) { |
| 710 | stats_ = other.stats_; |
| 711 | event_ = other.event_; |
| 712 | ++stats_->move_count; |
| 713 | return *this; |
| 714 | } |
| 715 | |
| 716 | void operator()() { event_->Set(); } |
| 717 | |
| 718 | private: |
| 719 | Stats* stats_; |
| 720 | Event* event_; |
| 721 | }; |
| 722 | |
| 723 | // A functor that verifies the thread it was destroyed on. |
| 724 | class DestructionFunctor { |
| 725 | public: |
| 726 | DestructionFunctor(Thread* thread, bool* thread_was_current, Event* event) |
| 727 | : thread_(thread), |
| 728 | thread_was_current_(thread_was_current), |
| 729 | event_(event) {} |
| 730 | ~DestructionFunctor() { |
| 731 | // Only signal the event if this was the functor that was invoked to avoid |
| 732 | // the event being signaled due to the destruction of temporary/moved |
| 733 | // versions of this object. |
| 734 | if (was_invoked_) { |
| 735 | *thread_was_current_ = thread_->IsCurrent(); |
| 736 | event_->Set(); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void operator()() { was_invoked_ = true; } |
| 741 | |
| 742 | private: |
| 743 | Thread* thread_; |
| 744 | bool* thread_was_current_; |
| 745 | Event* event_; |
| 746 | bool was_invoked_ = false; |
| 747 | }; |
| 748 | |
| 749 | TEST(ThreadPostTaskTest, InvokesWithBind) { |
| 750 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 751 | background_thread->Start(); |
| 752 | |
| 753 | Event event; |
| 754 | background_thread->PostTask(RTC_FROM_HERE, Bind(&Event::Set, &event)); |
| 755 | event.Wait(Event::kForever); |
| 756 | } |
| 757 | |
| 758 | TEST(ThreadPostTaskTest, InvokesWithLambda) { |
| 759 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 760 | background_thread->Start(); |
| 761 | |
| 762 | Event event; |
| 763 | background_thread->PostTask(RTC_FROM_HERE, [&event] { event.Set(); }); |
| 764 | event.Wait(Event::kForever); |
| 765 | } |
| 766 | |
| 767 | TEST(ThreadPostTaskTest, InvokesWithCopiedFunctor) { |
| 768 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 769 | background_thread->Start(); |
| 770 | |
| 771 | LifeCycleFunctor::Stats stats; |
| 772 | Event event; |
| 773 | LifeCycleFunctor functor(&stats, &event); |
| 774 | background_thread->PostTask(RTC_FROM_HERE, functor); |
| 775 | event.Wait(Event::kForever); |
| 776 | |
| 777 | EXPECT_EQ(1u, stats.copy_count); |
| 778 | EXPECT_EQ(0u, stats.move_count); |
| 779 | } |
| 780 | |
| 781 | TEST(ThreadPostTaskTest, InvokesWithMovedFunctor) { |
| 782 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 783 | background_thread->Start(); |
| 784 | |
| 785 | LifeCycleFunctor::Stats stats; |
| 786 | Event event; |
| 787 | LifeCycleFunctor functor(&stats, &event); |
| 788 | background_thread->PostTask(RTC_FROM_HERE, std::move(functor)); |
| 789 | event.Wait(Event::kForever); |
| 790 | |
| 791 | EXPECT_EQ(0u, stats.copy_count); |
| 792 | EXPECT_EQ(1u, stats.move_count); |
| 793 | } |
| 794 | |
| 795 | TEST(ThreadPostTaskTest, InvokesWithReferencedFunctorShouldCopy) { |
| 796 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 797 | background_thread->Start(); |
| 798 | |
| 799 | LifeCycleFunctor::Stats stats; |
| 800 | Event event; |
| 801 | LifeCycleFunctor functor(&stats, &event); |
| 802 | LifeCycleFunctor& functor_ref = functor; |
| 803 | background_thread->PostTask(RTC_FROM_HERE, functor_ref); |
| 804 | event.Wait(Event::kForever); |
| 805 | |
| 806 | EXPECT_EQ(1u, stats.copy_count); |
| 807 | EXPECT_EQ(0u, stats.move_count); |
| 808 | } |
| 809 | |
| 810 | TEST(ThreadPostTaskTest, InvokesWithCopiedFunctorDestroyedOnTargetThread) { |
| 811 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 812 | background_thread->Start(); |
| 813 | |
| 814 | Event event; |
| 815 | bool was_invoked_on_background_thread = false; |
| 816 | DestructionFunctor functor(background_thread.get(), |
| 817 | &was_invoked_on_background_thread, &event); |
| 818 | background_thread->PostTask(RTC_FROM_HERE, functor); |
| 819 | event.Wait(Event::kForever); |
| 820 | |
| 821 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 822 | } |
| 823 | |
| 824 | TEST(ThreadPostTaskTest, InvokesWithMovedFunctorDestroyedOnTargetThread) { |
| 825 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 826 | background_thread->Start(); |
| 827 | |
| 828 | Event event; |
| 829 | bool was_invoked_on_background_thread = false; |
| 830 | DestructionFunctor functor(background_thread.get(), |
| 831 | &was_invoked_on_background_thread, &event); |
| 832 | background_thread->PostTask(RTC_FROM_HERE, std::move(functor)); |
| 833 | event.Wait(Event::kForever); |
| 834 | |
| 835 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 836 | } |
| 837 | |
| 838 | TEST(ThreadPostTaskTest, |
| 839 | InvokesWithReferencedFunctorShouldCopyAndDestroyedOnTargetThread) { |
| 840 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 841 | background_thread->Start(); |
| 842 | |
| 843 | Event event; |
| 844 | bool was_invoked_on_background_thread = false; |
| 845 | DestructionFunctor functor(background_thread.get(), |
| 846 | &was_invoked_on_background_thread, &event); |
| 847 | DestructionFunctor& functor_ref = functor; |
| 848 | background_thread->PostTask(RTC_FROM_HERE, functor_ref); |
| 849 | event.Wait(Event::kForever); |
| 850 | |
| 851 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 852 | } |
| 853 | |
| 854 | TEST(ThreadPostTaskTest, InvokesOnBackgroundThread) { |
| 855 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 856 | background_thread->Start(); |
| 857 | |
| 858 | Event event; |
| 859 | bool was_invoked_on_background_thread = false; |
| 860 | background_thread->PostTask(RTC_FROM_HERE, |
| 861 | Bind(&ThreadIsCurrent, background_thread.get(), |
| 862 | &was_invoked_on_background_thread, &event)); |
| 863 | event.Wait(Event::kForever); |
| 864 | |
| 865 | EXPECT_TRUE(was_invoked_on_background_thread); |
| 866 | } |
| 867 | |
| 868 | TEST(ThreadPostTaskTest, InvokesAsynchronously) { |
| 869 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 870 | background_thread->Start(); |
| 871 | |
| 872 | // The first event ensures that SendSingleMessage() is not blocking this |
| 873 | // thread. The second event ensures that the message is processed. |
| 874 | Event event_set_by_test_thread; |
| 875 | Event event_set_by_background_thread; |
| 876 | background_thread->PostTask(RTC_FROM_HERE, |
| 877 | Bind(&WaitAndSetEvent, &event_set_by_test_thread, |
| 878 | &event_set_by_background_thread)); |
| 879 | event_set_by_test_thread.Set(); |
| 880 | event_set_by_background_thread.Wait(Event::kForever); |
| 881 | } |
| 882 | |
| 883 | TEST(ThreadPostTaskTest, InvokesInPostedOrder) { |
| 884 | std::unique_ptr<rtc::Thread> background_thread(rtc::Thread::Create()); |
| 885 | background_thread->Start(); |
| 886 | |
| 887 | Event first; |
| 888 | Event second; |
| 889 | Event third; |
| 890 | Event fourth; |
| 891 | |
| 892 | background_thread->PostTask(RTC_FROM_HERE, |
| 893 | Bind(&WaitAndSetEvent, &first, &second)); |
| 894 | background_thread->PostTask(RTC_FROM_HERE, |
| 895 | Bind(&WaitAndSetEvent, &second, &third)); |
| 896 | background_thread->PostTask(RTC_FROM_HERE, |
| 897 | Bind(&WaitAndSetEvent, &third, &fourth)); |
| 898 | |
| 899 | // All tasks have been posted before the first one is unblocked. |
| 900 | first.Set(); |
| 901 | // Only if the chain is invoked in posted order will the last event be set. |
| 902 | fourth.Wait(Event::kForever); |
| 903 | } |
| 904 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame^] | 905 | class ThreadFactory : public webrtc::TaskQueueFactory { |
| 906 | public: |
| 907 | std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter> |
| 908 | CreateTaskQueue(absl::string_view /* name */, |
| 909 | Priority /*priority*/) const override { |
| 910 | std::unique_ptr<Thread> thread = Thread::Create(); |
| 911 | thread->Start(); |
| 912 | return std::unique_ptr<webrtc::TaskQueueBase, webrtc::TaskQueueDeleter>( |
| 913 | thread.release()); |
| 914 | } |
| 915 | }; |
| 916 | |
| 917 | using ::webrtc::TaskQueueTest; |
| 918 | |
| 919 | INSTANTIATE_TEST_SUITE_P(RtcThread, |
| 920 | TaskQueueTest, |
| 921 | ::testing::Values(std::make_unique<ThreadFactory>)); |
| 922 | |
Mirko Bonadei | e10b163 | 2018-12-11 18:43:40 +0100 | [diff] [blame] | 923 | } // namespace |
| 924 | } // namespace rtc |