2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / runtime.h
blob94113b8db8397fd497e8dcecf94748d26f2f4086
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. */
7 #include "config.h"
9 #include "go-assert.h"
10 #include <setjmp.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <pthread.h>
19 #include <semaphore.h>
20 #include <ucontext.h>
22 #ifdef HAVE_SYS_MMAN_H
23 #include <sys/mman.h>
24 #endif
26 #include "array.h"
27 #include "go-alloc.h"
28 #include "go-panic.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
33 of the code. */
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)));
47 /* Defined types. */
49 typedef uint8 bool;
50 typedef uint8 byte;
51 typedef struct G G;
52 typedef union Lock Lock;
53 typedef struct M M;
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);
79 extern M runtime_m0;
80 extern G runtime_g0;
83 * defined constants
85 enum
87 // G status
89 // If you add to this list, add to the list
90 // of "okay during garbage collection" status
91 // in mgc0.c too.
92 Gidle,
93 Grunnable,
94 Grunning,
95 Gsyscall,
96 Gwaiting,
97 Gmoribund,
98 Gdead,
100 enum
102 true = 1,
103 false = 0,
107 * structures
109 union Lock
111 uint32 key; // futex-based impl
112 M* waitm; // linked list of waiting M's (sema-based impl)
114 union Note
116 uint32 key; // futex-based impl
117 M* waitm; // waiting M (sema-based impl)
119 struct G
121 Defer* defer;
122 Panic* panic;
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;
128 void* gcnext_sp;
129 void* gcinitial_sp;
130 jmp_buf gcregs;
131 byte* entry; // initial function
132 G* alllink; // on allg
133 void* param; // passed parameter on wakeup
134 bool fromgogo; // reached from gogo
135 int16 status;
136 int32 goid;
137 uint32 selgen; // valid sudog pointer
138 const char* waitreason; // if status==Gwaiting
139 G* schedlink;
140 bool readyonstop;
141 bool ispanic;
142 M* m; // for debuggers, but offset not hard-coded
143 M* lockedm;
144 M* idlem;
145 // int32 sig;
146 // uintptr sigcode0;
147 // uintptr sigcode1;
148 // uintptr sigpc;
149 uintptr gopc; // pc of go statement that created this goroutine
151 ucontext_t context;
152 void* stack_context[10];
155 struct M
157 G* g0; // goroutine with scheduling stack
158 G* gsignal; // signal-handling G
159 G* curg; // current running goroutine
160 int32 id;
161 int32 mallocing;
162 int32 gcing;
163 int32 locks;
164 int32 nomemprof;
165 int32 waitnextg;
166 int32 dying;
167 int32 profilehz;
168 int32 helpgc;
169 uint32 fastrand;
170 Note havenextg;
171 G* nextg;
172 M* alllink; // on allm
173 M* schedlink;
174 MCache *mcache;
175 G* lockedg;
176 G* idleg;
177 M* nextwaitm; // next M waiting for lock
178 uintptr waitsema; // semaphore for parking on locks
179 uint32 waitsemacount;
180 uint32 waitsemalock;
183 struct SigTab
185 int32 sig;
186 int32 flags;
188 enum
190 SigCatch = 1<<0,
191 SigIgnore = 1<<1,
192 SigRestart = 1<<2,
193 SigQueue = 1<<3,
194 SigPanic = 1<<4,
197 /* Macros. */
199 #ifdef GOOS_windows
200 enum {
201 Windows = 1
203 #else
204 enum {
205 Windows = 0
207 #endif
209 struct Timers
211 Lock;
212 G *timerproc;
213 bool sleeping;
214 bool rescheduling;
215 Note waitnote;
216 Timer **t;
217 int32 len;
218 int32 cap;
221 // Package time knows the layout of this structure.
222 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
223 struct Timer
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.
230 int64 when;
231 int64 period;
232 void (*f)(int64, Eface);
233 Eface arg;
237 * defined macros
238 * you need super-gopher-guru privilege
239 * to add this list.
241 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
242 #define nil ((void*)0)
243 #define USED(v) ((void) v)
246 * external data
248 G* runtime_allg;
249 G* runtime_lastg;
250 M* runtime_allm;
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
255 int32 runtime_ncpu;
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");
285 void siginit(void);
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
319 * notewakeup.
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*);
332 // or
333 void runtime_futexsleep(uint32*, uint32, int64);
334 void runtime_futexwakeup(uint32*, uint32);
337 * runtime go-called
339 void runtime_panic(Eface);
341 /* Functions. */
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);
351 void free(void *v);
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");
384 * low level C-called
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,
393 void **, void **)
394 asm ("libgo_reflect.reflect.call");
396 #ifdef __rtems__
397 void __wrap_rtems_task_variable_add(void **);
398 #endif
400 void runtime_time_scan(void (*)(byte*, int64));