runtime/cgo: runtime changes for new cgo

Formerly known as libcgo.
Almost no code here is changing; the diffs
are shown relative to the originals in libcgo.

R=r
CC=golang-dev
https://golang.org/cl/3420043
diff --git a/src/pkg/Makefile b/src/pkg/Makefile
index 4864984..22da9f9 100644
--- a/src/pkg/Makefile
+++ b/src/pkg/Makefile
@@ -109,6 +109,7 @@
 	rpc\
 	rpc/jsonrpc\
 	runtime\
+	runtime/cgo\
 	runtime/pprof\
 	scanner\
 	smtp\
@@ -152,6 +153,7 @@
 	image/jpeg\
 	net/dict\
 	rand\
+	runtime/cgo\
 	runtime/pprof\
 	syscall\
 	testing\
@@ -247,3 +249,4 @@
 -include Make.deps
 
 ../cmd/cgo.install: ../libcgo.install
+runtime/cgo.install: ../cmd/cgo.install
diff --git a/src/pkg/runtime/cgocall.c b/src/pkg/runtime/cgocall.c
index b2852be..80ae97e 100644
--- a/src/pkg/runtime/cgocall.c
+++ b/src/pkg/runtime/cgocall.c
@@ -15,9 +15,12 @@
 {
 	G *oldlock;
 
-	if(initcgo == nil)
+	if(!runtime·iscgo)
 		runtime·throw("cgocall unavailable");
 
+	if(fn == 0)
+		runtime·throw("cgocall nil");
+
 	ncgocall++;
 
 	/*
@@ -94,7 +97,7 @@
 void*
 runtime·cmalloc(uintptr n)
 {
-	struct a {
+	struct {
 		uint64 n;
 		void *ret;
 	} a;
diff --git a/src/pkg/runtime/darwin/thread.c b/src/pkg/runtime/darwin/thread.c
index 58aa9da..185f0ce 100644
--- a/src/pkg/runtime/darwin/thread.c
+++ b/src/pkg/runtime/darwin/thread.c
@@ -144,8 +144,7 @@
 	// Register our thread-creation callback (see {amd64,386}/sys.s)
 	// but only if we're not using cgo.  If we are using cgo we need
 	// to let the C pthread libary install its own thread-creation callback.
-	extern void (*libcgo_thread_start)(void*);
-	if(libcgo_thread_start == nil)
+	if(!runtime·iscgo)
 		runtime·bsdthread_register();
 }
 
diff --git a/src/pkg/runtime/proc.c b/src/pkg/runtime/proc.c
index ff8673a..3d5ee24 100644
--- a/src/pkg/runtime/proc.c
+++ b/src/pkg/runtime/proc.c
@@ -8,6 +8,8 @@
 #include "malloc.h"
 #include "os.h"
 
+bool	runtime·iscgo;
+
 static void unwindstack(G*, byte*);
 
 typedef struct Sched Sched;
@@ -426,8 +428,11 @@
 			runtime·allm = m;
 			m->id = runtime·sched.mcount++;
 
-			if(libcgo_thread_start != nil) {
+			if(runtime·iscgo) {
 				CgoThreadStart ts;
+
+				if(libcgo_thread_start == nil)
+					runtime·throw("libcgo_thread_start missing");
 				// pthread_create will make us a stack.
 				m->g0 = runtime·malg(-1);
 				ts.m = m;
diff --git a/src/pkg/runtime/runtime.h b/src/pkg/runtime/runtime.h
index 3f6fb77..ececacc 100644
--- a/src/pkg/runtime/runtime.h
+++ b/src/pkg/runtime/runtime.h
@@ -361,6 +361,7 @@
 extern	int32	runtime·fd;	// usually 1; set to 2 when panicking
 extern	int32	runtime·gcwaiting;		// gc is waiting to run
 int8*	runtime·goos;
+extern	bool	runtime·iscgo;
 
 /*
  * common functions and data