My Project
Loading...
Searching...
No Matches
ms.h
Go to the documentation of this file.
1#ifndef X86_MATANEL_SYNCHRONIZATION_H
2#define X86_MATANEL_SYNCHRONIZATION_H
3
4/*++
5
6Module Name:
7
8 ms.h
9
10Purpose:
11
12 This module contains the header files & prototypes required for synchronization in a threaded - multiprocessing system.
13
14Author:
15
16 slep (Matanel) 2025.
17
18Revision History:
19
20--*/
21
22#include <stddef.h>
23#include <stdint.h>
24#include <stdbool.h>
25#include "../mtstatus.h"
26#include "annotations.h"
27#include "core.h"
28
29// ------------------ STRUCTURES ------------------
30
36typedef struct _SPINLOCK {
37 volatile uint32_t locked; /* 0 = unlocked, 1 = locked */
39
46
47typedef struct _RUNDOWN_REF {
48 uint64_t Count; // Reference count, bit 0-62 is used for reference counting, bit 63 is used to signify the object is being terminated. (teardown flag)
50
51typedef struct _Queue {
54 struct _SPINLOCK lock; /* embedded spinlock (do not change from embedded) */
56
60typedef enum _EVENT_TYPE {
61 NotificationEvent, /* wake all waiting threads */
62 SynchronizationEvent /* wake one thread at a time */
64
69typedef struct _EVENT {
70 enum _EVENT_TYPE type; /* Notification vs Synchronization */
71 volatile bool signaled; /* current state */
72 struct _SPINLOCK lock; /* protects signaled + waitingQueue */
73 struct _Queue waitingQueue; /* threads waiting on this event */
75
82typedef struct _MUTEX {
83 uint32_t ownerTid; /* owning thread id (0 if none) */
84 struct _EVENT SynchEvent; /* event used for waking waiters */
85 bool locked; /* fast-check boolean (protected by lock) */
86 struct _SPINLOCK lock; /* protects ownerTid/locked and wait list */
87 struct _ETHREAD* ownerThread; /* pointer to current thread that holds the mutex */
89
90// ------------------ FUNCTIONS ------------------
91
92//#ifndef MT_UP
93void
95 IN PSPINLOCK lock,
96 IN PIRQL OldIrql
97);
98
99void
101 IN PSPINLOCK lock,
102 IN IRQL OldIrql
103);
113
116 IN PMUTEX mut
117);
118
121 IN PMUTEX mut
122);
123
126 IN PMUTEX mut
127);
128
129bool
131 IN PRUNDOWN_REF rundown
132);
133
134void
136 IN PRUNDOWN_REF rundown
137);
138
139void
141 IN PRUNDOWN_REF rundown
142);
143
146 IN PEVENT event
147);
148
151 IN PEVENT event
152);
153
154void
156 IN PSPINLOCK Lock
157);
158
159void
161 IN PSPINLOCK Lock
162);
163
165void
168)
169
170{
171 Head->Flink = Head;
172 Head->Blink = Head;
173}
174
175// ->>>> CRASHES IN THESE FUNCTIONS USUALLY BECAUSE INITIALIZELISTHEAD WASNT USED ON THE DOUBLY LINKED LIST !!!!!!!
176
178void
182)
183
184{
186 // The last element is the one before Head (circular list style)
187 Blink = Head->Blink;
188 Entry->Flink = Head; // New entry points forward to Head
189 Entry->Blink = Blink; // New entry points back to old last node
190 Blink->Flink = Entry; // Old last node points forward to new entry
191 Head->Blink = Entry; // Head points back to new entry
192}
193
195void
199)
200{
202
203 // The first element is the one after Head (circular list)
204 First = Head->Flink;
205
206 Entry->Flink = First; // Entry -> next = old first
207 Entry->Blink = Head; // Entry -> prev = head
208
209 First->Blink = Entry; // old first -> prev = entry
210 Head->Flink = Entry; // head -> next = entry
211}
212
217)
218
219{
222
223 Entry = Head->Flink;
224 if (Entry == Head) {
225 // List is empty
226 return NULL;
227 }
228
229 Flink = Entry->Flink;
230 Head->Flink = Flink;
231 Flink->Blink = Head;
232
233 // Clear links
234 Entry->Flink = Entry->Blink = NULL;
235 return Entry;
236}
237
239void
242)
243{
246
247 Flink = Entry->Flink;
248 Blink = Entry->Blink;
249
250 /* Normal (minimal) unlink — identical to Windows' RemoveEntryList */
251 Blink->Flink = Flink;
252 Flink->Blink = Blink;
253
254 // Sanitize the removed entry so it doesn't look valid
255 Entry->Flink = NULL;
256 Entry->Blink = NULL;
257}
258
259
260/* Interlocked push: atomically push Entry onto *ListHeadPtr.
261 ListHeadPtr is PSINGLE_LINKED_LIST* (address of the head pointer).
262 Usage: InterlockedPushEntry(&Descriptor->FreeListHead.Next, &Header->Metadata.FreeListEntry);
263 */
265void
267 PSINGLE_LINKED_LIST* ListHeadPtr, /* &head_ptr */
268 PSINGLE_LINKED_LIST Entry /* entry->Next must be valid memory */
269)
270{
271 PSINGLE_LINKED_LIST oldHead;
272 do {
273 oldHead = __atomic_load_n(ListHeadPtr, __ATOMIC_RELAXED);
274 Entry->Next = oldHead;
275 /* try to replace head with Entry */
276 } while (!__atomic_compare_exchange_n(
277 ListHeadPtr, /* target */
278 &oldHead, /* expected (updated on failure) */
279 Entry, /* desired */
280 /*weak*/ false,
281 __ATOMIC_RELEASE, /* success: release so prior stores are visible */
282 __ATOMIC_RELAXED)); /* failure: relaxed */
283}
284
285/* Interlocked pop: atomically pop and return the old head (or NULL).
286 Returns the popped entry pointer.
287 Usage: InterlockedPopEntry(&Descriptor->FreeListHead.Next);
288 */
292 PSINGLE_LINKED_LIST* ListHeadPtr
293)
294{
295 PSINGLE_LINKED_LIST oldHead;
297
298 do {
299 oldHead = __atomic_load_n(ListHeadPtr, __ATOMIC_ACQUIRE);
300 if (oldHead == NULL)
301 return NULL;
302 next = oldHead->Next;
303 /* try to set head to next */
304 } while (!__atomic_compare_exchange_n(
305 ListHeadPtr,
306 &oldHead,
307 next,
308 /*weak*/ false,
309 __ATOMIC_ACQ_REL, /* success: acquire+release to pair with push */
310 __ATOMIC_RELAXED)); /* failure ordering */
311 return oldHead;
312}
313
314#endif // X86_MATANEL_SYNCHRONIZATION_H
#define FORCEINLINE
Definition annotations.h:22
#define IN
Definition annotations.h:7
struct _DOUBLY_LINKED_LIST * PDOUBLY_LINKED_LIST
enum _IRQL IRQL
enum _IRQL * PIRQL
ETHREAD * PETHREAD
Definition core.h:42
struct _SINGLE_LINKED_LIST * PSINGLE_LINKED_LIST
struct _EVENT * PEVENT
void MsAcquireSpinlock(IN PSPINLOCK lock, IN PIRQL OldIrql)
Definition spinlock.c:13
struct _RUNDOWN_REF * PRUNDOWN_REF
MTSTATUS MsWaitForEvent(IN PEVENT event)
Definition events.c:113
FORCEINLINE PDOUBLY_LINKED_LIST RemoveHeadList(PDOUBLY_LINKED_LIST Head)
Definition ms.h:215
struct _RUNDOWN_REF RUNDOWN_REF
struct _MUTEX * PMUTEX
FORCEINLINE void InitializeListHead(PDOUBLY_LINKED_LIST Head)
Definition ms.h:166
MTSTATUS MsReleaseMutexObject(IN PMUTEX mut)
Definition mutex.c:135
_EVENT_TYPE
Definition ms.h:60
@ NotificationEvent
Definition ms.h:61
@ SynchronizationEvent
Definition ms.h:62
bool MsAcquireRundownProtection(IN PRUNDOWN_REF rundown)
Definition rundown.c:7
FORCEINLINE PSINGLE_LINKED_LIST InterlockedPopEntry(PSINGLE_LINKED_LIST *ListHeadPtr)
Definition ms.h:291
FORCEINLINE void InterlockedPushEntry(PSINGLE_LINKED_LIST *ListHeadPtr, PSINGLE_LINKED_LIST Entry)
Definition ms.h:266
void MsReleaseRundownProtection(IN PRUNDOWN_REF rundown)
Definition rundown.c:40
MTSTATUS MsInitializeMutexObject(IN PMUTEX mut)
Definition mutex.c:13
enum _EVENT_TYPE EVENT_TYPE
MTSTATUS MsSetEvent(IN PEVENT event)
Definition events.c:13
struct _Queue Queue
void MsWaitForRundownProtectionRelease(IN PRUNDOWN_REF rundown)
Definition rundown.c:63
FORCEINLINE void InsertTailList(PDOUBLY_LINKED_LIST Head, PDOUBLY_LINKED_LIST Entry)
Definition ms.h:179
MTSTATUS MsAcquireMutexObject(IN PMUTEX mut)
Definition mutex.c:73
void MsReleaseSpinlockFromDpcLevel(IN PSPINLOCK Lock)
Definition spinlock.c:100
void MsReleaseSpinlock(IN PSPINLOCK lock, IN IRQL OldIrql)
Definition spinlock.c:45
struct _SPINLOCK * PSPINLOCK
void MsAcquireSpinlockAtDpcLevel(IN PSPINLOCK Lock)
Definition spinlock.c:74
struct _MUTEX MUTEX
FORCEINLINE void InsertHeadList(PDOUBLY_LINKED_LIST Head, PDOUBLY_LINKED_LIST Entry)
Definition ms.h:196
struct _SPINLOCK SPINLOCK
struct _EVENT EVENT
FORCEINLINE void RemoveEntryList(PDOUBLY_LINKED_LIST Entry)
Definition ms.h:240
int32_t MTSTATUS
Definition mtstatus.h:12
struct _DOUBLY_LINKED_LIST * Blink
Definition core.h:28
struct _DOUBLY_LINKED_LIST * Flink
Definition core.h:29
Definition ps.h:121
Definition ms.h:69
enum _EVENT_TYPE type
Definition ms.h:70
struct _Queue waitingQueue
Definition ms.h:73
volatile bool signaled
Definition ms.h:71
struct _SPINLOCK lock
Definition ms.h:72
Definition ms.h:82
bool locked
Definition ms.h:85
uint32_t ownerTid
Definition ms.h:83
struct _ETHREAD * ownerThread
Definition ms.h:87
struct _EVENT SynchEvent
Definition ms.h:84
struct _SPINLOCK lock
Definition ms.h:86
Definition ms.h:51
PETHREAD tail
Definition ms.h:53
PETHREAD head
Definition ms.h:52
struct _SPINLOCK lock
Definition ms.h:54
uint64_t Count
Definition ms.h:48
struct _SINGLE_LINKED_LIST * Next
Definition core.h:24
Definition ms.h:36
volatile uint32_t locked
Definition ms.h:37