2013-05-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / runtime / runtime.h
blob9392df162bffb5ab368b64ce42ccd36f664e6fe2
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.
5 #include "config.h"
7 #include "go-assert.h"
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <pthread.h>
17 #include <semaphore.h>
18 #include <ucontext.h>
20 #ifdef HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
24 #include "interface.h"
25 #include "go-alloc.h"
27 #define _STRINGIFY2_(x) #x
28 #define _STRINGIFY_(x) _STRINGIFY2_(x)
29 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
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. */
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 signed int intptr __attribute__ ((mode (pointer)));
46 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
48 typedef intptr intgo; // Go's int
49 typedef uintptr uintgo; // Go's uint
51 /* Defined types. */
53 typedef uint8 bool;
54 typedef uint8 byte;
55 typedef struct Func Func;
56 typedef struct G G;
57 typedef union Lock Lock;
58 typedef struct M M;
59 typedef union Note Note;
60 typedef struct SigTab SigTab;
61 typedef struct MCache MCache;
62 typedef struct FixAlloc FixAlloc;
63 typedef struct Hchan Hchan;
64 typedef struct Timers Timers;
65 typedef struct Timer Timer;
66 typedef struct GCStats GCStats;
67 typedef struct LFNode LFNode;
68 typedef struct ParFor ParFor;
69 typedef struct ParForThread ParForThread;
70 typedef struct CgoMal CgoMal;
72 typedef struct __go_open_array Slice;
73 typedef struct String String;
74 typedef struct __go_interface Iface;
75 typedef struct __go_empty_interface Eface;
76 typedef struct __go_type_descriptor Type;
77 typedef struct __go_defer_stack Defer;
78 typedef struct __go_panic_stack Panic;
80 typedef struct __go_ptr_type PtrType;
81 typedef struct __go_func_type FuncType;
82 typedef struct __go_map_type MapType;
84 typedef struct Traceback Traceback;
86 typedef struct Location Location;
89 * Per-CPU declaration.
91 extern M* runtime_m(void);
92 extern G* runtime_g(void);
94 extern M runtime_m0;
95 extern G runtime_g0;
98 * defined constants
100 enum
102 // G status
104 // If you add to this list, add to the list
105 // of "okay during garbage collection" status
106 // in mgc0.c too.
107 Gidle,
108 Grunnable,
109 Grunning,
110 Gsyscall,
111 Gwaiting,
112 Gmoribund,
113 Gdead,
115 enum
117 true = 1,
118 false = 0,
120 enum
122 PtrSize = sizeof(void*),
124 enum
126 // Per-M stack segment cache size.
127 StackCacheSize = 32,
128 // Global <-> per-M stack segment cache transfer batch size.
129 StackCacheBatch = 16,
133 * structures
135 union Lock
137 uint32 key; // futex-based impl
138 M* waitm; // linked list of waiting M's (sema-based impl)
140 union Note
142 uint32 key; // futex-based impl
143 M* waitm; // waiting M (sema-based impl)
145 struct String
147 const byte* str;
148 intgo len;
150 struct GCStats
152 // the struct must consist of only uint64's,
153 // because it is casted to uint64[].
154 uint64 nhandoff;
155 uint64 nhandoffcnt;
156 uint64 nprocyield;
157 uint64 nosyield;
158 uint64 nsleep;
161 // A location in the program, used for backtraces.
162 struct Location
164 uintptr pc;
165 String filename;
166 String function;
167 intgo lineno;
170 struct G
172 Defer* defer;
173 Panic* panic;
174 void* exception; // current exception being thrown
175 bool is_foreign; // whether current exception from other language
176 void *gcstack; // if status==Gsyscall, gcstack = stackbase to use during gc
177 uintptr gcstack_size;
178 void* gcnext_segment;
179 void* gcnext_sp;
180 void* gcinitial_sp;
181 ucontext_t gcregs;
182 byte* entry; // initial function
183 G* alllink; // on allg
184 void* param; // passed parameter on wakeup
185 bool fromgogo; // reached from gogo
186 int16 status;
187 int64 goid;
188 uint32 selgen; // valid sudog pointer
189 const char* waitreason; // if status==Gwaiting
190 G* schedlink;
191 bool readyonstop;
192 bool ispanic;
193 bool issystem;
194 int8 raceignore; // ignore race detection events
195 M* m; // for debuggers, but offset not hard-coded
196 M* lockedm;
197 M* idlem;
198 int32 sig;
199 int32 writenbuf;
200 byte* writebuf;
201 // DeferChunk *dchunk;
202 // DeferChunk *dchunknext;
203 uintptr sigcode0;
204 uintptr sigcode1;
205 // uintptr sigpc;
206 uintptr gopc; // pc of go statement that created this goroutine
208 int32 ncgo;
209 CgoMal* cgomal;
211 Traceback* traceback;
213 ucontext_t context;
214 void* stack_context[10];
217 struct M
219 G* g0; // goroutine with scheduling stack
220 G* gsignal; // signal-handling G
221 G* curg; // current running goroutine
222 int32 id;
223 int32 mallocing;
224 int32 throwing;
225 int32 gcing;
226 int32 locks;
227 int32 nomemprof;
228 int32 waitnextg;
229 int32 dying;
230 int32 profilehz;
231 int32 helpgc;
232 uint32 fastrand;
233 uint64 ncgocall; // number of cgo calls in total
234 Note havenextg;
235 G* nextg;
236 M* alllink; // on allm
237 M* schedlink;
238 MCache *mcache;
239 G* lockedg;
240 G* idleg;
241 Location createstack[32]; // Stack that created this thread.
242 M* nextwaitm; // next M waiting for lock
243 uintptr waitsema; // semaphore for parking on locks
244 uint32 waitsemacount;
245 uint32 waitsemalock;
246 GCStats gcstats;
247 bool racecall;
248 void* racepc;
250 uintptr settype_buf[1024];
251 uintptr settype_bufsize;
253 uintptr end[];
256 struct SigTab
258 int32 sig;
259 int32 flags;
261 enum
263 SigNotify = 1<<0, // let signal.Notify have signal, even if from kernel
264 SigKill = 1<<1, // if signal.Notify doesn't take it, exit quietly
265 SigThrow = 1<<2, // if signal.Notify doesn't take it, exit loudly
266 SigPanic = 1<<3, // if the signal is from the kernel, panic
267 SigDefault = 1<<4, // if the signal isn't explicitly requested, don't monitor it
270 #ifndef NSIG
271 #define NSIG 32
272 #endif
274 // NOTE(rsc): keep in sync with extern.go:/type.Func.
275 // Eventually, the loaded symbol table should be closer to this form.
276 struct Func
278 String name;
279 uintptr entry; // entry pc
283 #ifdef GOOS_windows
284 enum {
285 Windows = 1
287 #else
288 enum {
289 Windows = 0
291 #endif
293 struct Timers
295 Lock;
296 G *timerproc;
297 bool sleeping;
298 bool rescheduling;
299 Note waitnote;
300 Timer **t;
301 int32 len;
302 int32 cap;
305 // Package time knows the layout of this structure.
306 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
307 struct Timer
309 int32 i; // heap index
311 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
312 // each time calling f(now, arg) in the timer goroutine, so f must be
313 // a well-behaved function and not block.
314 int64 when;
315 int64 period;
316 void (*f)(int64, Eface);
317 Eface arg;
320 // Lock-free stack node.
321 struct LFNode
323 LFNode *next;
324 uintptr pushcnt;
327 // Parallel for descriptor.
328 struct ParFor
330 void (*body)(ParFor*, uint32); // executed for each element
331 uint32 done; // number of idle threads
332 uint32 nthr; // total number of threads
333 uint32 nthrmax; // maximum number of threads
334 uint32 thrseq; // thread id sequencer
335 uint32 cnt; // iteration space [0, cnt)
336 void *ctx; // arbitrary user context
337 bool wait; // if true, wait while all threads finish processing,
338 // otherwise parfor may return while other threads are still working
339 ParForThread *thr; // array of thread descriptors
340 // stats
341 uint64 nsteal;
342 uint64 nstealcnt;
343 uint64 nprocyield;
344 uint64 nosyield;
345 uint64 nsleep;
348 // Track memory allocated by code not written in Go during a cgo call,
349 // so that the garbage collector can see them.
350 struct CgoMal
352 CgoMal *next;
353 byte *alloc;
357 * defined macros
358 * you need super-gopher-guru privilege
359 * to add this list.
361 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
362 #define nil ((void*)0)
363 #define USED(v) ((void) v)
364 #define ROUND(x, n) (((x)+(n)-1)&~((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
367 * external data
369 extern uintptr runtime_zerobase;
370 extern G* runtime_allg;
371 extern G* runtime_lastg;
372 extern M* runtime_allm;
373 extern int32 runtime_gomaxprocs;
374 extern bool runtime_singleproc;
375 extern uint32 runtime_panicking;
376 extern int32 runtime_gcwaiting; // gc is waiting to run
377 extern int32 runtime_ncpu;
380 * common functions and data
382 intgo runtime_findnull(const byte*);
383 void runtime_dump(byte*, int32);
386 * very low level c-called
388 void runtime_args(int32, byte**);
389 void runtime_osinit();
390 void runtime_goargs(void);
391 void runtime_goenvs(void);
392 void runtime_goenvs_unix(void);
393 void runtime_throw(const char*) __attribute__ ((noreturn));
394 void runtime_panicstring(const char*) __attribute__ ((noreturn));
395 void runtime_prints(const char*);
396 void runtime_printf(const char*, ...);
397 void* runtime_mal(uintptr);
398 void runtime_schedinit(void);
399 void runtime_initsig(void);
400 void runtime_sigenable(uint32 sig);
401 int32 runtime_gotraceback(void);
402 void runtime_goroutineheader(G*);
403 void runtime_goroutinetrailer(G*);
404 void runtime_traceback();
405 void runtime_tracebackothers(G*);
406 void runtime_printtrace(Location*, int32, bool);
407 String runtime_gostring(const byte*);
408 String runtime_gostringnocopy(const byte*);
409 void* runtime_mstart(void*);
410 G* runtime_malg(int32, byte**, size_t*);
411 void runtime_minit(void);
412 void runtime_mallocinit(void);
413 void runtime_gosched(void);
414 void runtime_park(void(*)(Lock*), Lock*, const char*);
415 void runtime_tsleep(int64, const char*);
416 M* runtime_newm(void);
417 void runtime_goexit(void);
418 void runtime_entersyscall(void) __asm__ (GOSYM_PREFIX "syscall.Entersyscall");
419 void runtime_exitsyscall(void) __asm__ (GOSYM_PREFIX "syscall.Exitsyscall");
420 void siginit(void);
421 bool __go_sigsend(int32 sig);
422 int32 runtime_callers(int32, Location*, int32);
423 int64 runtime_nanotime(void);
424 int64 runtime_cputicks(void);
425 int64 runtime_tickspersecond(void);
426 void runtime_blockevent(int64, int32);
427 extern int64 runtime_blockprofilerate;
429 void runtime_stoptheworld(void);
430 void runtime_starttheworld(void);
431 extern uint32 runtime_worldsema;
432 G* __go_go(void (*pfn)(void*), void*);
435 * mutual exclusion locks. in the uncontended case,
436 * as fast as spin locks (just a few user-level instructions),
437 * but on the contention path they sleep in the kernel.
438 * a zeroed Lock is unlocked (no need to initialize each lock).
440 void runtime_lock(Lock*);
441 void runtime_unlock(Lock*);
444 * sleep and wakeup on one-time events.
445 * before any calls to notesleep or notewakeup,
446 * must call noteclear to initialize the Note.
447 * then, exactly one thread can call notesleep
448 * and exactly one thread can call notewakeup (once).
449 * once notewakeup has been called, the notesleep
450 * will return. future notesleep will return immediately.
451 * subsequent noteclear must be called only after
452 * previous notesleep has returned, e.g. it's disallowed
453 * to call noteclear straight after notewakeup.
455 * notetsleep is like notesleep but wakes up after
456 * a given number of nanoseconds even if the event
457 * has not yet happened. if a goroutine uses notetsleep to
458 * wake up early, it must wait to call noteclear until it
459 * can be sure that no other goroutine is calling
460 * notewakeup.
462 void runtime_noteclear(Note*);
463 void runtime_notesleep(Note*);
464 void runtime_notewakeup(Note*);
465 void runtime_notetsleep(Note*, int64);
468 * low-level synchronization for implementing the above
470 uintptr runtime_semacreate(void);
471 int32 runtime_semasleep(int64);
472 void runtime_semawakeup(M*);
473 // or
474 void runtime_futexsleep(uint32*, uint32, int64);
475 void runtime_futexwakeup(uint32*, uint32);
478 * Lock-free stack.
479 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
480 * The stack does not keep pointers to nodes,
481 * so they can be garbage collected if there are no other pointers to nodes.
483 void runtime_lfstackpush(uint64 *head, LFNode *node)
484 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
485 LFNode* runtime_lfstackpop(uint64 *head);
488 * Parallel for over [0, n).
489 * body() is executed for each iteration.
490 * nthr - total number of worker threads.
491 * ctx - arbitrary user context.
492 * if wait=true, threads return from parfor() when all work is done;
493 * otherwise, threads can return while other threads are still finishing processing.
495 ParFor* runtime_parforalloc(uint32 nthrmax);
496 void runtime_parforsetup(ParFor *desc, uint32 nthr, uint32 n, void *ctx, bool wait, void (*body)(ParFor*, uint32));
497 void runtime_parfordo(ParFor *desc) __asm__ (GOSYM_PREFIX "runtime.parfordo");
500 * low level C-called
502 #define runtime_mmap mmap
503 #define runtime_munmap munmap
504 #define runtime_madvise madvise
505 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
506 #define runtime_getcallerpc(p) __builtin_return_address(0)
508 #ifdef __rtems__
509 void __wrap_rtems_task_variable_add(void **);
510 #endif
513 * Names generated by gccgo.
515 #define runtime_printbool __go_print_bool
516 #define runtime_printfloat __go_print_double
517 #define runtime_printint __go_print_int64
518 #define runtime_printiface __go_print_interface
519 #define runtime_printeface __go_print_empty_interface
520 #define runtime_printstring __go_print_string
521 #define runtime_printpointer __go_print_pointer
522 #define runtime_printuint __go_print_uint64
523 #define runtime_printslice __go_print_slice
524 #define runtime_printcomplex __go_print_complex
527 * runtime go-called
529 void runtime_printbool(_Bool);
530 void runtime_printfloat(double);
531 void runtime_printint(int64);
532 void runtime_printiface(Iface);
533 void runtime_printeface(Eface);
534 void runtime_printstring(String);
535 void runtime_printpc(void*);
536 void runtime_printpointer(void*);
537 void runtime_printuint(uint64);
538 void runtime_printhex(uint64);
539 void runtime_printslice(Slice);
540 void runtime_printcomplex(__complex double);
542 struct __go_func_type;
543 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
544 void **, void **)
545 __asm__ (GOSYM_PREFIX "reflect.call");
547 /* Functions. */
548 #define runtime_panic __go_panic
549 #define runtime_write(d, v, n) write((d), (v), (n))
550 #define runtime_malloc(s) __go_alloc(s)
551 #define runtime_free(p) __go_free(p)
552 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
553 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
554 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
555 #define runtime_exit(s) exit(s)
556 MCache* runtime_allocmcache(void);
557 void free(void *v);
558 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
559 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
560 #define runtime_cas64(pval, pold, new) __atomic_compare_exchange_n (pval, pold, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
561 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
562 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
563 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
564 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
565 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
566 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
567 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
568 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
569 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
570 #define PREFETCH(p) __builtin_prefetch(p)
572 struct __go_func_type;
573 bool runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
574 #define runtime_getcallersp(p) __builtin_frame_address(1)
575 int32 runtime_mcount(void);
576 int32 runtime_gcount(void);
577 void runtime_dopanic(int32) __attribute__ ((noreturn));
578 void runtime_startpanic(void);
579 void runtime_ready(G*);
580 const byte* runtime_getenv(const char*);
581 int32 runtime_atoi(const byte*);
582 uint32 runtime_fastrand1(void);
584 void runtime_sigprof();
585 void runtime_resetcpuprofiler(int32);
586 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
587 void runtime_usleep(uint32);
590 * runtime c-called (but written in Go)
592 void runtime_printany(Eface)
593 __asm__ (GOSYM_PREFIX "runtime.Printany");
594 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
595 __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
596 void runtime_newErrorString(String, Eface*)
597 __asm__ (GOSYM_PREFIX "runtime.NewErrorString");
600 * wrapped for go users
602 #define ISNAN(f) __builtin_isnan(f)
603 void runtime_semacquire(uint32 volatile *);
604 void runtime_semrelease(uint32 volatile *);
605 int32 runtime_gomaxprocsfunc(int32 n);
606 void runtime_procyield(uint32);
607 void runtime_osyield(void);
608 void runtime_LockOSThread(void) __asm__ (GOSYM_PREFIX "runtime.LockOSThread");
609 void runtime_UnlockOSThread(void) __asm__ (GOSYM_PREFIX "runtime.UnlockOSThread");
611 bool runtime_showframe(String, bool);
613 uintptr runtime_memlimit(void);
615 // If appropriate, ask the operating system to control whether this
616 // thread should receive profiling signals. This is only necessary on OS X.
617 // An operating system should not deliver a profiling signal to a
618 // thread that is not actually executing (what good is that?), but that's
619 // what OS X prefers to do. When profiling is turned on, we mask
620 // away the profiling signal when threads go to sleep, so that OS X
621 // is forced to deliver the signal to a thread that's actually running.
622 // This is a no-op on other systems.
623 void runtime_setprof(bool);
625 enum
627 UseSpanType = 1,
630 void runtime_setsig(int32, bool, bool);
631 #define runtime_setitimer setitimer
633 void runtime_check(void);
635 // A list of global variables that the garbage collector must scan.
636 struct root_list {
637 struct root_list *next;
638 struct root {
639 void *decl;
640 size_t size;
641 } roots[];
644 void __go_register_gc_roots(struct root_list*);
646 // Size of stack space allocated using Go's allocator.
647 // This will be 0 when using split stacks, as in that case
648 // the stacks are allocated by the splitstack library.
649 extern uintptr runtime_stacks_sys;
651 struct backtrace_state;
652 extern struct backtrace_state *__go_get_backtrace_state(void);
653 extern _Bool __go_file_line(uintptr, String*, String*, intgo *);
654 extern byte* runtime_progname();
656 int32 getproccount(void);