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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#include "decoder_ipc_control.h"
#include <atomic>
#include <cstdint>
#include <limits>
#if defined (_MSC_VER)
#include <intrin.h>
#endif
static_assert (sizeof (int) == sizeof (std::int32_t),
"decoder IPC requires 32-bit control words");
namespace
{
std::atomic<int const *> progressGeneration {nullptr};
std::atomic<int const *> progressState {nullptr};
std::atomic<int const *> progressVersion {nullptr};
std::atomic<int *> progressCounter {nullptr};
#if defined (_MSC_VER)
static_assert (sizeof (long) == sizeof (int),
"decoder IPC requires 32-bit MSVC interlocked words");
int atomicLoad (int const * value)
{
return static_cast<int> (_InterlockedCompareExchange (
reinterpret_cast<volatile long *> (const_cast<int *> (value)), 0, 0));
}
void atomicStore (int * value, int desired)
{
_InterlockedExchange (reinterpret_cast<volatile long *> (value), desired);
}
bool compareExchange (int * value, int expected, int desired)
{
return expected == _InterlockedCompareExchange (
reinterpret_cast<volatile long *> (value), desired, expected);
}
#else
static_assert (__atomic_always_lock_free (sizeof (int), nullptr),
"decoder IPC control words must be lock-free");
int atomicLoad (int const * value)
{
return __atomic_load_n (value, __ATOMIC_ACQUIRE);
}
void atomicStore (int * value, int desired)
{
__atomic_store_n (value, desired, __ATOMIC_RELEASE);
}
bool compareExchange (int * value, int expected, int desired)
{
return __atomic_compare_exchange_n (value, &expected, desired, false,
__ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
}
#endif
bool hasCurrentVersion (int const * version)
{
return DECODER_IPC_VERSION == atomicLoad (version);
}
}
extern "C" int decoder_ipc_atomic_load (int const * value)
{
return atomicLoad (value);
}
extern "C" void decoder_ipc_control_initialize (int * generation, int * state,
int * version, int * progress)
{
atomicStore (generation, 0);
atomicStore (progress, 0);
atomicStore (version, DECODER_IPC_VERSION);
atomicStore (state, DECODER_IPC_IDLE);
}
extern "C" void decoder_ipc_control_shutdown (int * state,
int *)
{
atomicStore (state, DECODER_IPC_SHUTDOWN);
}
extern "C" int decoder_ipc_control_publish (int * generation, int * state,
int const * version,
int * progress,
int request_generation)
{
if (request_generation <= 0 || !hasCurrentVersion (version)
|| DECODER_IPC_IDLE != atomicLoad (state))
{
return 0;
}
atomicStore (progress, 0);
atomicStore (generation, request_generation);
return compareExchange (state, DECODER_IPC_IDLE, DECODER_IPC_READY);
}
extern "C" int decoder_ipc_control_try_claim (int * generation, int * state,
int const * version,
int * claimed_generation)
{
if (!hasCurrentVersion (version)) return DECODER_IPC_CLAIM_INCOMPATIBLE;
auto const currentState = atomicLoad (state);
if (DECODER_IPC_SHUTDOWN == currentState) return DECODER_IPC_CLAIM_SHUTDOWN;
if (DECODER_IPC_READY != currentState) return DECODER_IPC_CLAIM_NONE;
auto const currentGeneration = atomicLoad (generation);
if (currentGeneration <= 0) return DECODER_IPC_CLAIM_INVALID;
if (!compareExchange (state, DECODER_IPC_READY, DECODER_IPC_DECODING))
{
return DECODER_IPC_CLAIM_NONE;
}
*claimed_generation = currentGeneration;
return DECODER_IPC_CLAIMED;
}
extern "C" int decoder_ipc_control_finish (int const * generation, int * state,
int const * version,
int request_generation)
{
return request_generation > 0
&& hasCurrentVersion (version)
&& request_generation == atomicLoad (generation)
&& compareExchange (state, DECODER_IPC_DECODING, DECODER_IPC_COMPLETE);
}
extern "C" int decoder_ipc_control_consume (int const * generation, int * state,
int const * version,
int request_generation)
{
if (request_generation <= 0 || !hasCurrentVersion (version)
|| request_generation != atomicLoad (generation))
{
return 0;
}
return compareExchange (state, DECODER_IPC_COMPLETE, DECODER_IPC_IDLE);
}
extern "C" void decoder_ipc_progress_bind (int const * generation,
int const * state,
int const * version,
int * progress)
{
progressGeneration.store (generation, std::memory_order_relaxed);
progressState.store (state, std::memory_order_relaxed);
progressVersion.store (version, std::memory_order_relaxed);
progressCounter.store (progress, std::memory_order_release);
}
extern "C" void decoder_ipc_progress_unbind ()
{
progressCounter.store (nullptr, std::memory_order_release);
progressVersion.store (nullptr, std::memory_order_relaxed);
progressState.store (nullptr, std::memory_order_relaxed);
progressGeneration.store (nullptr, std::memory_order_relaxed);
}
extern "C" void decoder_ipc_progress_report (int request_generation)
{
auto * progress = progressCounter.load (std::memory_order_acquire);
auto const * version = progressVersion.load (std::memory_order_relaxed);
auto const * state = progressState.load (std::memory_order_relaxed);
auto const * generation = progressGeneration.load (std::memory_order_relaxed);
if (!progress || !version || !state || !generation
|| request_generation <= 0
|| DECODER_IPC_VERSION != atomicLoad (version)
|| DECODER_IPC_DECODING != atomicLoad (state)
|| request_generation != atomicLoad (generation))
{
return;
}
for (;;)
{
auto const current = atomicLoad (progress);
auto const next = std::numeric_limits<int>::max () == current
? 1 : current + 1;
if (compareExchange (progress, current, next)) return;
}
}
|