[M3devel] race conditions in lock-free code...
Jay K
jay.krell at cornell.edu
Mon Aug 27 05:08:31 CEST 2012
We have race conditions, like this, m3core/src/Text.m3:
VAR fromCharCache := ARRAY CHAR OF T {NIL, ..}; (* 1-char texts *)
PROCEDURE FromChar (c: CHAR): T =
VAR buf: ARRAY [0..0] OF CHAR;
BEGIN
IF fromCharCache [c] = NIL THEN
buf [0] := c;
fromCharCache[c] := Text8.New (buf);
END;
RETURN fromCharCache [c]
END FromChar;
It should be:
PROCEDURE FromChar (c: CHAR): T =
VAR buf: ARRAY [0..0] OF CHAR;
BEGIN
IF fromCharCache [c] = NIL THEN
buf [0] := c;
WITH a = Text8.New (buf) DO
MemoryBarrier();
fromCharCache[c] := a;
END;
END;
RETURN fromCharCache [c]
END FromChar;
to ensure that all of Text8.New() finishes before the assignment to fromCharCache[c] is made.
Can the compiler somehow catch these?
I fear they are a small epidemic.
For a long time people didn't realize where all the compiler and processor could reorder.
Do we have the right constructs by now to fix them?
I think we do.
- Jay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20120827/34ea11e2/attachment-0001.html>
More information about the M3devel
mailing list