github.com/golang/gofrontend@v0.0.0-20240429183944-60f985a78526/libgo/misc/cgo/testcarchive/testdata/libgo2/libgo2.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 main
     6  
     7  /*
     8  #include <signal.h>
     9  #include <unistd.h>
    10  #include <stdlib.h>
    11  #include <stdio.h>
    12  
    13  // Raise SIGPIPE.
    14  static void CRaiseSIGPIPE() {
    15  	int fds[2];
    16  
    17  	if (pipe(fds) == -1) {
    18  		perror("pipe");
    19  		exit(EXIT_FAILURE);
    20  	}
    21  	// Close the reader end
    22  	close(fds[0]);
    23  	// Write to the writer end to provoke a SIGPIPE
    24  	if (write(fds[1], "some data", 9) != -1) {
    25  		fprintf(stderr, "write to a closed pipe succeeded\n");
    26  		exit(EXIT_FAILURE);
    27  	}
    28  	close(fds[1]);
    29  }
    30  */
    31  import "C"
    32  
    33  import (
    34  	"fmt"
    35  	"os"
    36  	"runtime"
    37  )
    38  
    39  // RunGoroutines starts some goroutines that don't do anything.
    40  // The idea is to get some threads going, so that a signal will be delivered
    41  // to a thread started by Go.
    42  //export RunGoroutines
    43  func RunGoroutines() {
    44  	for i := 0; i < 4; i++ {
    45  		go func() {
    46  			runtime.LockOSThread()
    47  			select {}
    48  		}()
    49  	}
    50  }
    51  
    52  var P *byte
    53  
    54  // TestSEGV makes sure that an invalid address turns into a run-time Go panic.
    55  //export TestSEGV
    56  func TestSEGV() {
    57  	defer func() {
    58  		if recover() == nil {
    59  			fmt.Fprintln(os.Stderr, "no panic from segv")
    60  			os.Exit(1)
    61  		}
    62  	}()
    63  	*P = 0
    64  	fmt.Fprintln(os.Stderr, "continued after segv")
    65  	os.Exit(1)
    66  }
    67  
    68  // Noop ensures that the Go runtime is initialized.
    69  //export Noop
    70  func Noop() {
    71  }
    72  
    73  // Raise SIGPIPE.
    74  //export GoRaiseSIGPIPE
    75  func GoRaiseSIGPIPE() {
    76  	C.CRaiseSIGPIPE()
    77  }
    78  
    79  func main() {
    80  }