| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h" | 5 #include "mojo/public/cpp/bindings/lib/sync_handle_watcher.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace mojo { | 9 namespace mojo { |
| 10 namespace internal { | 10 namespace internal { |
| 11 | 11 |
| 12 SyncHandleWatcher::SyncHandleWatcher( | 12 SyncHandleWatcher::SyncHandleWatcher( |
| 13 const Handle& handle, | 13 const Handle& handle, |
| 14 MojoHandleSignals handle_signals, | 14 MojoHandleSignals handle_signals, |
| 15 const SyncHandleRegistry::HandleCallback& callback) | 15 const SyncHandleRegistry::HandleCallback& callback) |
| 16 : handle_(handle), | 16 : handle_(handle), |
| 17 handle_signals_(handle_signals), | 17 handle_signals_(handle_signals), |
| 18 callback_(callback), | 18 callback_(callback), |
| 19 registered_(false), | 19 registered_(false), |
| 20 register_request_count_(0), | 20 register_request_count_(0), |
| 21 registry_(SyncHandleRegistry::current()), |
| 21 destroyed_(new base::RefCountedData<bool>(false)) {} | 22 destroyed_(new base::RefCountedData<bool>(false)) {} |
| 22 | 23 |
| 23 SyncHandleWatcher::~SyncHandleWatcher() { | 24 SyncHandleWatcher::~SyncHandleWatcher() { |
| 24 DCHECK(thread_checker_.CalledOnValidThread()); | 25 DCHECK(thread_checker_.CalledOnValidThread()); |
| 25 if (registered_) | 26 if (registered_) |
| 26 SyncHandleRegistry::current()->UnregisterHandle(handle_); | 27 registry_->UnregisterHandle(handle_); |
| 27 | 28 |
| 28 destroyed_->data = true; | 29 destroyed_->data = true; |
| 29 } | 30 } |
| 30 | 31 |
| 31 void SyncHandleWatcher::AllowWokenUpBySyncWatchOnSameThread() { | 32 void SyncHandleWatcher::AllowWokenUpBySyncWatchOnSameThread() { |
| 32 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 33 IncrementRegisterCount(); | 34 IncrementRegisterCount(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 bool SyncHandleWatcher::SyncWatch(const bool* should_stop) { | 37 bool SyncHandleWatcher::SyncWatch(const bool* should_stop) { |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); | 38 DCHECK(thread_checker_.CalledOnValidThread()); |
| 38 IncrementRegisterCount(); | 39 IncrementRegisterCount(); |
| 39 if (!registered_) { | 40 if (!registered_) { |
| 40 DecrementRegisterCount(); | 41 DecrementRegisterCount(); |
| 41 return false; | 42 return false; |
| 42 } | 43 } |
| 43 | 44 |
| 44 // This object may be destroyed during the WatchAllHandles() call. So we have | 45 // This object may be destroyed during the WatchAllHandles() call. So we have |
| 45 // to preserve the boolean that WatchAllHandles uses. | 46 // to preserve the boolean that WatchAllHandles uses. |
| 46 auto destroyed = destroyed_; | 47 auto destroyed = destroyed_; |
| 47 const bool* should_stop_array[] = {should_stop, &destroyed->data}; | 48 const bool* should_stop_array[] = {should_stop, &destroyed->data}; |
| 48 bool result = | 49 bool result = registry_->WatchAllHandles(should_stop_array, 2); |
| 49 SyncHandleRegistry::current()->WatchAllHandles(should_stop_array, 2); | |
| 50 | 50 |
| 51 // This object has been destroyed. | 51 // This object has been destroyed. |
| 52 if (destroyed->data) | 52 if (destroyed->data) |
| 53 return false; | 53 return false; |
| 54 | 54 |
| 55 DecrementRegisterCount(); | 55 DecrementRegisterCount(); |
| 56 return result; | 56 return result; |
| 57 } | 57 } |
| 58 | 58 |
| 59 void SyncHandleWatcher::IncrementRegisterCount() { | 59 void SyncHandleWatcher::IncrementRegisterCount() { |
| 60 register_request_count_++; | 60 register_request_count_++; |
| 61 if (!registered_) { | 61 if (!registered_) { |
| 62 registered_ = SyncHandleRegistry::current()->RegisterHandle( | 62 registered_ = |
| 63 handle_, handle_signals_, callback_); | 63 registry_->RegisterHandle(handle_, handle_signals_, callback_); |
| 64 } | 64 } |
| 65 } | 65 } |
| 66 | 66 |
| 67 void SyncHandleWatcher::DecrementRegisterCount() { | 67 void SyncHandleWatcher::DecrementRegisterCount() { |
| 68 DCHECK_GT(register_request_count_, 0u); | 68 DCHECK_GT(register_request_count_, 0u); |
| 69 | 69 |
| 70 register_request_count_--; | 70 register_request_count_--; |
| 71 if (register_request_count_ == 0 && registered_) { | 71 if (register_request_count_ == 0 && registered_) { |
| 72 SyncHandleRegistry::current()->UnregisterHandle(handle_); | 72 registry_->UnregisterHandle(handle_); |
| 73 registered_ = false; | 73 registered_ = false; |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 } // namespace internal | 77 } // namespace internal |
| 78 } // namespace mojo | 78 } // namespace mojo |
| OLD | NEW |