CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
ManualEvent.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
10#include <array>
11#include <condition_variable>
12
13namespace ctre {
14namespace phoenix {
15namespace threading {
16 /**
17 * Manual Event object, useful for signaling
18 * other threads about an event.
19 */
21 private:
22 size_t _signal;
23 mutable std::mutex _m;
24 mutable std::condition_variable _cv;
25
26 public:
27 /**
28 * \param initialSignal whether this event defaults to signaled
29 */
30 ManualEvent(bool initialSignal = false);
31
32 /**
33 * Wait for event to be signaled, or for timeout.
34 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
35 * \returns true if event is signaled, false if timed out
36 */
37 bool WaitForSignal(int timeoutMs) const;
38
39 /**
40 * Waits for all events to be signaled, or for timeout.
41 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
42 * \param minTimeoutMs when timeoutMs > 0, minimum amount of time to wait for
43 * a signal; this can be used to ensure that a non-zero
44 * timeout is used for every signal; typically 0 or 1 ms
45 * \param events comma-separated list of event references to wait on
46 * \returns true if all events are signaled, false if timed out
47 */
48 template < typename... Event, typename = std::enable_if_t<std::conjunction_v<std::is_same<Event, ManualEvent>...>> >
49 static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, Event &... events)
50 {
51 std::array eventsArr{(&events)...};
52 return WaitForAllSignals(timeoutMs, minTimeoutMs, eventsArr.data(), eventsArr.size());
53 }
54
55 /**
56 * Waits for all events to be signaled, or for timeout.
57 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
58 * \param minTimeoutMs when timeoutMs > 0, minimum amount of time to wait for
59 * a signal; this can be used to ensure that a non-zero
60 * timeout is used for every signal; typically 0 or 1 ms
61 * \param events pointer to an array of event pointers to wait on
62 * \param eventCnt number of events in the array
63 * \returns true if all events are signaled, false if timed out
64 */
65 static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, ManualEvent *const *events, size_t eventCnt);
66
67 /**
68 * Signal the event. Any threads waiting on WaitForSignal() will return true.
69 */
70 void Signal();
71 /**
72 * Clear the event signal without signaling other threads.
73 */
74 void Clear();
75 /**
76 * Reset the event signal to false and force any threads waiting on WaitForSignal() to time out.
77 */
78 void Reset();
79 };
80}
81}
82}
Manual Event object, useful for signaling other threads about an event.
Definition ManualEvent.hpp:20
static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, Event &... events)
Waits for all events to be signaled, or for timeout.
Definition ManualEvent.hpp:49
void Reset()
Reset the event signal to false and force any threads waiting on WaitForSignal() to time out.
static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, ManualEvent *const *events, size_t eventCnt)
Waits for all events to be signaled, or for timeout.
void Clear()
Clear the event signal without signaling other threads.
void Signal()
Signal the event.
ManualEvent(bool initialSignal=false)
bool WaitForSignal(int timeoutMs) const
Wait for event to be signaled, or for timeout.
#define CTREXPORT
Definition export.h:14
Definition StatusCodes.h:18