LEFT | RIGHT |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <dlfcn.h> | 6 #include <dlfcn.h> |
7 #include <pthread.h> | 7 #include <pthread.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
11 static __thread int tlsvar; | 11 static __thread int tlsvar; |
12 | 12 |
13 static void *threaded_callback(void *callback) { | 13 static void *threaded_callback(void *callback) { |
14 int v = ((int (*)(void))(callback))(); | 14 int v = ((int (*)(void))(callback))(); |
15 » if (v != 42) { | 15 » if(v != 42) { |
16 fprintf(stderr, "Incorrect result from Go function, expected %d,
got %d\n", 42, v); | 16 fprintf(stderr, "Incorrect result from Go function, expected %d,
got %d\n", 42, v); |
17 exit(1); | 17 exit(1); |
18 } | 18 } |
19 return NULL; | 19 return NULL; |
20 } | 20 } |
21 | 21 |
22 int main() { | 22 int main() { |
23 void *handle; | 23 void *handle; |
24 int (*callback)(void); | 24 int (*callback)(void); |
25 char *error; | 25 char *error; |
26 int err; | 26 int err; |
27 pthread_attr_t attr; | 27 pthread_attr_t attr; |
28 pthread_t p; | 28 pthread_t p; |
29 | 29 |
30 tlsvar = 1234; | 30 tlsvar = 1234; |
31 | 31 |
32 handle = dlopen("libgoshared.so", RTLD_LAZY); | 32 handle = dlopen("libgoshared.so", RTLD_LAZY); |
33 » if (!handle) { | 33 » if(!handle) { |
34 fprintf(stderr, "Failed to load go shared library: %s\n", dlerro
r()); | 34 fprintf(stderr, "Failed to load go shared library: %s\n", dlerro
r()); |
35 return 1; | 35 return 1; |
36 } | 36 } |
37 | 37 |
38 callback = (int (*)(void))dlsym(handle, "Go_callback"); | 38 callback = (int (*)(void))dlsym(handle, "Go_callback"); |
39 » if ((error = dlerror()) != NULL) { | 39 » if((error = dlerror()) != NULL) { |
40 fprintf(stderr, "Failed to find callback symbol: %s\n", error); | 40 fprintf(stderr, "Failed to find callback symbol: %s\n", error); |
41 return 1; | 41 return 1; |
42 } | 42 } |
43 | 43 |
44 pthread_attr_init(&attr); | 44 pthread_attr_init(&attr); |
45 err = pthread_create(&p, &attr, threaded_callback, callback); | 45 err = pthread_create(&p, &attr, threaded_callback, callback); |
46 if(err != 0) { | 46 if(err != 0) { |
47 fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", stre
rror(err)); | 47 fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", stre
rror(err)); |
48 exit(1); | 48 exit(1); |
49 } | 49 } |
50 int v = (*callback)(); | 50 int v = (*callback)(); |
51 » if (v != 42) { | 51 » if(v != 42) { |
52 fprintf(stderr, "Incorrect result from Go function, expected %d,
got %d\n", 42, v); | 52 fprintf(stderr, "Incorrect result from Go function, expected %d,
got %d\n", 42, v); |
53 exit(1); | 53 exit(1); |
54 } | 54 } |
55 | 55 |
56 err = pthread_join(p, NULL); | 56 err = pthread_join(p, NULL); |
57 if(err != 0) { | 57 if(err != 0) { |
58 fprintf(stderr, "runtime/cgo: pthread_join failed: %s\n", strerr
or(err)); | 58 fprintf(stderr, "runtime/cgo: pthread_join failed: %s\n", strerr
or(err)); |
59 exit(1); | 59 exit(1); |
60 } | 60 } |
61 » if (tlsvar != 1234) { | 61 » if(tlsvar != 1234) { |
62 fprintf(stderr, "Corrupt TLS variable, expected %d, got %d\n", 1
234, tlsvar); | 62 fprintf(stderr, "Corrupt TLS variable, expected %d, got %d\n", 1
234, tlsvar); |
63 exit(1); | 63 exit(1); |
64 } | 64 } |
65 | 65 |
66 exit(0); | 66 exit(0); |
67 } | 67 } |
LEFT | RIGHT |