WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Synchronization.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7/**
8 * Generic handle for all WPI handle-based interfaces.
9 *
10 * Handle data layout:
11 * - Bits 0-23: Type-specific
12 * - Bits 24-30: Type
13 * - Bit 31: Error
14 */
15typedef unsigned int WPI_Handle; // NOLINT
16
17/** An event handle. */
18typedef WPI_Handle WPI_EventHandle; // NOLINT
19
20/** A semaphore handle. */
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/**
28 * Creates an event. Events have binary state (signaled or not signaled) and
29 * may be either automatically reset or manually reset. Automatic-reset events
30 * go to non-signaled state when a WaitForObject is woken up by the event;
31 * manual-reset events require ResetEvent() to be called to set the event to
32 * non-signaled state; if ResetEvent() is not called, any waiter on that event
33 * will immediately wake when called.
34 *
35 * @param manual_reset true for manual reset, false for automatic reset
36 * @param initial_state true to make the event initially in signaled state
37 * @return Event handle
38 */
39WPI_EventHandle WPI_CreateEvent(int manual_reset, int initial_state);
40
41/**
42 * Destroys an event. Destruction wakes up any waiters.
43 *
44 * @param handle event handle
45 */
47
48/**
49 * Sets an event to signaled state.
50 *
51 * @param handle event handle
52 */
54
55/**
56 * Sets an event to non-signaled state.
57 *
58 * @param handle event handle
59 */
61
62/**
63 * Creates a semaphore. Semaphores keep an internal counter. Releasing the
64 * semaphore increases the count. A semaphore with a non-zero count is
65 * considered signaled. When a waiter wakes up it atomically decrements the
66 * count by 1. This is generally useful in a single-supplier,
67 * multiple-consumer scenario.
68 *
69 * @param initial_count initial value for the semaphore's internal counter
70 * @param maximum_count maximum value for the semaphore's internal counter
71 * @return Semaphore handle
72 */
73WPI_SemaphoreHandle WPI_CreateSemaphore(int initial_count, int maximum_count);
74
75/**
76 * Destroys a semaphore. Destruction wakes up any waiters.
77 *
78 * @param handle semaphore handle
79 */
81
82/**
83 * Releases N counts of a semaphore.
84 *
85 * @param handle semaphore handle
86 * @param release_count amount to add to semaphore's internal counter;
87 * must be positive
88 * @param prev_count if non-null, previous count (output parameter)
89 * @return Non-zero on successful release, zero on failure (e.g. release count
90 * would exceed maximum value, or handle invalid)
91 */
92int WPI_ReleaseSemaphore(WPI_SemaphoreHandle handle, int release_count,
93 int* prev_count);
94
95/**
96 * Waits for an handle to be signaled.
97 *
98 * @param handle handle to wait on
99 * @return Non-zero if handle was signaled, zero otherwise (e.g. object was
100 * destroyed)
101 */
103
104/**
105 * Waits for an handle to be signaled, with timeout.
106 *
107 * @param handle handle to wait on
108 * @param timeout timeout in seconds
109 * @param timed_out if non-null, set to non-zero if timeout reached without
110 * handle being signaled; set to zero otherwise (output)
111 * @return Non-zero if handle was signaled, zero otherwise (e.g. object was
112 * destroyed or timed out)
113 */
114int WPI_WaitForObjectTimeout(WPI_Handle handle, double timeout, int* timed_out);
115
116/**
117 * Waits for one or more handles to be signaled.
118 *
119 * Invalid handles are treated as signaled; the returned array will have the
120 * handle error bit set for any invalid handles.
121 *
122 * @param handles array of handles to wait on
123 * @param handles_count length of the handles array
124 * @param signaled output array for storage of signaled handles; must be at
125 * least the size of the handles input array
126 * @return number of signaled handles
127 */
128int WPI_WaitForObjects(const WPI_Handle* handles, int handles_count,
129 WPI_Handle* signaled);
130
131/**
132 * Waits for one or more handles to be signaled, with timeout.
133 *
134 * Invalid handles are treated as signaled; the returned array will have the
135 * handle error bit set for any invalid handles.
136 *
137 * @param handles array of handles to wait on
138 * @param handles_count length of the handles array
139 * @param signaled output array for storage of signaled handles; must be at
140 * least the size of the handles input array
141 * @param timeout timeout in seconds
142 * @param timed_out if non-null, set to non-zero if timeout reached without any
143 * handle being signaled; set to zero otherwise (output)
144 * @return number of signaled handles
145 */
146int WPI_WaitForObjectsTimeout(const WPI_Handle* handles, int handles_count,
147 WPI_Handle* signaled, double timeout,
148 int* timed_out);
149
150/**
151 * Sets up signaling for an arbitrary handle. With this function, any handle
152 * can operate like an event handle.
153 *
154 * @param handle handle
155 * @param manual_reset true for manual reset, false for automatic reset
156 * @param initial_state true to make the handle initially in signaled state
157 */
158void WPI_CreateSignalObject(WPI_Handle handle, int manual_reset,
159 int initial_state);
160
161/**
162 * Sets a handle to signaled state.
163 *
164 * @param handle handle
165 */
167
168/**
169 * Sets a handle to non-signaled state.
170 *
171 * @param handle handle
172 */
174
175/**
176 * Cleans up signaling for a handle. Destruction wakes up any waiters.
177 *
178 * @param handle handle
179 */
181
182#ifdef __cplusplus
183} // extern "C"
184#endif
void WPI_DestroySemaphore(WPI_SemaphoreHandle handle)
Destroys a semaphore.
int WPI_WaitForObjectsTimeout(const WPI_Handle *handles, int handles_count, WPI_Handle *signaled, double timeout, int *timed_out)
Waits for one or more handles to be signaled, with timeout.
WPI_SemaphoreHandle WPI_CreateSemaphore(int initial_count, int maximum_count)
Creates a semaphore.
void WPI_SetSignalObject(WPI_Handle handle)
Sets a handle to signaled state.
int WPI_ReleaseSemaphore(WPI_SemaphoreHandle handle, int release_count, int *prev_count)
Releases N counts of a semaphore.
void WPI_ResetSignalObject(WPI_Handle handle)
Sets a handle to non-signaled state.
WPI_Handle WPI_SemaphoreHandle
A semaphore handle.
Definition Synchronization.h:21
unsigned int WPI_Handle
Generic handle for all WPI handle-based interfaces.
Definition Synchronization.h:15
int WPI_WaitForObjectTimeout(WPI_Handle handle, double timeout, int *timed_out)
Waits for an handle to be signaled, with timeout.
WPI_Handle WPI_EventHandle
An event handle.
Definition Synchronization.h:18
void WPI_CreateSignalObject(WPI_Handle handle, int manual_reset, int initial_state)
Sets up signaling for an arbitrary handle.
void WPI_DestroyEvent(WPI_EventHandle handle)
Destroys an event.
WPI_EventHandle WPI_CreateEvent(int manual_reset, int initial_state)
Creates an event.
void WPI_DestroySignalObject(WPI_Handle handle)
Cleans up signaling for a handle.
void WPI_ResetEvent(WPI_EventHandle handle)
Sets an event to non-signaled state.
int WPI_WaitForObject(WPI_Handle handle)
Waits for an handle to be signaled.
void WPI_SetEvent(WPI_EventHandle handle)
Sets an event to signaled state.
int WPI_WaitForObjects(const WPI_Handle *handles, int handles_count, WPI_Handle *signaled)
Waits for one or more handles to be signaled.