-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncHashTable.cpp
93 lines (67 loc) · 2.2 KB
/
AsyncHashTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "AsyncHashTable.h"
#include "HashTable.h"
#include <SAL/Thread.h>
struct AsyncHashTable {
HashTable* BaseTable;
SAL_Mutex Lock;
};
AsyncHashTable* AsyncHashTable_New() {
AsyncHashTable* table;
table = Allocate(AsyncHashTable);
AsyncHashTable_Initialize(table);
return table;
}
void AsyncHashTable_Initialize(AsyncHashTable* table) {
assert(table != NULL);
table->BaseTable = HashTable_New();
table->Lock = SAL_Mutex_Create();
}
void AsyncHashTable_Free(AsyncHashTable* self) {
AsyncHashTable_Uninitialize(self);
Free(self);
}
void AsyncHashTable_Uninitialize(AsyncHashTable* self) {
assert(self != NULL);
HashTable_Free(self->BaseTable);
SAL_Mutex_Free(self->Lock);
}
void* AsyncHashTable_Get(AsyncHashTable* self, uint8* key, uint32 keyLength, void** value, uint32* valueLength) {
uint8* result;
assert(self != NULL);
SAL_Mutex_Acquire(self->Lock);
result = HashTable_Get(self->BaseTable, key, keyLength, value, valueLength);
SAL_Mutex_Release(self->Lock);
return result;
}
void AsyncHashTable_Add(AsyncHashTable* self, uint8* key, uint32 keyLength, void* value, uint32 valueLength) {
assert(self != NULL);
SAL_Mutex_Acquire(self->Lock);
HashTable_Add(self->BaseTable, key, keyLength, value, valueLength);
SAL_Mutex_Release(self->Lock);
}
void AsyncHashTable_Remove(AsyncHashTable* self, uint8* key, uint32 keyLength) {
assert(self != NULL);
SAL_Mutex_Acquire(self->Lock);
HashTable_Remove(self->BaseTable, key, keyLength);
SAL_Mutex_Release(self->Lock);
}
void* AsyncHashTable_GetInt(AsyncHashTable* self, uint64 key, void** value, uint32* valueLength) {
uint8* result;
assert(self != NULL);
SAL_Mutex_Acquire(self->Lock);
result = HashTable_GetInt(self->BaseTable, key, value, valueLength);
SAL_Mutex_Release(self->Lock);
return result;
}
void AsyncHashTable_AddInt(AsyncHashTable* self, uint64 key, void* value, uint32 valueLength) {
assert(self != NULL);
SAL_Mutex_Acquire(self->Lock);
HashTable_AddInt(self->BaseTable, key, value, valueLength);
SAL_Mutex_Release(self->Lock);
}
void AsyncHashTable_RemoveInt(AsyncHashTable* self, uint64 key) {
assert(self != NULL);
SAL_Mutex_Acquire(self->Lock);
HashTable_RemoveInt(self->BaseTable, key);
SAL_Mutex_Release(self->Lock);
}