|
| 1 | +/* Copyright (c) 2026, Red Hat, Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at: |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef SPSC_RING_H |
| 17 | +#define SPSC_RING_H |
| 18 | + |
| 19 | +#include <stdbool.h> |
| 20 | +#include <stdint.h> |
| 21 | + |
| 22 | +#include "openvswitch/util.h" |
| 23 | +#include "ovs-atomic.h" |
| 24 | + |
| 25 | +/* Single-Producer, Single-Consumer lock-free ring buffer. |
| 26 | + * |
| 27 | + * Thread-safety |
| 28 | + * ============= |
| 29 | + * |
| 30 | + * Exactly one thread (the producer) may call spsc_ring_push(). |
| 31 | + * Exactly one thread (the consumer) may call spsc_ring_pop(). |
| 32 | + * These two threads may be different and may operate concurrently |
| 33 | + * without any external synchronization. |
| 34 | + * |
| 35 | + * Memory ordering: sequential consistency (the default for OVS |
| 36 | + * atomic_read/atomic_store) on the read and write indices ensures |
| 37 | + * that slot data written by the producer is visible to the consumer |
| 38 | + * after it observes the updated write index (and vice versa for |
| 39 | + * read index updates freeing slots for reuse). |
| 40 | + * |
| 41 | + * Slot data is copied in and out by value (memcpy), so this is |
| 42 | + * best suited for small, fixed-size structs. |
| 43 | + * |
| 44 | + * Capacity must be a power of two. The ring uses unsigned 32-bit |
| 45 | + * wraparound arithmetic on read/write indices, which is correct as |
| 46 | + * long as capacity is much smaller than 2^32. |
| 47 | + */ |
| 48 | +struct spsc_ring { |
| 49 | + void *buffer; /* Pre-allocated slot array. */ |
| 50 | + size_t esize; /* Size of each element in bytes. */ |
| 51 | + uint32_t mask; /* capacity - 1 (for power-of-two modulo). */ |
| 52 | + atomic_uint32_t read; /* Next slot to consume (advanced by consumer). */ |
| 53 | + atomic_uint32_t write; /* Next slot to fill (advanced by producer). */ |
| 54 | +}; |
| 55 | + |
| 56 | +void spsc_ring_init(struct spsc_ring *, uint32_t capacity, size_t esize); |
| 57 | +void spsc_ring_destroy(struct spsc_ring *); |
| 58 | +bool spsc_ring_push(struct spsc_ring *, const void *data); |
| 59 | +bool spsc_ring_pop(struct spsc_ring *, void *data); |
| 60 | + |
| 61 | +/* Pop each element into VAR until the ring is empty. */ |
| 62 | +#define SPSC_RING_FOR_EACH_POP(RING, VAR) \ |
| 63 | + for (bool ITER_VAR(VAR) = spsc_ring_pop(RING, &(VAR)); \ |
| 64 | + ITER_VAR(VAR); \ |
| 65 | + ITER_VAR(VAR) = spsc_ring_pop(RING, &(VAR))) |
| 66 | + |
| 67 | +#endif /* lib/spsc-ring.h */ |
0 commit comments