summaryrefslogtreecommitdiff
path: root/misc/cgo
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2012-04-06 15:14:11 +0200
committerOndřej Surý <ondrej@sury.org>2012-04-06 15:14:11 +0200
commit505c19580e0f43fe5224431459cacb7c21edd93d (patch)
tree79e2634c253d60afc0cc0b2f510dc7dcbb48497b /misc/cgo
parent1336a7c91e596c423a49d1194ea42d98bca0d958 (diff)
downloadgolang-505c19580e0f43fe5224431459cacb7c21edd93d.tar.gz
Imported Upstream version 1upstream/1
Diffstat (limited to 'misc/cgo')
-rw-r--r--misc/cgo/gmp/Makefile38
-rw-r--r--misc/cgo/gmp/fib.go4
-rw-r--r--misc/cgo/gmp/gmp.go26
-rw-r--r--misc/cgo/gmp/pi.go6
-rw-r--r--misc/cgo/life/Makefile21
-rw-r--r--misc/cgo/life/main.go4
-rwxr-xr-xmisc/cgo/life/test.bash7
-rw-r--r--misc/cgo/stdio/Makefile17
-rw-r--r--misc/cgo/stdio/chain.go4
-rw-r--r--misc/cgo/stdio/fib.go6
-rw-r--r--misc/cgo/stdio/file.go2
-rw-r--r--misc/cgo/stdio/hello.go4
-rwxr-xr-xmisc/cgo/stdio/test.bash9
-rw-r--r--misc/cgo/test/Makefile22
-rw-r--r--misc/cgo/test/align.go4
-rw-r--r--misc/cgo/test/backdoor/backdoor.go7
-rw-r--r--misc/cgo/test/backdoor/runtime.c (renamed from misc/cgo/test/runtime.c)4
-rw-r--r--misc/cgo/test/basic.go47
-rw-r--r--misc/cgo/test/callback.go14
-rw-r--r--misc/cgo/test/callback_c.c25
-rw-r--r--misc/cgo/test/cgo_test.go3
-rw-r--r--misc/cgo/test/env.go9
-rw-r--r--misc/cgo/test/helpers.go35
-rw-r--r--misc/cgo/test/issue1328.go2
-rw-r--r--misc/cgo/test/issue1560.go15
-rw-r--r--misc/cgo/test/issue2462.go102
-rw-r--r--misc/cgo/test/sleep_windows.go16
-rw-r--r--misc/cgo/testso/cgoso.go19
-rw-r--r--misc/cgo/testso/cgoso_c.c11
-rw-r--r--misc/cgo/testso/main.go13
-rwxr-xr-xmisc/cgo/testso/test.bash10
31 files changed, 375 insertions, 131 deletions
diff --git a/misc/cgo/gmp/Makefile b/misc/cgo/gmp/Makefile
deleted file mode 100644
index fc6209f27..000000000
--- a/misc/cgo/gmp/Makefile
+++ /dev/null
@@ -1,38 +0,0 @@
-# Copyright 2009 The Go Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style
-# license that can be found in the LICENSE file.
-
-include ../../../src/Make.inc
-
-TARG=gmp
-
-# Can have plain GOFILES too, but this example doesn't.
-
-CGOFILES=\
- gmp.go
-
-CGO_LDFLAGS=-lgmp
-
-# To add flags necessary for locating the library or its include files,
-# set CGO_CFLAGS or CGO_LDFLAGS. For example, to use an
-# alternate installation of the library:
-# CGO_CFLAGS=-I/home/rsc/gmp32/include
-# CGO_LDFLAGS+=-L/home/rsc/gmp32/lib
-# Note the += on the second line.
-
-CLEANFILES+=pi fib
-
-include ../../../src/Make.pkg
-
-# Simple test programs
-
-# Computes 1000 digits of pi; single-threaded.
-pi: install pi.go
- $(GC) pi.go
- $(LD) -o $@ pi.$O
-
-# Computes 200 Fibonacci numbers; multi-threaded.
-fib: install fib.go
- $(GC) fib.go
- $(LD) -o $@ fib.$O
-
diff --git a/misc/cgo/gmp/fib.go b/misc/cgo/gmp/fib.go
index 3eda39e17..18434beaf 100644
--- a/misc/cgo/gmp/fib.go
+++ b/misc/cgo/gmp/fib.go
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// +build ignore
+
// Compute Fibonacci numbers with two goroutines
// that pass integers back and forth. No actual
// concurrency, just threads and synchronization
@@ -10,7 +12,7 @@
package main
import (
- big "gmp"
+ big "."
"runtime"
)
diff --git a/misc/cgo/gmp/gmp.go b/misc/cgo/gmp/gmp.go
index 3dbc022ce..3bcf99151 100644
--- a/misc/cgo/gmp/gmp.go
+++ b/misc/cgo/gmp/gmp.go
@@ -98,8 +98,20 @@ Go to hang on to a reference to the pointer until C is done with it.
*/
package gmp
-// #include <gmp.h>
-// #include <stdlib.h>
+/*
+#cgo LDFLAGS: -lgmp
+#include <gmp.h>
+#include <stdlib.h>
+
+// gmp 5.0.0+ changed the type of the 3rd argument to mp_bitcnt_t,
+// so, to support older versions, we wrap these two functions.
+void _mpz_mul_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
+ mpz_mul_2exp(a, b, n);
+}
+void _mpz_div_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
+ mpz_div_2exp(a, b, n);
+}
+*/
import "C"
import (
@@ -179,15 +191,15 @@ func (z *Int) SetInt64(x int64) *Int {
// SetString interprets s as a number in the given base
// and sets z to that value. The base must be in the range [2,36].
// SetString returns an error if s cannot be parsed or the base is invalid.
-func (z *Int) SetString(s string, base int) os.Error {
+func (z *Int) SetString(s string, base int) error {
z.doinit()
if base < 2 || base > 36 {
- return os.EINVAL
+ return os.ErrInvalid
}
p := C.CString(s)
defer C.free(unsafe.Pointer(p))
if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 {
- return os.EINVAL
+ return os.ErrInvalid
}
return nil
}
@@ -265,7 +277,7 @@ func (z *Int) Mod(x, y *Int) *Int {
func (z *Int) Lsh(x *Int, s uint) *Int {
x.doinit()
z.doinit()
- C.mpz_mul_2exp(&z.i[0], &x.i[0], C.mp_bitcnt_t(s))
+ C._mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s))
return z
}
@@ -273,7 +285,7 @@ func (z *Int) Lsh(x *Int, s uint) *Int {
func (z *Int) Rsh(x *Int, s uint) *Int {
x.doinit()
z.doinit()
- C.mpz_div_2exp(&z.i[0], &x.i[0], C.mp_bitcnt_t(s))
+ C._mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s))
return z
}
diff --git a/misc/cgo/gmp/pi.go b/misc/cgo/gmp/pi.go
index 45f61abbd..1914cf214 100644
--- a/misc/cgo/gmp/pi.go
+++ b/misc/cgo/gmp/pi.go
@@ -1,3 +1,5 @@
+// +build ignore
+
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
@@ -38,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- big "gmp"
+ big "."
"fmt"
"runtime"
)
@@ -100,5 +102,5 @@ func main() {
}
}
- fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.Cgocalls(), numer.Len(), accum.Len(), denom.Len())
+ fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.NumCgoCall(), numer.Len(), accum.Len(), denom.Len())
}
diff --git a/misc/cgo/life/Makefile b/misc/cgo/life/Makefile
deleted file mode 100644
index 5a10380ed..000000000
--- a/misc/cgo/life/Makefile
+++ /dev/null
@@ -1,21 +0,0 @@
-# Copyright 2010 The Go Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style
-# license that can be found in the LICENSE file.
-
-include ../../../src/Make.inc
-
-TARG=life
-
-CGOFILES=\
- life.go\
-
-CGO_OFILES=\
- c-life.o\
-
-CLEANFILES+=life
-
-include ../../../src/Make.pkg
-
-life: install main.go
- $(GC) main.go
- $(LD) -o $@ main.$O
diff --git a/misc/cgo/life/main.go b/misc/cgo/life/main.go
index 9cfed434b..47ae0e18c 100644
--- a/misc/cgo/life/main.go
+++ b/misc/cgo/life/main.go
@@ -2,14 +2,16 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// +build ignore
+
// Run the game of life in C using Go for parallelization.
package main
import (
+ "."
"flag"
"fmt"
- "life"
)
const MAXDIM = 100
diff --git a/misc/cgo/life/test.bash b/misc/cgo/life/test.bash
index 5c5fba1a9..bb483522c 100755
--- a/misc/cgo/life/test.bash
+++ b/misc/cgo/life/test.bash
@@ -4,8 +4,11 @@
# license that can be found in the LICENSE file.
set -e
-gomake life
+go build -o life main.go
+
echo '*' life >run.out
./life >>run.out
diff run.out golden.out
-gomake clean
+
+rm -f life
+
diff --git a/misc/cgo/stdio/Makefile b/misc/cgo/stdio/Makefile
deleted file mode 100644
index 3f7a4c01c..000000000
--- a/misc/cgo/stdio/Makefile
+++ /dev/null
@@ -1,17 +0,0 @@
-# Copyright 2009 The Go Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style
-# license that can be found in the LICENSE file.
-
-include ../../../src/Make.inc
-
-TARG=stdio
-CGOFILES=\
- file.go\
-
-CLEANFILES+=hello fib chain run.out
-
-include ../../../src/Make.pkg
-
-%: install %.go
- $(GC) $*.go
- $(LD) -o $@ $*.$O
diff --git a/misc/cgo/stdio/chain.go b/misc/cgo/stdio/chain.go
index c188b2dd9..1cf0b1fe5 100644
--- a/misc/cgo/stdio/chain.go
+++ b/misc/cgo/stdio/chain.go
@@ -2,13 +2,15 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// +build ignore
+
// Pass numbers along a chain of threads.
package main
import (
+ "../stdio"
"runtime"
- "stdio"
"strconv"
)
diff --git a/misc/cgo/stdio/fib.go b/misc/cgo/stdio/fib.go
index c02e31fd8..6d3ccfd52 100644
--- a/misc/cgo/stdio/fib.go
+++ b/misc/cgo/stdio/fib.go
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// +build ignore
+
// Compute Fibonacci numbers with two goroutines
// that pass integers back and forth. No actual
// concurrency, just threads and synchronization
@@ -10,8 +12,8 @@
package main
import (
+ "../stdio"
"runtime"
- "stdio"
"strconv"
)
@@ -26,7 +28,7 @@ func fibber(c, out chan int64, i int64) {
}
for {
j := <-c
- stdio.Stdout.WriteString(strconv.Itoa64(j) + "\n")
+ stdio.Stdout.WriteString(strconv.FormatInt(j, 10) + "\n")
out <- j
<-out
i += j
diff --git a/misc/cgo/stdio/file.go b/misc/cgo/stdio/file.go
index ab1e88436..6e7d479ad 100644
--- a/misc/cgo/stdio/file.go
+++ b/misc/cgo/stdio/file.go
@@ -28,7 +28,7 @@ var Stderr = (*File)(C.stderr)
// Test reference to library symbol.
// Stdout and stderr are too special to be a reliable test.
-var myerr = C.sys_errlist
+//var = C.environ
func (f *File) WriteString(s string) {
p := C.CString(s)
diff --git a/misc/cgo/stdio/hello.go b/misc/cgo/stdio/hello.go
index 58fc6d574..4ab3c7447 100644
--- a/misc/cgo/stdio/hello.go
+++ b/misc/cgo/stdio/hello.go
@@ -2,9 +2,11 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// +build ignore
+
package main
-import "stdio"
+import "../stdio"
func main() {
stdio.Stdout.WriteString(stdio.Greeting + "\n")
diff --git a/misc/cgo/stdio/test.bash b/misc/cgo/stdio/test.bash
index 82e3f7b45..21829fa31 100755
--- a/misc/cgo/stdio/test.bash
+++ b/misc/cgo/stdio/test.bash
@@ -4,7 +4,10 @@
# license that can be found in the LICENSE file.
set -e
-gomake hello fib chain
+go build hello.go
+go build fib.go
+go build chain.go
+
echo '*' hello >run.out
./hello >>run.out
echo '*' fib >>run.out
@@ -12,4 +15,6 @@ echo '*' fib >>run.out
echo '*' chain >>run.out
./chain >>run.out
diff run.out golden.out
-gomake clean
+
+rm -f hello fib chain
+
diff --git a/misc/cgo/test/Makefile b/misc/cgo/test/Makefile
index d4309be3c..2b7187acb 100644
--- a/misc/cgo/test/Makefile
+++ b/misc/cgo/test/Makefile
@@ -12,9 +12,11 @@ CGOFILES=\
callback.go\
env.go\
exports.go\
+ helpers.go\
issue1222.go\
issue1328.go\
issue1560.go\
+ issue2462.go\
duplicate_symbol.go\
CGO_OFILES=\
@@ -23,4 +25,24 @@ CGO_OFILES=\
OFILES=\
runtime.$O\
+ifeq ($(GOOS),windows)
+GCCVERSION=$(shell gcc -dumpversion)
+ifeq ($(GOARCH),386)
+GCCLIBDIR=/mingw/lib/gcc/mingw32/$(GCCVERSION)
+CHKSTK=_chkstk.o
+else
+GCCLIBDIR=/mingw/lib/gcc/x86_64-w64-mingw32/$(GCCVERSION)
+CHKSTK=_chkstk_ms.o
+endif
+
+CGOFILES+=sleep_windows.go
+CGO_OFILES+=$(CHKSTK)
+
+$(CHKSTK):
+ ar -x "$(GCCLIBDIR)/libgcc.a" $@
+endif
+
include ../../../src/Make.pkg
+
+test:
+ echo cgo: tests disabled. gotest is gone. TODO \ No newline at end of file
diff --git a/misc/cgo/test/align.go b/misc/cgo/test/align.go
index 07ab9ef50..a23b44fc3 100644
--- a/misc/cgo/test/align.go
+++ b/misc/cgo/test/align.go
@@ -1,3 +1,7 @@
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
package cgotest
/*
diff --git a/misc/cgo/test/backdoor/backdoor.go b/misc/cgo/test/backdoor/backdoor.go
new file mode 100644
index 000000000..efe4f01f4
--- /dev/null
+++ b/misc/cgo/test/backdoor/backdoor.go
@@ -0,0 +1,7 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package backdoor
+
+func LockedOSThread() bool // in runtime.c
diff --git a/misc/cgo/test/runtime.c b/misc/cgo/test/backdoor/runtime.c
index e087c7622..54e6a1ef8 100644
--- a/misc/cgo/test/runtime.c
+++ b/misc/cgo/test/backdoor/runtime.c
@@ -3,6 +3,8 @@
// license that can be found in the LICENSE file.
// Expose some runtime functions for testing.
+// Must be in a non-cgo-using package so that
+// the go command compiles this file with 6c, not gcc.
typedef char bool;
@@ -14,7 +16,7 @@ FLUSH(void*)
}
void
-·lockedOSThread(bool b)
+·LockedOSThread(bool b)
{
b = runtime·lockedOSThread();
FLUSH(&b);
diff --git a/misc/cgo/test/basic.go b/misc/cgo/test/basic.go
index b9d0953bd..70ec5e43a 100644
--- a/misc/cgo/test/basic.go
+++ b/misc/cgo/test/basic.go
@@ -48,10 +48,14 @@ struct ibv_async_event {
struct ibv_context {
xxpthread_mutex_t mutex;
};
+
+int add(int x, int y) {
+ return x+y;
+};
*/
import "C"
import (
- "os"
+ "syscall"
"testing"
"unsafe"
)
@@ -65,18 +69,7 @@ func uuidgen() {
C.uuid_generate(&uuid[0])
}
-func Size(name string) (int64, os.Error) {
- var st C.struct_stat
- p := C.CString(name)
- _, err := C.stat(p, &st)
- C.free(unsafe.Pointer(p))
- if err != nil {
- return 0, err
- }
- return int64(C.ulong(st.st_size)), nil
-}
-
-func Strtol(s string, base int) (int, os.Error) {
+func Strtol(s string, base int) (int, error) {
p := C.CString(s)
n, err := C.strtol(p, nil, C.int(base))
C.free(unsafe.Pointer(p))
@@ -108,9 +101,17 @@ func testAtol(t *testing.T) {
}
func testErrno(t *testing.T) {
- n, err := Strtol("asdf", 123)
- if n != 0 || err != os.EINVAL {
- t.Error("Strtol: ", n, err)
+ p := C.CString("no-such-file")
+ m := C.CString("r")
+ f, err := C.fopen(p, m)
+ C.free(unsafe.Pointer(p))
+ C.free(unsafe.Pointer(m))
+ if err == nil {
+ C.fclose(f)
+ t.Fatalf("C.fopen: should fail")
+ }
+ if err != syscall.ENOENT {
+ t.Fatalf("C.fopen: unexpected error: %v", err)
}
}
@@ -124,11 +125,19 @@ func testMultipleAssign(t *testing.T) {
}
var (
- uint = (C.uint)(0)
- ulong C.ulong
- char C.char
+ cuint = (C.uint)(0)
+ culong C.ulong
+ cchar C.char
)
type Context struct {
ctx *C.struct_ibv_context
}
+
+func benchCgoCall(b *testing.B) {
+ const x = C.int(2)
+ const y = C.int(3)
+ for i := 0; i < b.N; i++ {
+ C.add(x, y)
+ }
+}
diff --git a/misc/cgo/test/callback.go b/misc/cgo/test/callback.go
index 3edee9758..e6a1462b3 100644
--- a/misc/cgo/test/callback.go
+++ b/misc/cgo/test/callback.go
@@ -1,15 +1,17 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
package cgotest
/*
void callback(void *f);
-void callGoFoo(void) {
- extern void goFoo(void);
- goFoo();
-}
+void callGoFoo(void);
*/
import "C"
import (
+ "./backdoor"
"runtime"
"testing"
"unsafe"
@@ -39,7 +41,7 @@ func testCallbackGC(t *testing.T) {
nestedCall(runtime.GC)
}
-func lockedOSThread() bool // in runtime.c
+var lockedOSThread = backdoor.LockedOSThread
func testCallbackPanic(t *testing.T) {
// Make sure panic during callback unwinds properly.
@@ -65,7 +67,7 @@ func testCallbackPanic(t *testing.T) {
func testCallbackPanicLoop(t *testing.T) {
// Make sure we don't blow out m->g0 stack.
for i := 0; i < 100000; i++ {
- TestCallbackPanic(t)
+ testCallbackPanic(t)
}
}
diff --git a/misc/cgo/test/callback_c.c b/misc/cgo/test/callback_c.c
index 5983a5e11..47f07301b 100644
--- a/misc/cgo/test/callback_c.c
+++ b/misc/cgo/test/callback_c.c
@@ -8,5 +8,30 @@
void
callback(void *f)
{
+ // use some stack space
+ volatile char data[64*1024];
+
+ data[0] = 0;
goCallback(f);
+ data[sizeof(data)-1] = 0;
+}
+
+void
+callGoFoo(void)
+{
+ extern void goFoo(void);
+ goFoo();
+}
+
+void
+IntoC(void)
+{
+ BackIntoGo();
+}
+
+void
+twoSleep(int n)
+{
+ BackgroundSleep(n);
+ sleep(n);
}
diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go
index 94fba15db..34beee69d 100644
--- a/misc/cgo/test/cgo_test.go
+++ b/misc/cgo/test/cgo_test.go
@@ -26,3 +26,6 @@ func TestBlocking(t *testing.T) { testBlocking(t) }
func Test1328(t *testing.T) { test1328(t) }
func TestParallelSleep(t *testing.T) { testParallelSleep(t) }
func TestSetEnv(t *testing.T) { testSetEnv(t) }
+func TestHelpers(t *testing.T) { testHelpers(t) }
+
+func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
diff --git a/misc/cgo/test/env.go b/misc/cgo/test/env.go
index 1fb4e684c..8d3ba5877 100644
--- a/misc/cgo/test/env.go
+++ b/misc/cgo/test/env.go
@@ -10,12 +10,21 @@ package cgotest
import "C"
import (
"os"
+ "runtime"
"testing"
"unsafe"
)
// This is really an os package test but here for convenience.
func testSetEnv(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ // Go uses SetEnvironmentVariable on windows. Howerver,
+ // C runtime takes a *copy* at process startup of thei
+ // OS environment, and stores it in environ/envp.
+ // It is this copy that getenv/putenv manipulate.
+ t.Logf("skipping test")
+ return
+ }
const key = "CGO_OS_TEST_KEY"
const val = "CGO_OS_TEST_VALUE"
os.Setenv(key, val)
diff --git a/misc/cgo/test/helpers.go b/misc/cgo/test/helpers.go
new file mode 100644
index 000000000..890dcbdf1
--- /dev/null
+++ b/misc/cgo/test/helpers.go
@@ -0,0 +1,35 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgotest
+
+// const char *greeting = "hello, world";
+import "C"
+
+import (
+ "reflect"
+ "testing"
+ "unsafe"
+)
+
+const greeting = "hello, world"
+
+type testPair struct {
+ Name string
+ Got, Want interface{}
+}
+
+var testPairs = []testPair{
+ {"GoString", C.GoString(C.greeting), greeting},
+ {"GoStringN", C.GoStringN(C.greeting, 5), greeting[:5]},
+ {"GoBytes", C.GoBytes(unsafe.Pointer(C.greeting), 5), []byte(greeting[:5])},
+}
+
+func testHelpers(t *testing.T) {
+ for _, pair := range testPairs {
+ if !reflect.DeepEqual(pair.Got, pair.Want) {
+ t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want)
+ }
+ }
+}
diff --git a/misc/cgo/test/issue1328.go b/misc/cgo/test/issue1328.go
index e01207dd9..e1796d6f7 100644
--- a/misc/cgo/test/issue1328.go
+++ b/misc/cgo/test/issue1328.go
@@ -7,7 +7,7 @@ package cgotest
import "testing"
// extern void BackIntoGo(void);
-// void IntoC() { BackIntoGo(); }
+// void IntoC(void);
import "C"
//export BackIntoGo
diff --git a/misc/cgo/test/issue1560.go b/misc/cgo/test/issue1560.go
index e534cce47..3faa966e7 100644
--- a/misc/cgo/test/issue1560.go
+++ b/misc/cgo/test/issue1560.go
@@ -7,11 +7,10 @@ package cgotest
/*
#include <unistd.h>
+unsigned int sleep(unsigned int seconds);
+
extern void BackgroundSleep(int);
-void twoSleep(int n) {
- BackgroundSleep(n);
- sleep(n);
-}
+void twoSleep(int);
*/
import "C"
@@ -36,11 +35,11 @@ func BackgroundSleep(n int) {
}
func testParallelSleep(t *testing.T) {
- dt := -time.Nanoseconds()
+ start := time.Now()
parallelSleep(1)
- dt += time.Nanoseconds()
+ dt := time.Now().Sub(start)
// bug used to run sleeps in serial, producing a 2-second delay.
- if dt >= 1.3e9 {
- t.Fatalf("parallel 1-second sleeps slept for %f seconds", float64(dt)/1e9)
+ if dt >= 1300*time.Millisecond {
+ t.Fatalf("parallel 1-second sleeps slept for %f seconds", dt.Seconds())
}
}
diff --git a/misc/cgo/test/issue2462.go b/misc/cgo/test/issue2462.go
new file mode 100644
index 000000000..12cd91d05
--- /dev/null
+++ b/misc/cgo/test/issue2462.go
@@ -0,0 +1,102 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgotest
+
+import "C"
+
+//export exportbyte
+func exportbyte() byte {
+ return 0
+}
+
+//export exportbool
+func exportbool() bool {
+ return false
+}
+
+//export exportrune
+func exportrune() rune {
+ return 0
+}
+
+//export exporterror
+func exporterror() error {
+ return nil
+}
+
+//export exportint
+func exportint() int {
+ return 0
+}
+
+//export exportuint
+func exportuint() uint {
+ return 0
+}
+
+//export exportuintptr
+func exportuintptr() uintptr {
+ return (uintptr)(0)
+}
+
+//export exportint8
+func exportint8() int8 {
+ return 0
+}
+
+//export exportuint8
+func exportuint8() uint8 {
+ return 0
+}
+
+//export exportint16
+func exportint16() int16 {
+ return 0
+}
+
+//export exportuint16
+func exportuint16() uint16 {
+ return 0
+}
+
+//export exportint32
+func exportint32() int32 {
+ return 0
+}
+
+//export exportuint32
+func exportuint32() uint32 {
+ return 0
+}
+
+//export exportint64
+func exportint64() int64 {
+ return 0
+}
+
+//export exportuint64
+func exportuint64() uint64 {
+ return 0
+}
+
+//export exportfloat32
+func exportfloat32() float32 {
+ return 0
+}
+
+//export exportfloat64
+func exportfloat64() float64 {
+ return 0
+}
+
+//export exportcomplex64
+func exportcomplex64() complex64 {
+ return 0
+}
+
+//export exportcomplex128
+func exportcomplex128() complex128 {
+ return 0
+}
diff --git a/misc/cgo/test/sleep_windows.go b/misc/cgo/test/sleep_windows.go
new file mode 100644
index 000000000..007a1bb4c
--- /dev/null
+++ b/misc/cgo/test/sleep_windows.go
@@ -0,0 +1,16 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgotest
+
+/*
+#include <windows.h>
+
+unsigned int sleep(unsigned int seconds) {
+ Sleep(1000 * seconds);
+ return 0;
+}
+
+*/
+import "C"
diff --git a/misc/cgo/testso/cgoso.go b/misc/cgo/testso/cgoso.go
new file mode 100644
index 000000000..44fb616c1
--- /dev/null
+++ b/misc/cgo/testso/cgoso.go
@@ -0,0 +1,19 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgosotest
+
+/*
+#cgo LDFLAGS: -L. -lcgosotest
+void sofunc(void);
+*/
+import "C"
+
+func Test() {
+ C.sofunc()
+}
+
+//export goCallback
+func goCallback() {
+}
diff --git a/misc/cgo/testso/cgoso_c.c b/misc/cgo/testso/cgoso_c.c
new file mode 100644
index 000000000..8c15a6b9f
--- /dev/null
+++ b/misc/cgo/testso/cgoso_c.c
@@ -0,0 +1,11 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build ignore
+
+void sofunc(void)
+{
+ extern void goCallback(void);
+ goCallback();
+}
diff --git a/misc/cgo/testso/main.go b/misc/cgo/testso/main.go
new file mode 100644
index 000000000..88aa4322d
--- /dev/null
+++ b/misc/cgo/testso/main.go
@@ -0,0 +1,13 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build ignore
+
+package main
+
+import "."
+
+func main() {
+ cgosotest.Test()
+}
diff --git a/misc/cgo/testso/test.bash b/misc/cgo/testso/test.bash
new file mode 100755
index 000000000..ecef873c8
--- /dev/null
+++ b/misc/cgo/testso/test.bash
@@ -0,0 +1,10 @@
+#!/bin/sh
+# Copyright 2011 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+set -e
+gcc $(go env GOGCCFLAGS) -shared -o libcgosotest.so cgoso_c.c
+go build main.go
+LD_LIBRARY_PATH=. ./main
+rm -f libcgosotest.so main