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/sync_handle_watcher.h" | 5 #include "mojo/public/cpp/bindings/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 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 } | 34 } |
35 | 35 |
36 bool SyncHandleWatcher::SyncWatch(const bool* should_stop) { | 36 bool SyncHandleWatcher::SyncWatch(const bool* should_stop) { |
37 DCHECK(thread_checker_.CalledOnValidThread()); | 37 DCHECK(thread_checker_.CalledOnValidThread()); |
38 IncrementRegisterCount(); | 38 IncrementRegisterCount(); |
39 if (!registered_) { | 39 if (!registered_) { |
40 DecrementRegisterCount(); | 40 DecrementRegisterCount(); |
41 return false; | 41 return false; |
42 } | 42 } |
43 | 43 |
44 // This object may be destroyed during the WatchAllHandles() call. So we have | 44 // This object may be destroyed during the Wait() call. So we have to preserve |
45 // to preserve the boolean that WatchAllHandles uses. | 45 // the boolean that Wait uses. |
46 auto destroyed = destroyed_; | 46 auto destroyed = destroyed_; |
47 const bool* should_stop_array[] = {should_stop, &destroyed->data}; | 47 const bool* should_stop_array[] = {should_stop, &destroyed->data}; |
48 bool result = registry_->WatchAllHandles(should_stop_array, 2); | 48 bool result = registry_->Wait(should_stop_array, 2); |
49 | 49 |
50 // This object has been destroyed. | 50 // This object has been destroyed. |
51 if (destroyed->data) | 51 if (destroyed->data) |
52 return false; | 52 return false; |
53 | 53 |
54 DecrementRegisterCount(); | 54 DecrementRegisterCount(); |
55 return result; | 55 return result; |
56 } | 56 } |
57 | 57 |
58 void SyncHandleWatcher::IncrementRegisterCount() { | 58 void SyncHandleWatcher::IncrementRegisterCount() { |
59 register_request_count_++; | 59 register_request_count_++; |
60 if (!registered_) { | 60 if (!registered_) { |
61 registered_ = | 61 registered_ = |
62 registry_->RegisterHandle(handle_, handle_signals_, callback_); | 62 registry_->RegisterHandle(handle_, handle_signals_, callback_); |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 void SyncHandleWatcher::DecrementRegisterCount() { | 66 void SyncHandleWatcher::DecrementRegisterCount() { |
67 DCHECK_GT(register_request_count_, 0u); | 67 DCHECK_GT(register_request_count_, 0u); |
68 | 68 |
69 register_request_count_--; | 69 register_request_count_--; |
70 if (register_request_count_ == 0 && registered_) { | 70 if (register_request_count_ == 0 && registered_) { |
71 registry_->UnregisterHandle(handle_); | 71 registry_->UnregisterHandle(handle_); |
72 registered_ = false; | 72 registered_ = false; |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 } // namespace mojo | 76 } // namespace mojo |
OLD | NEW |