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_throw(const char*) __attribute__ ((noreturn
));
270 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
271 void* runtime_mal(uintptr
);
272 void runtime_schedinit(void);
273 void runtime_initsig(int32
);
274 String
runtime_gostringnocopy(const byte
*);
275 void* runtime_mstart(void*);
276 G
* runtime_malg(int32
, byte
**, size_t*);
277 void runtime_minit(void);
278 void runtime_mallocinit(void);
279 void runtime_gosched(void);
280 void runtime_tsleep(int64
);
281 M
* runtime_newm(void);
282 void runtime_goexit(void);
283 void runtime_entersyscall(void) __asm__("libgo_syscall.syscall.entersyscall");
284 void runtime_exitsyscall(void) __asm__("libgo_syscall.syscall.exitsyscall");
286 bool __go_sigsend(int32 sig
);
287 int64
runtime_nanotime(void);
289 void runtime_stoptheworld(void);
290 void runtime_starttheworld(bool);
291 G
* __go_go(void (*pfn
)(void*), void*);
294 * mutual exclusion locks. in the uncontended case,
295 * as fast as spin locks (just a few user-level instructions),
296 * but on the contention path they sleep in the kernel.
297 * a zeroed Lock is unlocked (no need to initialize each lock).
299 void runtime_lock(Lock
*);
300 void runtime_unlock(Lock
*);
303 * sleep and wakeup on one-time events.
304 * before any calls to notesleep or notewakeup,
305 * must call noteclear to initialize the Note.
306 * then, exactly one thread can call notesleep
307 * and exactly one thread can call notewakeup (once).
308 * once notewakeup has been called, the notesleep
309 * will return. future notesleep will return immediately.
310 * subsequent noteclear must be called only after
311 * previous notesleep has returned, e.g. it's disallowed
312 * to call noteclear straight after notewakeup.
314 * notetsleep is like notesleep but wakes up after
315 * a given number of nanoseconds even if the event
316 * has not yet happened. if a goroutine uses notetsleep to
317 * wake up early, it must wait to call noteclear until it
318 * can be sure that no other goroutine is calling
321 void runtime_noteclear(Note
*);
322 void runtime_notesleep(Note
*);
323 void runtime_notewakeup(Note
*);
324 void runtime_notetsleep(Note
*, int64
);
327 * low-level synchronization for implementing the above
329 uintptr
runtime_semacreate(void);
330 int32
runtime_semasleep(int64
);
331 void runtime_semawakeup(M
*);
333 void runtime_futexsleep(uint32
*, uint32
, int64
);
334 void runtime_futexwakeup(uint32
*, uint32
);
339 void runtime_panic(Eface
);
342 #define runtime_panic __go_panic
343 #define runtime_printf printf
344 #define runtime_malloc(s) __go_alloc(s)
345 #define runtime_free(p) __go_free(p)
346 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
347 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
348 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
349 #define runtime_exit(s) _exit(s)
350 MCache
* runtime_allocmcache(void);
352 struct __go_func_type
;
353 bool runtime_addfinalizer(void*, void(*fn
)(void*), const struct __go_func_type
*);
354 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
355 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
356 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
357 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
358 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
359 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
360 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
361 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
363 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
364 void runtime_startpanic(void);
365 void runtime_ready(G
*);
366 const byte
* runtime_getenv(const char*);
367 int32
runtime_atoi(const byte
*);
368 uint32
runtime_fastrand1(void);
370 void runtime_sigprof(uint8
*pc
, uint8
*sp
, uint8
*lr
, G
*gp
);
371 void runtime_resetcpuprofiler(int32
);
372 void runtime_setcpuprofilerate(void(*)(uintptr
*, int32
), int32
);
373 void runtime_usleep(uint32
);
375 void runtime_semacquire(uint32
volatile *);
376 void runtime_semrelease(uint32
volatile *);
377 int32
runtime_gomaxprocsfunc(int32 n
);
378 void runtime_procyield(uint32
);
379 void runtime_osyield(void);
380 void runtime_LockOSThread(void) __asm__("libgo_runtime.runtime.LockOSThread");
381 void runtime_UnlockOSThread(void) __asm__("libgo_runtime.runtime.UnlockOSThread");
386 #define runtime_mmap mmap
387 #define runtime_munmap munmap
388 #define runtime_madvise madvise
389 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
391 struct __go_func_type
;
392 void reflect_call(const struct __go_func_type
*, const void *, _Bool
, _Bool
,
394 asm ("libgo_reflect.reflect.call");
397 void __wrap_rtems_task_variable_add(void **);
400 void runtime_time_scan(void (*)(byte
*, int64
));