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