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 "SolidSyslogVxWorks64Mutex.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stddef.h>
9 : :
10 : : #include "SolidSyslogError.h"
11 : : #include "SolidSyslogErrorCategory.h"
12 : : #include "SolidSyslogNullMutex.h"
13 : : #include "SolidSyslogPoolAllocator.h"
14 : : #include "SolidSyslogPrival.h"
15 : : #include "SolidSyslogTunables.h"
16 : : #include "SolidSyslogVxWorks64MutexErrors.h"
17 : : #include "SolidSyslogVxWorks64MutexPrivate.h"
18 : :
19 : : struct SolidSyslogMutex;
20 : :
21 : : static inline size_t VxWorks64Mutex_IndexFromHandle(const struct SolidSyslogMutex* base);
22 : : static inline void VxWorks64Mutex_CleanupAtIndex(size_t index, void* context);
23 : :
24 : : static bool VxWorks64Mutex_InUse[SOLIDSYSLOG_MUTEX_POOL_SIZE];
25 : : static struct SolidSyslogVxWorks64Mutex VxWorks64Mutex_Pool[SOLIDSYSLOG_MUTEX_POOL_SIZE];
26 : : static struct SolidSyslogPoolAllocator VxWorks64Mutex_Allocator = {VxWorks64Mutex_InUse, SOLIDSYSLOG_MUTEX_POOL_SIZE};
27 : :
28 : 78 : struct SolidSyslogMutex* SolidSyslogVxWorks64Mutex_Create(void)
29 : : {
30 : 78 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&VxWorks64Mutex_Allocator);
31 : 78 : struct SolidSyslogMutex* handle = SolidSyslogNullMutex_Get();
32 [ + + ]: 78 : if (SolidSyslogPoolAllocator_IndexIsValid(&VxWorks64Mutex_Allocator, index) == true)
33 : : {
34 : 74 : bool created = SolidSyslogVxWorks64Mutex_Initialise(&VxWorks64Mutex_Pool[index].Base);
35 [ + + ]: 74 : if (created == true)
36 : : {
37 : 67 : handle = &VxWorks64Mutex_Pool[index].Base;
38 : : }
39 : : else
40 : : {
41 : : /* The slot goes back; the caller holds the NullMutex. */
42 : 7 : (void) SolidSyslogPoolAllocator_FreeIfInUse(
43 : : &VxWorks64Mutex_Allocator,
44 : : index,
45 : : VxWorks64Mutex_CleanupAtIndex,
46 : : NULL
47 : : );
48 : : }
49 : : }
50 : : else
51 : : {
52 : 4 : VxWorks64Mutex_Report(
53 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
54 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
55 : : SOLIDSYSLOG_MUTEX_ERROR_POOL_EXHAUSTED
56 : : );
57 : : }
58 : 78 : return handle;
59 : : }
60 : :
61 : 77 : void SolidSyslogVxWorks64Mutex_Destroy(struct SolidSyslogMutex* base)
62 : : {
63 : 77 : size_t index = VxWorks64Mutex_IndexFromHandle(base);
64 : 77 : bool released =
65 [ + + + + ]: 145 : SolidSyslogPoolAllocator_IndexIsValid(&VxWorks64Mutex_Allocator, index) &&
66 : 68 : SolidSyslogPoolAllocator_FreeIfInUse(&VxWorks64Mutex_Allocator, index, VxWorks64Mutex_CleanupAtIndex, NULL);
67 [ + + ]: 77 : if (!released)
68 : : {
69 : 10 : VxWorks64Mutex_Report(
70 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
71 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
72 : : SOLIDSYSLOG_MUTEX_ERROR_UNKNOWN_DESTROY
73 : : );
74 : : }
75 : 77 : }
76 : :
77 : 77 : static inline size_t VxWorks64Mutex_IndexFromHandle(const struct SolidSyslogMutex* base)
78 : : {
79 : 77 : size_t result = SOLIDSYSLOG_MUTEX_POOL_SIZE;
80 [ + + ]: 86 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_MUTEX_POOL_SIZE; poolIndex++)
81 : : {
82 [ + + ]: 77 : if (base == &VxWorks64Mutex_Pool[poolIndex].Base)
83 : : {
84 : 68 : result = poolIndex;
85 : 68 : break;
86 : : }
87 : : }
88 : 77 : return result;
89 : : }
90 : :
91 : 74 : static inline void VxWorks64Mutex_CleanupAtIndex(size_t index, void* context)
92 : : {
93 : : (void) context;
94 : 74 : SolidSyslogVxWorks64Mutex_Cleanup(&VxWorks64Mutex_Pool[index].Base);
95 : 74 : }
|