In core.atomic I think there is what's needed, cas and atomicOp:

I know what a cas is, but I don't have experience on this kind of coding. First try at a translation (doesn't work yet, and the global lock on buckets is missing still):


import core.atomic: atomicLoad, atomicOp, cas;
...

    public void transfer(in size_t from, in size_t to,
                         TBucketValue amount) {
        if (from == to) // Needed?
            return;
        buckets.RLock();
        while (true) {
            auto v1 = atomicLoad(buckets[from].value);
            if (amount > v1)
                amount = v1;
            if (cas(&buckets[from].value, v1, v1 - amount)) {
                atomicOp!"+="(buckets[to].value, amount);
                break;
            }
            // Else retry.
        }
        buckets.RUnlock();
        transfersCount++;
    }


Bye,
bearophile

Reply via email to