diff --git a/README.md b/README.md index 876491f..5c4238f 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,12 @@ for all the actors to terminate and clean up all the resources used. +### Valgrind + +To make the library aware of being run with helgrind, the program needs to be +built with `-d:usesValgrind`, otherwise false positives could be reported. + + ### More to come Sure. diff --git a/actors/pool.nim b/actors/pool.nim index f072af4..0415758 100644 --- a/actors/pool.nim +++ b/actors/pool.nim @@ -6,8 +6,6 @@ import isisolated import mallinfo import valgrind -{.emit:"#include ".} - type Pool* = object diff --git a/actors/valgrind.nim b/actors/valgrind.nim index 565df1d..6a9b3c6 100644 --- a/actors/valgrind.nim +++ b/actors/valgrind.nim @@ -1,20 +1,26 @@ -{.emit:"#include ".} +const + usesValgrind {.booldefine.} = false -template valgrind_annotate_happens_before*(x) = - block: - let y {.exportc,inject.} = x - {.emit:"ANNOTATE_HAPPENS_BEFORE(y);".} +when usesValgrind: + const header = "" -template valgrind_annotate_happens_after*(x) = - block: - let y {.exportc,inject.} = x - {.emit:"ANNOTATE_HAPPENS_AFTER(y);".} + proc valgrind_annotate_happens_before*(x: pointer) {. + header: header, importc: "ANNOTATE_HAPPENS_BEFORE".} + proc valgrind_annotate_happens_after*(x: pointer) {. + header: header, importc: "ANNOTATE_HAPPENS_AFTER".} + proc valgrind_annotate_happens_before_forget_all*(x: pointer) {. + header: header, importc: "ANNOTATE_HAPPENS_BEFORE_FORGET_ALL".} -template valgrind_annotate_happens_before_forget_all*(x) = - block: - let y {.exportc,inject.} = x - {.emit:"ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(y);".} + let enabled {.header: header, importc: "RUNNING_ON_VALGRIND".}: bool -proc running_on_valgrind*(): bool = - {.emit: "result = RUNNING_ON_VALGRIND;".} + proc running_on_valgrind*(): bool = + {.cast(noSideEffect), cast(gcSafe).}: + result = enabled + +else: + proc valgrind_annotate_happens_before*(x: pointer) = discard + proc valgrind_annotate_happens_after*(x: pointer) = discard + proc valgrind_annotate_happens_before_forget_all*(x: pointer) = discard + + template running_on_valgrind*(): bool = false diff --git a/tests/nim.cfg b/tests/nim.cfg new file mode 100644 index 0000000..fdc9b27 --- /dev/null +++ b/tests/nim.cfg @@ -0,0 +1 @@ +--define:usesValgrind \ No newline at end of file