]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/testcarchive/testdata/libgo2/libgo2.go
35c89ae92bbff744bc1be569d75f2639bcb279dd
[gostls13.git] / misc / cgo / testcarchive / testdata / libgo2 / libgo2.go
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 // Block blocks the current thread while running Go code.
53 //export Block
54 func Block() {
55         select {}
56 }
57
58 var P *byte
59
60 // TestSEGV makes sure that an invalid address turns into a run-time Go panic.
61 //export TestSEGV
62 func TestSEGV() {
63         defer func() {
64                 if recover() == nil {
65                         fmt.Fprintln(os.Stderr, "no panic from segv")
66                         os.Exit(1)
67                 }
68         }()
69         *P = 0
70         fmt.Fprintln(os.Stderr, "continued after segv")
71         os.Exit(1)
72 }
73
74 // Noop ensures that the Go runtime is initialized.
75 //export Noop
76 func Noop() {
77 }
78
79 // Raise SIGPIPE.
80 //export GoRaiseSIGPIPE
81 func GoRaiseSIGPIPE() {
82         C.CRaiseSIGPIPE()
83 }
84
85 func main() {
86 }