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. */
15 #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
;
75 * per-cpu declaration.
77 extern M
* runtime_m(void);
78 extern G
* runtime_g(void);
90 // If you add to this list, add to the list
91 // of "okay during garbage collection" status
112 uint32 key
; // futex-based impl
113 M
* waitm
; // linked list of waiting M's (sema-based impl)
117 uint32 key
; // futex-based impl
118 M
* waitm
; // waiting M (sema-based impl)
124 void* exception
; // current exception being thrown
125 bool is_foreign
; // whether current exception from other language
126 void *gcstack
; // if status==Gsyscall, gcstack = stackbase to use during gc
127 uintptr gcstack_size
;
128 void* gcnext_segment
;
132 byte
* entry
; // initial function
133 G
* alllink
; // on allg
134 void* param
; // passed parameter on wakeup
135 bool fromgogo
; // reached from gogo
138 uint32 selgen
; // valid sudog pointer
139 const char* waitreason
; // if status==Gwaiting
143 M
* m
; // for debuggers, but offset not hard-coded
152 uintptr gopc
; // pc of go statement that created this goroutine
155 void* stack_context
[10];
160 G
* g0
; // goroutine with scheduling stack
161 G
* gsignal
; // signal-handling G
162 G
* curg
; // current running goroutine
175 M
* alllink
; // on allm
180 uintptr createstack
[32]; // Stack that created this thread.
181 M
* nextwaitm
; // next M waiting for lock
182 uintptr waitsema
; // semaphore for parking on locks
183 uint32 waitsemacount
;
194 SigNotify
= 1<<0, // let signal.Notify have signal, even if from kernel
195 SigKill
= 1<<1, // if signal.Notify doesn't take it, exit quietly
196 SigThrow
= 1<<2, // if signal.Notify doesn't take it, exit loudly
197 SigPanic
= 1<<3, // if the signal is from the kernel, panic
198 SigDefault
= 1<<4, // if the signal isn't explicitly requested, don't monitor it
205 // NOTE(rsc): keep in sync with extern.go:/type.Func.
206 // Eventually, the loaded symbol table should be closer to this form.
210 uintptr entry
; // entry pc
237 // Package time knows the layout of this structure.
238 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
241 int32 i
; // heap index
243 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
244 // each time calling f(now, arg) in the timer goroutine, so f must be
245 // a well-behaved function and not block.
248 void (*f
)(int64
, Eface
);
254 * you need super-gopher-guru privilege
257 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
258 #define nil ((void*)0)
259 #define USED(v) ((void) v)
267 extern int32 runtime_gomaxprocs
;
268 extern bool runtime_singleproc
;
269 extern uint32 runtime_panicking
;
270 extern int32 runtime_gcwaiting
; // gc is waiting to run
274 * common functions and data
276 int32
runtime_findnull(const byte
*);
279 * very low level c-called
281 void runtime_args(int32
, byte
**);
282 void runtime_osinit();
283 void runtime_goargs(void);
284 void runtime_goenvs(void);
285 void runtime_goenvs_unix(void);
286 void runtime_throw(const char*) __attribute__ ((noreturn
));
287 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
288 void* runtime_mal(uintptr
);
289 void runtime_schedinit(void);
290 void runtime_initsig(void);
291 void runtime_sigenable(uint32 sig
);
292 String
runtime_gostringnocopy(const byte
*);
293 void* runtime_mstart(void*);
294 G
* runtime_malg(int32
, byte
**, size_t*);
295 void runtime_minit(void);
296 void runtime_mallocinit(void);
297 void runtime_gosched(void);
298 void runtime_tsleep(int64
);
299 M
* runtime_newm(void);
300 void runtime_goexit(void);
301 void runtime_entersyscall(void) __asm__("libgo_syscall.syscall.Entersyscall");
302 void runtime_exitsyscall(void) __asm__("libgo_syscall.syscall.Exitsyscall");
304 bool __go_sigsend(int32 sig
);
305 int32
runtime_callers(int32
, uintptr
*, int32
);
306 int64
runtime_nanotime(void);
307 int64
runtime_cputicks(void);
309 void runtime_stoptheworld(void);
310 void runtime_starttheworld(bool);
311 extern uint32 runtime_worldsema
;
312 G
* __go_go(void (*pfn
)(void*), void*);
315 * mutual exclusion locks. in the uncontended case,
316 * as fast as spin locks (just a few user-level instructions),
317 * but on the contention path they sleep in the kernel.
318 * a zeroed Lock is unlocked (no need to initialize each lock).
320 void runtime_lock(Lock
*);
321 void runtime_unlock(Lock
*);
324 * sleep and wakeup on one-time events.
325 * before any calls to notesleep or notewakeup,
326 * must call noteclear to initialize the Note.
327 * then, exactly one thread can call notesleep
328 * and exactly one thread can call notewakeup (once).
329 * once notewakeup has been called, the notesleep
330 * will return. future notesleep will return immediately.
331 * subsequent noteclear must be called only after
332 * previous notesleep has returned, e.g. it's disallowed
333 * to call noteclear straight after notewakeup.
335 * notetsleep is like notesleep but wakes up after
336 * a given number of nanoseconds even if the event
337 * has not yet happened. if a goroutine uses notetsleep to
338 * wake up early, it must wait to call noteclear until it
339 * can be sure that no other goroutine is calling
342 void runtime_noteclear(Note
*);
343 void runtime_notesleep(Note
*);
344 void runtime_notewakeup(Note
*);
345 void runtime_notetsleep(Note
*, int64
);
348 * low-level synchronization for implementing the above
350 uintptr
runtime_semacreate(void);
351 int32
runtime_semasleep(int64
);
352 void runtime_semawakeup(M
*);
354 void runtime_futexsleep(uint32
*, uint32
, int64
);
355 void runtime_futexwakeup(uint32
*, uint32
);
360 #define runtime_mmap mmap
361 #define runtime_munmap munmap
362 #define runtime_madvise madvise
363 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
364 #define runtime_getcallerpc(p) __builtin_return_address(0)
367 void __wrap_rtems_task_variable_add(void **);
373 void runtime_panic(Eface
);
374 struct __go_func_type
;
375 void reflect_call(const struct __go_func_type
*, const void *, _Bool
, _Bool
,
377 asm ("libgo_reflect.reflect.call");
380 #define runtime_panic __go_panic
381 #define runtime_printf printf
382 #define runtime_malloc(s) __go_alloc(s)
383 #define runtime_free(p) __go_free(p)
384 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
385 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
386 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
387 #define runtime_exit(s) exit(s)
388 MCache
* runtime_allocmcache(void);
390 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
391 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
392 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
393 #define runtime_xchg(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_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
397 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
399 struct __go_func_type
;
400 bool runtime_addfinalizer(void*, void(*fn
)(void*), const struct __go_func_type
*);
401 #define runtime_getcallersp(p) __builtin_frame_address(1)
402 int32
runtime_mcount(void);
403 int32
runtime_gcount(void);
404 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
405 void runtime_startpanic(void);
406 void runtime_ready(G
*);
407 const byte
* runtime_getenv(const char*);
408 int32
runtime_atoi(const byte
*);
409 uint32
runtime_fastrand1(void);
411 void runtime_sigprof(uint8
*pc
, uint8
*sp
, uint8
*lr
, G
*gp
);
412 void runtime_resetcpuprofiler(int32
);
413 void runtime_setcpuprofilerate(void(*)(uintptr
*, int32
), int32
);
414 void runtime_usleep(uint32
);
417 * runtime c-called (but written in Go)
419 void runtime_printany(Eface
)
420 __asm__("libgo_runtime.runtime.Printany");
421 void runtime_newTypeAssertionError(const String
*, const String
*, const String
*, const String
*, Eface
*)
422 __asm__("libgo_runtime.runtime.NewTypeAssertionError");
423 void runtime_newErrorString(String
, Eface
*)
424 __asm__("libgo_runtime.runtime.NewErrorString");
427 * wrapped for go users
429 void runtime_semacquire(uint32
volatile *);
430 void runtime_semrelease(uint32
volatile *);
431 int32
runtime_gomaxprocsfunc(int32 n
);
432 void runtime_procyield(uint32
);
433 void runtime_osyield(void);
434 void runtime_LockOSThread(void) __asm__("libgo_runtime.runtime.LockOSThread");
435 void runtime_UnlockOSThread(void) __asm__("libgo_runtime.runtime.UnlockOSThread");
437 uintptr
runtime_memlimit(void);
439 // If appropriate, ask the operating system to control whether this
440 // thread should receive profiling signals. This is only necessary on OS X.
441 // An operating system should not deliver a profiling signal to a
442 // thread that is not actually executing (what good is that?), but that's
443 // what OS X prefers to do. When profiling is turned on, we mask
444 // away the profiling signal when threads go to sleep, so that OS X
445 // is forced to deliver the signal to a thread that's actually running.
446 // This is a no-op on other systems.
447 void runtime_setprof(bool);
449 void runtime_time_scan(void (*)(byte
*, int64
));
451 void runtime_setsig(int32
, bool, bool);
452 #define runtime_setitimer setitimer
454 void runtime_check(void);
456 // A list of global variables that the garbage collector must scan.
458 struct root_list
*next
;
465 void __go_register_gc_roots(struct root_list
*);