Index: misc/cgo/testshared/mainadv.c |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/misc/cgo/testshared/mainadv.c |
@@ -0,0 +1,67 @@ |
+// Copyright 2013 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 <stdio.h> |
+#include <dlfcn.h> |
+#include <pthread.h> |
+#include <stdlib.h> |
+#include <string.h> |
+ |
+static __thread int tlsvar; |
+ |
+static void *threaded_callback(void *callback) { |
+ int v = ((int (*)(void))(callback))(); |
+ if(v != 42) { |
+ fprintf(stderr, "Incorrect result from Go function, expected %d, got %d\n", 42, v); |
+ exit(1); |
+ } |
+ return NULL; |
+} |
+ |
+int main() { |
+ void *handle; |
+ int (*callback)(void); |
+ char *error; |
+ int err; |
+ pthread_attr_t attr; |
+ pthread_t p; |
+ |
+ tlsvar = 1234; |
+ |
+ handle = dlopen("libgoshared.so", RTLD_LAZY); |
+ if(!handle) { |
+ fprintf(stderr, "Failed to load go shared library: %s\n", dlerror()); |
+ return 1; |
+ } |
+ |
+ callback = (int (*)(void))dlsym(handle, "Go_callback"); |
+ if((error = dlerror()) != NULL) { |
+ fprintf(stderr, "Failed to find callback symbol: %s\n", error); |
+ return 1; |
+ } |
+ |
+ pthread_attr_init(&attr); |
+ err = pthread_create(&p, &attr, threaded_callback, callback); |
+ if(err != 0) { |
+ fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err)); |
+ exit(1); |
+ } |
+ int v = (*callback)(); |
+ if(v != 42) { |
+ fprintf(stderr, "Incorrect result from Go function, expected %d, got %d\n", 42, v); |
+ exit(1); |
+ } |
+ |
+ err = pthread_join(p, NULL); |
+ if(err != 0) { |
+ fprintf(stderr, "runtime/cgo: pthread_join failed: %s\n", strerror(err)); |
+ exit(1); |
+ } |
+ if(tlsvar != 1234) { |
+ fprintf(stderr, "Corrupt TLS variable, expected %d, got %d\n", 1234, tlsvar); |
+ exit(1); |
+ } |
+ |
+ exit(0); |
+} |