[wpiutil] Add synchronization primitives

These enable more consistent use of synchronization across the
native libraries.  Users can create Event and Semaphore primitives, but
in addition, libraries can set up any handle as an Event-type signal.
This commit is contained in:
Peter Johnson
2021-08-03 00:05:47 -07:00
parent e32499c546
commit 87e34967ef
7 changed files with 1424 additions and 1 deletions

View File

@@ -0,0 +1,56 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpi/Synchronization.h" // NOLINT(build/include_order)
#include <thread>
#include "gtest/gtest.h"
TEST(EventTest, AutoReset) {
auto event = wpi::CreateEvent(false, false);
std::thread thr([&] { wpi::SetEvent(event); });
wpi::WaitForObject(event);
thr.join();
bool timedOut;
wpi::WaitForObject(event, 0, &timedOut);
ASSERT_EQ(timedOut, true);
}
TEST(EventTest, ManualReset) {
auto event = wpi::CreateEvent(true, false);
int done = 0;
std::thread thr([&] {
wpi::SetEvent(event);
++done;
});
wpi::WaitForObject(event);
thr.join();
ASSERT_EQ(done, 1);
bool timedOut;
wpi::WaitForObject(event, 0, &timedOut);
ASSERT_EQ(timedOut, false);
}
TEST(EventTest, InitialSet) {
auto event = wpi::CreateEvent(false, true);
bool timedOut;
wpi::WaitForObject(event, 0, &timedOut);
ASSERT_EQ(timedOut, false);
}
TEST(EventTest, WaitMultiple) {
auto event1 = wpi::CreateEvent(false, false);
auto event2 = wpi::CreateEvent(false, false);
std::thread thr([&] { wpi::SetEvent(event2); });
WPI_Handle signaled[2];
auto result1 = wpi::WaitForObjects({event1, event2}, signaled);
thr.join();
ASSERT_EQ(result1.size(), 1u);
ASSERT_EQ(result1[0], event2);
bool timedOut;
auto result2 = wpi::WaitForObjects({event1, event2}, signaled, 0, &timedOut);
ASSERT_EQ(timedOut, true);
ASSERT_EQ(result2.size(), 0u);
}