runtime: Call exit rather than _exit.
[gcc.git] / libgo / runtime / runtime.h
1 /* runtime.h -- runtime support for Go.
2
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
6
7 #include "config.h"
8
9 #include "go-assert.h"
10 #include <setjmp.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <pthread.h>
19 #include <semaphore.h>
20 #include <ucontext.h>
21
22 #ifdef HAVE_SYS_MMAN_H
23 #include <sys/mman.h>
24 #endif
25
26 #include "array.h"
27 #include "go-alloc.h"
28 #include "go-panic.h"
29 #include "go-string.h"
30
31 /* This file supports C files copied from the 6g runtime library.
32 This is a version of the 6g runtime.h rewritten for gccgo's version
33 of the code. */
34
35 typedef signed int int8 __attribute__ ((mode (QI)));
36 typedef unsigned int uint8 __attribute__ ((mode (QI)));
37 typedef signed int int16 __attribute__ ((mode (HI)));
38 typedef unsigned int uint16 __attribute__ ((mode (HI)));
39 typedef signed int int32 __attribute__ ((mode (SI)));
40 typedef unsigned int uint32 __attribute__ ((mode (SI)));
41 typedef signed int int64 __attribute__ ((mode (DI)));
42 typedef unsigned int uint64 __attribute__ ((mode (DI)));
43 typedef float float32 __attribute__ ((mode (SF)));
44 typedef double float64 __attribute__ ((mode (DF)));
45 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
46
47 /* Defined types. */
48
49 typedef uint8 bool;
50 typedef uint8 byte;
51 typedef struct G G;
52 typedef union Lock Lock;
53 typedef struct M M;
54 typedef union Note Note;
55 typedef struct SigTab SigTab;
56 typedef struct MCache MCache;
57 typedef struct FixAlloc FixAlloc;
58 typedef struct Hchan Hchan;
59 typedef struct Timers Timers;
60 typedef struct Timer Timer;
61
62 typedef struct __go_open_array Slice;
63 typedef struct __go_string String;
64 typedef struct __go_interface Iface;
65 typedef struct __go_empty_interface Eface;
66 typedef struct __go_type_descriptor Type;
67 typedef struct __go_defer_stack Defer;
68 typedef struct __go_panic_stack Panic;
69
70 typedef struct __go_func_type FuncType;
71 typedef struct __go_map_type MapType;
72
73 /*
74 * per-cpu declaration.
75 */
76 extern M* runtime_m(void);
77 extern G* runtime_g(void);
78
79 extern M runtime_m0;
80 extern G runtime_g0;
81
82 /*
83 * defined constants
84 */
85 enum
86 {
87 // G status
88 //
89 // If you add to this list, add to the list
90 // of "okay during garbage collection" status
91 // in mgc0.c too.
92 Gidle,
93 Grunnable,
94 Grunning,
95 Gsyscall,
96 Gwaiting,
97 Gmoribund,
98 Gdead,
99 };
100 enum
101 {
102 true = 1,
103 false = 0,
104 };
105
106 /*
107 * structures
108 */
109 union Lock
110 {
111 uint32 key; // futex-based impl
112 M* waitm; // linked list of waiting M's (sema-based impl)
113 };
114 union Note
115 {
116 uint32 key; // futex-based impl
117 M* waitm; // waiting M (sema-based impl)
118 };
119 struct G
120 {
121 Defer* defer;
122 Panic* panic;
123 void* exception; // current exception being thrown
124 bool is_foreign; // whether current exception from other language
125 void *gcstack; // if status==Gsyscall, gcstack = stackbase to use during gc
126 uintptr gcstack_size;
127 void* gcnext_segment;
128 void* gcnext_sp;
129 void* gcinitial_sp;
130 jmp_buf gcregs;
131 byte* entry; // initial function
132 G* alllink; // on allg
133 void* param; // passed parameter on wakeup
134 bool fromgogo; // reached from gogo
135 int16 status;
136 int32 goid;
137 uint32 selgen; // valid sudog pointer
138 const char* waitreason; // if status==Gwaiting
139 G* schedlink;
140 bool readyonstop;
141 bool ispanic;
142 M* m; // for debuggers, but offset not hard-coded
143 M* lockedm;
144 M* idlem;
145 // int32 sig;
146 // uintptr sigcode0;
147 // uintptr sigcode1;
148 // uintptr sigpc;
149 uintptr gopc; // pc of go statement that created this goroutine
150
151 ucontext_t context;
152 void* stack_context[10];
153 };
154
155 struct M
156 {
157 G* g0; // goroutine with scheduling stack
158 G* gsignal; // signal-handling G
159 G* curg; // current running goroutine
160 int32 id;
161 int32 mallocing;
162 int32 gcing;
163 int32 locks;
164 int32 nomemprof;
165 int32 waitnextg;
166 int32 dying;
167 int32 profilehz;
168 int32 helpgc;
169 uint32 fastrand;
170 Note havenextg;
171 G* nextg;
172 M* alllink; // on allm
173 M* schedlink;
174 MCache *mcache;
175 G* lockedg;
176 G* idleg;
177 M* nextwaitm; // next M waiting for lock
178 uintptr waitsema; // semaphore for parking on locks
179 uint32 waitsemacount;
180 uint32 waitsemalock;
181 };
182
183 struct SigTab
184 {
185 int32 sig;
186 int32 flags;
187 };
188 enum
189 {
190 SigCatch = 1<<0,
191 SigIgnore = 1<<1,
192 SigRestart = 1<<2,
193 SigQueue = 1<<3,
194 SigPanic = 1<<4,
195 };
196
197 /* Macros. */
198
199 #ifdef GOOS_windows
200 enum {
201 Windows = 1
202 };
203 #else
204 enum {
205 Windows = 0
206 };
207 #endif
208
209 struct Timers
210 {
211 Lock;
212 G *timerproc;
213 bool sleeping;
214 bool rescheduling;
215 Note waitnote;
216 Timer **t;
217 int32 len;
218 int32 cap;
219 };
220
221 // Package time knows the layout of this structure.
222 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
223 struct Timer
224 {
225 int32 i; // heap index
226
227 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
228 // each time calling f(now, arg) in the timer goroutine, so f must be
229 // a well-behaved function and not block.
230 int64 when;
231 int64 period;
232 void (*f)(int64, Eface);
233 Eface arg;
234 };
235
236 /*
237 * defined macros
238 * you need super-gopher-guru privilege
239 * to add this list.
240 */
241 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
242 #define nil ((void*)0)
243 #define USED(v) ((void) v)
244
245 /*
246 * external data
247 */
248 G* runtime_allg;
249 G* runtime_lastg;
250 M* runtime_allm;
251 extern int32 runtime_gomaxprocs;
252 extern bool runtime_singleproc;
253 extern uint32 runtime_panicking;
254 extern int32 runtime_gcwaiting; // gc is waiting to run
255 int32 runtime_ncpu;
256
257 /*
258 * common functions and data
259 */
260 int32 runtime_findnull(const byte*);
261
262 /*
263 * very low level c-called
264 */
265 void runtime_args(int32, byte**);
266 void runtime_osinit();
267 void runtime_goargs(void);
268 void runtime_goenvs(void);
269 void runtime_goenvs_unix(void);
270 void runtime_throw(const char*) __attribute__ ((noreturn));
271 void runtime_panicstring(const char*) __attribute__ ((noreturn));
272 void* runtime_mal(uintptr);
273 void runtime_schedinit(void);
274 void runtime_initsig(int32);
275 String runtime_gostringnocopy(const byte*);
276 void* runtime_mstart(void*);
277 G* runtime_malg(int32, byte**, size_t*);
278 void runtime_minit(void);
279 void runtime_mallocinit(void);
280 void runtime_gosched(void);
281 void runtime_tsleep(int64);
282 M* runtime_newm(void);
283 void runtime_goexit(void);
284 void runtime_entersyscall(void) __asm__("libgo_syscall.syscall.entersyscall");
285 void runtime_exitsyscall(void) __asm__("libgo_syscall.syscall.exitsyscall");
286 void siginit(void);
287 bool __go_sigsend(int32 sig);
288 int64 runtime_nanotime(void);
289 int64 runtime_cputicks(void);
290
291 void runtime_stoptheworld(void);
292 void runtime_starttheworld(bool);
293 G* __go_go(void (*pfn)(void*), void*);
294
295 /*
296 * mutual exclusion locks. in the uncontended case,
297 * as fast as spin locks (just a few user-level instructions),
298 * but on the contention path they sleep in the kernel.
299 * a zeroed Lock is unlocked (no need to initialize each lock).
300 */
301 void runtime_lock(Lock*);
302 void runtime_unlock(Lock*);
303
304 /*
305 * sleep and wakeup on one-time events.
306 * before any calls to notesleep or notewakeup,
307 * must call noteclear to initialize the Note.
308 * then, exactly one thread can call notesleep
309 * and exactly one thread can call notewakeup (once).
310 * once notewakeup has been called, the notesleep
311 * will return. future notesleep will return immediately.
312 * subsequent noteclear must be called only after
313 * previous notesleep has returned, e.g. it's disallowed
314 * to call noteclear straight after notewakeup.
315 *
316 * notetsleep is like notesleep but wakes up after
317 * a given number of nanoseconds even if the event
318 * has not yet happened. if a goroutine uses notetsleep to
319 * wake up early, it must wait to call noteclear until it
320 * can be sure that no other goroutine is calling
321 * notewakeup.
322 */
323 void runtime_noteclear(Note*);
324 void runtime_notesleep(Note*);
325 void runtime_notewakeup(Note*);
326 void runtime_notetsleep(Note*, int64);
327
328 /*
329 * low-level synchronization for implementing the above
330 */
331 uintptr runtime_semacreate(void);
332 int32 runtime_semasleep(int64);
333 void runtime_semawakeup(M*);
334 // or
335 void runtime_futexsleep(uint32*, uint32, int64);
336 void runtime_futexwakeup(uint32*, uint32);
337
338 /*
339 * runtime go-called
340 */
341 void runtime_panic(Eface);
342
343 /* Functions. */
344 #define runtime_panic __go_panic
345 #define runtime_printf printf
346 #define runtime_malloc(s) __go_alloc(s)
347 #define runtime_free(p) __go_free(p)
348 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
349 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
350 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
351 #define runtime_exit(s) exit(s)
352 MCache* runtime_allocmcache(void);
353 void free(void *v);
354 struct __go_func_type;
355 bool runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
356 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
357 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
358 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
359 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
360 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
361 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
362 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
363 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
364
365 void runtime_dopanic(int32) __attribute__ ((noreturn));
366 void runtime_startpanic(void);
367 void runtime_ready(G*);
368 const byte* runtime_getenv(const char*);
369 int32 runtime_atoi(const byte*);
370 uint32 runtime_fastrand1(void);
371
372 void runtime_sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
373 void runtime_resetcpuprofiler(int32);
374 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
375 void runtime_usleep(uint32);
376
377 void runtime_semacquire(uint32 volatile *);
378 void runtime_semrelease(uint32 volatile *);
379 int32 runtime_gomaxprocsfunc(int32 n);
380 void runtime_procyield(uint32);
381 void runtime_osyield(void);
382 void runtime_LockOSThread(void) __asm__("libgo_runtime.runtime.LockOSThread");
383 void runtime_UnlockOSThread(void) __asm__("libgo_runtime.runtime.UnlockOSThread");
384
385 /*
386 * low level C-called
387 */
388 #define runtime_mmap mmap
389 #define runtime_munmap munmap
390 #define runtime_madvise madvise
391 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
392
393 struct __go_func_type;
394 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
395 void **, void **)
396 asm ("libgo_reflect.reflect.call");
397
398 #ifdef __rtems__
399 void __wrap_rtems_task_variable_add(void **);
400 #endif
401
402 void runtime_time_scan(void (*)(byte*, int64));