| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_THREAD_BARRIER_H_ | 5 #ifndef VM_THREAD_BARRIER_H_ |
| 6 #define VM_THREAD_BARRIER_H_ | 6 #define VM_THREAD_BARRIER_H_ |
| 7 | 7 |
| 8 #include "vm/globals.h" | 8 #include "vm/globals.h" |
| 9 #include "vm/os_thread.h" | 9 #include "vm/os_thread.h" |
| 10 #include "vm/lockers.h" | 10 #include "vm/lockers.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // /* Idle while tasks are working */ fooWork(); barWork(); | 39 // /* Idle while tasks are working */ fooWork(); barWork(); |
| 40 // barrier.Sync(); barrier_->Sync(); barrier_->Sync(); | 40 // barrier.Sync(); barrier_->Sync(); barrier_->Sync(); |
| 41 // collectResultsFromTasks(); barrier_->Exit(); barrier_->Exit(); | 41 // collectResultsFromTasks(); barrier_->Exit(); barrier_->Exit(); |
| 42 // barrier.Exit(); | 42 // barrier.Exit(); |
| 43 // | 43 // |
| 44 // Note that the calls to Sync() "line up" in time, but there is no such | 44 // Note that the calls to Sync() "line up" in time, but there is no such |
| 45 // guarantee for Exit(). | 45 // guarantee for Exit(). |
| 46 // | 46 // |
| 47 class ThreadBarrier { | 47 class ThreadBarrier { |
| 48 public: | 48 public: |
| 49 explicit ThreadBarrier(intptr_t num_threads) | 49 explicit ThreadBarrier(intptr_t num_threads, |
| 50 Monitor* monitor, |
| 51 Monitor* done_monitor) |
| 50 : num_threads_(num_threads), | 52 : num_threads_(num_threads), |
| 53 monitor_(monitor), |
| 51 remaining_(num_threads), | 54 remaining_(num_threads), |
| 52 parity_(false), | 55 parity_(false), |
| 56 done_monitor_(done_monitor), |
| 53 done_(false) { | 57 done_(false) { |
| 54 ASSERT(remaining_ > 0); | 58 ASSERT(remaining_ > 0); |
| 55 } | 59 } |
| 56 | 60 |
| 57 void Sync() { | 61 void Sync() { |
| 58 MonitorLocker ml(&monitor_); | 62 MonitorLocker ml(monitor_); |
| 59 ASSERT(remaining_ > 0); | 63 ASSERT(remaining_ > 0); |
| 60 if (--remaining_ > 0) { | 64 if (--remaining_ > 0) { |
| 61 // I'm not last to arrive; wait until next round. | 65 // I'm not last to arrive; wait until next round. |
| 62 bool old_parity = parity_; | 66 bool old_parity = parity_; |
| 63 while (parity_ == old_parity) { | 67 while (parity_ == old_parity) { |
| 64 ml.Wait(); | 68 ml.Wait(); |
| 65 } | 69 } |
| 66 } else { | 70 } else { |
| 67 // Last one to arrive initiates the next round. | 71 // Last one to arrive initiates the next round. |
| 68 remaining_ = num_threads_; | 72 remaining_ = num_threads_; |
| 69 parity_ = !parity_; | 73 parity_ = !parity_; |
| 70 // Tell everyone else about the new round. | 74 // Tell everyone else about the new round. |
| 71 ml.NotifyAll(); | 75 ml.NotifyAll(); |
| 72 } | 76 } |
| 73 } | 77 } |
| 74 | 78 |
| 75 void Exit() { | 79 void Exit() { |
| 76 bool last = false; | 80 bool last = false; |
| 77 { | 81 { |
| 78 MonitorLocker ml(&monitor_); | 82 MonitorLocker ml(monitor_); |
| 79 ASSERT(remaining_ > 0); | 83 ASSERT(remaining_ > 0); |
| 80 last = (--remaining_ == 0); | 84 last = (--remaining_ == 0); |
| 81 } | 85 } |
| 82 if (last) { | 86 if (last) { |
| 83 // Last one to exit sets done_. | 87 // Last one to exit sets done_. |
| 84 MonitorLocker ml(&done_monitor_); | 88 MonitorLocker ml(done_monitor_); |
| 85 ASSERT(!done_); | 89 ASSERT(!done_); |
| 86 done_ = true; | 90 done_ = true; |
| 87 // Tell the destructor in case it's already waiting. | 91 // Tell the destructor in case it's already waiting. |
| 88 ml.Notify(); | 92 ml.Notify(); |
| 89 } | 93 } |
| 90 } | 94 } |
| 91 | 95 |
| 92 ~ThreadBarrier() { | 96 ~ThreadBarrier() { |
| 93 MonitorLocker ml(&done_monitor_); | 97 MonitorLocker ml(done_monitor_); |
| 94 // Wait for everyone to exit before destroying the monitors. | 98 // Wait for everyone to exit before destroying the monitors. |
| 95 while (!done_) { | 99 while (!done_) { |
| 96 ml.Wait(); | 100 ml.Wait(); |
| 97 } | 101 } |
| 98 ASSERT(remaining_ == 0); | 102 ASSERT(remaining_ == 0); |
| 99 } | 103 } |
| 100 | 104 |
| 101 private: | 105 private: |
| 102 const intptr_t num_threads_; | 106 const intptr_t num_threads_; |
| 103 | 107 |
| 104 Monitor monitor_; | 108 Monitor* monitor_; |
| 105 intptr_t remaining_; | 109 intptr_t remaining_; |
| 106 bool parity_; | 110 bool parity_; |
| 107 | 111 |
| 108 Monitor done_monitor_; // TODO(koda): Try to optimize this away. | 112 Monitor* done_monitor_; // TODO(koda): Try to optimize this away. |
| 109 bool done_; | 113 bool done_; |
| 110 | 114 |
| 111 DISALLOW_COPY_AND_ASSIGN(ThreadBarrier); | 115 DISALLOW_COPY_AND_ASSIGN(ThreadBarrier); |
| 112 }; | 116 }; |
| 113 | 117 |
| 114 } // namespace dart | 118 } // namespace dart |
| 115 | 119 |
| 116 #endif // VM_THREAD_BARRIER_H_ | 120 #endif // VM_THREAD_BARRIER_H_ |
| OLD | NEW |