Oh, my GoD! Goroutines on D
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Wed Jan 8 11:37:48 UTC 2025
On 08/01/2025 11:10 PM, Jin wrote:
> On Monday, 6 January 2025 at 10:15:39 UTC, Jin wrote:
>> I found a bug. I first checked if there was anything in the queue, and
>> if not, I checked if the queue was finalized. Sometimes the queue
>> would fill up between these two steps and I would lose data. I moved
>> the finalization check to the beginning and now everything is stable.
>
> I looked into the core.atomic code and was very disappointed. I expected
> to see memory barriers (necessary for wait-free), but I saw a CAS-
> spinlock there (and this is lock-free):
>
> ```d
> asm pure nothrow @nogc @trusted
> {
> naked;
> push RBX;
> mov R8, RDX;
> mov RAX, [RDX];
> mov RDX, 8[RDX];
> mov RBX, [RCX];
> mov RCX, 8[RCX];
> L1: lock; cmpxchg16b [R8];
> jne L1;
> pop RBX;
> ret;
> }
> ```
Dmd will not inline functions with inline assembly, any function calls
should prevent reordering cpu side.
So any concern for ordering you have shouldn't matter for dmd, its ldc
and gdc that you need to be worried about.
More information about the Digitalmars-d
mailing list