github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/runtime/race/race_windows_test.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build windows && race 6 // +build windows,race 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 // Test that atomic operations work on "external" memory. Previously they crashed (#16206). 19 // Also do a sanity correctness check: under race detector atomic operations 20 // are implemented inside of race runtime. 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 }