CTRE Phoenix 6 C++ 26.70.0-alpha-2
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 _signal{initialSignal ? (size_t)1 : (size_t)0}
35 {}
36
37 /**
38 * Wait for event to be signaled, or for timeout.
39 *
40 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
41 * \returns true if event is signaled, false if timed out
42 */
43 bool WaitForSignal(int timeoutMs) const;
44
45 /**
46 * Waits for all events to be signaled, or for timeout.
47 *
48 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
49 * \param minTimeoutMs when timeoutMs > 0, minimum amount of time to wait for
50 * a signal; this can be used to ensure that a non-zero
51 * timeout is used for every signal; typically 0 or 1 ms
52 * \param events comma-separated list of event references to wait on
53 * \returns true if all events are signaled, false if timed out
54 */
55 template <std::same_as<ManualEvent>... Events>
56 static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, Events const &... events)
57 {
58 return WaitForAllSignals(timeoutMs, minTimeoutMs, std::array<ManualEvent const *, sizeof...(Events)>{(&events)...});
59 }
60
61 /**
62 * Waits for all events to be signaled, or for timeout.
63 *
64 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
65 * \param minTimeoutMs when timeoutMs > 0, minimum amount of time to wait for
66 * a signal; this can be used to ensure that a non-zero
67 * timeout is used for every signal; typically 0 or 1 ms
68 * \param events a span of event pointers to wait on
69 * \returns true if all events are signaled, false if timed out
70 */
71 static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, std::span<ManualEvent const* const> events);
72
73 /**
74 * Waits for any event to be signaled, or for timeout.
75 *
76 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
77 * \param events comma-separated list of event references to wait on
78 * \returns true if any event is signaled, false if timed out
79 */
80 template <std::same_as<ManualEvent>... Events>
81 static bool WaitForAnySignal(int timeoutMs, Events const &... events)
82 {
83 return WaitForAnySignal(timeoutMs, std::array<ManualEvent const *, sizeof...(Events)>{(&events)...});
84 }
85
86 /**
87 * Waits for any event to be signaled, or for timeout.
88 *
89 * \param timeoutMs timeout of the wait; -1 is indefinite, 0 is a fast check
90 * \param events a span of event pointers to wait on
91 * \returns true if any event is signaled, false if timed out
92 */
93 static bool WaitForAnySignal(int timeoutMs, std::span<ManualEvent const* const> events);
94
95 /**
96 * Signal the event. Any threads waiting on WaitForSignal() will return true.
97 */
98 void Signal();
99 /**
100 * Clear the event signal without signaling other threads.
101 */
102 void Clear();
103 /**
104 * Reset the event signal to false and force any threads waiting on WaitForSignal() to time out.
105 */
106 void Reset();
107 };
108}
109}
110}
static bool WaitForAllSignals(int timeoutMs, int minTimeoutMs, Events const &... events)
Waits for all events to be signaled, or for timeout.
Definition ManualEvent.hpp:56
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:81
ManualEvent(bool initialSignal=false)
Definition ManualEvent.hpp:33
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:16
Definition ManualEvent.hpp:17
Definition StatusCodes.h:22
Definition motor_constants.h:14