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 "SolidSyslogPosixMutex.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : : #include <stdint.h>
10 : :
11 : : #include "SolidSyslogError.h"
12 : : #include "SolidSyslogErrorCategory.h"
13 : : #include "SolidSyslogNullMutex.h"
14 : : #include "SolidSyslogPoolAllocator.h"
15 : : #include "SolidSyslogPosixMutexErrors.h"
16 : : #include "SolidSyslogPosixMutexPrivate.h"
17 : : #include "SolidSyslogPrival.h"
18 : : #include "SolidSyslogTunables.h"
19 : :
20 : : struct SolidSyslogMutex;
21 : :
22 : : static inline size_t PosixMutex_IndexFromHandle(const struct SolidSyslogMutex* base);
23 : : static inline void PosixMutex_CleanupAtIndex(size_t index, void* context);
24 : :
25 : : static bool PosixMutex_InUse[SOLIDSYSLOG_MUTEX_POOL_SIZE];
26 : : static struct SolidSyslogPosixMutex PosixMutex_Pool[SOLIDSYSLOG_MUTEX_POOL_SIZE];
27 : : static struct SolidSyslogPoolAllocator PosixMutex_Allocator = {PosixMutex_InUse, SOLIDSYSLOG_MUTEX_POOL_SIZE};
28 : :
29 : 16 : struct SolidSyslogMutex* SolidSyslogPosixMutex_Create(void)
30 : : {
31 : 16 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&PosixMutex_Allocator);
32 : 16 : struct SolidSyslogMutex* handle = SolidSyslogNullMutex_Get();
33 [ + + ]: 16 : if (SolidSyslogPoolAllocator_IndexIsValid(&PosixMutex_Allocator, index) == true)
34 : : {
35 : 12 : SolidSyslogPosixMutex_Initialise(&PosixMutex_Pool[index].Base);
36 : 12 : handle = &PosixMutex_Pool[index].Base;
37 : : }
38 : : else
39 : : {
40 : 4 : PosixMutex_Report(
41 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
42 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
43 : : SOLIDSYSLOG_MUTEX_ERROR_POOL_EXHAUSTED
44 : : );
45 : : }
46 : 16 : return handle;
47 : : }
48 : :
49 : 19 : void SolidSyslogPosixMutex_Destroy(struct SolidSyslogMutex* base)
50 : : {
51 : 19 : size_t index = PosixMutex_IndexFromHandle(base);
52 [ + + + + ]: 32 : bool released = SolidSyslogPoolAllocator_IndexIsValid(&PosixMutex_Allocator, index) &&
53 : 13 : SolidSyslogPoolAllocator_FreeIfInUse(&PosixMutex_Allocator, index, PosixMutex_CleanupAtIndex, NULL);
54 [ + + ]: 19 : if (!released)
55 : : {
56 : 7 : PosixMutex_Report(
57 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
58 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
59 : : SOLIDSYSLOG_MUTEX_ERROR_UNKNOWN_DESTROY
60 : : );
61 : : }
62 : 19 : }
63 : :
64 : 19 : static inline size_t PosixMutex_IndexFromHandle(const struct SolidSyslogMutex* base)
65 : : {
66 : 19 : size_t result = SOLIDSYSLOG_MUTEX_POOL_SIZE;
67 [ + + ]: 25 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_MUTEX_POOL_SIZE; poolIndex++)
68 : : {
69 [ + + ]: 19 : if (base == &PosixMutex_Pool[poolIndex].Base)
70 : : {
71 : 13 : result = poolIndex;
72 : 13 : break;
73 : : }
74 : : }
75 : 19 : return result;
76 : : }
77 : :
78 : 12 : static inline void PosixMutex_CleanupAtIndex(size_t index, void* context)
79 : : {
80 : : (void) context;
81 : 12 : SolidSyslogPosixMutex_Cleanup(&PosixMutex_Pool[index].Base);
82 : 12 : }
|