Skip to content

Commit 80d1ba4

Browse files
Further imrpove sc, sp behaviors.
1 parent 3b9fc2d commit 80d1ba4

File tree

2 files changed

+113
-7
lines changed

2 files changed

+113
-7
lines changed

.clang-format

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
AccessModifierOffset: -4
3+
AlignAfterOpenBracket: Align
4+
AlignConsecutiveAssignments: false
5+
AlignConsecutiveDeclarations: false
6+
AlignEscapedNewlines: Right
7+
AlignOperands: true
8+
AlignTrailingComments: false
9+
AllowAllParametersOfDeclarationOnNextLine: true
10+
AllowShortBlocksOnASingleLine: false
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortFunctionsOnASingleLine: All
13+
AllowShortIfStatementsOnASingleLine: false
14+
AllowShortLoopsOnASingleLine: false
15+
AlwaysBreakAfterDefinitionReturnType: None
16+
AlwaysBreakAfterReturnType: None
17+
AlwaysBreakBeforeMultilineStrings: false
18+
AlwaysBreakTemplateDeclarations: true
19+
BinPackArguments: true
20+
BinPackParameters: true
21+
BreakBeforeBraces: Custom
22+
BraceWrapping:
23+
AfterClass: true
24+
AfterControlStatement: true
25+
AfterEnum: true
26+
AfterFunction: true
27+
AfterNamespace: false
28+
AfterObjCDeclaration: false
29+
AfterStruct: true
30+
AfterUnion: true
31+
BeforeCatch: true
32+
BeforeElse: true
33+
IndentBraces: false
34+
SplitEmptyFunction: false
35+
SplitEmptyRecord: false
36+
BreakBeforeBinaryOperators: None
37+
BreakBeforeBraces: Allman
38+
BreakBeforeTernaryOperators: false
39+
BreakConstructorInitializersBeforeComma: false
40+
BreakStringLiterals: false
41+
ColumnLimit: 0
42+
CompactNamespaces: false
43+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
44+
ConstructorInitializerIndentWidth: 4
45+
ContinuationIndentWidth: 4
46+
Cpp11BracedListStyle: true
47+
DerivePointerAlignment: false
48+
DisableFormat: false
49+
ExperimentalAutoDetectBinPacking: false
50+
IndentCaseLabels: false
51+
FixNamespaceComments: true
52+
IncludeBlocks: Regroup
53+
IndentCaseLabels: false
54+
IndentPPDirectives: None
55+
IndentWidth: 4
56+
IndentWrappedFunctionNames: false
57+
KeepEmptyLinesAtTheStartOfBlocks: false
58+
Language: Cpp
59+
MaxEmptyLinesToKeep: 1
60+
NamespaceIndentation: None
61+
PenaltyBreakBeforeFirstCallParameter: 19
62+
PenaltyBreakComment: 300
63+
PenaltyBreakFirstLessLess: 120
64+
PenaltyBreakString: 1000
65+
PenaltyExcessCharacter: 1000000
66+
PenaltyReturnTypeOnItsOwnLine: 60
67+
PointerAlignment: Right
68+
ReflowComments: false
69+
SortIncludes: false
70+
SortUsingDeclarations: false
71+
SpaceAfterCStyleCast: false
72+
SpaceAfterTemplateKeyword: true
73+
SpaceBeforeAssignmentOperators: true
74+
SpaceBeforeParens: ControlStatements
75+
SpaceInEmptyParentheses: false
76+
SpacesBeforeTrailingComments: 1
77+
SpacesInAngles: false
78+
SpacesInCStyleCastParentheses: false
79+
SpacesInContainerLiterals: false
80+
SpacesInParentheses: false
81+
SpacesInSquareBrackets: false
82+
Standard: Auto
83+
TabWidth: 4
84+
UseTab: Never
85+
86+
...

WaitFreeRingBufferUtilities/Include/details/ring-buffer-type-constructor.inl

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ struct Element : ElementFeatures...
4545
template <typename Element, std::size_t Count>
4646
struct RingBufferStateBase
4747
{
48-
//protected:
48+
protected:
4949
using ElementType = typename Element::ElementType;
5050

5151
enum : std::size_t
5252
{
53-
COUNT = Count,
5453
COUNT_MASK = Count - 1,
5554
};
5655

5756
std::array<CacheAlignedAndPaddedObject<Element>, Count> elements{};
5857

5958
public:
59+
enum : std::size_t
60+
{
61+
COUNT = Count,
62+
};
63+
6064
static_assert(Count > 0 && !(COUNT_MASK & Count), "Count should be a power of two.");
6165
};
6266

