compiler, runtime: replace hashmap code with Go 1.7 hashmap
[official-gcc.git] / libgo / runtime / runtime.h
blobdc00b421f996c4976793ee7f794e12a56b6debe6
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 <complex.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <pthread.h>
18 #include <semaphore.h>
19 #include <ucontext.h>
21 #ifdef HAVE_SYS_MMAN_H
22 #include <sys/mman.h>
23 #endif
25 #include "interface.h"
26 #include "go-alloc.h"
28 #define _STRINGIFY2_(x) #x
29 #define _STRINGIFY_(x) _STRINGIFY2_(x)
30 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
32 /* This file supports C files copied from the 6g runtime library.
33 This is a version of the 6g runtime.h rewritten for gccgo's version
34 of the code. */
36 typedef signed int int8 __attribute__ ((mode (QI)));
37 typedef unsigned int uint8 __attribute__ ((mode (QI)));
38 typedef signed int int16 __attribute__ ((mode (HI)));
39 typedef unsigned int uint16 __attribute__ ((mode (HI)));
40 typedef signed int int32 __attribute__ ((mode (SI)));
41 typedef unsigned int uint32 __attribute__ ((mode (SI)));
42 typedef signed int int64 __attribute__ ((mode (DI)));
43 typedef unsigned int uint64 __attribute__ ((mode (DI)));
44 typedef float float32 __attribute__ ((mode (SF)));
45 typedef double float64 __attribute__ ((mode (DF)));
46 typedef signed int intptr __attribute__ ((mode (pointer)));
47 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
49 typedef intptr intgo; // Go's int
50 typedef uintptr uintgo; // Go's uint
52 typedef uintptr uintreg;
54 /* Defined types. */
56 typedef uint8 bool;
57 typedef uint8 byte;
58 typedef struct Func Func;
59 typedef struct g G;
60 typedef struct mutex Lock;
61 typedef struct m M;
62 typedef struct p P;
63 typedef struct note Note;
64 typedef struct String String;
65 typedef struct FuncVal FuncVal;
66 typedef struct SigTab SigTab;
67 typedef struct mcache MCache;
68 typedef struct FixAlloc FixAlloc;
69 typedef struct Hchan Hchan;
70 typedef struct Timers Timers;
71 typedef struct Timer Timer;
72 typedef struct gcstats GCStats;
73 typedef struct LFNode LFNode;
74 typedef struct ParFor ParFor;
75 typedef struct ParForThread ParForThread;
76 typedef struct cgoMal CgoMal;
77 typedef struct PollDesc PollDesc;
78 typedef struct DebugVars DebugVars;
80 typedef struct __go_open_array Slice;
81 typedef struct __go_interface Iface;
82 typedef struct __go_empty_interface Eface;
83 typedef struct __go_type_descriptor Type;
84 typedef struct _defer Defer;
85 typedef struct _panic Panic;
87 typedef struct __go_ptr_type PtrType;
88 typedef struct __go_func_type FuncType;
89 typedef struct __go_interface_type InterfaceType;
90 typedef struct __go_map_type MapType;
91 typedef struct __go_channel_type ChanType;
93 typedef struct traceback Traceback;
95 typedef struct location Location;
97 struct String
99 const byte* str;
100 intgo len;
103 struct FuncVal
105 void (*fn)(void);
106 // variable-size, fn-specific data here
109 #include "array.h"
110 #include "interface.h"
112 #include "runtime.inc"
115 * Per-CPU declaration.
117 extern M* runtime_m(void);
118 extern G* runtime_g(void);
120 extern M runtime_m0;
121 extern G runtime_g0;
123 enum
125 true = 1,
126 false = 0,
128 enum
130 PtrSize = sizeof(void*),
132 enum
134 // Per-M stack segment cache size.
135 StackCacheSize = 32,
136 // Global <-> per-M stack segment cache transfer batch size.
137 StackCacheBatch = 16,
140 struct SigTab
142 int32 sig;
143 int32 flags;
144 void* fwdsig;
147 // Layout of in-memory per-function information prepared by linker
148 // See http://golang.org/s/go12symtab.
149 // Keep in sync with linker and with ../../libmach/sym.c
150 // and with package debug/gosym.
151 struct Func
153 String name;
154 uintptr entry; // entry pc
157 #ifdef GOOS_nacl
158 enum {
159 NaCl = 1,
161 #else
162 enum {
163 NaCl = 0,
165 #endif
167 #ifdef GOOS_windows
168 enum {
169 Windows = 1
171 #else
172 enum {
173 Windows = 0
175 #endif
176 #ifdef GOOS_solaris
177 enum {
178 Solaris = 1
180 #else
181 enum {
182 Solaris = 0
184 #endif
186 struct Timers
188 Lock;
189 G *timerproc;
190 bool sleeping;
191 bool rescheduling;
192 Note waitnote;
193 Timer **t;
194 int32 len;
195 int32 cap;
198 // Package time knows the layout of this structure.
199 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
200 // For GOOS=nacl, package syscall knows the layout of this structure.
201 // If this struct changes, adjust ../syscall/net_nacl.go:/runtimeTimer.
202 struct Timer
204 intgo i; // heap index
206 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
207 // each time calling f(now, arg) in the timer goroutine, so f must be
208 // a well-behaved function and not block.
209 int64 when;
210 int64 period;
211 FuncVal *fv;
212 Eface arg;
213 uintptr seq;
216 // Lock-free stack node.
217 struct LFNode
219 LFNode *next;
220 uintptr pushcnt;
223 // Parallel for descriptor.
224 struct ParFor
226 const FuncVal *body; // executed for each element
227 uint32 done; // number of idle threads
228 uint32 nthr; // total number of threads
229 uint32 nthrmax; // maximum number of threads
230 uint32 thrseq; // thread id sequencer
231 uint32 cnt; // iteration space [0, cnt)
232 bool wait; // if true, wait while all threads finish processing,
233 // otherwise parfor may return while other threads are still working
234 ParForThread *thr; // array of thread descriptors
235 // stats
236 uint64 nsteal;
237 uint64 nstealcnt;
238 uint64 nprocyield;
239 uint64 nosyield;
240 uint64 nsleep;
243 // Holds variables parsed from GODEBUG env var.
244 struct DebugVars
246 int32 allocfreetrace;
247 int32 cgocheck;
248 int32 efence;
249 int32 gccheckmark;
250 int32 gcpacertrace;
251 int32 gcshrinkstackoff;
252 int32 gcstackbarrieroff;
253 int32 gcstackbarrierall;
254 int32 gcstoptheworld;
255 int32 gctrace;
256 int32 gcdead;
257 int32 invalidptr;
258 int32 sbrk;
259 int32 scavenge;
260 int32 scheddetail;
261 int32 schedtrace;
262 int32 wbshadow;
265 extern bool runtime_precisestack;
266 extern bool runtime_copystack;
269 * defined macros
270 * you need super-gopher-guru privilege
271 * to add this list.
273 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
274 #define nil ((void*)0)
275 #define USED(v) ((void) v)
276 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
278 byte* runtime_startup_random_data;
279 uint32 runtime_startup_random_data_len;
280 void runtime_get_random_data(byte**, int32*);
282 enum {
283 // hashinit wants this many random bytes
284 HashRandomBytes = 32
286 void runtime_hashinit(void);
288 void runtime_traceback(void);
289 void runtime_tracebackothers(G*);
290 enum
292 // The maximum number of frames we print for a traceback
293 TracebackMaxFrames = 100,
297 * external data
299 extern uintptr runtime_zerobase;
300 extern G** runtime_allg;
301 extern uintptr runtime_allglen;
302 extern G* runtime_lastg;
303 extern M* runtime_allm;
304 extern P** runtime_allp;
305 extern int32 runtime_gomaxprocs;
306 extern uint32 runtime_needextram;
307 extern uint32 runtime_panicking;
308 extern int8* runtime_goos;
309 extern int32 runtime_ncpu;
310 extern void (*runtime_sysargs)(int32, uint8**);
311 extern uint32 runtime_Hchansize;
312 extern DebugVars runtime_debug;
313 extern uintptr runtime_maxstacksize;
315 extern bool runtime_isstarted;
316 extern bool runtime_isarchive;
319 * common functions and data
321 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
322 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
323 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
324 intgo runtime_findnull(const byte*);
325 intgo runtime_findnullw(const uint16*);
326 void runtime_dump(byte*, int32);
328 void runtime_gogo(G*);
329 struct __go_func_type;
330 void runtime_args(int32, byte**);
331 void runtime_osinit();
332 void runtime_goargs(void);
333 void runtime_goenvs(void);
334 void runtime_goenvs_unix(void);
335 void runtime_throw(const char*) __attribute__ ((noreturn));
336 void runtime_panicstring(const char*) __attribute__ ((noreturn));
337 bool runtime_canpanic(G*);
338 void runtime_prints(const char*);
339 void runtime_printf(const char*, ...);
340 int32 runtime_snprintf(byte*, int32, const char*, ...);
341 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
342 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
343 void* runtime_mal(uintptr);
344 String runtime_gostring(const byte*);
345 String runtime_gostringnocopy(const byte*);
346 void runtime_schedinit(void);
347 void runtime_initsig(bool);
348 void runtime_sigenable(uint32 sig);
349 void runtime_sigdisable(uint32 sig);
350 void runtime_sigignore(uint32 sig);
351 int32 runtime_gotraceback(bool *crash);
352 void runtime_goroutineheader(G*);
353 void runtime_printtrace(Location*, int32, bool);
354 #define runtime_open(p, f, m) open((p), (f), (m))
355 #define runtime_read(d, v, n) read((d), (v), (n))
356 #define runtime_write(d, v, n) write((d), (v), (n))
357 #define runtime_close(d) close(d)
358 void runtime_ready(G*);
359 String runtime_getenv(const char*);
360 int32 runtime_atoi(const byte*, intgo);
361 void* runtime_mstart(void*);
362 G* runtime_malg(int32, byte**, uintptr*);
363 void runtime_mpreinit(M*);
364 void runtime_minit(void);
365 void runtime_unminit(void);
366 void runtime_needm(void);
367 void runtime_dropm(void);
368 void runtime_signalstack(byte*, int32);
369 MCache* runtime_allocmcache(void);
370 void runtime_freemcache(MCache*);
371 void runtime_mallocinit(void);
372 void runtime_mprofinit(void);
373 #define runtime_malloc(s) __go_alloc(s)
374 #define runtime_free(p) __go_free(p)
375 #define runtime_getcallersp(p) __builtin_frame_address(1)
376 int32 runtime_mcount(void);
377 int32 runtime_gcount(void);
378 void runtime_mcall(void(*)(G*));
379 uint32 runtime_fastrand1(void) __asm__ (GOSYM_PREFIX "runtime.fastrand1");
380 int32 runtime_timediv(int64, int32, int32*);
381 int32 runtime_round2(int32 x); // round x up to a power of 2.
383 // atomic operations
384 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
385 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
386 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
387 // Don't confuse with XADD x86 instruction,
388 // this one is actually 'addx', that is, add-and-fetch.
389 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
390 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
391 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
392 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
393 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
394 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
395 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
396 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
397 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
398 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
399 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
401 void runtime_setg(G*);
402 void runtime_newextram(void);
403 #define runtime_exit(s) exit(s)
404 #define runtime_breakpoint() __builtin_trap()
405 void runtime_gosched(void);
406 void runtime_gosched0(G*);
407 void runtime_schedtrace(bool);
408 void runtime_park(bool(*)(G*, void*), void*, const char*);
409 void runtime_parkunlock(Lock*, const char*);
410 void runtime_tsleep(int64, const char*);
411 M* runtime_newm(void);
412 void runtime_goexit(void);
413 void runtime_entersyscall(void) __asm__ (GOSYM_PREFIX "syscall.Entersyscall");
414 void runtime_entersyscallblock(void);
415 void runtime_exitsyscall(void) __asm__ (GOSYM_PREFIX "syscall.Exitsyscall");
416 G* __go_go(void (*pfn)(void*), void*);
417 void siginit(void);
418 bool __go_sigsend(int32 sig);
419 int32 runtime_callers(int32, Location*, int32, bool keep_callers);
420 int64 runtime_nanotime(void); // monotonic time
421 int64 runtime_unixnanotime(void); // real time, can skip
422 void runtime_dopanic(int32) __attribute__ ((noreturn));
423 void runtime_startpanic(void);
424 void runtime_freezetheworld(void);
425 void runtime_unwindstack(G*, byte*);
426 void runtime_sigprof();
427 void runtime_resetcpuprofiler(int32);
428 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
429 void runtime_usleep(uint32);
430 int64 runtime_cputicks(void);
431 int64 runtime_tickspersecond(void);
432 void runtime_blockevent(int64, int32);
433 extern int64 runtime_blockprofilerate;
434 void runtime_addtimer(Timer*);
435 bool runtime_deltimer(Timer*);
436 G* runtime_netpoll(bool);
437 void runtime_netpollinit(void);
438 int32 runtime_netpollopen(uintptr, PollDesc*);
439 int32 runtime_netpollclose(uintptr);
440 void runtime_netpollready(G**, PollDesc*, int32);
441 uintptr runtime_netpollfd(PollDesc*);
442 void runtime_netpollarm(PollDesc*, int32);
443 void** runtime_netpolluser(PollDesc*);
444 bool runtime_netpollclosing(PollDesc*);
445 void runtime_netpolllock(PollDesc*);
446 void runtime_netpollunlock(PollDesc*);
447 void runtime_crash(void);
448 void runtime_parsedebugvars(void);
449 void _rt0_go(void);
450 void* runtime_funcdata(Func*, int32);
451 int32 runtime_setmaxthreads(int32);
452 G* runtime_timejump(void);
453 void runtime_iterate_finq(void (*callback)(FuncVal*, void*, const FuncType*, const PtrType*));
455 void runtime_stoptheworld(void);
456 void runtime_starttheworld(void);
457 extern uint32 runtime_worldsema;
460 * mutual exclusion locks. in the uncontended case,
461 * as fast as spin locks (just a few user-level instructions),
462 * but on the contention path they sleep in the kernel.
463 * a zeroed Lock is unlocked (no need to initialize each lock).
465 void runtime_lock(Lock*);
466 void runtime_unlock(Lock*);
469 * sleep and wakeup on one-time events.
470 * before any calls to notesleep or notewakeup,
471 * must call noteclear to initialize the Note.
472 * then, exactly one thread can call notesleep
473 * and exactly one thread can call notewakeup (once).
474 * once notewakeup has been called, the notesleep
475 * will return. future notesleep will return immediately.
476 * subsequent noteclear must be called only after
477 * previous notesleep has returned, e.g. it's disallowed
478 * to call noteclear straight after notewakeup.
480 * notetsleep is like notesleep but wakes up after
481 * a given number of nanoseconds even if the event
482 * has not yet happened. if a goroutine uses notetsleep to
483 * wake up early, it must wait to call noteclear until it
484 * can be sure that no other goroutine is calling
485 * notewakeup.
487 * notesleep/notetsleep are generally called on g0,
488 * notetsleepg is similar to notetsleep but is called on user g.
490 void runtime_noteclear(Note*);
491 void runtime_notesleep(Note*);
492 void runtime_notewakeup(Note*);
493 bool runtime_notetsleep(Note*, int64); // false - timeout
494 bool runtime_notetsleepg(Note*, int64); // false - timeout
497 * low-level synchronization for implementing the above
499 uintptr runtime_semacreate(void);
500 int32 runtime_semasleep(int64);
501 void runtime_semawakeup(M*);
502 // or
503 void runtime_futexsleep(uint32*, uint32, int64);
504 void runtime_futexwakeup(uint32*, uint32);
507 * Lock-free stack.
508 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
509 * The stack does not keep pointers to nodes,
510 * so they can be garbage collected if there are no other pointers to nodes.
512 void runtime_lfstackpush(uint64 *head, LFNode *node)
513 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
514 LFNode* runtime_lfstackpop(uint64 *head);
517 * Parallel for over [0, n).
518 * body() is executed for each iteration.
519 * nthr - total number of worker threads.
520 * if wait=true, threads return from parfor() when all work is done;
521 * otherwise, threads can return while other threads are still finishing processing.
523 ParFor* runtime_parforalloc(uint32 nthrmax);
524 void runtime_parforsetup(ParFor *desc, uint32 nthr, uint32 n, bool wait, const FuncVal *body);
525 void runtime_parfordo(ParFor *desc);
526 void runtime_parforiters(ParFor*, uintptr, uintptr*, uintptr*);
529 * low level C-called
531 #define runtime_mmap mmap
532 #define runtime_munmap munmap
533 #define runtime_madvise madvise
534 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
535 #define runtime_getcallerpc(p) __builtin_return_address(0)
537 #ifdef __rtems__
538 void __wrap_rtems_task_variable_add(void **);
539 #endif
542 * Names generated by gccgo.
544 #define runtime_printbool __go_print_bool
545 #define runtime_printfloat __go_print_double
546 #define runtime_printint __go_print_int64
547 #define runtime_printiface __go_print_interface
548 #define runtime_printeface __go_print_empty_interface
549 #define runtime_printstring __go_print_string
550 #define runtime_printpointer __go_print_pointer
551 #define runtime_printuint __go_print_uint64
552 #define runtime_printslice __go_print_slice
553 #define runtime_printcomplex __go_print_complex
556 * runtime go-called
558 void runtime_printbool(_Bool);
559 void runtime_printbyte(int8);
560 void runtime_printfloat(double);
561 void runtime_printint(int64);
562 void runtime_printiface(Iface);
563 void runtime_printeface(Eface);
564 void runtime_printstring(String);
565 void runtime_printpc(void*);
566 void runtime_printpointer(void*);
567 void runtime_printuint(uint64);
568 void runtime_printhex(uint64);
569 void runtime_printslice(Slice);
570 void runtime_printcomplex(complex double);
571 void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool,
572 void **, void **)
573 __asm__ (GOSYM_PREFIX "reflect.call");
574 #define runtime_panic __go_panic
577 * runtime c-called (but written in Go)
579 void runtime_printany(Eface)
580 __asm__ (GOSYM_PREFIX "runtime.Printany");
581 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
582 __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
583 void runtime_newErrorCString(const char*, Eface*)
584 __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
587 * wrapped for go users
589 void runtime_semacquire(uint32 volatile *, bool);
590 void runtime_semrelease(uint32 volatile *);
591 int32 runtime_gomaxprocsfunc(int32 n);
592 void runtime_procyield(uint32);
593 void runtime_osyield(void);
594 void runtime_lockOSThread(void);
595 void runtime_unlockOSThread(void);
596 bool runtime_lockedOSThread(void);
598 bool runtime_showframe(String, bool);
599 void runtime_printcreatedby(G*);
601 uintptr runtime_memlimit(void);
603 #define ISNAN(f) __builtin_isnan(f)
605 enum
607 UseSpanType = 1,
610 #define runtime_setitimer setitimer
612 void runtime_check(void);
614 // A list of global variables that the garbage collector must scan.
615 struct root_list {
616 struct root_list *next;
617 struct root {
618 void *decl;
619 size_t size;
620 } roots[];
623 void __go_register_gc_roots(struct root_list*);
625 // Size of stack space allocated using Go's allocator.
626 // This will be 0 when using split stacks, as in that case
627 // the stacks are allocated by the splitstack library.
628 extern uintptr runtime_stacks_sys;
630 struct backtrace_state;
631 extern struct backtrace_state *__go_get_backtrace_state(void);
632 extern _Bool __go_file_line(uintptr, int, String*, String*, intgo *);
633 extern byte* runtime_progname();
634 extern void runtime_main(void*);
635 extern uint32 runtime_in_callers;
637 int32 getproccount(void);
639 #define PREFETCH(p) __builtin_prefetch(p)
641 bool runtime_gcwaiting(void);
642 void runtime_badsignal(int);
643 Defer* runtime_newdefer(void);
644 void runtime_freedefer(Defer*);
646 struct time_now_ret
648 int64_t sec;
649 int32_t nsec;
652 struct time_now_ret now() __asm__ (GOSYM_PREFIX "time.now")
653 __attribute__ ((no_split_stack));
655 extern void _cgo_wait_runtime_init_done (void);
656 extern void _cgo_notify_runtime_init_done (void);
657 extern _Bool runtime_iscgo;
658 extern _Bool runtime_cgoHasExtraM;
659 extern Hchan *runtime_main_init_done;
660 extern uintptr __go_end __attribute__ ((weak));