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