blob: c21ecf671a25376997ac5c1861c29c04184b5118 (
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
|
// run
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"runtime"
)
const N = 100000
func main() {
// Allocate more Ps than processors. This raises
// the chance that we get interrupted by the OS
// in exactly the right (wrong!) place.
p := runtime.NumCPU()
runtime.GOMAXPROCS(2 * p)
// Allocate some pointers.
ptrs := make([]*int, p)
for i := 0; i < p; i++ {
ptrs[i] = new(int)
}
// Arena where we read and write pointers like crazy.
collider := make([]*int, p)
done := make(chan struct{}, 2*p)
// Start writers. They alternately write a pointer
// and nil to a slot in the collider.
for i := 0; i < p; i++ {
i := i
go func() {
for j := 0; j < N; j++ {
// Write a pointer using memmove.
copy(collider[i:i+1], ptrs[i:i+1])
// Write nil using memclr.
// (This is a magic loop that gets lowered to memclr.)
r := collider[i : i+1]
for k := range r {
r[k] = nil
}
}
done <- struct{}{}
}()
}
// Start readers. They read pointers from slots
// and make sure they are valid.
for i := 0; i < p; i++ {
i := i
go func() {
for j := 0; j < N; j++ {
var ptr [1]*int
copy(ptr[:], collider[i:i+1])
if ptr[0] != nil && ptr[0] != ptrs[i] {
panic(fmt.Sprintf("bad pointer read %p!", ptr[0]))
}
}
done <- struct{}{}
}()
}
for i := 0; i < 2*p; i++ {
<-done
}
}
|