summaryrefslogtreecommitdiff
path: root/libphobos/libdruntime/core/internal/spinlock.d
blob: fb689dc561fc163335ee094f4810b922fa853e98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * SpinLock for runtime internal usage.
 *
 * Copyright: Copyright Digital Mars 2015 -.
 * License:   $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
 * Authors:   Martin Nowak
 * Source: $(DRUNTIMESRC core/internal/_spinlock.d)
 */
module core.internal.spinlock;

import core.atomic, core.thread;

shared struct SpinLock
{
    /// for how long is the lock usually contended
    enum Contention : ubyte
    {
        brief,
        medium,
        lengthy,
    }

@trusted @nogc nothrow:
    this(Contention contention)
    {
        this.contention = contention;
    }

    void lock()
    {
        if (cas(&val, size_t(0), size_t(1)))
            return;
        // Try to reduce the chance of another cas failure
        // TTAS lock (https://en.wikipedia.org/wiki/Test_and_test-and-set)
        immutable step = 1 << contention;
        while (true)
        {
            for (size_t n; atomicLoad!(MemoryOrder.raw)(val); n += step)
                yield(n);
            if (cas(&val, size_t(0), size_t(1)))
                return;
        }
    }

    void unlock()
    {
        atomicStore!(MemoryOrder.rel)(val, size_t(0));
    }

    /// yield with backoff
    void yield(size_t k)
    {
        if (k < pauseThresh)
            return pause();
        else if (k < 32)
            return Thread.yield();
        Thread.sleep(1.msecs);
    }

private:
    version (D_InlineAsm_X86)
        enum X86 = true;
    else version (D_InlineAsm_X86_64)
        enum X86 = true;
    else
        enum X86 = false;

    static if (X86)
    {
        enum pauseThresh = 16;
        void pause()
        {
            asm @trusted @nogc nothrow
            {
                // pause instruction
                rep;
                nop;
            }
        }
    }
    else
    {
        enum pauseThresh = 4;
        void pause()
        {
        }
    }

    size_t val;
    Contention contention;
}

// aligned to cacheline to avoid false sharing
shared align(64) struct AlignedSpinLock
{
    this(SpinLock.Contention contention)
    {
        impl = shared(SpinLock)(contention);
    }

    SpinLock impl;
    alias impl this;
}