/
Atomic.pm
86 lines (73 loc) · 2.29 KB
/
Atomic.pm
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
#?if moar
my native atomicint is repr('P6int') is Int is ctype('atomic') { }
my class Atomic is repr('Uninstantiable') {
proto method fetch($) {*}
multi method fetch($source is rw) {
nqp::atomicload($source)
}
multi method fetch(atomicint $source is rw) {
nqp::atomicload_i($source)
}
proto method assign($, $) {*}
multi method assign($target is rw, $value) {
nqp::atomicstore($target, $value)
}
multi method assign(atomicint $target is rw, int $value) {
nqp::atomicstore_i($target, $value)
}
multi method assign(atomicint $target is rw, Int $value) {
nqp::atomicstore_i($target, $value)
}
multi method assign(atomicint $target is rw, $value) {
nqp::atomicstore_i($target, $value.Int)
}
method inc(atomicint $target is rw --> atomicint) {
nqp::atomicinc_i($target)
}
method dec(atomicint $target is rw --> atomicint) {
nqp::atomicdec_i($target)
}
method add(atomicint $target is rw, int $add --> atomicint) {
nqp::atomicadd_i($target, $add)
}
method full-barrier(--> Nil) {
nqp::barrierfull()
}
}
multi sub cas($target is rw, $expected, $value) {
nqp::cas($target, $expected, $value)
}
multi sub cas(atomicint $target is rw, int $expected, int $value) {
nqp::cas_i($target, $expected, $value)
}
multi sub cas(atomicint $target is rw, Int $expected, Int $value) {
nqp::cas_i($target, $expected, $value)
}
multi sub cas(atomicint $target is rw, $expected, $value) {
nqp::cas_i($target, $expected.Int, $value.Int)
}
multi sub cas($target is rw, &code) {
my $current := nqp::atomicload($target);
loop {
my $updated := code($current);
my $seen := nqp::cas($target, $current, $updated);
return $updated if nqp::eqaddr($seen, $current);
$current := $seen;
}
}
multi sub cas(atomicint $target is rw, &code) {
my int $current = nqp::atomicload_i($target);
loop {
my int $updated = code($current);
my int $seen = nqp::cas_i($target, $current, $updated);
return $updated if $seen == $current;
$current = $seen;
}
}
#?endif
#?if !moar
# Retain cheating cas for the sake of spectests that use it.
multi sub cas($target is rw, &code) {
$target = code($target)
}
#?endif