...
Run Format

Source file src/runtime/race/race_linux_test.go

Documentation: runtime/race

     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  // +build linux,race
     6  
     7  package race_test
     8  
     9  import (
    10  	"sync/atomic"
    11  	"syscall"
    12  	"testing"
    13  	"unsafe"
    14  )
    15  
    16  func TestAtomicMmap(t *testing.T) {
    17  	// Test that atomic operations work on "external" memory. Previously they crashed (#16206).
    18  	// Also do a sanity correctness check: under race detector atomic operations
    19  	// are implemented inside of race runtime.
    20  	mem, err := syscall.Mmap(-1, 0, 1<<20, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
    21  	if err != nil {
    22  		t.Fatalf("mmap failed: %v", err)
    23  	}
    24  	defer syscall.Munmap(mem)
    25  	a := (*uint64)(unsafe.Pointer(&mem[0]))
    26  	if *a != 0 {
    27  		t.Fatalf("bad atomic value: %v, want 0", *a)
    28  	}
    29  	atomic.AddUint64(a, 1)
    30  	if *a != 1 {
    31  		t.Fatalf("bad atomic value: %v, want 1", *a)
    32  	}
    33  	atomic.AddUint64(a, 1)
    34  	if *a != 2 {
    35  		t.Fatalf("bad atomic value: %v, want 2", *a)
    36  	}
    37  }
    38  

View as plain text