Black Lives Matter. Support the Equal Justice Initiative.

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  //go:build linux && race
     6  // +build linux,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  	mem, err := syscall.Mmap(-1, 0, 1<<20, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_PRIVATE)
    22  	if err != nil {
    23  		t.Fatalf("mmap failed: %v", err)
    24  	}
    25  	defer syscall.Munmap(mem)
    26  	a := (*uint64)(unsafe.Pointer(&mem[0]))
    27  	if *a != 0 {
    28  		t.Fatalf("bad atomic value: %v, want 0", *a)
    29  	}
    30  	atomic.AddUint64(a, 1)
    31  	if *a != 1 {
    32  		t.Fatalf("bad atomic value: %v, want 1", *a)
    33  	}
    34  	atomic.AddUint64(a, 1)
    35  	if *a != 2 {
    36  		t.Fatalf("bad atomic value: %v, want 2", *a)
    37  	}
    38  }
    39  

View as plain text