Branch data Line data Source code
1 : : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
2 : : * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
3 : : */
4 : :
5 : : #include "SolidSyslogStdAtomicCounter.h"
6 : :
7 : : #include <stdatomic.h>
8 : : #include <stdint.h>
9 : :
10 : : #include "SolidSyslogAtomicCounter.h"
11 : : #include "SolidSyslogAtomicCounterDefinition.h"
12 : : #include "SolidSyslogError.h"
13 : : #include "SolidSyslogNullAtomicCounter.h"
14 : : #include "SolidSyslogStdAtomicCounterErrors.h"
15 : : #include "SolidSyslogStdAtomicCounterPrivate.h"
16 : :
17 : : const struct SolidSyslogErrorSource SolidSyslogStdAtomicCounterErrorSource = {
18 : : "StdAtomicCounter",
19 : : SolidSyslogAtomicCounterErrors_Name
20 : : };
21 : :
22 : : static uint32_t StdAtomicCounter_Increment(struct SolidSyslogAtomicCounter* base);
23 : : static void StdAtomicCounter_Init(struct SolidSyslogStdAtomicCounter* self, uint32_t value);
24 : :
25 : : static inline struct SolidSyslogStdAtomicCounter* StdAtomicCounter_SelfFromBase(struct SolidSyslogAtomicCounter* base);
26 : :
27 : 45 : void SolidSyslogStdAtomicCounter_Initialise(struct SolidSyslogAtomicCounter* base)
28 : : {
29 : 45 : struct SolidSyslogStdAtomicCounter* self = StdAtomicCounter_SelfFromBase(base);
30 : 45 : self->Base.Increment = StdAtomicCounter_Increment;
31 : 45 : StdAtomicCounter_Init(self, 0U);
32 : 45 : }
33 : :
34 : 81 : static inline struct SolidSyslogStdAtomicCounter* StdAtomicCounter_SelfFromBase(struct SolidSyslogAtomicCounter* base)
35 : : {
36 : 81 : return (struct SolidSyslogStdAtomicCounter*) base;
37 : : }
38 : :
39 : : /* atomic_store rather than atomic_init: a released pool slot is handed out again,
40 : : * so this runs a second time on an object C11 lets you initialise only once. */
41 : 48 : static void StdAtomicCounter_Init(struct SolidSyslogStdAtomicCounter* self, uint32_t value)
42 : : {
43 : 48 : atomic_store_explicit(&self->Value, value, memory_order_relaxed);
44 : 48 : }
45 : :
46 : 45 : void SolidSyslogStdAtomicCounter_Cleanup(struct SolidSyslogAtomicCounter* base)
47 : : {
48 : : /* Overwrite the abstract base with the shared NullAtomicCounter vtable so
49 : : * use-after-destroy is a safe no-op rather than a NULL-fn-pointer crash. */
50 : 45 : *base = *SolidSyslogNullAtomicCounter_Get();
51 : 45 : }
52 : :
53 : 33 : static uint32_t StdAtomicCounter_Increment(struct SolidSyslogAtomicCounter* base)
54 : : {
55 : 33 : struct SolidSyslogStdAtomicCounter* self = StdAtomicCounter_SelfFromBase(base);
56 : 33 : uint32_t current = atomic_load_explicit(&self->Value, memory_order_relaxed);
57 : 33 : uint32_t next = 0U;
58 : : do
59 : : {
60 [ + + ]: 33 : next = (current >= SOLIDSYSLOG_SEQUENCE_ID_MAX) ? 1U : (current + 1U);
61 [ - + ]: 33 : } while (!atomic_compare_exchange_strong_explicit(
62 : : &self->Value,
63 : : ¤t,
64 : : next,
65 : : memory_order_relaxed,
66 : : memory_order_relaxed
67 : : ));
68 : 33 : return next;
69 : : }
|