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 "vxWorks.h"
11 : :
12 : : #include "semLib.h"
13 : :
14 : : #include "SolidSyslogError.h"
15 : : #include "SolidSyslogErrorCategory.h"
16 : : #include "SolidSyslogMutexDefinition.h"
17 : : #include "SolidSyslogNullMutex.h"
18 : : #include "SolidSyslogVxWorks64MutexPrivate.h"
19 : :
20 : : const struct SolidSyslogErrorSource SolidSyslogVxWorks64MutexErrorSource = {
21 : : "VxWorks64Mutex",
22 : : SolidSyslogMutexErrors_Name
23 : : };
24 : :
25 : : static void VxWorks64Mutex_Lock(struct SolidSyslogMutex* base);
26 : : static void VxWorks64Mutex_Unlock(struct SolidSyslogMutex* base);
27 : :
28 : : static inline struct SolidSyslogVxWorks64Mutex* VxWorks64Mutex_SelfFromBase(struct SolidSyslogMutex* base);
29 : : static inline bool VxWorks64Mutex_HasSemaphore(const struct SolidSyslogVxWorks64Mutex* self);
30 : :
31 : 74 : bool SolidSyslogVxWorks64Mutex_Initialise(struct SolidSyslogMutex* base)
32 : : {
33 : 74 : struct SolidSyslogVxWorks64Mutex* self = VxWorks64Mutex_SelfFromBase(base);
34 : 74 : self->Id = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
35 : 74 : bool created = VxWorks64Mutex_HasSemaphore(self);
36 [ + + ]: 74 : if (created == true)
37 : : {
38 : 67 : self->Base.Lock = VxWorks64Mutex_Lock;
39 : 67 : self->Base.Unlock = VxWorks64Mutex_Unlock;
40 : : }
41 : : else
42 : : {
43 : : /* On a sized image, a refused semaphore is a configuration fault. */
44 : 7 : VxWorks64Mutex_Report(
45 : : SOLIDSYSLOG_BAD_CONFIG_FATAL_SEVERITY,
46 : : SOLIDSYSLOG_CAT_BAD_CONFIG,
47 : : SOLIDSYSLOG_MUTEX_ERROR_CREATE_FAILED
48 : : );
49 : : }
50 : 74 : return created;
51 : : }
52 : :
53 : 358 : static inline struct SolidSyslogVxWorks64Mutex* VxWorks64Mutex_SelfFromBase(struct SolidSyslogMutex* base)
54 : : {
55 : 358 : return (struct SolidSyslogVxWorks64Mutex*) base;
56 : : }
57 : :
58 : 148 : static inline bool VxWorks64Mutex_HasSemaphore(const struct SolidSyslogVxWorks64Mutex* self)
59 : : {
60 : 148 : return self->Id != NULL;
61 : : }
62 : :
63 : 74 : void SolidSyslogVxWorks64Mutex_Cleanup(struct SolidSyslogMutex* base)
64 : : {
65 : 74 : struct SolidSyslogVxWorks64Mutex* self = VxWorks64Mutex_SelfFromBase(base);
66 [ + + ]: 74 : if (VxWorks64Mutex_HasSemaphore(self) == true)
67 : : {
68 : 67 : (void) semDelete(self->Id);
69 : : }
70 : : /* Use-after-destroy lands on the NullMutex vtable. */
71 : 74 : *base = *SolidSyslogNullMutex_Get();
72 : 74 : }
73 : :
74 : 106 : static void VxWorks64Mutex_Lock(struct SolidSyslogMutex* base)
75 : : {
76 : 106 : (void) semTake(VxWorks64Mutex_SelfFromBase(base)->Id, WAIT_FOREVER);
77 : 106 : }
78 : :
79 : 104 : static void VxWorks64Mutex_Unlock(struct SolidSyslogMutex* base)
80 : : {
81 : 104 : (void) semGive(VxWorks64Mutex_SelfFromBase(base)->Id);
82 : 104 : }
|