ldc-developers / ldc

The LLVM-based D Compiler.
http://wiki.dlang.org/LDC
Other
1.19k stars 257 forks source link

Thread sanitizer detects atomicOp!"" as data races #973

Open HK47196 opened 9 years ago

HK47196 commented 9 years ago

Hi, I'm not sure how well supported threadsanitizer is by LDC(it seems to work OK,) but using atomicOp!"" on the same variable seems to cause tsan to think it's a data race.

I'm not 100% sure this is a bug, as I'm not an expert with atomics.

I think it has to do with it not detecting atomicLoad as an atomic operation(??) as it says cas contains an atomic write, but it just calls atomicLoad a regular load.

Happens in both LDC 0.15.1 and a HEAD build.

example:

import core.atomic;
import std.parallelism;
import std.range;
import std.stdio;
void main(){
    shared(size_t) num = 0;
    foreach(k; parallel(iota(1_000))){
        atomicOp!"+="(num, k);
    }
    writeln(atomicLoad(num));
}

compile with: ldc -sanitize=thread filename.d

(BTW, this seems to output errors related to a potential deadlock aswell. I'm unsure if that's related to this bug.)

JohanEngelen commented 3 years ago

I cannot reproduce this with LDC 1.24.