Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(338)

Unified Diff: misc/cgo/testshared/mainadv.c

Issue 9738047: code review 9738047: runtime: Add shared library support (linux/amd64)
Patch Set: diff -r 3833ddddde2b1a2741a396c4e965b04d525a133b https://go.googlecode.com/hg/ Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « misc/cgo/testshared/main.c ('k') | misc/cgo/testshared/mainmult.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+}
« no previous file with comments | « misc/cgo/testshared/main.c ('k') | misc/cgo/testshared/mainmult.c » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b