int __thread theft = 0;
static void flush_local_count_sig(int unused)
{
if (READ_ONCE(theft) != THEFT_REQ) (*)
return;
smp_mb();
WRITE_ONCE(theft, THEFT_ACK)
if (!counting) {
WRITE_ONCE(theft, THEFT_READY);
}
smp_mb();
}
Why are there READ_ONCE and WRITE_ONCE wrappers around the uses of
the theft per-thread variable?
答案是
可以认为第一个(用(*)注释)是不必要的。
Why the first READ_ONCE is not necessary? After all, the compiler can try to be too smart and optimize it out.
int __thread theft = 0;
static void flush_local_count_sig(int unused)
{
if (theft != THEFT_REQ)
return;
smp_mb(); (*)
WRITE_ONCE(theft, THEFT_ACK)
if (!counting) {
WRITE_ONCE(theft, THEFT_READY);
}
smp_mb();
}
theft is not accessed concurrently- it is accessed by one thread and the signal handler.
What about correctness if we remove line annotated with (*)?