github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/runtime/internal/atomic/atomic_test.go (about) 1 // Copyright 2015 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 package atomic_test 6 7 import ( 8 "runtime" 9 "runtime/internal/atomic" 10 "runtime/internal/sys" 11 "testing" 12 "unsafe" 13 ) 14 15 func runParallel(N, iter int, f func()) { 16 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(int(N))) 17 done := make(chan bool) 18 for i := 0; i < N; i++ { 19 go func() { 20 for j := 0; j < iter; j++ { 21 f() 22 } 23 done <- true 24 }() 25 } 26 for i := 0; i < N; i++ { 27 <-done 28 } 29 } 30 31 func TestXadduintptr(t *testing.T) { 32 const N = 20 33 const iter = 100000 34 inc := uintptr(100) 35 total := uintptr(0) 36 runParallel(N, iter, func() { 37 atomic.Xadduintptr(&total, inc) 38 }) 39 if want := uintptr(N * iter * inc); want != total { 40 t.Fatalf("xadduintpr error, want %d, got %d", want, total) 41 } 42 total = 0 43 runParallel(N, iter, func() { 44 atomic.Xadduintptr(&total, inc) 45 atomic.Xadduintptr(&total, uintptr(-int64(inc))) 46 }) 47 if total != 0 { 48 t.Fatalf("xadduintpr total error, want %d, got %d", 0, total) 49 } 50 } 51 52 // Tests that xadduintptr correctly updates 64-bit values. The place where 53 // we actually do so is mstats.go, functions mSysStat{Inc,Dec}. 54 func TestXadduintptrOnUint64(t *testing.T) { 55 if sys.BigEndian != 0 { 56 // On big endian architectures, we never use xadduintptr to update 57 // 64-bit values and hence we skip the test. (Note that functions 58 // mSysStat{Inc,Dec} in mstats.go have explicit checks for 59 // big-endianness.) 60 t.Skip("skip xadduintptr on big endian architecture") 61 } 62 const inc = 100 63 val := uint64(0) 64 atomic.Xadduintptr((*uintptr)(unsafe.Pointer(&val)), inc) 65 if inc != val { 66 t.Fatalf("xadduintptr should increase lower-order bits, want %d, got %d", inc, val) 67 } 68 }