@@ -85,6 +89,7 @@ struct MultiProducerTypeTraits
8589
template <typename BaseType>
8690
struct Behavior : BaseType
8791
{
92+
private:
8893
CacheAlignedAndPaddedObject<std::atomic_size_t> end{std::size_t(0)};
8994

9095
public:
@@ -126,6 +131,7 @@ struct MultiProducerTypeTraits
126131
template <typename BaseType, typename = Private::CountInt64CompatibilityCheck<BaseType::COUNT>>
127132
struct SharedState : BaseType
128133
{
134+
protected:
129135
CacheAlignedAndPaddedObject<std::atomic<std::int64_t>> push_task_count{static_cast<std::int64_t>(BaseType::COUNT)};
130136
};
131137

@@ -185,6 +191,7 @@ struct MultiConsumerTypeTraits
185191
template <typename BaseType, typename = Private::CountInt64CompatibilityCheck<BaseType::COUNT>>
186192
struct SharedState : BaseType
187193
{
194+
protected:
188195
CacheAlignedAndPaddedObject<std::atomic<std::int64_t>> pop_task_count{std::int64_t{0}};
189196
};
190197

@@ -203,6 +210,7 @@ struct SingleProducerTypeTraits
203210
struct State
204211
{
205212
std::size_t pushed_task_count{0};
213+
std::size_t local_push_task_count{BaseType::COUNT};
206214
std::size_t end{0};
207215
};
208216

@@ -214,8 +222,13 @@ struct SingleProducerTypeTraits
214222
template <typename... Args>
215223
bool push(Args &&... args)
216224
{
217-
if (BaseType::push_task_count.load(std::memory_order_acquire) == state.pushed_task_count)
218-
return false;
225+
if (state.local_push_task_count == state.pushed_task_count)
226+
{
227+
const auto stack_local_push_task_count = BaseType::push_task_count.load(std::memory_order_acquire);
228+
if (state.local_push_task_count == stack_local_push_task_count)
229+
return false;
230+
state.local_push_task_count = stack_local_push_task_count;
231+
}
219232

220233
state.pushed_task_count++;
221234

@@ -238,6 +251,7 @@ struct SingleProducerTypeTraits
238251
template <typename BaseType>
239252
struct SharedState : BaseType
240253
{
254+
protected:
241255
CacheAlignedAndPaddedObject<std::atomic_size_t> push_task_count{BaseType::COUNT};
242256
};
243257

@@ -248,14 +262,14 @@ struct SingleProducerTypeTraits
248262

249263
struct SingleConsumerTypeTraits
250264
{
251-
252265
template <typename BaseType>
253266
struct Behavior : BaseType
254267
{
255268
private:
256269
struct State
257270
{
258271
std::size_t popped_task_count{0};
272+
std::size_t local_pop_task_count{0};
259273
std::size_t begin{0};
260274
};
261275

@@ -266,8 +280,13 @@ struct SingleConsumerTypeTraits
266280

267281
OptionalType<ElementType> pop()
268282
{
269-
if (BaseType::pop_task_count.load(std::memory_order_acquire) == state.popped_task_count)
270-
return OptionalType<ElementType>{};
283+
if (state.local_pop_task_count == state.popped_task_count)
284+
{
285+
const auto stack_local_pop_task_count = BaseType::pop_task_count.load(std::memory_order_acquire);
286+
if (state.local_pop_task_count == stack_local_pop_task_count)
287+
return OptionalType<ElementType>{};
288+
state.local_pop_task_count = stack_local_pop_task_count;
289+
}
271290

272291
state.popped_task_count++;
273292

@@ -293,6 +312,7 @@ struct SingleConsumerTypeTraits
293312
template <typename BaseType>
294313
struct SharedState : BaseType
295314
{
315+
protected:
296316
CacheAlignedAndPaddedObject<std::atomic_size_t> pop_task_count{std::size_t{0}};
297317
};
298318

0 commit comments

Comments
 (0)