...

Source file src/cmd/cgo/internal/testcarchive/testdata/libgo2/libgo2.go

Documentation: cmd/cgo/internal/testcarchive/testdata/libgo2

     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  //
    43  //export RunGoroutines
    44  func RunGoroutines() {
    45  	for i := 0; i < 4; i++ {
    46  		go func() {
    47  			runtime.LockOSThread()
    48  			select {}
    49  		}()
    50  	}
    51  }
    52  
    53  // Block blocks the current thread while running Go code.
    54  //
    55  //export Block
    56  func Block() {
    57  	select {}
    58  }
    59  
    60  var P *byte
    61  
    62  // TestSEGV makes sure that an invalid address turns into a run-time Go panic.
    63  //
    64  //export TestSEGV
    65  func TestSEGV() {
    66  	defer func() {
    67  		if recover() == nil {
    68  			fmt.Fprintln(os.Stderr, "no panic from segv")
    69  			os.Exit(1)
    70  		}
    71  	}()
    72  	*P = 0
    73  	fmt.Fprintln(os.Stderr, "continued after segv")
    74  	os.Exit(1)
    75  }
    76  
    77  // Noop ensures that the Go runtime is initialized.
    78  //
    79  //export Noop
    80  func Noop() {
    81  }
    82  
    83  // Raise SIGPIPE.
    84  //
    85  //export GoRaiseSIGPIPE
    86  func GoRaiseSIGPIPE() {
    87  	C.CRaiseSIGPIPE()
    88  }
    89  
    90  func main() {
    91  }
    92  

View as plain text