Flutter Engine
The Flutter Engine
semaphore_unittest.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <thread>
6
7#include "flutter/fml/synchronization/semaphore.h"
8#include "flutter/fml/thread.h"
9#include "flutter/fml/time/time_point.h"
10#include "gtest/gtest.h"
11
12TEST(SemaphoreTest, SimpleValidity) {
13 fml::Semaphore sem(100);
14 ASSERT_TRUE(sem.IsValid());
15}
16
17TEST(SemaphoreTest, WaitOnZero) {
18 fml::Semaphore sem(0);
19 ASSERT_FALSE(sem.TryWait());
20}
21
22TEST(SemaphoreTest, WaitOnZeroSignalThenWait) {
23 fml::Semaphore sem(0);
24 ASSERT_FALSE(sem.TryWait());
25 std::thread thread([&sem]() { sem.Signal(); });
26 thread.join();
27 ASSERT_TRUE(sem.TryWait());
28 ASSERT_FALSE(sem.TryWait());
29}
30
31TEST(SemaphoreTest, IndefiniteWait) {
33 constexpr double wait_in_seconds = 0.25;
34 fml::Semaphore sem(0);
35 ASSERT_TRUE(sem.IsValid());
36 fml::Thread signaller("signaller_thread");
37 signaller.GetTaskRunner()->PostTaskForTime(
38 [&sem]() { sem.Signal(); },
39 start + fml::TimeDelta::FromSecondsF(wait_in_seconds));
40 ASSERT_TRUE(sem.Wait());
42 ASSERT_GE(delta.ToSecondsF(), wait_in_seconds);
43 signaller.Join();
44}
A traditional counting semaphore. Waits decrement the counter and Signal increments it.
Definition: semaphore.h:26
bool IsValid() const
Check if the underlying semaphore handle could be created. Failure modes are platform specific and ma...
Definition: semaphore.cc:177
void Signal()
Increment the count by one. Any pending Waits will be resolved at this point.
Definition: semaphore.cc:189
bool TryWait()
Decrement the counts if it is greater than zero. Returns false if the counter is already at zero.
Definition: semaphore.cc:185
bool Wait()
Decrements the count and waits indefinitely if the value is less than zero for a Signal.
Definition: semaphore.cc:181
virtual void PostTaskForTime(const fml::closure &task, fml::TimePoint target_time)
Definition: task_runner.cc:28
void Join()
Definition: thread.cc:168
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
Definition: thread.cc:164
static constexpr TimeDelta FromSecondsF(double seconds)
Definition: time_delta.h:53
static TimePoint Now()
Definition: time_point.cc:49
TEST(SemaphoreTest, SimpleValidity)