| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 : main_(Thread::Current()), | 23 : main_(Thread::Current()), |
| 24 worker_(this, use_socket_server), | 24 worker_(this, use_socket_server), |
| 25 state_(kInit), | 25 state_(kInit), |
| 26 refcount_(1) { | 26 refcount_(1) { |
| 27 main_->SignalQueueDestroyed.connect(this, | 27 main_->SignalQueueDestroyed.connect(this, |
| 28 &SignalThread::OnMainThreadDestroyed); | 28 &SignalThread::OnMainThreadDestroyed); |
| 29 worker_.SetName("SignalThread", this); | 29 worker_.SetName("SignalThread", this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 SignalThread::~SignalThread() { | 32 SignalThread::~SignalThread() { |
| 33 ASSERT(refcount_ == 0); | 33 RTC_DCHECK(refcount_ == 0); |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool SignalThread::SetName(const std::string& name, const void* obj) { | 36 bool SignalThread::SetName(const std::string& name, const void* obj) { |
| 37 EnterExit ee(this); | 37 EnterExit ee(this); |
| 38 ASSERT(main_->IsCurrent()); | 38 RTC_DCHECK(main_->IsCurrent()); |
| 39 ASSERT(kInit == state_); | 39 RTC_DCHECK(kInit == state_); |
| 40 return worker_.SetName(name, obj); | 40 return worker_.SetName(name, obj); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void SignalThread::Start() { | 43 void SignalThread::Start() { |
| 44 EnterExit ee(this); | 44 EnterExit ee(this); |
| 45 ASSERT(main_->IsCurrent()); | 45 RTC_DCHECK(main_->IsCurrent()); |
| 46 if (kInit == state_ || kComplete == state_) { | 46 if (kInit == state_ || kComplete == state_) { |
| 47 state_ = kRunning; | 47 state_ = kRunning; |
| 48 OnWorkStart(); | 48 OnWorkStart(); |
| 49 worker_.Start(); | 49 worker_.Start(); |
| 50 } else { | 50 } else { |
| 51 RTC_NOTREACHED(); | 51 RTC_NOTREACHED(); |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 | 54 |
| 55 void SignalThread::Destroy(bool wait) { | 55 void SignalThread::Destroy(bool wait) { |
| 56 EnterExit ee(this); | 56 EnterExit ee(this); |
| 57 ASSERT(main_->IsCurrent()); | 57 RTC_DCHECK(main_->IsCurrent()); |
| 58 if ((kInit == state_) || (kComplete == state_)) { | 58 if ((kInit == state_) || (kComplete == state_)) { |
| 59 refcount_--; | 59 refcount_--; |
| 60 } else if (kRunning == state_ || kReleasing == state_) { | 60 } else if (kRunning == state_ || kReleasing == state_) { |
| 61 state_ = kStopping; | 61 state_ = kStopping; |
| 62 // OnWorkStop() must follow Quit(), so that when the thread wakes up due to | 62 // OnWorkStop() must follow Quit(), so that when the thread wakes up due to |
| 63 // OWS(), ContinueWork() will return false. | 63 // OWS(), ContinueWork() will return false. |
| 64 worker_.Quit(); | 64 worker_.Quit(); |
| 65 OnWorkStop(); | 65 OnWorkStop(); |
| 66 if (wait) { | 66 if (wait) { |
| 67 // Release the thread's lock so that it can return from ::Run. | 67 // Release the thread's lock so that it can return from ::Run. |
| 68 cs_.Leave(); | 68 cs_.Leave(); |
| 69 worker_.Stop(); | 69 worker_.Stop(); |
| 70 cs_.Enter(); | 70 cs_.Enter(); |
| 71 refcount_--; | 71 refcount_--; |
| 72 } | 72 } |
| 73 } else { | 73 } else { |
| 74 RTC_NOTREACHED(); | 74 RTC_NOTREACHED(); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 void SignalThread::Release() { | 78 void SignalThread::Release() { |
| 79 EnterExit ee(this); | 79 EnterExit ee(this); |
| 80 ASSERT(main_->IsCurrent()); | 80 RTC_DCHECK(main_->IsCurrent()); |
| 81 if (kComplete == state_) { | 81 if (kComplete == state_) { |
| 82 refcount_--; | 82 refcount_--; |
| 83 } else if (kRunning == state_) { | 83 } else if (kRunning == state_) { |
| 84 state_ = kReleasing; | 84 state_ = kReleasing; |
| 85 } else { | 85 } else { |
| 86 // if (kInit == state_) use Destroy() | 86 // if (kInit == state_) use Destroy() |
| 87 RTC_NOTREACHED(); | 87 RTC_NOTREACHED(); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool SignalThread::ContinueWork() { | 91 bool SignalThread::ContinueWork() { |
| 92 EnterExit ee(this); | 92 EnterExit ee(this); |
| 93 ASSERT(worker_.IsCurrent()); | 93 RTC_DCHECK(worker_.IsCurrent()); |
| 94 return worker_.ProcessMessages(0); | 94 return worker_.ProcessMessages(0); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void SignalThread::OnMessage(Message *msg) { | 97 void SignalThread::OnMessage(Message *msg) { |
| 98 EnterExit ee(this); | 98 EnterExit ee(this); |
| 99 if (ST_MSG_WORKER_DONE == msg->message_id) { | 99 if (ST_MSG_WORKER_DONE == msg->message_id) { |
| 100 ASSERT(main_->IsCurrent()); | 100 RTC_DCHECK(main_->IsCurrent()); |
| 101 OnWorkDone(); | 101 OnWorkDone(); |
| 102 bool do_delete = false; | 102 bool do_delete = false; |
| 103 if (kRunning == state_) { | 103 if (kRunning == state_) { |
| 104 state_ = kComplete; | 104 state_ = kComplete; |
| 105 } else { | 105 } else { |
| 106 do_delete = true; | 106 do_delete = true; |
| 107 } | 107 } |
| 108 if (kStopping != state_) { | 108 if (kStopping != state_) { |
| 109 // Before signaling that the work is done, make sure that the worker | 109 // Before signaling that the work is done, make sure that the worker |
| 110 // thread actually is done. We got here because DoWork() finished and | 110 // thread actually is done. We got here because DoWork() finished and |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 void SignalThread::OnMainThreadDestroyed() { | 146 void SignalThread::OnMainThreadDestroyed() { |
| 147 EnterExit ee(this); | 147 EnterExit ee(this); |
| 148 main_ = NULL; | 148 main_ = NULL; |
| 149 } | 149 } |
| 150 | 150 |
| 151 } // namespace rtc | 151 } // namespace rtc |
| OLD | NEW |