github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/misc/cgo/test/issue6997_linux.go (about)

     1  // Copyright 2014 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  // Test that pthread_cancel works as expected
     6  // (NPTL uses SIGRTMIN to implement thread cancelation)
     7  // See https://golang.org/issue/6997
     8  package cgotest
     9  
    10  /*
    11  #cgo CFLAGS: -pthread
    12  #cgo LDFLAGS: -pthread
    13  extern int StartThread();
    14  extern int CancelThread();
    15  */
    16  import "C"
    17  
    18  import "testing"
    19  import "time"
    20  
    21  func test6997(t *testing.T) {
    22  	r := C.StartThread()
    23  	if r != 0 {
    24  		t.Error("pthread_create failed")
    25  	}
    26  	c := make(chan C.int)
    27  	go func() {
    28  		time.Sleep(500 * time.Millisecond)
    29  		c <- C.CancelThread()
    30  	}()
    31  
    32  	select {
    33  	case r = <-c:
    34  		if r == 0 {
    35  			t.Error("pthread finished but wasn't canceled??")
    36  		}
    37  	case <-time.After(30 * time.Second):
    38  		t.Error("hung in pthread_cancel/pthread_join")
    39  	}
    40  }