Source file
src/runtime/race/race_windows_test.go
1
2
3
4
5
6
7
8 package race_test
9
10 import (
11 "sync/atomic"
12 "syscall"
13 "testing"
14 "unsafe"
15 )
16
17 func TestAtomicMmap(t *testing.T) {
18
19
20
21 kernel32 := syscall.NewLazyDLL("kernel32.dll")
22 VirtualAlloc := kernel32.NewProc("VirtualAlloc")
23 VirtualFree := kernel32.NewProc("VirtualFree")
24 const (
25 MEM_COMMIT = 0x00001000
26 MEM_RESERVE = 0x00002000
27 MEM_RELEASE = 0x8000
28 PAGE_READWRITE = 0x04
29 )
30 mem, _, err := syscall.Syscall6(VirtualAlloc.Addr(), 4, 0, 1<<20, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE, 0, 0)
31 if err != 0 {
32 t.Fatalf("VirtualAlloc failed: %v", err)
33 }
34 defer syscall.Syscall(VirtualFree.Addr(), 3, mem, 1<<20, MEM_RELEASE)
35 a := (*uint64)(unsafe.Pointer(mem))
36 if *a != 0 {
37 t.Fatalf("bad atomic value: %v, want 0", *a)
38 }
39 atomic.AddUint64(a, 1)
40 if *a != 1 {
41 t.Fatalf("bad atomic value: %v, want 1", *a)
42 }
43 atomic.AddUint64(a, 1)
44 if *a != 2 {
45 t.Fatalf("bad atomic value: %v, want 2", *a)
46 }
47 }
48
View as plain text