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. */
10 #include "go-assert.h"
16 #include <sys/types.h>
20 #include <semaphore.h>
23 #ifdef HAVE_SYS_MMAN_H
30 #include "go-string.h"
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
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 unsigned int uintptr __attribute__ ((mode (pointer)));
53 typedef union Lock Lock;
55 typedef union Note Note;
56 typedef struct MCache MCache;
57 typedef struct FixAlloc FixAlloc;
58 typedef struct Hchan Hchan;
60 typedef struct __go_open_array Slice;
61 typedef struct __go_string String;
62 typedef struct __go_interface Iface;
63 typedef struct __go_empty_interface Eface;
64 typedef struct __go_type_descriptor Type;
65 typedef struct __go_defer_stack Defer;
66 typedef struct __go_panic_stack Panic;
68 typedef struct __go_func_type FuncType;
69 typedef struct __go_map_type MapType;
72 * per-cpu declaration.
74 extern M* runtime_m(void);
75 extern G* runtime_g(void);
87 // If you add to this list, add to the list
88 // of "okay during garbage collection" status
109 uint32 key; // futex-based impl
110 M* waitm; // linked list of waiting M's (sema-based impl)
114 uint32 key; // futex-based impl
115 M* waitm; // waiting M (sema-based impl)
121 void* exception; // current exception being thrown
122 bool is_foreign; // whether current exception from other language
123 void *gcstack; // if status==Gsyscall, gcstack = stackbase to use during gc
124 uintptr gcstack_size;
125 void* gcnext_segment;
129 byte* entry; // initial function
130 G* alllink; // on allg
131 void* param; // passed parameter on wakeup
132 bool fromgogo; // reached from gogo
135 uint32 selgen; // valid sudog pointer
136 const char* waitreason; // if status==Gwaiting
140 M* m; // for debuggers, but offset not hard-coded
147 uintptr gopc; // pc of go statement that created this goroutine
150 void* stack_context[10];
155 G* g0; // goroutine with scheduling stack
156 G* gsignal; // signal-handling G
157 G* curg; // current running goroutine
170 M* alllink; // on allm
175 M* nextwaitm; // next M waiting for lock
176 uintptr waitsema; // semaphore for parking on locks
177 uint32 waitsemacount;
193 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
194 #define nil ((void*)0)
195 #define USED(v) ((void) v)
203 extern int32 runtime_gomaxprocs;
204 extern bool runtime_singleproc;
205 extern uint32 runtime_panicking;
206 extern int32 runtime_gcwaiting; // gc is waiting to run
210 * common functions and data
212 int32 runtime_findnull(const byte*);
215 * very low level c-called
217 void runtime_args(int32, byte**);
218 void runtime_osinit();
219 void runtime_goargs(void);
220 void runtime_goenvs(void);
221 void runtime_throw(const char*);
222 void runtime_panicstring(const char*) __attribute__ ((noreturn));
223 void* runtime_mal(uintptr);
224 void runtime_schedinit(void);
225 void runtime_initsig(int32);
226 String runtime_gostringnocopy(const byte*);
227 void* runtime_mstart(void*);
228 G* runtime_malg(int32, byte**, size_t*);
229 void runtime_minit(void);
230 void runtime_mallocinit(void);
231 void runtime_gosched(void);
232 void runtime_goexit(void);
233 void runtime_entersyscall(void) __asm__("libgo_syscall.syscall.entersyscall");
234 void runtime_exitsyscall(void) __asm__("libgo_syscall.syscall.exitsyscall");
236 bool __go_sigsend(int32 sig);
237 int64 runtime_nanotime(void);
239 void runtime_stoptheworld(void);
240 void runtime_starttheworld(bool);
241 G* __go_go(void (*pfn)(void*), void*);
244 * mutual exclusion locks. in the uncontended case,
245 * as fast as spin locks (just a few user-level instructions),
246 * but on the contention path they sleep in the kernel.
247 * a zeroed Lock is unlocked (no need to initialize each lock).
249 void runtime_lock(Lock*);
250 void runtime_unlock(Lock*);
253 * sleep and wakeup on one-time events.
254 * before any calls to notesleep or notewakeup,
255 * must call noteclear to initialize the Note.
256 * then, exactly one thread can call notesleep
257 * and exactly one thread can call notewakeup (once).
258 * once notewakeup has been called, the notesleep
259 * will return. future notesleep will return immediately.
260 * subsequent noteclear must be called only after
261 * previous notesleep has returned, e.g. it's disallowed
262 * to call noteclear straight after notewakeup.
264 * notetsleep is like notesleep but wakes up after
265 * a given number of nanoseconds even if the event
266 * has not yet happened. if a goroutine uses notetsleep to
267 * wake up early, it must wait to call noteclear until it
268 * can be sure that no other goroutine is calling
271 void runtime_noteclear(Note*);
272 void runtime_notesleep(Note*);
273 void runtime_notewakeup(Note*);
274 void runtime_notetsleep(Note*, int64);
277 * low-level synchronization for implementing the above
279 uintptr runtime_semacreate(void);
280 int32 runtime_semasleep(int64);
281 void runtime_semawakeup(M*);
283 void runtime_futexsleep(uint32*, uint32, int64);
284 void runtime_futexwakeup(uint32*, uint32);
289 void runtime_panic(Eface);
292 #define runtime_panic __go_panic
293 #define runtime_printf printf
294 #define runtime_malloc(s) __go_alloc(s)
295 #define runtime_free(p) __go_free(p)
296 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
297 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
298 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
299 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
300 #define runtime_exit(s) _exit(s)
301 MCache* runtime_allocmcache(void);
303 struct __go_func_type;
304 bool runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
305 #define runtime_mmap mmap
306 #define runtime_munmap(p, s) munmap((p), (s))
307 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
308 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
309 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
310 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
311 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
312 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
313 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
314 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
316 void runtime_dopanic(int32) __attribute__ ((noreturn));
317 void runtime_startpanic(void);
318 void runtime_ready(G*);
319 const byte* runtime_getenv(const char*);
320 int32 runtime_atoi(const byte*);
321 void runtime_sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
322 void runtime_resetcpuprofiler(int32);
323 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
324 uint32 runtime_fastrand1(void);
325 void runtime_semacquire(uint32 volatile *);
326 void runtime_semrelease(uint32 volatile *);
327 int32 runtime_gomaxprocsfunc(int32 n);
328 void runtime_procyield(uint32);
329 void runtime_osyield(void);
330 void runtime_usleep(uint32);
332 struct __go_func_type;
333 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
335 asm ("libgo_reflect.reflect.call");
338 void __wrap_rtems_task_variable_add(void **);