CTRE Phoenix 6 C++ 26.0.0-beta-1
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#include <span>
13#include <vector>
14
15namespace ctre {
16namespace phoenix {
17namespace threading {
18 /**
19 * Manual Event object, useful for signaling
20 * other threads about an event.
21 */
23 private:
24 size_t _signal;
25 mutable std::mutex _m;
26 mutable std::condition_variable _cv;
27 mutable std::vector<ManualEvent *> _anyEvents;
28
29 public:
30 /**
31 * \param initialSignal whether this event defaults to signaled
32 */
33 ManualEvent(bool initialSignal = false);
34
35 /**
36 * Wait for event to be signaled, or for timeout.
37 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
38 * \returns true if event is signaled, false if timed out
39 */
40 bool WaitForSignal(int timeoutMs) const;
41
42 /**
43 * Waits for all events to be signaled, or for timeout.
44 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
45 * \param minTimeoutMs when timeoutMs > 0, minimum amount of time to wait for
46 * a signal; this can be used to ensure that a non-zero
47 * timeout is used for every signal; typically 0 or 1 ms
48 * \param events comma-separated list of event references to wait on
49 * \returns true if all events are signaled, false if timed out
50 */
51 template <std::same_as<ManualEvent>... Events>
52 static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, Events const &... events)
53 {
54 return WaitForAllSignals(timeoutMs, minTimeoutMs, std::array<ManualEvent const *, sizeof...(Events)>{(&events)...});
55 }
56
57 /**
58 * Waits for all events to be signaled, or for timeout.
59 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
60 * \param minTimeoutMs when timeoutMs > 0, minimum amount of time to wait for
61 * a signal; this can be used to ensure that a non-zero
62 * timeout is used for every signal; typically 0 or 1 ms
63 * \param events a span of event pointers to wait on
64 * \returns true if all events are signaled, false if timed out
65 */
66 static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, std::span<ManualEvent const* const> events);
67
68 /**
69 * Waits for any event to be signaled, or for timeout.
70 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
71 * \param events comma-separated list of event references to wait on
72 * \returns true if any event is signaled, false if timed out
73 */
74 template <std::same_as<ManualEvent>... Events>
75 static bool WaitForAnySignal(int timeoutMs, Events const &... events)
76 {
77 return WaitForAnySignal(timeoutMs, std::array<ManualEvent const *, sizeof...(Events)>{(&events)...});
78 }
79
80 /**
81 * Waits for any event to be signaled, or for timeout.
82 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
83 * \param events a span of event pointers to wait on
84 * \returns true if any event is signaled, false if timed out
85 */
86 static bool WaitForAnySignal(int timeoutMs, std::span<ManualEvent const* const> events);
87
88 /**
89 * Signal the event. Any threads waiting on WaitForSignal() will return true.
90 */
91 void Signal();
92 /**
93 * Clear the event signal without signaling other threads.
94 */
95 void Clear();
96 /**
97 * Reset the event signal to false and force any threads waiting on WaitForSignal() to time out.
98 */
99 void Reset();
100 };
101}
102}
103}
Manual Event object, useful for signaling other threads about an event.
Definition ManualEvent.hpp:22
static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, Events const &... events)
Waits for all events to be signaled, or for timeout.
Definition ManualEvent.hpp:52
void Reset()
Reset the event signal to false and force any threads waiting on WaitForSignal() to time out.
void Clear()
Clear the event signal without signaling other threads.
void Signal()
Signal the event.
static bool WaitForAnySignal(int timeoutMs, Events const &... events)
Waits for any event to be signaled, or for timeout.
Definition ManualEvent.hpp:75
ManualEvent(bool initialSignal=false)
static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, std::span< ManualEvent const *const > events)
Waits for all events to be signaled, or for timeout.
static bool WaitForAnySignal(int timeoutMs, std::span< ManualEvent const *const > events)
Waits for any event to be signaled, or for timeout.
bool WaitForSignal(int timeoutMs) const
Wait for event to be signaled, or for timeout.
#define CTREXPORT
Definition export.h:14
Definition motor_constants.h:14