github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/runtime/race/testdata/mutex_test.go (about)

     1  // Copyright 2012 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 race_test
     6  
     7  import (
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestNoRaceMutex(t *testing.T) {
    14  	var mu sync.Mutex
    15  	var x int16 = 0
    16  	ch := make(chan bool, 2)
    17  	go func() {
    18  		mu.Lock()
    19  		defer mu.Unlock()
    20  		x = 1
    21  		ch <- true
    22  	}()
    23  	go func() {
    24  		mu.Lock()
    25  		x = 2
    26  		mu.Unlock()
    27  		ch <- true
    28  	}()
    29  	<-ch
    30  	<-ch
    31  }
    32  
    33  func TestRaceMutex(t *testing.T) {
    34  	var mu sync.Mutex
    35  	var x int16 = 0
    36  	ch := make(chan bool, 2)
    37  	go func() {
    38  		x = 1
    39  		mu.Lock()
    40  		defer mu.Unlock()
    41  		ch <- true
    42  	}()
    43  	go func() {
    44  		x = 2
    45  		mu.Lock()
    46  		mu.Unlock()
    47  		ch <- true
    48  	}()
    49  	<-ch
    50  	<-ch
    51  }
    52  
    53  func TestRaceMutex2(t *testing.T) {
    54  	var mu1 sync.Mutex
    55  	var mu2 sync.Mutex
    56  	var x int8 = 0
    57  	ch := make(chan bool, 2)
    58  	go func() {
    59  		mu1.Lock()
    60  		defer mu1.Unlock()
    61  		x = 1
    62  		ch <- true
    63  	}()
    64  	go func() {
    65  		mu2.Lock()
    66  		x = 2
    67  		mu2.Unlock()
    68  		ch <- true
    69  	}()
    70  	<-ch
    71  	<-ch
    72  }
    73  
    74  func TestNoRaceMutexPureHappensBefore(t *testing.T) {
    75  	var mu sync.Mutex
    76  	var x int16 = 0
    77  	ch := make(chan bool, 2)
    78  	go func() {
    79  		x = 1
    80  		mu.Lock()
    81  		mu.Unlock()
    82  		ch <- true
    83  	}()
    84  	go func() {
    85  		<-time.After(1e5)
    86  		mu.Lock()
    87  		mu.Unlock()
    88  		x = 1
    89  		ch <- true
    90  	}()
    91  	<-ch
    92  	<-ch
    93  }
    94  
    95  func TestNoRaceMutexSemaphore(t *testing.T) {
    96  	var mu sync.Mutex
    97  	ch := make(chan bool, 2)
    98  	x := 0
    99  	mu.Lock()
   100  	go func() {
   101  		x = 1
   102  		mu.Unlock()
   103  		ch <- true
   104  	}()
   105  	go func() {
   106  		mu.Lock()
   107  		x = 2
   108  		mu.Unlock()
   109  		ch <- true
   110  	}()
   111  	<-ch
   112  	<-ch
   113  }
   114  
   115  // from doc/go_mem.html
   116  func TestNoRaceMutexExampleFromHtml(t *testing.T) {
   117  	var l sync.Mutex
   118  	a := ""
   119  
   120  	l.Lock()
   121  	go func() {
   122  		a = "hello, world"
   123  		l.Unlock()
   124  	}()
   125  	l.Lock()
   126  	_ = a
   127  }
   128  
   129  func TestRaceMutexOverwrite(t *testing.T) {
   130  	c := make(chan bool, 1)
   131  	var mu sync.Mutex
   132  	go func() {
   133  		mu = sync.Mutex{}
   134  		c <- true
   135  	}()
   136  	mu.Lock()
   137  	<-c
   138  }