A question about QuickQuiz 5.46
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that balance_count() and flush_local_count() are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable. Therefore, I believe that this quiz may be referring to the global variables globalcount and globalreserve.
Thanks,
Hao Lee
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.
Right.
And that's exactly what is mentioned in the answer to the quiz.
I guess it confused you because you didn't expect such an easy one, no ? :-)
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
Quoted 10 lines · Akira Yokosawa
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.
Quoted 5 lines
Right.
And that's exactly what is mentioned in the answer to the quiz.I guess it confused you because you didn't expect such an easy one, no ? :-)
In fact I'm confused about the question description.
I believe that this quiz should focus on the concurrency of global variables, rather than on the concurrency of the atomic variable counterandmax, which is currently the main focus of the description.
Quoted 9 lines
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
Quoted 14 lines · Hao Lee
Quoted 13 lines · Akira Yokosawa
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.Right.
And that's exactly what is mentioned in the answer to the quiz.
Quoted 7 lines
Quoted 2 lines
I guess it confused you because you didn't expect such an easy one, no ? :-)
In fact I'm confused about the question description.
I believe that this quiz should focus on the concurrency of global variables, rather than on the concurrency of the atomic variablecounterandmax, which is currently the main focus of the description.
Hmm, sorry but I'm afraid I don't get your point ...
Could you share the question you'd expect to see in place of Quick Quiz 5.46?
Thanks, Akira
Quoted 10 lines
Quoted 10 lines
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
Quoted 23 lines · Akira Yokosawa
Quoted 14 lines · Hao Lee
Quoted 13 lines · Akira Yokosawa
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.Right.
And that's exactly what is mentioned in the answer to the quiz.Quoted 7 lines
Quoted 2 lines
I guess it confused you because you didn't expect such an easy one, no ? :-)
In fact I'm confused about the question description.
I believe that this quiz should focus on the concurrency of global variables, rather than on the concurrency of the atomic variablecounterandmax, which is currently the main focus of the description.
Quoted 4 lines
Hmm, sorry but I'm afraid I don't get your point ...
Could you share the question you'd expect to see in place of Quick Quiz 5.46?
Hi, I don't think this question should exist according to my understanding...
Let's approach this question from a different perspective. The answer emphasizes that the gblcnt_mutex prevents concurrency problems. Let's assume that neither balance_count() nor flush_local_count() is holding the gblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.
Quoted 13 lines
Thanks, Akira
Quoted 10 lines
Quoted 10 lines
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
Quoted 32 lines · Hao Lee
Quoted 23 lines · Akira Yokosawa
Quoted 14 lines · Hao Lee
Quoted 13 lines · Akira Yokosawa
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.Right.
And that's exactly what is mentioned in the answer to the quiz.Quoted 7 lines
Quoted 2 lines
I guess it confused you because you didn't expect such an easy one, no ? :-)
In fact I'm confused about the question description.
I believe that this quiz should focus on the concurrency of global variables, rather than on the concurrency of the atomic variablecounterandmax, which is currently the main focus of the description.Quoted 4 lines
Hmm, sorry but I'm afraid I don't get your point ...
Could you share the question you'd expect to see in place of Quick Quiz 5.46?
Hi, I don't think this question should exist according to my understanding...
Quoted 6 lines
Let's approach this question from a different perspective. The answer emphasizes that the
gblcnt_mutexprevents concurrency problems. Let's assume that neitherbalance_count()norflush_local_count()is holding thegblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.
I'm not sure, but you might be missing subtle wording in the quiz part of Quick Quiz 5.46 (quoted below):
Given that the atomic_set() primitive does a simple store to the specified atomic_t, ...
Here, "simple store" means a normal store which doesn't involve any atomic operations (at instruction level).
atomic_read() and atomic_set() are for accessing atomic_t variables without any costly atomic operations.
Which is why Quick Quiz 5.46 has been there for more than a decade.
Perhaps, you might be interested in Quick Quiz B.12 as well.
HTH,
Akira
Quoted 14 lines
Quoted 14 lines
Thanks, Akira
Quoted 10 lines
Quoted 10 lines
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
Hi all,
I have a comment on this old thread.
Quoted 43 lines · Akira Yokosawa
Quoted 32 lines · Hao Lee
Quoted 23 lines · Akira Yokosawa
Quoted 14 lines · Hao Lee
Quoted 13 lines · Akira Yokosawa
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.Right.
And that's exactly what is mentioned in the answer to the quiz.Quoted 7 lines
Quoted 2 lines
I guess it confused you because you didn't expect such an easy one, no ? :-)
In fact I'm confused about the question description.
I believe that this quiz should focus on the concurrency of global variables, rather than on the concurrency of the atomic variablecounterandmax, which is currently the main focus of the description.Quoted 4 lines
Hmm, sorry but I'm afraid I don't get your point ...
Could you share the question you'd expect to see in place of Quick Quiz 5.46?
Hi, I don't think this question should exist according to my understanding...
Quoted 6 lines
Let's approach this question from a different perspective. The answer emphasizes that the
gblcnt_mutexprevents concurrency problems. Let's assume that neitherbalance_count()norflush_local_count()is holding thegblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.I'm not sure, but you might be missing subtle wording in the quiz part of Quick Quiz 5.46 (quoted below):
Quoted 8 lines
Given that the
atomic_set()primitive does a simple store to the specifiedatomic_t, ...Here, "simple store" means a normal store which doesn't involve any atomic operations (at instruction level).
atomic_read()andatomic_set()are for accessingatomic_tvariables without any costly atomic operations.
This was obvious up until commit cee469f9a7aa ("api: Switch x86 atomics to gcc C11-like atomics").
Before the change, atomic_read() and atomic_set() were defined in CodeSamples/api.h as:
typedef struct { volatile int counter; } atomic_t;
#define atomic_read(v) ((v)->counter)
#define atomic_set(v, i) (((v)->counter) = (i))Now, they are defined in CodeSamples/api-pthreads/api-gcc.h as:
typedef struct { volatile int counter; } atomic_t;
#define atomic_read(v) \
__atomic_load_n(&(v)->counter, __ATOMIC_RELAXED)
#define atomic_set(v, i) \
__atomic_store_n(&(v)->counter, (i), __ATOMIC_RELAXED)It is the C compiler's call what instructions to use for accessing atomic variables. Therefore, what Hao said above (quoted again for your convenience):
Quoted 5 lines
Quoted 5 lines
Let's
assume that neitherbalance_count()norflush_local_count()is holding thegblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.
is actually quite true.
Now I think QQz 5.46 needs at least some rewording.
Hao, apologies for my missing the change under CodeSamples/ at the time.
Paul, what do you think?
Thanks, Akira
Quoted 22 lines
Which is why Quick Quiz 5.46 has been there for more than a decade.
Perhaps, you might be interested in Quick Quiz B.12 as well.
HTH,
AkiraQuoted 14 lines
Quoted 14 lines
Thanks, Akira
Quoted 10 lines
Quoted 10 lines
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
Quoted 87 lines · Akira Yokosawa
Hi all,
I have a comment on this old thread.
Quoted 43 lines · Akira Yokosawa
Quoted 32 lines · Hao Lee
Quoted 23 lines · Akira Yokosawa
Quoted 14 lines · Hao Lee
Quoted 13 lines · Akira Yokosawa
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.Right.
And that's exactly what is mentioned in the answer to the quiz.Quoted 7 lines
Quoted 2 lines
I guess it confused you because you didn't expect such an easy one, no ? :-)
In fact I'm confused about the question description.
I believe that this quiz should focus on the concurrency of global variables, rather than on the concurrency of the atomic variablecounterandmax, which is currently the main focus of the description.Quoted 4 lines
Hmm, sorry but I'm afraid I don't get your point ...
Could you share the question you'd expect to see in place of Quick Quiz 5.46?
Hi, I don't think this question should exist according to my understanding...
Quoted 6 lines
Let's approach this question from a different perspective. The answer emphasizes that the
gblcnt_mutexprevents concurrency problems. Let's assume that neitherbalance_count()norflush_local_count()is holding thegblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.I'm not sure, but you might be missing subtle wording in the quiz part of Quick Quiz 5.46 (quoted below):
Quoted 8 lines
Given that the
atomic_set()primitive does a simple store to the specifiedatomic_t, ...Here, "simple store" means a normal store which doesn't involve any atomic operations (at instruction level).
atomic_read()andatomic_set()are for accessingatomic_tvariables without any costly atomic operations.This was obvious up until commit
cee469f9a7aa("api: Switch x86 atomics to gcc C11-like atomics").Before the change,
atomic_read()andatomic_set()were defined inCodeSamples/api.has:typedef struct { volatile int counter; } atomic_t; #define atomic_read(v) ((v)->counter) #define atomic_set(v, i) (((v)->counter) = (i))Now, they are defined in
CodeSamples/api-pthreads/api-gcc.has:typedef struct { volatile int counter; } atomic_t; #define atomic_read(v) \ __atomic_load_n(&(v)->counter, __ATOMIC_RELAXED) #define atomic_set(v, i) \ __atomic_store_n(&(v)->counter, (i), __ATOMIC_RELAXED)It is the C compiler's call what instructions to use for accessing atomic variables. Therefore, what Hao said above (quoted again for your convenience):
Quoted 5 lines
Quoted 5 lines
Let's
assume that neitherbalance_count()norflush_local_count()is holding thegblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.
Quoted 7 lines
is actually quite true.
Now I think QQz 5.46 needs at least some rewording.
Hao, apologies for my missing the change under CodeSamples/ at the time.
Paul, what do you think?
Let's see...
Hao Lee initially suggested that the focus be on the global nonatomics, which could work. A related question might be "Why does counterandmax need to be atomic?" Another approach would be to focus on wrong settings for counterandmax based on concurrent invocation. And there are a number of other possible confusions that might be caused by this code.
So would either of you like to propose a specific change?
Thanx, Paul
Quoted 25 lines
Thanks, Akira
Quoted 22 lines
Which is why Quick Quiz 5.46 has been there for more than a decade.
Perhaps, you might be interested in Quick Quiz B.12 as well.
HTH,
AkiraQuoted 14 lines
Quoted 14 lines
Thanks, Akira
Quoted 10 lines
Quoted 10 lines
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
Quoted 98 lines · Paul E. McKenney
Quoted 87 lines · Akira Yokosawa
Hi all,
I have a comment on this old thread.
Quoted 43 lines · Akira Yokosawa
Quoted 32 lines · Hao Lee
Quoted 23 lines · Akira Yokosawa
Quoted 14 lines · Hao Lee
Quoted 13 lines · Akira Yokosawa
Hi,
Quoted 6 lines · Hao Lee
Hi, Paul
I noticed that QuickQuiz 5.46 is causing confusion due to the fact that
balance_count()andflush_local_count()are both located within the slowpath and are called sequentially. As a result, it is impossible for them to compete for the counterandmax thread-local variable.Right.
And that's exactly what is mentioned in the answer to the quiz.Quoted 7 lines
Quoted 2 lines
I guess it confused you because you didn't expect such an easy one, no ? :-)
In fact I'm confused about the question description.
I believe that this quiz should focus on the concurrency of global variables, rather than on the concurrency of the atomic variablecounterandmax, which is currently the main focus of the description.Quoted 4 lines
Hmm, sorry but I'm afraid I don't get your point ...
Could you share the question you'd expect to see in place of Quick Quiz 5.46?
Hi, I don't think this question should exist according to my understanding...
Quoted 6 lines
Let's approach this question from a different perspective. The answer emphasizes that the
gblcnt_mutexprevents concurrency problems. Let's assume that neitherbalance_count()norflush_local_count()is holding thegblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.I'm not sure, but you might be missing subtle wording in the quiz part of Quick Quiz 5.46 (quoted below):
Quoted 8 lines
Given that the
atomic_set()primitive does a simple store to the specifiedatomic_t, ...Here, "simple store" means a normal store which doesn't involve any atomic operations (at instruction level).
atomic_read()andatomic_set()are for accessingatomic_tvariables without any costly atomic operations.This was obvious up until commit
cee469f9a7aa("api: Switch x86 atomics to gcc C11-like atomics").Before the change,
atomic_read()andatomic_set()were defined inCodeSamples/api.has:typedef struct { volatile int counter; } atomic_t; #define atomic_read(v) ((v)->counter) #define atomic_set(v, i) (((v)->counter) = (i))Now, they are defined in
CodeSamples/api-pthreads/api-gcc.has:typedef struct { volatile int counter; } atomic_t; #define atomic_read(v) \ __atomic_load_n(&(v)->counter, __ATOMIC_RELAXED) #define atomic_set(v, i) \ __atomic_store_n(&(v)->counter, (i), __ATOMIC_RELAXED)It is the C compiler's call what instructions to use for accessing atomic variables. Therefore, what Hao said above (quoted again for your convenience):
Quoted 5 lines
Quoted 5 lines
Let's
assume that neitherbalance_count()norflush_local_count()is holding thegblcnt_mutex. Would the counterandmax variable encounter concurrency problems? I don't think so because counterandmax is an atomic variable and is always safe, even without using any locks.Quoted 7 lines
is actually quite true.
Now I think QQz 5.46 needs at least some rewording.
Hao, apologies for my missing the change under CodeSamples/ at the time.
Paul, what do you think?
Let's see...
Quoted 7 lines
Hao Lee initially suggested that the focus be on the global nonatomics, which could work. A related question might be "Why does counterandmax need to be atomic?" Another approach would be to focus on wrong settings for counterandmax based on concurrent invocation. And there are a number of other possible confusions that might be caused by this code.
So would either of you like to propose a specific change?
I'm sending a patch set as replies.
1/2 is a trivial fix of coding style.
2/2 is an RFC updating the QQ.
diff stat looks like this:
-------
Akira Yokosawa (2):
count_lim_atomic: Enclose complex loop with {}
count: Update QQ 5.46
CodeSamples/count/count_lim_atomic.c | 5 ++-- count/count.tex | 36 +++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-)
base-commit: 4ca36874306b39ed5bfce5a50e289f966f485334
Signature · 1 line
2.34.1
Quoted 28 lines
Thanx, Paul
Quoted 25 lines
Thanks, Akira
Quoted 22 lines
Which is why Quick Quiz 5.46 has been there for more than a decade.
Perhaps, you might be interested in Quick Quiz B.12 as well.
HTH,
AkiraQuoted 14 lines
Quoted 14 lines
Thanks, Akira
Quoted 10 lines
Quoted 10 lines
Thanks, Akira
Quoted 6 lines
Therefore,
I believe that this quiz may be referring to the global variables globalcount and globalreserve.Thanks,
Hao Lee
PATCH 1/2 count_lim_atomic.c: Enclose complex loop with {}
Subject: [PATCH -perfbook 1/2] count_lim_atomic.c: Enclose complex loop with {}
Let's get consistent with coding style of Linux kernel.
--- CodeSamples/count/count_lim_atomic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
CodeSamples/count/count_lim_atomic.c +3 -2
@@ -84,13 +84,14 @@ static void flush_local_count(void) //\lnlbl{flush:b}if (globalreserve ==0 )//\lnlbl{flush:checkrsv} return ;//\lnlbl{flush:return:n} zero =merge_counterandmax (0 ,0 );//\lnlbl{flush:initzero} -for_each_thread (t)//\lnlbl{flush:loop:b} +for_each_thread (t) {//\lnlbl{flush:loop:b} if (counterp[t] !=NULL ) {//\lnlbl{flush:checkp} old =atomic_xchg (counterp[t], zero);//\lnlbl{flush:atmxchg} split_counterandmax_int (old, &c, &cm);//\lnlbl{flush:split} globalcount += c;//\lnlbl{flush:glbcnt} globalreserve -= cm;//\lnlbl{flush:glbrsv} - }//\lnlbl{flush:loop:e} + } + }//\lnlbl{flush:loop:e} }//\lnlbl{flush:e} //\end{snippet}
Signature · 1 line
2.34.1
RFC PATCH 2/2 count: Update QQ 5.46
In reply to Akira YokosawaCommit cee469f9a7aa ("api: Switch x86 atomics to gcc C11-like atomics") changed the definition of atomic_set() as follows:
#define atomic_set(v, i) \ __atomic_store_n(&(v)->counter, (i), __ATOMIC_RELAXED)
Previously, it was defined for x86 as follows:
#define atomic_set(v, i) (((v)->counter) = (i))
QQ 5.46 assumed the old definition and it can confuse those who have followed the definition under CodeSamples/.
Reword QQ 5.46 to prevent such confusions.
Add a related QQ asking the need of atomic operation for atomic_set().
--- count/count.tex | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-)
count/count.tex +30 -6
@@ -2257,19 +2257,43 @@ as it is with the \co{count_register_thread()} function starting on \clnref{unregister:b}. \end{fcvref} -\QuickQuiz{ - Given that the \co{atomic_set()} primitive does a simple - store to the specified \co{atomic_t}, how can +\QuickQuizSeries{% +\QuickQuizB{ + How can \clnrefr{ln:count:count_lim_atomic:utility2:balance:atmcset} of \co{balance_count()} in \cref{lst:count:Atomic Limit Counter Utility Functions 2} work correctly in face of concurrent \co{flush_local_count()} updates to this variable? -}\QuickQuizAnswer{ - The caller of both \co{balance_count()} and +}\QuickQuizAnswerB{ + It wouldn't work if they were executed concurrently! + But no worries. + The callers of both \co{balance_count()} and \co{flush_local_count()} hold \co{gblcnt_mutex}, so only one may be executing at a given time. -}\QuickQuizEnd +}\QuickQuizEndB +% +\QuickQuizE{ + Does the \co{atomic_set()} primitive in \co{balance_count()} + really need to be atomic? +}\QuickQuizAnswerE{ + No, it does not. + As far as the code in question is concerned, + the \co{atomic_set()} updates the per-thread variable + \co{counterandmax} of its own, whose value can only be updated + either from its own thread's fastpaths or from slowpaths of all + updaters. + As the \co{atomic_set()} is in the slowpath, it can never + race with other atomic operations. + + So, even if this code is ported to an ISA where a plain store + to an \co{atomic_t} can interfere with atomic read-modify-write + operations, a non-atomic \co{atomic_set()} should work. + + That said, such an optimization in slowpaths which is effective + only for those peculiar ISAs might not be worth bothering. +}\QuickQuizEndE +} % End of \QuickQuizSeries The next section qualitatively evaluates this design. \ebFloatBarrier
Signature · 1 line
2.34.1
Paul E. McKenney
AppliedRe: [RFC PATCH -perfbook 2/2] count: Update QQ 5.46
Quoted 13 lines · Akira Yokosawa
Commit
cee469f9a7aa("api: Switch x86 atomics to gcc C11-like atomics") changed the definition ofatomic_set()as follows:#define atomic_set(v, i) \ __atomic_store_n(&(v)->counter, (i), __ATOMIC_RELAXED)Previously, it was defined for x86 as follows:
#define atomic_set(v, i) (((v)->counter) = (i))QQ 5.46 assumed the old definition and it can confuse those who have followed the definition under CodeSamples/.
Quoted 7 lines
Reword QQ 5.46 to prevent such confusions.
Add a related QQ asking the need of atomic operation for
atomic_set().Reported-by Hao Lee <[email protected]>Signed-off-by Akira Yokosawa <[email protected]>
Queued and pushed both, thank you, and please accept my apologies for the delay!
Thanx, Paul
Quoted 62 lines
--- count/count.tex | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-)count/count.tex +30
-6@@ -2257,19 +2257,43 @@ as it is with the \co{count_register_thread()} function starting on \clnref{unregister:b}. \end{fcvref} -\QuickQuiz{ - Given that the \co{atomic_set()} primitive does a simple - store to the specified \co{atomic_t}, how can +\QuickQuizSeries{% +\QuickQuizB{ + How can \clnrefr{ln:count:count_lim_atomic:utility2:balance:atmcset} of \co{balance_count()} in \cref{lst:count:Atomic Limit Counter Utility Functions 2} work correctly in face of concurrent \co{flush_local_count()} updates to this variable? -}\QuickQuizAnswer{ - The caller of both \co{balance_count()} and +}\QuickQuizAnswerB{ + It wouldn't work if they were executed concurrently! + But no worries. + The callers of both \co{balance_count()} and \co{flush_local_count()} hold \co{gblcnt_mutex}, so only one may be executing at a given time. -}\QuickQuizEnd +}\QuickQuizEndB +% +\QuickQuizE{ + Does the \co{atomic_set()} primitive in \co{balance_count()} + really need to be atomic? +}\QuickQuizAnswerE{ + No, it does not. + As far as the code in question is concerned, + the \co{atomic_set()} updates the per-thread variable + \co{counterandmax} of its own, whose value can only be updated + either from its own thread's fastpaths or from slowpaths of all + updaters. + As the \co{atomic_set()} is in the slowpath, it can never + race with other atomic operations. + + So, even if this code is ported to an ISA where a plain store + to an \co{atomic_t} can interfere with atomic read-modify-write + operations, a non-atomic \co{atomic_set()} should work. + + That said, such an optimization in slowpaths which is effective + only for those peculiar ISAs might not be worth bothering. +}\QuickQuizEndE +} % End of \QuickQuizSeries The next section qualitatively evaluates this design. \ebFloatBarrierSignature · 1 line
2.34.1