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
)));
52 typedef union Lock Lock
;
54 typedef union Note Note
;
55 typedef struct SigTab SigTab
;
56 typedef struct MCache MCache
;
57 typedef struct FixAlloc FixAlloc
;
58 typedef struct Hchan Hchan
;
59 typedef struct Timers Timers
;
60 typedef struct Timer Timer
;
62 typedef struct __go_open_array Slice
;
63 typedef struct __go_string String
;
64 typedef struct __go_interface Iface
;
65 typedef struct __go_empty_interface Eface
;
66 typedef struct __go_type_descriptor Type
;
67 typedef struct __go_defer_stack Defer
;
68 typedef struct __go_panic_stack Panic
;
70 typedef struct __go_func_type FuncType
;
71 typedef struct __go_map_type MapType
;
74 * per-cpu declaration.
76 extern M
* runtime_m(void);
77 extern G
* runtime_g(void);
89 // If you add to this list, add to the list
90 // of "okay during garbage collection" status
111 uint32 key
; // futex-based impl
112 M
* waitm
; // linked list of waiting M's (sema-based impl)
116 uint32 key
; // futex-based impl
117 M
* waitm
; // waiting M (sema-based impl)
123 void* exception
; // current exception being thrown
124 bool is_foreign
; // whether current exception from other language
125 void *gcstack
; // if status==Gsyscall, gcstack = stackbase to use during gc
126 uintptr gcstack_size
;
127 void* gcnext_segment
;
131 byte
* entry
; // initial function
132 G
* alllink
; // on allg
133 void* param
; // passed parameter on wakeup
134 bool fromgogo
; // reached from gogo
137 uint32 selgen
; // valid sudog pointer
138 const char* waitreason
; // if status==Gwaiting
142 M
* m
; // for debuggers, but offset not hard-coded
149 uintptr gopc
; // pc of go statement that created this goroutine
152 void* stack_context
[10];
157 G
* g0
; // goroutine with scheduling stack
158 G
* gsignal
; // signal-handling G
159 G
* curg
; // current running goroutine
172 M
* alllink
; // on allm
177 M
* nextwaitm
; // next M waiting for lock
178 uintptr waitsema
; // semaphore for parking on locks
179 uint32 waitsemacount
;
221 // Package time knows the layout of this structure.
222 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
225 int32 i
; // heap index
227 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
228 // each time calling f(now, arg) in the timer goroutine, so f must be
229 // a well-behaved function and not block.
232 void (*f
)(int64
, Eface
);
238 * you need super-gopher-guru privilege
241 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
242 #define nil ((void*)0)
243 #define USED(v) ((void) v)
251 extern int32 runtime_gomaxprocs
;
252 extern bool runtime_singleproc
;
253 extern uint32 runtime_panicking
;
254 extern int32 runtime_gcwaiting
; // gc is waiting to run
258 * common functions and data
260 int32
runtime_findnull(const byte
*);
263 * very low level c-called
265 void runtime_args(int32
, byte
**);
266 void runtime_osinit();
267 void runtime_goargs(void);
268 void runtime_goenvs(void);
269 void runtime_goenvs_unix(void);
270 void runtime_throw(const char*) __attribute__ ((noreturn
));
271 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
272 void* runtime_mal(uintptr
);
273 void runtime_schedinit(void);
274 void runtime_initsig(int32
);
275 String
runtime_gostringnocopy(const byte
*);
276 void* runtime_mstart(void*);
277 G
* runtime_malg(int32
, byte
**, size_t*);
278 void runtime_minit(void);
279 void runtime_mallocinit(void);
280 void runtime_gosched(void);
281 void runtime_tsleep(int64
);
282 M
* runtime_newm(void);
283 void runtime_goexit(void);
284 void runtime_entersyscall(void) __asm__("libgo_syscall.syscall.entersyscall");
285 void runtime_exitsyscall(void) __asm__("libgo_syscall.syscall.exitsyscall");
287 bool __go_sigsend(int32 sig
);
288 int64
runtime_nanotime(void);
289 int64
runtime_cputicks(void);
291 void runtime_stoptheworld(void);
292 void runtime_starttheworld(bool);
293 G
* __go_go(void (*pfn
)(void*), void*);
296 * mutual exclusion locks. in the uncontended case,
297 * as fast as spin locks (just a few user-level instructions),
298 * but on the contention path they sleep in the kernel.
299 * a zeroed Lock is unlocked (no need to initialize each lock).
301 void runtime_lock(Lock
*);
302 void runtime_unlock(Lock
*);
305 * sleep and wakeup on one-time events.
306 * before any calls to notesleep or notewakeup,
307 * must call noteclear to initialize the Note.
308 * then, exactly one thread can call notesleep
309 * and exactly one thread can call notewakeup (once).
310 * once notewakeup has been called, the notesleep
311 * will return. future notesleep will return immediately.
312 * subsequent noteclear must be called only after
313 * previous notesleep has returned, e.g. it's disallowed
314 * to call noteclear straight after notewakeup.
316 * notetsleep is like notesleep but wakes up after
317 * a given number of nanoseconds even if the event
318 * has not yet happened. if a goroutine uses notetsleep to
319 * wake up early, it must wait to call noteclear until it
320 * can be sure that no other goroutine is calling
323 void runtime_noteclear(Note
*);
324 void runtime_notesleep(Note
*);
325 void runtime_notewakeup(Note
*);
326 void runtime_notetsleep(Note
*, int64
);
329 * low-level synchronization for implementing the above
331 uintptr
runtime_semacreate(void);
332 int32
runtime_semasleep(int64
);
333 void runtime_semawakeup(M
*);
335 void runtime_futexsleep(uint32
*, uint32
, int64
);
336 void runtime_futexwakeup(uint32
*, uint32
);
341 void runtime_panic(Eface
);
344 #define runtime_panic __go_panic
345 #define runtime_printf printf
346 #define runtime_malloc(s) __go_alloc(s)
347 #define runtime_free(p) __go_free(p)
348 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
349 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
350 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
351 #define runtime_exit(s) exit(s)
352 MCache
* runtime_allocmcache(void);
354 struct __go_func_type
;
355 bool runtime_addfinalizer(void*, void(*fn
)(void*), const struct __go_func_type
*);
356 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
357 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
358 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
359 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
360 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
361 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
362 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
363 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
365 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
366 void runtime_startpanic(void);
367 void runtime_ready(G
*);
368 const byte
* runtime_getenv(const char*);
369 int32
runtime_atoi(const byte
*);
370 uint32
runtime_fastrand1(void);
372 void runtime_sigprof(uint8
*pc
, uint8
*sp
, uint8
*lr
, G
*gp
);
373 void runtime_resetcpuprofiler(int32
);
374 void runtime_setcpuprofilerate(void(*)(uintptr
*, int32
), int32
);
375 void runtime_usleep(uint32
);
377 void runtime_semacquire(uint32
volatile *);
378 void runtime_semrelease(uint32
volatile *);
379 int32
runtime_gomaxprocsfunc(int32 n
);
380 void runtime_procyield(uint32
);
381 void runtime_osyield(void);
382 void runtime_LockOSThread(void) __asm__("libgo_runtime.runtime.LockOSThread");
383 void runtime_UnlockOSThread(void) __asm__("libgo_runtime.runtime.UnlockOSThread");
388 #define runtime_mmap mmap
389 #define runtime_munmap munmap
390 #define runtime_madvise madvise
391 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
393 struct __go_func_type
;
394 void reflect_call(const struct __go_func_type
*, const void *, _Bool
, _Bool
,
396 asm ("libgo_reflect.reflect.call");
399 void __wrap_rtems_task_variable_add(void **);
402 void runtime_time_scan(void (*)(byte
*, int64
));