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