The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
atomic_queue.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: bd00b1f00a94ca7a476aa84c001b28a19c8a90fe $
20 *
21 * @file io/atomic_queue.h
22 * @brief Thread-safe queues.
23 *
24 * @copyright 2016 Alan DeKok (aland@freeradius.org)
25 */
26RCSIDH(atomic_queue_h, "$Id: bd00b1f00a94ca7a476aa84c001b28a19c8a90fe $")
27
28
29#ifdef HAVE_STDATOMIC_H
30# include <stdatomic.h>
31#else
32# include <freeradius-devel/util/stdatomic.h>
33#endif
34#include <freeradius-devel/util/talloc.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
41
42fr_atomic_queue_t *fr_atomic_queue_talloc(TALLOC_CTX *ctx, size_t num, size_t elem_len);
43fr_atomic_queue_t *fr_atomic_queue_malloc(TALLOC_CTX *ctx, size_t num, size_t elem_len);
44fr_atomic_queue_t *fr_atomic_queue_mmap(TALLOC_CTX *ctx, int *fd, size_t num,
45 size_t elem_len) CC_HINT(nonnull(2));
47
48bool fr_atomic_queue_mpmc_push(fr_atomic_queue_t *aq, void const *data) CC_HINT(nonnull);
50
51bool fr_atomic_queue_spsc_push(fr_atomic_queue_t *aq, void const *data) CC_HINT(nonnull);
54
55/** Unbounded segmented single-producer / single-consumer queue
56 *
57 * Internally a linked list of fixed-size `fr_atomic_queue_t` segments.
58 * When the producer's current segment fills, a new segment is malloc'd
59 * and linked in; the consumer drains segments in order and frees them
60 * as it advances. Allocation on the producer side uses raw malloc
61 * (not talloc), so the producer thread is free to be one that cannot
62 * safely use talloc (e.g. a library-owned callback thread).
63 *
64 * Exactly one producer and one consumer. Not safe for MPMC use.
65 */
67
68fr_atomic_ring_t *fr_atomic_ring_alloc(TALLOC_CTX *ctx, size_t seg_size);
71bool fr_atomic_ring_pop(fr_atomic_ring_t *ring, void **p_data);
72
73#ifdef WITH_VERIFY_PTR
74void fr_atomic_queue_verify(fr_atomic_queue_t *aq);
75#endif
76
77#ifndef NDEBUG
79#endif
80
81
82#ifdef __cplusplus
83}
84#endif
size_t elem_len
Size of one element.
size_t num
Elements the queue holds.
size_t seg_size
Capacity of each segment.
Structure to hold the atomic queue.
void fr_atomic_queue_debug(FILE *fp, fr_atomic_queue_t *aq)
Dump an atomic queue.
bool fr_atomic_ring_push(fr_atomic_ring_t *ring, void *data)
Push a pointer into the ring; allocate a new segment on overflow.
fr_atomic_queue_t * fr_atomic_queue_malloc(TALLOC_CTX *ctx, size_t num, size_t elem_len)
Create fixed-size atomic queue outside the talloc hierarchy, owned by a talloc context.
fr_atomic_ring_t * fr_atomic_ring_alloc(TALLOC_CTX *ctx, size_t seg_size)
Allocate an empty SPSC ring.
bool fr_atomic_queue_mpmc_pop(fr_atomic_queue_t *aq, void *data)
Copy an element out of the atomic queue, any number of consumers.
bool fr_atomic_ring_pop(fr_atomic_ring_t *ring, void **p_data)
Pop a pointer from the ring, advancing past drained segments.
void fr_atomic_ring_free(fr_atomic_ring_t **ring)
Free the ring and all remaining segments.
size_t fr_atomic_queue_size(fr_atomic_queue_t *aq)
bool fr_atomic_queue_mpmc_push(fr_atomic_queue_t *aq, void const *data)
Copy an element into the atomic queue, any number of producers.
void fr_atomic_queue_free(fr_atomic_queue_t **aq)
Free an atomic queue if it's not freed by ctx.
bool fr_atomic_queue_spsc_push(fr_atomic_queue_t *aq, void const *data)
Copy an element into the atomic queue, one producer only.
fr_atomic_queue_t * fr_atomic_queue_talloc(TALLOC_CTX *ctx, size_t num, size_t elem_len)
Create a fixed-size atomic queue.
fr_atomic_queue_t * fr_atomic_queue_mmap(TALLOC_CTX *ctx, int *fd, size_t num, size_t elem_len))
Create or attach to a fixed-size atomic queue in shared memory, owned by a talloc context.
bool fr_atomic_queue_spsc_pop(fr_atomic_queue_t *aq, void *data)
Copy an element out of the atomic queue, one consumer only.
#define RCSIDH(h, id)
Definition build.h:604
static fr_slen_t data
Definition value.h:1366
int nonnull(2, 5))