-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsecutiveIDGenerator.c
170 lines (151 loc) · 7.19 KB
/
ConsecutiveIDGenerator.c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2018, Stefan Johnson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define CC_QUICK_COMPILE
#include "ConsecutiveIDGenerator.h"
#include "MemoryAllocation.h"
#include "Assertion.h"
#include <stdatomic.h>
/*
CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE enforces standard compliant usage of atomic types. This however will
result in no batched operations.
*/
#ifndef CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
#define CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE 0
#endif
#if !CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
#if !CC_HARDWARE_PTR_64 && !CC_HARDWARE_PTR_32
#undef CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
#define CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE 1
#endif
#endif
typedef struct {
size_t size;
#if CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
atomic_flag pool[];
#else
_Atomic(uint8_t) pool[];
#endif
} CCConsecutiveIDGeneratorInternal;
#if !CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
#if CC_HARDWARE_PTR_64
typedef uint64_t CCConsecutiveIDGeneratorSample;
#define CC_CONCURRENT_ID_POOL_SAMPLE_MAX UINT64_MAX
#else
typedef uint32_t CCConsecutiveIDGeneratorSample;
#define CC_CONCURRENT_ID_POOL_SAMPLE_MAX UINT32_MAX
#endif
#endif
#if !CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
#if CC_HARDWARE_PTR_64
_Static_assert(sizeof(_Atomic(CCConsecutiveIDGeneratorSample)) == 8, "Native types are not atomic, please set CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE");
#elif CC_HARDWARE_PTR_32
_Static_assert(sizeof(_Atomic(CCConsecutiveIDGeneratorSample)) == 4, "Native types are not atomic, please set CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE");
#endif
#endif
static void *CCConsecutiveIDGeneratorConstructor(CCAllocatorType Allocator, size_t Count);
static void CCConsecutiveIDGeneratorDestructor(CCConsecutiveIDGeneratorInternal *Internal);
static _Bool CCConsecutiveIDGeneratorTryAssign(CCConsecutiveIDGeneratorInternal *Internal, uintptr_t *ID);
static void CCConsecutiveIDGeneratorRecycle(CCConsecutiveIDGeneratorInternal *Internal, uintptr_t ID);
static uintptr_t CCConsecutiveIDGeneratorGetMaxID(CCConsecutiveIDGeneratorInternal *Internal);
const CCConcurrentIDGeneratorInterface CCConsecutiveIDGeneratorInterface = {
.create = CCConsecutiveIDGeneratorConstructor,
.destroy = (CCConcurrentIDGeneratorDestructorCallback)CCConsecutiveIDGeneratorDestructor,
.try = (CCConcurrentIDGeneratorTryAssignCallback)CCConsecutiveIDGeneratorTryAssign,
.recycle = (CCConcurrentIDGeneratorRecycleCallback)CCConsecutiveIDGeneratorRecycle,
.max = (CCConcurrentIDGeneratorGetMaxIDCallback)CCConsecutiveIDGeneratorGetMaxID
};
const CCConcurrentIDGeneratorInterface * const CCConsecutiveIDGenerator = &CCConsecutiveIDGeneratorInterface;
void *CCConsecutiveIDGeneratorConstructor(CCAllocatorType Allocator, size_t PoolSize)
{
CCConsecutiveIDGeneratorInternal *IDPool;
#if CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
const size_t PaddedPoolCount = PoolSize;
#else
const size_t PaddedPoolCount = ((((PoolSize - 1) / (sizeof(CCConsecutiveIDGeneratorSample) / sizeof(typeof(IDPool->pool[0])))) + 1) * (sizeof(CCConsecutiveIDGeneratorSample) / sizeof(typeof(IDPool->pool[0]))));
#endif
IDPool = CCMalloc(Allocator, sizeof(CCConsecutiveIDGeneratorInternal) + (PaddedPoolCount * sizeof(typeof(IDPool->pool[0]))), NULL, CC_DEFAULT_ERROR_CALLBACK);
if (IDPool)
{
IDPool->size = PoolSize;
#if CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
for (size_t Loop = 0; Loop < PoolSize; Loop++) atomic_flag_clear_explicit(&IDPool->pool[Loop], memory_order_relaxed);
#else
for (size_t Loop = 0; Loop < PoolSize; Loop++) atomic_init(&IDPool->pool[Loop], 0);
for (size_t Loop = PoolSize, PadCount = PaddedPoolCount; Loop < PadCount; Loop++) atomic_init(&IDPool->pool[Loop], UINT8_MAX);
#endif
}
return IDPool;
}
void CCConsecutiveIDGeneratorDestructor(CCConsecutiveIDGeneratorInternal *IDPool)
{
CCFree(IDPool);
}
_Bool CCConsecutiveIDGeneratorTryAssign(CCConsecutiveIDGeneratorInternal *IDPool, uintptr_t *ID)
{
#if CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
for (size_t Loop = 0, Count = IDPool->size; Loop < Count; Loop++)
{
if (!atomic_flag_test_and_set_explicit(&IDPool->pool[Loop], memory_order_relaxed))
{
*ID = Loop;
atomic_thread_fence(memory_order_acquire);
return TRUE;
}
}
#else
for (size_t Loop = 0, Count = IDPool->size; Loop < Count; Loop += (sizeof(CCConsecutiveIDGeneratorSample) / sizeof(typeof(IDPool->pool[0]))))
{
CCConsecutiveIDGeneratorSample Sample = atomic_load_explicit(((_Atomic(CCConsecutiveIDGeneratorSample)*){ (_Atomic(CCConsecutiveIDGeneratorSample)*)&IDPool->pool[Loop] }), memory_order_relaxed);
if (Sample != CC_CONCURRENT_ID_POOL_SAMPLE_MAX)
{
for (size_t Loop2 = Loop, SampleCount = Loop + (sizeof(CCConsecutiveIDGeneratorSample) / sizeof(typeof(IDPool->pool[0]))); Loop2 < SampleCount; Loop2++)
{
if (atomic_exchange_explicit(&IDPool->pool[Loop2], UINT8_MAX, memory_order_relaxed) == 0)
{
*ID = Loop2;
atomic_thread_fence(memory_order_acquire);
return TRUE;
}
}
}
}
#endif
return FALSE;
}
void CCConsecutiveIDGeneratorRecycle(CCConsecutiveIDGeneratorInternal *IDPool, uintptr_t ID)
{
CCAssertLog(ID < IDPool->size, "ID must have been assigned from this pool");
#if CC_CONSECUTIVE_ID_GENERATOR_STRICT_COMPLIANCE
CCAssertLog(atomic_flag_test_and_set_explicit(&IDPool->pool[ID], memory_order_relaxed), "ID must be assigned");
atomic_flag_clear_explicit(&IDPool->pool[ID], memory_order_release);
#else
CCAssertLog(atomic_load_explicit(&IDPool->pool[ID], memory_order_relaxed), "ID must be assigned");
atomic_store_explicit(&IDPool->pool[ID], 0, memory_order_release);
#endif
}
size_t CCConsecutiveIDGeneratorGetMaxID(CCConsecutiveIDGeneratorInternal *IDPool)
{
return IDPool->size;
}