atomic_test.go 2.89 KB
Newer Older
1 2 3 4
// 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.

5
package atomic_test
6 7 8

import (
	"runtime"
9
	"runtime/internal/atomic"
10
	"runtime/internal/sys"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	"testing"
	"unsafe"
)

func runParallel(N, iter int, f func()) {
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(int(N)))
	done := make(chan bool)
	for i := 0; i < N; i++ {
		go func() {
			for j := 0; j < iter; j++ {
				f()
			}
			done <- true
		}()
	}
	for i := 0; i < N; i++ {
		<-done
	}
}

func TestXadduintptr(t *testing.T) {
Russ Cox's avatar
Russ Cox committed
32 33 34 35 36 37
	N := 20
	iter := 100000
	if testing.Short() {
		N = 10
		iter = 10000
	}
38 39 40
	inc := uintptr(100)
	total := uintptr(0)
	runParallel(N, iter, func() {
41
		atomic.Xadduintptr(&total, inc)
42
	})
Russ Cox's avatar
Russ Cox committed
43
	if want := uintptr(N*iter) * inc; want != total {
44 45 46 47
		t.Fatalf("xadduintpr error, want %d, got %d", want, total)
	}
	total = 0
	runParallel(N, iter, func() {
48 49
		atomic.Xadduintptr(&total, inc)
		atomic.Xadduintptr(&total, uintptr(-int64(inc)))
50 51 52 53 54 55
	})
	if total != 0 {
		t.Fatalf("xadduintpr total error, want %d, got %d", 0, total)
	}
}

56
// Tests that xadduintptr correctly updates 64-bit values. The place where
57 58
// we actually do so is mstats.go, functions mSysStat{Inc,Dec}.
func TestXadduintptrOnUint64(t *testing.T) {
59
	if sys.BigEndian {
60 61 62 63
		// On big endian architectures, we never use xadduintptr to update
		// 64-bit values and hence we skip the test.  (Note that functions
		// mSysStat{Inc,Dec} in mstats.go have explicit checks for
		// big-endianness.)
64 65
		t.Skip("skip xadduintptr on big endian architecture")
	}
66 67
	const inc = 100
	val := uint64(0)
68
	atomic.Xadduintptr((*uintptr)(unsafe.Pointer(&val)), inc)
69 70 71 72
	if inc != val {
		t.Fatalf("xadduintptr should increase lower-order bits, want %d, got %d", inc, val)
	}
}
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

func shouldPanic(t *testing.T, name string, f func()) {
	defer func() {
		if recover() == nil {
			t.Errorf("%s did not panic", name)
		}
	}()
	f()
}

// Variant of sync/atomic's TestUnaligned64:
func TestUnaligned64(t *testing.T) {
	// Unaligned 64-bit atomics on 32-bit systems are
	// a continual source of pain. Test that on 32-bit systems they crash
	// instead of failing silently.

	switch runtime.GOARCH {
	default:
		if unsafe.Sizeof(int(0)) != 4 {
			t.Skip("test only runs on 32-bit systems")
		}
94 95
	case "amd64p32":
		// amd64p32 can handle unaligned atomics.
96 97 98 99
		t.Skipf("test not needed on %v", runtime.GOARCH)
	}

	x := make([]uint32, 4)
100 101 102 103
	u := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) | 4) // force alignment to 4

	up64 := (*uint64)(u) // misaligned
	p64 := (*int64)(u)   // misaligned
104 105 106 107

	shouldPanic(t, "Load64", func() { atomic.Load64(up64) })
	shouldPanic(t, "Loadint64", func() { atomic.Loadint64(p64) })
	shouldPanic(t, "Store64", func() { atomic.Store64(up64, 0) })
108 109 110
	shouldPanic(t, "Xadd64", func() { atomic.Xadd64(up64, 1) })
	shouldPanic(t, "Xchg64", func() { atomic.Xchg64(up64, 1) })
	shouldPanic(t, "Cas64", func() { atomic.Cas64(up64, 1, 2) })
111
}