* README.Portability: Remove note on an Irix compatibility issue.
[official-gcc.git] / libgo / runtime / runtime.h
blob5fd115589a7c13bfab003e4d59b3b6d11e326d3a
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 <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <pthread.h>
19 #include <semaphore.h>
20 #include <ucontext.h>
22 #ifdef HAVE_SYS_MMAN_H
23 #include <sys/mman.h>
24 #endif
26 #define _STRINGIFY2_(x) #x
27 #define _STRINGIFY_(x) _STRINGIFY2_(x)
28 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
30 /* This file supports C files copied from the 6g runtime library.
31 This is a version of the 6g runtime.h rewritten for gccgo's version
32 of the code. */
34 typedef signed int int8 __attribute__ ((mode (QI)));
35 typedef unsigned int uint8 __attribute__ ((mode (QI)));
36 typedef signed int int16 __attribute__ ((mode (HI)));
37 typedef unsigned int uint16 __attribute__ ((mode (HI)));
38 typedef signed int int32 __attribute__ ((mode (SI)));
39 typedef unsigned int uint32 __attribute__ ((mode (SI)));
40 typedef signed int int64 __attribute__ ((mode (DI)));
41 typedef unsigned int uint64 __attribute__ ((mode (DI)));
42 typedef float float32 __attribute__ ((mode (SF)));
43 typedef double float64 __attribute__ ((mode (DF)));
44 typedef signed int intptr __attribute__ ((mode (pointer)));
45 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
47 typedef intptr intgo; // Go's int
48 typedef uintptr uintgo; // Go's uint
50 typedef uintptr uintreg;
52 /* Defined types. */
54 typedef _Bool bool;
55 typedef uint8 byte;
56 typedef struct g G;
57 typedef struct mutex Lock;
58 typedef struct m M;
59 typedef struct p P;
60 typedef struct note Note;
61 typedef struct String String;
62 typedef struct FuncVal FuncVal;
63 typedef struct SigTab SigTab;
64 typedef struct mcache MCache;
65 typedef struct FixAlloc FixAlloc;
66 typedef struct hchan Hchan;
67 typedef struct timer Timer;
68 typedef struct gcstats GCStats;
69 typedef struct lfnode LFNode;
70 typedef struct ParFor ParFor;
71 typedef struct ParForThread ParForThread;
72 typedef struct cgoMal CgoMal;
73 typedef struct PollDesc PollDesc;
74 typedef struct sudog SudoG;
75 typedef struct schedt Sched;
77 typedef struct __go_open_array Slice;
78 typedef struct iface Iface;
79 typedef struct eface Eface;
80 typedef struct __go_type_descriptor Type;
81 typedef struct _defer Defer;
82 typedef struct _panic Panic;
84 typedef struct __go_ptr_type PtrType;
85 typedef struct __go_func_type FuncType;
86 typedef struct __go_interface_type InterfaceType;
87 typedef struct __go_map_type MapType;
88 typedef struct __go_channel_type ChanType;
90 typedef struct tracebackg Traceback;
92 typedef struct location Location;
94 struct String
96 const byte* str;
97 intgo len;
100 struct FuncVal
102 void (*fn)(void);
103 // variable-size, fn-specific data here
106 #include "array.h"
108 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
109 // the name conflict.
110 #define timeval go_timeval
111 #define timespec go_timespec
113 #include "runtime.inc"
115 #undef timeval
116 #undef timespec
119 * Per-CPU declaration.
121 extern M* runtime_m(void);
122 extern G* runtime_g(void)
123 __asm__(GOSYM_PREFIX "runtime.getg");
125 extern M runtime_m0;
126 extern G runtime_g0;
128 enum
130 true = 1,
131 false = 0,
133 enum
135 PtrSize = sizeof(void*),
137 enum
139 // Per-M stack segment cache size.
140 StackCacheSize = 32,
141 // Global <-> per-M stack segment cache transfer batch size.
142 StackCacheBatch = 16,
145 struct SigTab
147 int32 sig;
148 int32 flags;
149 void* fwdsig;
152 #ifdef GOOS_nacl
153 enum {
154 NaCl = 1,
156 #else
157 enum {
158 NaCl = 0,
160 #endif
162 #ifdef GOOS_windows
163 enum {
164 Windows = 1
166 #else
167 enum {
168 Windows = 0
170 #endif
171 #ifdef GOOS_solaris
172 enum {
173 Solaris = 1
175 #else
176 enum {
177 Solaris = 0
179 #endif
181 // Parallel for descriptor.
182 struct ParFor
184 const FuncVal *body; // executed for each element
185 uint32 done; // number of idle threads
186 uint32 nthr; // total number of threads
187 uint32 nthrmax; // maximum number of threads
188 uint32 thrseq; // thread id sequencer
189 uint32 cnt; // iteration space [0, cnt)
190 bool wait; // if true, wait while all threads finish processing,
191 // otherwise parfor may return while other threads are still working
192 ParForThread *thr; // array of thread descriptors
193 // stats
194 uint64 nsteal;
195 uint64 nstealcnt;
196 uint64 nprocyield;
197 uint64 nosyield;
198 uint64 nsleep;
201 extern bool runtime_precisestack;
202 extern bool runtime_copystack;
205 * defined macros
206 * you need super-gopher-guru privilege
207 * to add this list.
209 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
210 #define nil ((void*)0)
211 #define USED(v) ((void) v)
212 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
214 enum {
215 // hashinit wants this many random bytes
216 HashRandomBytes = 32
218 void runtime_hashinit(void);
220 void runtime_traceback(int32)
221 __asm__ (GOSYM_PREFIX "runtime.traceback");
222 void runtime_tracebackothers(G*)
223 __asm__ (GOSYM_PREFIX "runtime.tracebackothers");
224 enum
226 // The maximum number of frames we print for a traceback
227 TracebackMaxFrames = 100,
231 * external data
233 extern uintptr* runtime_getZerobase(void)
234 __asm__(GOSYM_PREFIX "runtime.getZerobase");
235 extern G* runtime_getallg(intgo)
236 __asm__(GOSYM_PREFIX "runtime.getallg");
237 extern uintptr runtime_getallglen(void)
238 __asm__(GOSYM_PREFIX "runtime.getallglen");
239 extern G* runtime_lastg;
240 extern M* runtime_getallm(void)
241 __asm__(GOSYM_PREFIX "runtime.getallm");
242 extern P** runtime_allp;
243 extern Sched* runtime_sched;
244 extern uint32 runtime_panicking(void)
245 __asm__ (GOSYM_PREFIX "runtime.getPanicking");
246 extern int8* runtime_goos;
247 extern int32 runtime_ncpu;
248 extern void (*runtime_sysargs)(int32, uint8**);
249 extern struct debugVars runtime_debug;
251 extern bool runtime_isstarted;
252 extern bool runtime_isarchive;
255 * common functions and data
257 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
258 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
259 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
260 intgo runtime_findnull(const byte*)
261 __asm__ (GOSYM_PREFIX "runtime.findnull");
263 void runtime_gogo(G*)
264 __asm__ (GOSYM_PREFIX "runtime.gogo");
265 struct __go_func_type;
266 void runtime_args(int32, byte**)
267 __asm__ (GOSYM_PREFIX "runtime.args");
268 void runtime_osinit();
269 void runtime_alginit(void)
270 __asm__ (GOSYM_PREFIX "runtime.alginit");
271 void runtime_goargs(void)
272 __asm__ (GOSYM_PREFIX "runtime.goargs");
273 void runtime_goenvs(void);
274 void runtime_goenvs_unix(void)
275 __asm__ (GOSYM_PREFIX "runtime.goenvs_unix");
276 void runtime_throw(const char*) __attribute__ ((noreturn));
277 void runtime_panicstring(const char*) __attribute__ ((noreturn));
278 bool runtime_canpanic(G*);
279 void runtime_printf(const char*, ...);
280 int32 runtime_snprintf(byte*, int32, const char*, ...);
281 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
282 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
283 void* runtime_mal(uintptr);
284 String runtime_gostringnocopy(const byte*)
285 __asm__ (GOSYM_PREFIX "runtime.gostringnocopy");
286 void runtime_schedinit(void);
287 void runtime_initsig(bool)
288 __asm__ (GOSYM_PREFIX "runtime.initsig");
289 int32 runtime_gotraceback(bool *crash);
290 void runtime_goroutineheader(G*)
291 __asm__ (GOSYM_PREFIX "runtime.goroutineheader");
292 void runtime_printtrace(Slice, G*)
293 __asm__ (GOSYM_PREFIX "runtime.printtrace");
294 #define runtime_open(p, f, m) open((p), (f), (m))
295 #define runtime_read(d, v, n) read((d), (v), (n))
296 #define runtime_write(d, v, n) write((d), (v), (n))
297 #define runtime_close(d) close(d)
298 void runtime_ready(G*, intgo, bool)
299 __asm__ (GOSYM_PREFIX "runtime.ready");
300 String runtime_getenv(const char*);
301 int32 runtime_atoi(const byte*, intgo);
302 void* runtime_mstart(void*);
303 G* runtime_malg(bool, bool, byte**, uintptr*)
304 __asm__(GOSYM_PREFIX "runtime.malg");
305 void runtime_minit(void)
306 __asm__ (GOSYM_PREFIX "runtime.minit");
307 void runtime_signalstack(byte*, uintptr)
308 __asm__ (GOSYM_PREFIX "runtime.signalstack");
309 MCache* runtime_allocmcache(void)
310 __asm__ (GOSYM_PREFIX "runtime.allocmcache");
311 void runtime_freemcache(MCache*)
312 __asm__ (GOSYM_PREFIX "runtime.freemcache");
313 void runtime_mallocinit(void);
314 void runtime_mprofinit(void);
315 #define runtime_getcallersp(p) __builtin_frame_address(0)
316 void runtime_mcall(void(*)(G*));
317 uint32 runtime_fastrand(void) __asm__ (GOSYM_PREFIX "runtime.fastrand");
318 int32 runtime_timediv(int64, int32, int32*)
319 __asm__ (GOSYM_PREFIX "runtime.timediv");
320 int32 runtime_round2(int32 x); // round x up to a power of 2.
322 // atomic operations
323 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
324 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
325 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
326 // Don't confuse with XADD x86 instruction,
327 // this one is actually 'addx', that is, add-and-fetch.
328 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
329 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
330 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
331 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
332 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
333 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
334 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
335 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
336 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
337 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
338 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
340 void runtime_setg(G*)
341 __asm__ (GOSYM_PREFIX "runtime.setg");
342 void runtime_newextram(void)
343 __asm__ (GOSYM_PREFIX "runtime.newextram");
344 #define runtime_exit(s) exit(s)
345 #define runtime_breakpoint() __builtin_trap()
346 void runtime_gosched(void);
347 void runtime_gosched0(G*);
348 void runtime_schedtrace(bool)
349 __asm__ (GOSYM_PREFIX "runtime.schedtrace");
350 void runtime_park(bool(*)(G*, void*), void*, const char*);
351 void runtime_parkunlock(Lock*, const char*);
352 void runtime_tsleep(int64, const char*);
353 M* runtime_newm(void);
354 void runtime_goexit1(void)
355 __asm__ (GOSYM_PREFIX "runtime.goexit1");
356 void runtime_entersyscall(int32)
357 __asm__ (GOSYM_PREFIX "runtime.entersyscall");
358 void runtime_entersyscallblock(int32)
359 __asm__ (GOSYM_PREFIX "runtime.entersyscallblock");
360 void runtime_exitsyscall(int32)
361 __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
362 G* __go_go(void (*pfn)(void*), void*);
363 int32 runtime_callers(int32, Location*, int32, bool keep_callers);
364 int64 runtime_nanotime(void) // monotonic time
365 __asm__(GOSYM_PREFIX "runtime.nanotime");
366 int64 runtime_unixnanotime(void) // real time, can skip
367 __asm__ (GOSYM_PREFIX "runtime.unixnanotime");
368 void runtime_dopanic(int32) __attribute__ ((noreturn));
369 void runtime_startpanic(void)
370 __asm__ (GOSYM_PREFIX "runtime.startpanic");
371 void runtime_unwindstack(G*, byte*);
372 void runtime_sigprof()
373 __asm__ (GOSYM_PREFIX "runtime.sigprof");
374 void runtime_resetcpuprofiler(int32)
375 __asm__ (GOSYM_PREFIX "runtime.resetcpuprofiler");
376 void runtime_setcpuprofilerate_m(int32)
377 __asm__ (GOSYM_PREFIX "runtime.setcpuprofilerate_m");
378 void runtime_cpuprofAdd(Slice)
379 __asm__ (GOSYM_PREFIX "runtime.cpuprofAdd");
380 void runtime_usleep(uint32)
381 __asm__ (GOSYM_PREFIX "runtime.usleep");
382 int64 runtime_cputicks(void)
383 __asm__ (GOSYM_PREFIX "runtime.cputicks");
384 int64 runtime_tickspersecond(void)
385 __asm__ (GOSYM_PREFIX "runtime.tickspersecond");
386 void runtime_blockevent(int64, int32);
387 extern int64 runtime_blockprofilerate;
388 G* runtime_netpoll(bool)
389 __asm__ (GOSYM_PREFIX "runtime.netpoll");
390 void runtime_crash(void)
391 __asm__ (GOSYM_PREFIX "runtime.crash");
392 void runtime_parsedebugvars(void)
393 __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
394 void _rt0_go(void);
395 G* runtime_timejump(void);
396 void runtime_iterate_finq(void (*callback)(FuncVal*, void*, const FuncType*, const PtrType*));
398 void runtime_stopTheWorldWithSema(void)
399 __asm__(GOSYM_PREFIX "runtime.stopTheWorldWithSema");
400 void runtime_startTheWorldWithSema(void)
401 __asm__(GOSYM_PREFIX "runtime.startTheWorldWithSema");
402 void runtime_acquireWorldsema(void)
403 __asm__(GOSYM_PREFIX "runtime.acquireWorldsema");
404 void runtime_releaseWorldsema(void)
405 __asm__(GOSYM_PREFIX "runtime.releaseWorldsema");
408 * mutual exclusion locks. in the uncontended case,
409 * as fast as spin locks (just a few user-level instructions),
410 * but on the contention path they sleep in the kernel.
411 * a zeroed Lock is unlocked (no need to initialize each lock).
413 void runtime_lock(Lock*)
414 __asm__(GOSYM_PREFIX "runtime.lock");
415 void runtime_unlock(Lock*)
416 __asm__(GOSYM_PREFIX "runtime.unlock");
419 * sleep and wakeup on one-time events.
420 * before any calls to notesleep or notewakeup,
421 * must call noteclear to initialize the Note.
422 * then, exactly one thread can call notesleep
423 * and exactly one thread can call notewakeup (once).
424 * once notewakeup has been called, the notesleep
425 * will return. future notesleep will return immediately.
426 * subsequent noteclear must be called only after
427 * previous notesleep has returned, e.g. it's disallowed
428 * to call noteclear straight after notewakeup.
430 * notetsleep is like notesleep but wakes up after
431 * a given number of nanoseconds even if the event
432 * has not yet happened. if a goroutine uses notetsleep to
433 * wake up early, it must wait to call noteclear until it
434 * can be sure that no other goroutine is calling
435 * notewakeup.
437 * notesleep/notetsleep are generally called on g0,
438 * notetsleepg is similar to notetsleep but is called on user g.
440 void runtime_noteclear(Note*)
441 __asm__ (GOSYM_PREFIX "runtime.noteclear");
442 void runtime_notesleep(Note*)
443 __asm__ (GOSYM_PREFIX "runtime.notesleep");
444 void runtime_notewakeup(Note*)
445 __asm__ (GOSYM_PREFIX "runtime.notewakeup");
446 bool runtime_notetsleep(Note*, int64) // false - timeout
447 __asm__ (GOSYM_PREFIX "runtime.notetsleep");
448 bool runtime_notetsleepg(Note*, int64) // false - timeout
449 __asm__ (GOSYM_PREFIX "runtime.notetsleepg");
452 * Lock-free stack.
453 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
454 * The stack does not keep pointers to nodes,
455 * so they can be garbage collected if there are no other pointers to nodes.
457 void runtime_lfstackpush(uint64 *head, LFNode *node)
458 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
459 void* runtime_lfstackpop(uint64 *head)
460 __asm__ (GOSYM_PREFIX "runtime.lfstackpop");
463 * Parallel for over [0, n).
464 * body() is executed for each iteration.
465 * nthr - total number of worker threads.
466 * if wait=true, threads return from parfor() when all work is done;
467 * otherwise, threads can return while other threads are still finishing processing.
469 ParFor* runtime_parforalloc(uint32 nthrmax);
470 void runtime_parforsetup(ParFor *desc, uint32 nthr, uint32 n, bool wait, const FuncVal *body);
471 void runtime_parfordo(ParFor *desc);
472 void runtime_parforiters(ParFor*, uintptr, uintptr*, uintptr*);
475 * low level C-called
477 #define runtime_mmap mmap
478 #define runtime_munmap munmap
479 #define runtime_madvise madvise
480 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
481 #define runtime_getcallerpc(p) __builtin_return_address(0)
483 #ifdef __rtems__
484 void __wrap_rtems_task_variable_add(void **);
485 #endif
488 * runtime go-called
490 void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool,
491 void **, void **)
492 __asm__ (GOSYM_PREFIX "reflect.call");
493 void runtime_panic(Eface)
494 __asm__ (GOSYM_PREFIX "runtime.gopanic");
495 void runtime_panic(Eface)
496 __attribute__ ((noreturn));
499 * runtime c-called (but written in Go)
501 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
502 __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
503 void runtime_newErrorCString(const char*, Eface*)
504 __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
507 * wrapped for go users
509 void runtime_procyield(uint32)
510 __asm__(GOSYM_PREFIX "runtime.procyield");
511 void runtime_osyield(void)
512 __asm__(GOSYM_PREFIX "runtime.osyield");
513 void runtime_lockOSThread(void)
514 __asm__(GOSYM_PREFIX "runtime.lockOSThread");
515 void runtime_unlockOSThread(void)
516 __asm__(GOSYM_PREFIX "runtime.unlockOSThread");
518 void runtime_printcreatedby(G*)
519 __asm__(GOSYM_PREFIX "runtime.printcreatedby");
521 uintptr runtime_memlimit(void);
523 #define ISNAN(f) __builtin_isnan(f)
525 enum
527 UseSpanType = 1,
530 #define runtime_setitimer setitimer
532 void runtime_check(void)
533 __asm__ (GOSYM_PREFIX "runtime.check");
535 // A list of global variables that the garbage collector must scan.
536 struct root_list {
537 struct root_list *next;
538 struct root {
539 void *decl;
540 size_t size;
541 } roots[];
544 void __go_register_gc_roots(struct root_list*);
546 // Size of stack space allocated using Go's allocator.
547 // This will be 0 when using split stacks, as in that case
548 // the stacks are allocated by the splitstack library.
549 extern uintptr runtime_stacks_sys;
551 struct backtrace_state;
552 extern struct backtrace_state *__go_get_backtrace_state(void);
553 extern _Bool __go_file_line(uintptr, int, String*, String*, intgo *);
554 extern void runtime_main(void*);
555 extern uint32 runtime_in_callers;
557 int32 getproccount(void);
559 #define PREFETCH(p) __builtin_prefetch(p)
561 bool runtime_gcwaiting(void);
562 void runtime_badsignal(int);
563 Defer* runtime_newdefer(void);
564 void runtime_freedefer(Defer*);
566 struct time_now_ret
568 int64_t sec;
569 int32_t nsec;
572 struct time_now_ret now() __asm__ (GOSYM_PREFIX "time.now")
573 __attribute__ ((no_split_stack));
575 extern void _cgo_wait_runtime_init_done (void);
576 extern void _cgo_notify_runtime_init_done (void);
577 extern _Bool runtime_iscgo;
578 extern uintptr __go_end __attribute__ ((weak));
579 extern void *getitab(const struct __go_type_descriptor *,
580 const struct __go_type_descriptor *,
581 _Bool)
582 __asm__ (GOSYM_PREFIX "runtime.getitab");
584 extern void runtime_cpuinit(void);
585 extern void setIsCgo(void)
586 __asm__ (GOSYM_PREFIX "runtime.setIsCgo");
587 extern void setCpuidECX(uint32)
588 __asm__ (GOSYM_PREFIX "runtime.setCpuidECX");
589 extern void setSupportAES(bool)
590 __asm__ (GOSYM_PREFIX "runtime.setSupportAES");
591 extern void makeMainInitDone(void)
592 __asm__ (GOSYM_PREFIX "runtime.makeMainInitDone");
593 extern void closeMainInitDone(void)
594 __asm__ (GOSYM_PREFIX "runtime.closeMainInitDone");
595 extern void typedmemmove(const Type *, void *, const void *)
596 __asm__ (GOSYM_PREFIX "runtime.typedmemmove");