]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/barrier.c
b89167d415d5e8d50fe09923b33cf666307a25cf
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
12 #include <sys/eventfd.h>
13 #include <sys/types.h>
22 * This barrier implementation provides a simple synchronization method based
23 * on file-descriptors that can safely be used between threads and processes. A
24 * barrier object contains 2 shared counters based on eventfd. Both processes
25 * can now place barriers and wait for the other end to reach a random or
27 * Barriers are numbered, so you can either wait for the other end to reach any
28 * barrier or the last barrier that you placed. This way, you can use barriers
29 * for one-way *and* full synchronization. Note that even-though barriers are
30 * numbered, these numbers are internal and recycled once both sides reached the
31 * same barrier (implemented as a simple signed counter). It is thus not
32 * possible to address barriers by their ID.
34 * Barrier-API: Both ends can place as many barriers via barrier_place() as
35 * they want and each pair of barriers on both sides will be implicitly linked.
36 * Each side can use the barrier_wait/sync_*() family of calls to wait for the
37 * other side to place a specific barrier. barrier_wait_next() waits until the
38 * other side calls barrier_place(). No links between the barriers are
39 * considered and this simply serves as most basic asynchronous barrier.
40 * barrier_sync_next() is like barrier_wait_next() and waits for the other side
41 * to place their next barrier via barrier_place(). However, it only waits for
42 * barriers that are linked to a barrier we already placed. If the other side
43 * already placed more barriers than we did, barrier_sync_next() returns
45 * barrier_sync() extends barrier_sync_next() and waits until the other end
46 * placed as many barriers via barrier_place() as we did. If they already placed
47 * as many as we did (or more), it returns immediately.
49 * Additionally to basic barriers, an abortion event is available.
50 * barrier_abort() places an abortion event that cannot be undone. An abortion
51 * immediately cancels all placed barriers and replaces them. Any running and
52 * following wait/sync call besides barrier_wait_abortion() will immediately
53 * return false on both sides (otherwise, they always return true).
54 * barrier_abort() can be called multiple times on both ends and will be a
55 * no-op if already called on this side.
56 * barrier_wait_abortion() can be used to wait for the other side to call
57 * barrier_abort() and is the only wait/sync call that does not return
58 * immediately if we aborted outself. It only returns once the other side
59 * called barrier_abort().
61 * Barriers can be used for in-process and inter-process synchronization.
62 * However, for in-process synchronization you could just use mutexes.
63 * Therefore, main target is IPC and we require both sides to *not* share the FD
64 * table. If that's given, barriers provide target tracking: If the remote side
65 * exit()s, an abortion event is implicitly queued on the other side. This way,
66 * a sync/wait call will be woken up if the remote side crashed or exited
67 * unexpectedly. However, note that these abortion events are only queued if the
68 * barrier-queue has been drained. Therefore, it is safe to place a barrier and
69 * exit. The other side can safely wait on the barrier even though the exit
70 * queued an abortion event. Usually, the abortion event would overwrite the
71 * barrier, however, that's not true for exit-abortion events. Those are only
72 * queued if the barrier-queue is drained (thus, the receiving side has placed
73 * more barriers than the remote side).
77 * barrier_create() - Initialize a barrier object
78 * @obj: barrier to initialize
80 * This initializes a barrier object. The caller is responsible of allocating
81 * the memory and keeping it valid. The memory does not have to be zeroed
83 * Two eventfd objects are allocated for each barrier. If allocation fails, an
86 * If this function fails, the barrier is reset to an invalid state so it is
87 * safe to call barrier_destroy() on the object regardless whether the
88 * initialization succeeded or not.
90 * The caller is responsible to destroy the object via barrier_destroy() before
91 * releasing the underlying memory.
93 * Returns: 0 on success, negative error code on failure.
95 int barrier_create(Barrier
*b
) {
96 _cleanup_(barrier_destroyp
) Barrier
*staging
= b
;
101 b
->me
= eventfd(0, EFD_CLOEXEC
| EFD_NONBLOCK
);
105 b
->them
= eventfd(0, EFD_CLOEXEC
| EFD_NONBLOCK
);
109 r
= pipe2(b
->pipe
, O_CLOEXEC
| O_NONBLOCK
);
118 * barrier_destroy() - Destroy a barrier object
119 * @b: barrier to destroy or NULL
121 * This destroys a barrier object that has previously been passed to
122 * barrier_create(). The object is released and reset to invalid
123 * state. Therefore, it is safe to call barrier_destroy() multiple
124 * times or even if barrier_create() failed. However, barrier must be
125 * always initialized with BARRIER_NULL.
127 * If @b is NULL, this is a no-op.
129 void barrier_destroy(Barrier
*b
) {
133 b
->me
= safe_close(b
->me
);
134 b
->them
= safe_close(b
->them
);
135 safe_close_pair(b
->pipe
);
140 * barrier_set_role() - Set the local role of the barrier
141 * @b: barrier to operate on
142 * @role: role to set on the barrier
144 * This sets the roles on a barrier object. This is needed to know
145 * which side of the barrier you're on. Usually, the parent creates
146 * the barrier via barrier_create() and then calls fork() or clone().
147 * Therefore, the FDs are duplicated and the child retains the same
150 * Both sides need to call barrier_set_role() after fork() or clone()
151 * are done. If this is not done, barriers will not work correctly.
153 * Note that barriers could be supported without fork() or clone(). However,
154 * this is currently not needed so it hasn't been implemented.
156 void barrier_set_role(Barrier
*b
, unsigned int role
) {
160 assert(IN_SET(role
, BARRIER_PARENT
, BARRIER_CHILD
));
161 /* make sure this is only called once */
162 assert(b
->pipe
[0] >= 0 && b
->pipe
[1] >= 0);
164 if (role
== BARRIER_PARENT
)
165 b
->pipe
[1] = safe_close(b
->pipe
[1]);
167 b
->pipe
[0] = safe_close(b
->pipe
[0]);
169 /* swap me/them for children */
176 /* places barrier; returns false if we aborted, otherwise true */
177 static bool barrier_write(Barrier
*b
, uint64_t buf
) {
180 /* prevent new sync-points if we already aborted */
181 if (barrier_i_aborted(b
))
186 len
= write(b
->me
, &buf
, sizeof(buf
));
187 } while (len
< 0 && IN_SET(errno
, EAGAIN
, EINTR
));
189 if (len
!= sizeof(buf
))
192 /* lock if we aborted */
193 if (buf
>= (uint64_t)BARRIER_ABORTION
) {
194 if (barrier_they_aborted(b
))
195 b
->barriers
= BARRIER_WE_ABORTED
;
197 b
->barriers
= BARRIER_I_ABORTED
;
198 } else if (!barrier_is_aborted(b
))
201 return !barrier_i_aborted(b
);
204 /* If there is an unexpected error, we have to make this fatal. There
205 * is no way we can recover from sync-errors. Therefore, we close the
206 * pipe-ends and treat this as abortion. The other end will notice the
207 * pipe-close and treat it as abortion, too. */
209 safe_close_pair(b
->pipe
);
210 b
->barriers
= BARRIER_WE_ABORTED
;
214 /* waits for barriers; returns false if they aborted, otherwise true */
215 static bool barrier_read(Barrier
*b
, int64_t comp
) {
216 if (barrier_they_aborted(b
))
219 while (b
->barriers
> comp
) {
220 struct pollfd pfd
[2] = {
221 { .fd
= b
->pipe
[0] >= 0 ? b
->pipe
[0] : b
->pipe
[1],
228 r
= poll(pfd
, 2, -1);
229 if (r
< 0 && IN_SET(errno
, EAGAIN
, EINTR
))
234 if (pfd
[1].revents
) {
237 /* events on @them signal new data for us */
238 len
= read(b
->them
, &buf
, sizeof(buf
));
239 if (len
< 0 && IN_SET(errno
, EAGAIN
, EINTR
))
242 if (len
!= sizeof(buf
))
244 } else if (pfd
[0].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
245 /* POLLHUP on the pipe tells us the other side exited.
246 * We treat this as implicit abortion. But we only
247 * handle it if there's no event on the eventfd. This
248 * guarantees that exit-abortions do not overwrite real
250 buf
= BARRIER_ABORTION
;
254 /* lock if they aborted */
255 if (buf
>= (uint64_t)BARRIER_ABORTION
) {
256 if (barrier_i_aborted(b
))
257 b
->barriers
= BARRIER_WE_ABORTED
;
259 b
->barriers
= BARRIER_THEY_ABORTED
;
260 } else if (!barrier_is_aborted(b
))
264 return !barrier_they_aborted(b
);
267 /* If there is an unexpected error, we have to make this fatal. There
268 * is no way we can recover from sync-errors. Therefore, we close the
269 * pipe-ends and treat this as abortion. The other end will notice the
270 * pipe-close and treat it as abortion, too. */
272 safe_close_pair(b
->pipe
);
273 b
->barriers
= BARRIER_WE_ABORTED
;
278 * barrier_place() - Place a new barrier
281 * This places a new barrier on the barrier object. If either side already
282 * aborted, this is a no-op and returns "false". Otherwise, the barrier is
283 * placed and this returns "true".
285 * Returns: true if barrier was placed, false if either side aborted.
287 bool barrier_place(Barrier
*b
) {
290 if (barrier_is_aborted(b
))
293 barrier_write(b
, BARRIER_SINGLE
);
298 * barrier_abort() - Abort the synchronization
299 * @b: barrier object to abort
301 * This aborts the barrier-synchronization. If barrier_abort() was already
302 * called on this side, this is a no-op. Otherwise, the barrier is put into the
303 * ABORT-state and will stay there. The other side is notified about the
304 * abortion. Any following attempt to place normal barriers or to wait on normal
305 * barriers will return immediately as "false".
307 * You can wait for the other side to call barrier_abort(), too. Use
308 * barrier_wait_abortion() for that.
310 * Returns: false if the other side already aborted, true otherwise.
312 bool barrier_abort(Barrier
*b
) {
315 barrier_write(b
, BARRIER_ABORTION
);
316 return !barrier_they_aborted(b
);
320 * barrier_wait_next() - Wait for the next barrier of the other side
321 * @b: barrier to operate on
323 * This waits until the other side places its next barrier. This is independent
324 * of any barrier-links and just waits for any next barrier of the other side.
326 * If either side aborted, this returns false.
328 * Returns: false if either side aborted, true otherwise.
330 bool barrier_wait_next(Barrier
*b
) {
333 if (barrier_is_aborted(b
))
336 barrier_read(b
, b
->barriers
- 1);
337 return !barrier_is_aborted(b
);
341 * barrier_wait_abortion() - Wait for the other side to abort
342 * @b: barrier to operate on
344 * This waits until the other side called barrier_abort(). This can be called
345 * regardless whether the local side already called barrier_abort() or not.
347 * If the other side has already aborted, this returns immediately.
349 * Returns: false if the local side aborted, true otherwise.
351 bool barrier_wait_abortion(Barrier
*b
) {
354 barrier_read(b
, BARRIER_THEY_ABORTED
);
355 return !barrier_i_aborted(b
);
359 * barrier_sync_next() - Wait for the other side to place a next linked barrier
360 * @b: barrier to operate on
362 * This is like barrier_wait_next() and waits for the other side to call
363 * barrier_place(). However, this only waits for linked barriers. That means, if
364 * the other side already placed more barriers than (or as much as) we did, this
365 * returns immediately instead of waiting.
367 * If either side aborted, this returns false.
369 * Returns: false if either side aborted, true otherwise.
371 bool barrier_sync_next(Barrier
*b
) {
374 if (barrier_is_aborted(b
))
377 barrier_read(b
, MAX((int64_t)0, b
->barriers
- 1));
378 return !barrier_is_aborted(b
);
382 * barrier_sync() - Wait for the other side to place as many barriers as we did
383 * @b: barrier to operate on
385 * This is like barrier_sync_next() but waits for the other side to call
386 * barrier_place() as often as we did (in total). If they already placed as much
387 * as we did (or more), this returns immediately instead of waiting.
389 * If either side aborted, this returns false.
391 * Returns: false if either side aborted, true otherwise.
393 bool barrier_sync(Barrier
*b
) {
396 if (barrier_is_aborted(b
))
400 return !barrier_is_aborted(b
);