forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CountDownLatch to fml with tests. (flutter#6574)
- Loading branch information
1 parent
705738f
commit 979de07
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2018 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/synchronization/count_down_latch.h" | ||
|
||
#include "flutter/fml/logging.h" | ||
|
||
namespace fml { | ||
|
||
CountDownLatch::CountDownLatch(size_t count) : count_(count) { | ||
if (count_ == 0) { | ||
waitable_event_.Signal(); | ||
} | ||
} | ||
|
||
CountDownLatch::~CountDownLatch() = default; | ||
|
||
void CountDownLatch::Wait() { | ||
waitable_event_.Wait(); | ||
} | ||
|
||
void CountDownLatch::CountDown() { | ||
if (--count_ == 0) { | ||
waitable_event_.Signal(); | ||
} | ||
} | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2018 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include <atomic> | ||
|
||
#include "flutter/fml/macros.h" | ||
#include "flutter/fml/synchronization/waitable_event.h" | ||
|
||
namespace fml { | ||
|
||
class CountDownLatch { | ||
public: | ||
CountDownLatch(size_t count); | ||
|
||
~CountDownLatch(); | ||
|
||
void Wait(); | ||
|
||
void CountDown(); | ||
|
||
private: | ||
std::atomic_size_t count_; | ||
ManualResetWaitableEvent waitable_event_; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(CountDownLatch); | ||
}; | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2018 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include <chrono> | ||
#include <thread> | ||
|
||
#include "flutter/fml/synchronization/count_down_latch.h" | ||
#include "flutter/fml/thread.h" | ||
#include "flutter/testing/testing.h" | ||
|
||
namespace fml { | ||
|
||
TEST(CountDownLatchTest, CanWaitOnZero) { | ||
CountDownLatch latch(0); | ||
latch.Wait(); | ||
} | ||
|
||
TEST(CountDownLatchTest, CanWait) { | ||
fml::Thread thread("test_thread"); | ||
const size_t count = 100; | ||
size_t current_count = 0; | ||
CountDownLatch latch(count); | ||
auto decrement_latch_on_thread = [runner = thread.GetTaskRunner(), &latch, | ||
¤t_count]() { | ||
runner->PostTask([&latch, ¤t_count]() { | ||
std::this_thread::sleep_for(std::chrono::microseconds(100)); | ||
current_count++; | ||
latch.CountDown(); | ||
}); | ||
}; | ||
for (size_t i = 0; i < count; ++i) { | ||
decrement_latch_on_thread(); | ||
} | ||
latch.Wait(); | ||
ASSERT_EQ(current_count, count); | ||
} | ||
|
||
} // namespace fml |