blob: 96bdd65155a9f520d492dd554fe2b4baa9e06ba4 [file] [log] [blame]
deadbeef8290ddf2017-07-11 16:56:05 -07001/*
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
Markus Handellc23d7492020-05-27 16:39:46 +020011#include "rtc_base/deprecated/signal_thread.h"
deadbeef8290ddf2017-07-11 16:56:05 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <memory>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
Yves Gerey988cc082018-10-23 12:03:01 +020016#include "rtc_base/location.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/null_socket_server.h"
18#include "rtc_base/socket_server.h"
deadbeef8290ddf2017-07-11 16:56:05 -070019
20namespace rtc {
21
22///////////////////////////////////////////////////////////////////////////////
23// SignalThread
24///////////////////////////////////////////////////////////////////////////////
25
Markus Handellc23d7492020-05-27 16:39:46 +020026DEPRECATED_SignalThread::DEPRECATED_SignalThread()
Yves Gerey665174f2018-06-19 15:03:05 +020027 : main_(Thread::Current()), worker_(this), state_(kInit), refcount_(1) {
Markus Handellc23d7492020-05-27 16:39:46 +020028 main_->SignalQueueDestroyed.connect(
29 this, &DEPRECATED_SignalThread::OnMainThreadDestroyed);
deadbeef8290ddf2017-07-11 16:56:05 -070030 worker_.SetName("SignalThread", this);
31}
32
Markus Handellc23d7492020-05-27 16:39:46 +020033DEPRECATED_SignalThread::~DEPRECATED_SignalThread() {
Markus Handellff84d862020-05-26 18:22:24 +020034 rtc::CritScope lock(&cs_);
deadbeef8290ddf2017-07-11 16:56:05 -070035 RTC_DCHECK(refcount_ == 0);
36}
37
Markus Handellc23d7492020-05-27 16:39:46 +020038bool DEPRECATED_SignalThread::SetName(const std::string& name,
39 const void* obj) {
deadbeef8290ddf2017-07-11 16:56:05 -070040 EnterExit ee(this);
Markus Handellff84d862020-05-26 18:22:24 +020041 RTC_DCHECK(!destroy_called_);
deadbeef8290ddf2017-07-11 16:56:05 -070042 RTC_DCHECK(main_->IsCurrent());
43 RTC_DCHECK(kInit == state_);
44 return worker_.SetName(name, obj);
45}
46
Markus Handellc23d7492020-05-27 16:39:46 +020047void DEPRECATED_SignalThread::Start() {
deadbeef8290ddf2017-07-11 16:56:05 -070048 EnterExit ee(this);
Markus Handellff84d862020-05-26 18:22:24 +020049 RTC_DCHECK(!destroy_called_);
deadbeef8290ddf2017-07-11 16:56:05 -070050 RTC_DCHECK(main_->IsCurrent());
51 if (kInit == state_ || kComplete == state_) {
52 state_ = kRunning;
53 OnWorkStart();
54 worker_.Start();
55 } else {
56 RTC_NOTREACHED();
57 }
58}
59
Markus Handellc23d7492020-05-27 16:39:46 +020060void DEPRECATED_SignalThread::Destroy(bool wait) {
deadbeef8290ddf2017-07-11 16:56:05 -070061 EnterExit ee(this);
Markus Handellff84d862020-05-26 18:22:24 +020062 // Sometimes the caller can't guarantee which thread will call Destroy, only
63 // that it will be the last thing it does.
64 // RTC_DCHECK(main_->IsCurrent());
65 RTC_DCHECK(!destroy_called_);
66 destroy_called_ = true;
deadbeef8290ddf2017-07-11 16:56:05 -070067 if ((kInit == state_) || (kComplete == state_)) {
68 refcount_--;
69 } else if (kRunning == state_ || kReleasing == state_) {
70 state_ = kStopping;
71 // OnWorkStop() must follow Quit(), so that when the thread wakes up due to
72 // OWS(), ContinueWork() will return false.
73 worker_.Quit();
74 OnWorkStop();
75 if (wait) {
76 // Release the thread's lock so that it can return from ::Run.
77 cs_.Leave();
78 worker_.Stop();
79 cs_.Enter();
80 refcount_--;
81 }
82 } else {
83 RTC_NOTREACHED();
84 }
85}
86
Markus Handellc23d7492020-05-27 16:39:46 +020087void DEPRECATED_SignalThread::Release() {
deadbeef8290ddf2017-07-11 16:56:05 -070088 EnterExit ee(this);
Markus Handellff84d862020-05-26 18:22:24 +020089 RTC_DCHECK(!destroy_called_);
deadbeef8290ddf2017-07-11 16:56:05 -070090 RTC_DCHECK(main_->IsCurrent());
91 if (kComplete == state_) {
92 refcount_--;
93 } else if (kRunning == state_) {
94 state_ = kReleasing;
95 } else {
96 // if (kInit == state_) use Destroy()
97 RTC_NOTREACHED();
98 }
99}
100
Markus Handellc23d7492020-05-27 16:39:46 +0200101bool DEPRECATED_SignalThread::ContinueWork() {
deadbeef8290ddf2017-07-11 16:56:05 -0700102 EnterExit ee(this);
Markus Handellff84d862020-05-26 18:22:24 +0200103 RTC_DCHECK(!destroy_called_);
deadbeef8290ddf2017-07-11 16:56:05 -0700104 RTC_DCHECK(worker_.IsCurrent());
105 return worker_.ProcessMessages(0);
106}
107
Markus Handellc23d7492020-05-27 16:39:46 +0200108void DEPRECATED_SignalThread::OnMessage(Message* msg) {
deadbeef8290ddf2017-07-11 16:56:05 -0700109 EnterExit ee(this);
110 if (ST_MSG_WORKER_DONE == msg->message_id) {
111 RTC_DCHECK(main_->IsCurrent());
112 OnWorkDone();
113 bool do_delete = false;
114 if (kRunning == state_) {
115 state_ = kComplete;
116 } else {
117 do_delete = true;
118 }
119 if (kStopping != state_) {
120 // Before signaling that the work is done, make sure that the worker
121 // thread actually is done. We got here because DoWork() finished and
122 // Run() posted the ST_MSG_WORKER_DONE message. This means the worker
123 // thread is about to go away anyway, but sometimes it doesn't actually
124 // finish before SignalWorkDone is processed, and for a reusable
125 // SignalThread this makes an assert in thread.cc fire.
126 //
127 // Calling Stop() on the worker ensures that the OS thread that underlies
128 // the worker will finish, and will be set to null, enabling us to call
129 // Start() again.
130 worker_.Stop();
131 SignalWorkDone(this);
132 }
133 if (do_delete) {
134 refcount_--;
135 }
136 }
137}
138
Markus Handellc23d7492020-05-27 16:39:46 +0200139DEPRECATED_SignalThread::Worker::Worker(DEPRECATED_SignalThread* parent)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200140 : Thread(std::make_unique<NullSocketServer>(), /*do_init=*/false),
Taylor Brandstetter08672602018-03-02 15:20:33 -0800141 parent_(parent) {
142 DoInit();
143}
144
Markus Handellc23d7492020-05-27 16:39:46 +0200145DEPRECATED_SignalThread::Worker::~Worker() {
deadbeef8290ddf2017-07-11 16:56:05 -0700146 Stop();
147}
148
Markus Handellc23d7492020-05-27 16:39:46 +0200149void DEPRECATED_SignalThread::Worker::Run() {
deadbeef8290ddf2017-07-11 16:56:05 -0700150 parent_->Run();
151}
152
Markus Handellc23d7492020-05-27 16:39:46 +0200153void DEPRECATED_SignalThread::Run() {
deadbeef8290ddf2017-07-11 16:56:05 -0700154 DoWork();
155 {
156 EnterExit ee(this);
157 if (main_) {
158 main_->Post(RTC_FROM_HERE, this, ST_MSG_WORKER_DONE);
159 }
160 }
161}
162
Markus Handellc23d7492020-05-27 16:39:46 +0200163void DEPRECATED_SignalThread::OnMainThreadDestroyed() {
deadbeef8290ddf2017-07-11 16:56:05 -0700164 EnterExit ee(this);
165 main_ = nullptr;
166}
167
Markus Handellc23d7492020-05-27 16:39:46 +0200168bool DEPRECATED_SignalThread::Worker::IsProcessingMessagesForTesting() {
deadbeef8290ddf2017-07-11 16:56:05 -0700169 return false;
170}
171
172} // namespace rtc