The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 3a566defb63f15aa51287e250b83ac3320ddca85 $
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: 3a566defb63f15aa51287e250b83ac3320ddca85 $")
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
48
49/** Unbounded segmented single-producer / single-consumer queue
50 *
51 * Internally a linked list of fixed-size `fr_atomic_queue_t` segments.
52 * When the producer's current segment fills, a new segment is malloc'd
53 * and linked in; the consumer drains segments in order and frees them
54 * as it advances. Allocation on the producer side uses raw malloc
55 * (not talloc), so the producer thread is free to be one that cannot
56 * safely use talloc (e.g. a library-owned callback thread).
57 *
58 * Exactly one producer and one consumer. Not safe for MPMC use.
59 */
61
62fr_atomic_ring_t *fr_atomic_ring_alloc(TALLOC_CTX *ctx, size_t seg_size);
65bool fr_atomic_ring_pop(fr_atomic_ring_t *ring, void **p_data);
66
67#ifdef WITH_VERIFY_PTR
68void fr_atomic_queue_verify(fr_atomic_queue_t *aq);
69#endif
70
71#ifndef NDEBUG
73#endif
74
75
76#ifdef __cplusplus
77}
78#endif
size_t size
The length of the queue.
size_t seg_size
Capacity of each segment.
Structure to hold the atomic queue.
bool fr_atomic_queue_pop(fr_atomic_queue_t *aq, void **p_data)
Pop a pointer from 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(size_t size)
Create fixed-size atomic queue outside any talloc hierarchy.
fr_atomic_ring_t * fr_atomic_ring_alloc(TALLOC_CTX *ctx, size_t seg_size)
Allocate an empty SPSC ring.
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_push(fr_atomic_queue_t *aq, void *data)
Push a pointer into the atomic queue.
void fr_atomic_queue_free(fr_atomic_queue_t **aq)
Free an atomic queue if it's not freed by ctx.
fr_atomic_queue_t * fr_atomic_queue_talloc(TALLOC_CTX *ctx, size_t size)
Create fixed-size atomic queue.
#define RCSIDH(h, id)
Definition build.h:513
static fr_atomic_queue_t ** aq
static fr_slen_t data
Definition value.h:1340