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 "FreeRTOS.h"
6 : :
7 : : /* This component requires FreeRTOS built with static allocation and mutexes. */
8 : : #if (configSUPPORT_STATIC_ALLOCATION == 1) && (configUSE_MUTEXES == 1)
9 : :
10 : : #include "SolidSyslogFreeRtosMutex.h"
11 : :
12 : : #include <stdbool.h>
13 : : #include <stddef.h>
14 : : #include <stdint.h>
15 : :
16 : : #include "SolidSyslogError.h"
17 : : #include "SolidSyslogErrorCategory.h"
18 : : #include "SolidSyslogFreeRtosMutexErrors.h"
19 : : #include "SolidSyslogFreeRtosMutexPrivate.h"
20 : : #include "SolidSyslogNullMutex.h"
21 : : #include "SolidSyslogPoolAllocator.h"
22 : : #include "SolidSyslogPrival.h"
23 : : #include "SolidSyslogTunables.h"
24 : :
25 : : struct SolidSyslogMutex;
26 : :
27 : : static inline size_t FreeRtosMutex_IndexFromHandle(const struct SolidSyslogMutex* base);
28 : : static inline void FreeRtosMutex_CleanupAtIndex(size_t index, void* context);
29 : :
30 : : static bool FreeRtosMutex_InUse[SOLIDSYSLOG_MUTEX_POOL_SIZE];
31 : : static struct SolidSyslogFreeRtosMutex FreeRtosMutex_Pool[SOLIDSYSLOG_MUTEX_POOL_SIZE];
32 : : static struct SolidSyslogPoolAllocator FreeRtosMutex_Allocator = {FreeRtosMutex_InUse, SOLIDSYSLOG_MUTEX_POOL_SIZE};
33 : :
34 : 20 : struct SolidSyslogMutex* SolidSyslogFreeRtosMutex_Create(void)
35 : : {
36 : 20 : size_t index = SolidSyslogPoolAllocator_AcquireFirstFree(&FreeRtosMutex_Allocator);
37 : 20 : struct SolidSyslogMutex* handle = SolidSyslogNullMutex_Get();
38 [ + + ]: 20 : if (SolidSyslogPoolAllocator_IndexIsValid(&FreeRtosMutex_Allocator, index) == true)
39 : : {
40 : 16 : SolidSyslogFreeRtosMutex_Initialise(&FreeRtosMutex_Pool[index].Base);
41 : 16 : handle = &FreeRtosMutex_Pool[index].Base;
42 : : }
43 : : else
44 : : {
45 : 4 : FreeRtosMutex_Report(
46 : : SOLIDSYSLOG_POOL_EXHAUSTED_SEVERITY,
47 : : SOLIDSYSLOG_CAT_POOL_EXHAUSTED,
48 : : SOLIDSYSLOG_MUTEX_ERROR_POOL_EXHAUSTED
49 : : );
50 : : }
51 : 20 : return handle;
52 : : }
53 : :
54 : 25 : void SolidSyslogFreeRtosMutex_Destroy(struct SolidSyslogMutex* base)
55 : : {
56 : 25 : size_t index = FreeRtosMutex_IndexFromHandle(base);
57 : 25 : bool released =
58 [ + + + + ]: 42 : SolidSyslogPoolAllocator_IndexIsValid(&FreeRtosMutex_Allocator, index) &&
59 : 17 : SolidSyslogPoolAllocator_FreeIfInUse(&FreeRtosMutex_Allocator, index, FreeRtosMutex_CleanupAtIndex, NULL);
60 [ + + ]: 25 : if (!released)
61 : : {
62 : 9 : FreeRtosMutex_Report(
63 : : SOLIDSYSLOG_UNKNOWN_DESTROY_SEVERITY,
64 : : SOLIDSYSLOG_CAT_UNKNOWN_DESTROY,
65 : : SOLIDSYSLOG_MUTEX_ERROR_UNKNOWN_DESTROY
66 : : );
67 : : }
68 : 25 : }
69 : :
70 : 25 : static inline size_t FreeRtosMutex_IndexFromHandle(const struct SolidSyslogMutex* base)
71 : : {
72 : 25 : size_t result = SOLIDSYSLOG_MUTEX_POOL_SIZE;
73 [ + + ]: 33 : for (size_t poolIndex = 0U; poolIndex < SOLIDSYSLOG_MUTEX_POOL_SIZE; poolIndex++)
74 : : {
75 [ + + ]: 25 : if (base == &FreeRtosMutex_Pool[poolIndex].Base)
76 : : {
77 : 17 : result = poolIndex;
78 : 17 : break;
79 : : }
80 : : }
81 : 25 : return result;
82 : : }
83 : :
84 : 16 : static inline void FreeRtosMutex_CleanupAtIndex(size_t index, void* context)
85 : : {
86 : : (void) context;
87 : 16 : SolidSyslogFreeRtosMutex_Cleanup(&FreeRtosMutex_Pool[index].Base);
88 : 16 : }
89 : :
90 : : #else
91 : :
92 : : /* ISO C forbids an empty translation unit. */
93 : : typedef int FreeRtosMutexStatic_EmptyTranslationUnit;
94 : :
95 : : #endif /* configSUPPORT_STATIC_ALLOCATION && configUSE_MUTEXES */
|