1 /* runtime.h -- runtime support for Go.
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. */
14 #include <sys/types.h>
19 #include <semaphore.h>
22 #ifdef HAVE_SYS_MMAN_H
29 #include "go-string.h"
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
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)));
51 typedef struct Func Func;
53 typedef union Lock Lock;
55 typedef union Note Note;
56 typedef struct SigTab SigTab;
57 typedef struct MCache MCache;
58 typedef struct FixAlloc FixAlloc;
59 typedef struct Hchan Hchan;
60 typedef struct Timers Timers;
61 typedef struct Timer Timer;
63 typedef struct __go_open_array Slice;
64 typedef struct __go_string String;
65 typedef struct __go_interface Iface;
66 typedef struct __go_empty_interface Eface;
67 typedef struct __go_type_descriptor Type;
68 typedef struct __go_defer_stack Defer;
69 typedef struct __go_panic_stack Panic;
71 typedef struct __go_func_type FuncType;
72 typedef struct __go_map_type MapType;
74 typedef struct Traceback Traceback;
77 * per-cpu declaration.
79 extern M* runtime_m(void);
80 extern G* runtime_g(void);
92 // If you add to this list, add to the list
93 // of "okay during garbage collection" status
114 uint32 key; // futex-based impl
115 M* waitm; // linked list of waiting M's (sema-based impl)
119 uint32 key; // futex-based impl
120 M* waitm; // waiting M (sema-based impl)
126 void* exception; // current exception being thrown
127 bool is_foreign; // whether current exception from other language
128 void *gcstack; // if status==Gsyscall, gcstack = stackbase to use during gc
129 uintptr gcstack_size;
130 void* gcnext_segment;
134 byte* entry; // initial function
135 G* alllink; // on allg
136 void* param; // passed parameter on wakeup
137 bool fromgogo; // reached from gogo
140 uint32 selgen; // valid sudog pointer
141 const char* waitreason; // if status==Gwaiting
145 M* m; // for debuggers, but offset not hard-coded
154 uintptr gopc; // pc of go statement that created this goroutine
156 Traceback* traceback;
159 void* stack_context[10];
164 G* g0; // goroutine with scheduling stack
165 G* gsignal; // signal-handling G
166 G* curg; // current running goroutine
179 M* alllink; // on allm
184 uintptr createstack[32]; // Stack that created this thread.
185 M* nextwaitm; // next M waiting for lock
186 uintptr waitsema; // semaphore for parking on locks
187 uint32 waitsemacount;
198 SigNotify = 1<<0, // let signal.Notify have signal, even if from kernel
199 SigKill = 1<<1, // if signal.Notify doesn't take it, exit quietly
200 SigThrow = 1<<2, // if signal.Notify doesn't take it, exit loudly
201 SigPanic = 1<<3, // if the signal is from the kernel, panic
202 SigDefault = 1<<4, // if the signal isn't explicitly requested, don't monitor it
209 // NOTE(rsc): keep in sync with extern.go:/type.Func.
210 // Eventually, the loaded symbol table should be closer to this form.
214 uintptr entry; // entry pc
241 // Package time knows the layout of this structure.
242 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
245 int32 i; // heap index
247 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
248 // each time calling f(now, arg) in the timer goroutine, so f must be
249 // a well-behaved function and not block.
252 void (*f)(int64, Eface);
258 * you need super-gopher-guru privilege
261 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
262 #define nil ((void*)0)
263 #define USED(v) ((void) v)
271 extern int32 runtime_gomaxprocs;
272 extern bool runtime_singleproc;
273 extern uint32 runtime_panicking;
274 extern int32 runtime_gcwaiting; // gc is waiting to run
278 * common functions and data
280 int32 runtime_findnull(const byte*);
281 void runtime_dump(byte*, int32);
284 * very low level c-called
286 void runtime_args(int32, byte**);
287 void runtime_osinit();
288 void runtime_goargs(void);
289 void runtime_goenvs(void);
290 void runtime_goenvs_unix(void);
291 void runtime_throw(const char*) __attribute__ ((noreturn));
292 void runtime_panicstring(const char*) __attribute__ ((noreturn));
293 void runtime_prints(const char*);
294 void runtime_printf(const char*, ...);
295 void* runtime_mal(uintptr);
296 void runtime_schedinit(void);
297 void runtime_initsig(void);
298 void runtime_sigenable(uint32 sig);
299 int32 runtime_gotraceback(void);
300 void runtime_goroutineheader(G*);
301 void runtime_goroutinetrailer(G*);
302 void runtime_traceback();
303 void runtime_tracebackothers(G*);
304 void runtime_printtrace(uintptr*, int32);
305 String runtime_gostringnocopy(const byte*);
306 void* runtime_mstart(void*);
307 G* runtime_malg(int32, byte**, size_t*);
308 void runtime_minit(void);
309 void runtime_mallocinit(void);
310 void runtime_gosched(void);
311 void runtime_tsleep(int64);
312 M* runtime_newm(void);
313 void runtime_goexit(void);
314 void runtime_entersyscall(void) __asm__("syscall.Entersyscall");
315 void runtime_exitsyscall(void) __asm__("syscall.Exitsyscall");
317 bool __go_sigsend(int32 sig);
318 int32 runtime_callers(int32, uintptr*, int32);
319 int64 runtime_nanotime(void);
320 int64 runtime_cputicks(void);
322 void runtime_stoptheworld(void);
323 void runtime_starttheworld(bool);
324 extern uint32 runtime_worldsema;
325 G* __go_go(void (*pfn)(void*), void*);
328 * mutual exclusion locks. in the uncontended case,
329 * as fast as spin locks (just a few user-level instructions),
330 * but on the contention path they sleep in the kernel.
331 * a zeroed Lock is unlocked (no need to initialize each lock).
333 void runtime_lock(Lock*);
334 void runtime_unlock(Lock*);
337 * sleep and wakeup on one-time events.
338 * before any calls to notesleep or notewakeup,
339 * must call noteclear to initialize the Note.
340 * then, exactly one thread can call notesleep
341 * and exactly one thread can call notewakeup (once).
342 * once notewakeup has been called, the notesleep
343 * will return. future notesleep will return immediately.
344 * subsequent noteclear must be called only after
345 * previous notesleep has returned, e.g. it's disallowed
346 * to call noteclear straight after notewakeup.
348 * notetsleep is like notesleep but wakes up after
349 * a given number of nanoseconds even if the event
350 * has not yet happened. if a goroutine uses notetsleep to
351 * wake up early, it must wait to call noteclear until it
352 * can be sure that no other goroutine is calling
355 void runtime_noteclear(Note*);
356 void runtime_notesleep(Note*);
357 void runtime_notewakeup(Note*);
358 void runtime_notetsleep(Note*, int64);
361 * low-level synchronization for implementing the above
363 uintptr runtime_semacreate(void);
364 int32 runtime_semasleep(int64);
365 void runtime_semawakeup(M*);
367 void runtime_futexsleep(uint32*, uint32, int64);
368 void runtime_futexwakeup(uint32*, uint32);
373 #define runtime_mmap mmap
374 #define runtime_munmap munmap
375 #define runtime_madvise madvise
376 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
377 #define runtime_getcallerpc(p) __builtin_return_address(0)
380 void __wrap_rtems_task_variable_add(void **);
384 * Names generated by gccgo.
386 #define runtime_printbool __go_print_bool
387 #define runtime_printfloat __go_print_double
388 #define runtime_printint __go_print_int64
389 #define runtime_printiface __go_print_interface
390 #define runtime_printeface __go_print_empty_interface
391 #define runtime_printstring __go_print_string
392 #define runtime_printpointer __go_print_pointer
393 #define runtime_printuint __go_print_uint64
394 #define runtime_printslice __go_print_slice
395 #define runtime_printcomplex __go_print_complex
400 void runtime_printbool(_Bool);
401 void runtime_printfloat(double);
402 void runtime_printint(int64);
403 void runtime_printiface(Iface);
404 void runtime_printeface(Eface);
405 void runtime_printstring(String);
406 void runtime_printpc(void*);
407 void runtime_printpointer(void*);
408 void runtime_printuint(uint64);
409 void runtime_printhex(uint64);
410 void runtime_printslice(Slice);
411 void runtime_printcomplex(__complex double);
413 struct __go_func_type;
414 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
416 asm ("reflect.call");
419 #define runtime_panic __go_panic
420 #define runtime_write(d, v, n) write((d), (v), (n))
421 #define runtime_malloc(s) __go_alloc(s)
422 #define runtime_free(p) __go_free(p)
423 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
424 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
425 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
426 #define runtime_exit(s) exit(s)
427 MCache* runtime_allocmcache(void);
429 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
430 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
431 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
432 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
433 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
434 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
435 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
436 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
438 struct __go_func_type;
439 bool runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
440 #define runtime_getcallersp(p) __builtin_frame_address(1)
441 int32 runtime_mcount(void);
442 int32 runtime_gcount(void);
443 void runtime_dopanic(int32) __attribute__ ((noreturn));
444 void runtime_startpanic(void);
445 void runtime_ready(G*);
446 const byte* runtime_getenv(const char*);
447 int32 runtime_atoi(const byte*);
448 uint32 runtime_fastrand1(void);
450 void runtime_sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
451 void runtime_resetcpuprofiler(int32);
452 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
453 void runtime_usleep(uint32);
456 * runtime c-called (but written in Go)
458 void runtime_printany(Eface)
459 __asm__("runtime.Printany");
460 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
461 __asm__("runtime.NewTypeAssertionError");
462 void runtime_newErrorString(String, Eface*)
463 __asm__("runtime.NewErrorString");
466 * wrapped for go users
468 bool runtime_isInf(float64 f, int32 sign);
469 #define runtime_isNaN(f) __builtin_isnan(f)
470 void runtime_semacquire(uint32 volatile *);
471 void runtime_semrelease(uint32 volatile *);
472 int32 runtime_gomaxprocsfunc(int32 n);
473 void runtime_procyield(uint32);
474 void runtime_osyield(void);
475 void runtime_LockOSThread(void) __asm__("runtime.LockOSThread");
476 void runtime_UnlockOSThread(void) __asm__("runtime.UnlockOSThread");
478 bool runtime_showframe(const unsigned char*);
480 uintptr runtime_memlimit(void);
482 // If appropriate, ask the operating system to control whether this
483 // thread should receive profiling signals. This is only necessary on OS X.
484 // An operating system should not deliver a profiling signal to a
485 // thread that is not actually executing (what good is that?), but that's
486 // what OS X prefers to do. When profiling is turned on, we mask
487 // away the profiling signal when threads go to sleep, so that OS X
488 // is forced to deliver the signal to a thread that's actually running.
489 // This is a no-op on other systems.
490 void runtime_setprof(bool);
492 void runtime_time_scan(void (*)(byte*, int64));
493 void runtime_trampoline_scan(void (*)(byte *, int64));
495 void runtime_setsig(int32, bool, bool);
496 #define runtime_setitimer setitimer
498 void runtime_check(void);
500 // A list of global variables that the garbage collector must scan.
502 struct root_list *next;
509 void __go_register_gc_roots(struct root_list*);
511 // Size of stack space allocated using Go's allocator.
512 // This will be 0 when using split stacks, as in that case
513 // the stacks are allocated by the splitstack library.
514 extern uintptr runtime_stacks_sys;
516 extern _Bool __go_file_line (uintptr, String*, String*, int *);