CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
ssdshared.h
1#pragma once
2
4#include <cx/meta/block.h>
5#include <cx/thread/rwlock.h>
6
7#if DEBUG_LEVEL > 1
8#define SSD_LOCK_DEBUG 1
9#endif
10
11CX_C_BEGIN
12
13typedef struct SSDTree SSDTree;
14typedef struct SSDNode SSDNode;
15typedef SSDNode* (*SSDNodeFactory)(SSDTree* info);
16
19
24
34
36
37typedef struct SSDLockDebug {
38 int64 time;
39 const char* file;
40 int32 line;
41} SSDLockDebug;
42saDeclare(SSDLockDebug);
43
44typedef struct SSDLockState {
45 union {
46 bool _is_SSDLockState;
47 bool init; // lock state structure initialized
48 };
49 bool rdlock; // read lock held by current thread
50 bool wrlock; // write lock held by current thread
51#ifdef SSD_LOCK_DEBUG
52 SSDLockDebug dbg;
53#endif
54} SSDLockState;
55
56enum SSD_LOCK_STATE_ENUM { _ssdCurrentLockState = 0 };
57
58// Initializes a lock structure
59#ifdef SSD_LOCK_DEBUG
60SSDLockState* __ssdLockStateInit(_Out_ SSDLockState* lstate, _In_z_ const char* fn, int lnum);
61#define _ssdLockStateInit(lstate) __ssdLockStateInit(lstate, __FILE__, __LINE__)
62#else
63SSDLockState* _ssdLockStateInit(_Out_ SSDLockState* lstate);
64#endif
65
79
96#ifdef SSD_LOCK_DEBUG
97#define ssdLockRead(root) \
98 _ssdLockRead(SSDNode(root), \
99 (SSDLockState*)&_ssdCurrentLockState->_is_SSDLockState, \
100 __FILE__, \
101 __LINE__)
102#define _ssdManualLockRead(root, lstate) _ssdLockRead(SSDNode(root), lstate, __FILE__, __LINE__)
103bool _ssdLockRead(_Inout_ SSDNode* root, _Inout_ SSDLockState* lstate, _In_z_ const char* fn,
104 int lnum);
105#else
106#define ssdLockRead(root) _ssdLockRead(SSDNode(root), _ssdCurrentLockState)
107#define _ssdManualLockRead(root, lstate) _ssdLockRead(SSDNode(root), lstate)
108bool _ssdLockRead(_Inout_ SSDNode* root, _Inout_ SSDLockState* lstate);
109#endif
110
131#ifdef SSD_LOCK_DEBUG
132#define ssdLockWrite(root) \
133 _ssdLockWrite(SSDNode(root), \
134 (SSDLockState*)&_ssdCurrentLockState->_is_SSDLockState, \
135 __FILE__, \
136 __LINE__)
137#define _ssdManualLockWrite(root, lstate) _ssdLockWrite(SSDNode(root), lstate, __FILE__, __LINE__)
138bool _ssdLockWrite(_Inout_ SSDNode* root, _Inout_ SSDLockState* lstate, _In_z_ const char* fn,
139 int lnum);
140#else
141#define ssdLockWrite(root) _ssdLockWrite(SSDNode(root), _ssdCurrentLockState)
142#define _ssdManualLockWrite(root, lstate) _ssdLockWrite(SSDNode(root), lstate)
143bool _ssdLockWrite(_Inout_ SSDNode* root, _Inout_ SSDLockState* lstate);
144#endif
145
156#define ssdUnlock(root) \
157 _ssdUnlock(SSDNode(root), (SSDLockState*)&_ssdCurrentLockState->_is_SSDLockState)
158bool _ssdUnlock(_Inout_ SSDNode* root, _Inout_ SSDLockState* lstate);
159
169#define ssdLockEnd(root) \
170 _ssdLockEnd(SSDNode(root), (SSDLockState*)&_ssdCurrentLockState->_is_SSDLockState)
171bool _ssdLockEnd(_Inout_ SSDNode* root, _Inout_ SSDLockState* lstate);
172
197#define ssdLockedTransaction(root) \
198 _blkStart _inhibitReturn _blkFull(SSDLockState _ssdTransientLockState = { 0 }, \
199 (root), \
200 _ssdLockEnd(SSDNode(root), &_ssdTransientLockState)) \
201 _blkBefore(SSDLockState* _ssdCurrentLockStateShadow = (SSDLockState*)_ssdCurrentLockState) \
202 _blkBefore(SSDLockState* _ssdCurrentLockState = _ssdCurrentLockStateShadow ? \
203 _ssdCurrentLockStateShadow : \
204 _ssdLockStateInit(&_ssdTransientLockState)) _blkEnd
205
207
208CX_C_END
Block wrapping macros for automatic resource management.
#define saDeclare(name)
Definition sarray.h:93
enum SSD_CREATE_TYPE_ENUM SSDCreateType
Node creation types for specifying which kind of node to create.
SSD_CREATE_TYPE_ENUM
Node creation types for specifying which kind of node to create.
Definition ssdshared.h:26
SSD_FLAGS_ENUM
Flags for configuring SSD tree behavior.
Definition ssdshared.h:21
@ SSD_Create_Single
Create a single-value node.
Definition ssdshared.h:30
@ SSD_Create_Array
Create an array (indexed) node.
Definition ssdshared.h:29
@ SSD_Create_Hashtable
Create a hashtable (key-value) node.
Definition ssdshared.h:28
@ SSD_Create_None
Do not create a node.
Definition ssdshared.h:27
@ SSD_Create_Count
Total number of creation types (internal use)
Definition ssdshared.h:32
@ SSD_CaseInsensitive
Keys in hashtables are case-insensitive.
Definition ssdshared.h:22
Reader-writer lock synchronization primitive.
Dynamic arrays with type-safe generic programming.