comparison foosdk/sdk/pfc/synchro_win.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 class _critical_section_base {
4 protected:
5 CRITICAL_SECTION sec;
6 public:
7 _critical_section_base() = default;
8 inline void enter() noexcept {EnterCriticalSection(&sec);}
9 inline void leave() noexcept {LeaveCriticalSection(&sec);}
10 inline void create() noexcept {
11 #ifdef PFC_WINDOWS_DESKTOP_APP
12 InitializeCriticalSection(&sec);
13 #else
14 InitializeCriticalSectionEx(&sec,0,0);
15 #endif
16 }
17 inline void destroy() noexcept {DeleteCriticalSection(&sec);}
18 inline bool tryEnter() noexcept { return !!TryEnterCriticalSection(&sec); }
19 private:
20 _critical_section_base(const _critical_section_base&) = delete;
21 void operator=(const _critical_section_base&) = delete;
22 };
23
24 // Static-lifetime critical section, no cleanup - valid until process termination
25 class critical_section_static : public _critical_section_base {
26 public:
27 critical_section_static() noexcept {create();}
28 #if !PFC_LEAK_STATIC_OBJECTS
29 ~critical_section_static() noexcept {destroy();}
30 #endif
31 };
32
33 // Regular critical section, intended for any lifetime scopes
34 class critical_section : public _critical_section_base {
35 public:
36 critical_section() noexcept {create();}
37 ~critical_section() noexcept {destroy();}
38 };
39
40 namespace pfc {
41
42 // Read write lock - Vista-and-newer friendly lock that allows concurrent reads from a resource that permits such
43 // Warning, non-recursion proof
44 class readWriteLock {
45 public:
46 readWriteLock() = default;
47
48 void enterRead() noexcept {
49 AcquireSRWLockShared( & theLock );
50 }
51 void enterWrite() noexcept {
52 AcquireSRWLockExclusive( & theLock );
53 }
54 void leaveRead() noexcept {
55 ReleaseSRWLockShared( & theLock );
56 }
57 void leaveWrite() noexcept {
58 ReleaseSRWLockExclusive( &theLock );
59 }
60
61 private:
62 readWriteLock(const readWriteLock&) = delete;
63 void operator=(const readWriteLock&) = delete;
64
65 SRWLOCK theLock = SRWLOCK_INIT;
66 };
67
68 typedef ::_critical_section_base mutexBase_t;
69 typedef ::critical_section mutex;
70
71 }