]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/cgo/libcgo.h
runtime/cgo: store M for C-created thread in pthread key
[gostls13.git] / src / runtime / cgo / libcgo.h
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #undef nil
10 #define nil ((void*)0)
11 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
12
13 typedef uint32_t uint32;
14 typedef uint64_t uint64;
15 typedef uintptr_t uintptr;
16
17 /*
18  * The beginning of the per-goroutine structure,
19  * as defined in ../pkg/runtime/runtime.h.
20  * Just enough to edit these two fields.
21  */
22 typedef struct G G;
23 struct G
24 {
25         uintptr stacklo;
26         uintptr stackhi;
27 };
28
29 /*
30  * Arguments to the _cgo_thread_start call.
31  * Also known to ../pkg/runtime/runtime.h.
32  */
33 typedef struct ThreadStart ThreadStart;
34 struct ThreadStart
35 {
36         G *g;
37         uintptr *tls;
38         void (*fn)(void);
39 };
40
41 /*
42  * Called by 5c/6c/8c world.
43  * Makes a local copy of the ThreadStart and
44  * calls _cgo_sys_thread_start(ts).
45  */
46 extern void (*_cgo_thread_start)(ThreadStart *ts);
47
48 /*
49  * Creates a new operating system thread without updating any Go state
50  * (OS dependent).
51  */
52 extern void (*_cgo_sys_thread_create)(void* (*func)(void*), void* arg);
53
54 /*
55  * Indicates whether a dummy pthread per-thread variable is allocated.
56  */
57 extern uintptr_t *_cgo_pthread_key_created;
58
59 /*
60  * Creates the new operating system thread (OS, arch dependent).
61  */
62 void _cgo_sys_thread_start(ThreadStart *ts);
63
64 /*
65  * Waits for the Go runtime to be initialized (OS dependent).
66  * If runtime.SetCgoTraceback is used to set a context function,
67  * calls the context function and returns the context value.
68  */
69 uintptr_t _cgo_wait_runtime_init_done(void);
70
71 /*
72  * Call fn in the 6c world.
73  */
74 void crosscall_amd64(void (*fn)(void), void (*setg_gcc)(void*), void *g);
75
76 /*
77  * Call fn in the 8c world.
78  */
79 void crosscall_386(void (*fn)(void));
80
81 /*
82  * Prints error then calls abort. For linux and android.
83  */
84 void fatalf(const char* format, ...);
85
86 /*
87  * Registers the current mach thread port for EXC_BAD_ACCESS processing.
88  */
89 void darwin_arm_init_thread_exception_port(void);
90
91 /*
92  * Starts a mach message server processing EXC_BAD_ACCESS.
93  */
94 void darwin_arm_init_mach_exception_handler(void);
95
96 /*
97  * The cgo context function. See runtime.SetCgoTraceback.
98  */
99 struct context_arg {
100         uintptr_t Context;
101 };
102 extern void (*(_cgo_get_context_function(void)))(struct context_arg*);
103
104 /*
105  * The argument for the cgo traceback callback. See runtime.SetCgoTraceback.
106  */
107 struct cgoTracebackArg {
108         uintptr_t  Context;
109         uintptr_t  SigContext;
110         uintptr_t* Buf;
111         uintptr_t  Max;
112 };
113
114 /*
115  * TSAN support.  This is only useful when building with
116  *   CGO_CFLAGS="-fsanitize=thread" CGO_LDFLAGS="-fsanitize=thread" go install
117  */
118 #undef CGO_TSAN
119 #if defined(__has_feature)
120 # if __has_feature(thread_sanitizer)
121 #  define CGO_TSAN
122 # endif
123 #elif defined(__SANITIZE_THREAD__)
124 # define CGO_TSAN
125 #endif
126
127 #ifdef CGO_TSAN
128
129 // These must match the definitions in yesTsanProlog in cmd/cgo/out.go.
130 // In general we should call _cgo_tsan_acquire when we enter C code,
131 // and call _cgo_tsan_release when we return to Go code.
132 // This is only necessary when calling code that might be instrumented
133 // by TSAN, which mostly means system library calls that TSAN intercepts.
134 // See the comment in cmd/cgo/out.go for more details.
135
136 long long _cgo_sync __attribute__ ((common));
137
138 extern void __tsan_acquire(void*);
139 extern void __tsan_release(void*);
140
141 __attribute__ ((unused))
142 static void _cgo_tsan_acquire() {
143         __tsan_acquire(&_cgo_sync);
144 }
145
146 __attribute__ ((unused))
147 static void _cgo_tsan_release() {
148         __tsan_release(&_cgo_sync);
149 }
150
151 #else // !defined(CGO_TSAN)
152
153 #define _cgo_tsan_acquire()
154 #define _cgo_tsan_release()
155
156 #endif // !defined(CGO_TSAN)