OSDN Git Service

91044184b1fb3cc81c7ffd62ec69defcca5084f7
[pf3gnuchains/gcc-fork.git] / libgo / runtime / runtime.h
1 /* runtime.h -- runtime support for Go.
2
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.  */
6
7 #include "config.h"
8
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>
21
22 #ifdef HAVE_SYS_MMAN_H
23 #include <sys/mman.h>
24 #endif
25
26 #include "array.h"
27 #include "go-alloc.h"
28 #include "go-panic.h"
29 #include "go-string.h"
30
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.  */
34
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)));
46
47 /* Defined types.  */
48
49 typedef uint8                   bool;
50 typedef uint8                   byte;
51 typedef struct  Func            Func;
52 typedef struct  G               G;
53 typedef union   Lock            Lock;
54 typedef struct  M               M;
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;
62
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;
70
71 typedef struct  __go_func_type          FuncType;
72 typedef struct  __go_map_type           MapType;
73
74 /*
75  * per-cpu declaration.
76  */
77 extern M*       runtime_m(void);
78 extern G*       runtime_g(void);
79
80 extern M        runtime_m0;
81 extern G        runtime_g0;
82
83 /*
84  * defined constants
85  */
86 enum
87 {
88         // G status
89         //
90         // If you add to this list, add to the list
91         // of "okay during garbage collection" status
92         // in mgc0.c too.
93         Gidle,
94         Grunnable,
95         Grunning,
96         Gsyscall,
97         Gwaiting,
98         Gmoribund,
99         Gdead,
100 };
101 enum
102 {
103         true    = 1,
104         false   = 0,
105 };
106
107 /*
108  * structures
109  */
110 union   Lock
111 {
112         uint32  key;    // futex-based impl
113         M*      waitm;  // linked list of waiting M's (sema-based impl)
114 };
115 union   Note
116 {
117         uint32  key;    // futex-based impl
118         M*      waitm;  // waiting M (sema-based impl)
119 };
120 struct  G
121 {
122         Defer*  defer;
123         Panic*  panic;
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;
129         void*   gcnext_sp;
130         void*   gcinitial_sp;
131         jmp_buf gcregs;
132         byte*   entry;          // initial function
133         G*      alllink;        // on allg
134         void*   param;          // passed parameter on wakeup
135         bool    fromgogo;       // reached from gogo
136         int16   status;
137         int32   goid;
138         uint32  selgen;         // valid sudog pointer
139         const char*     waitreason;     // if status==Gwaiting
140         G*      schedlink;
141         bool    readyonstop;
142         bool    ispanic;
143         M*      m;              // for debuggers, but offset not hard-coded
144         M*      lockedm;
145         M*      idlem;
146         int32   sig;
147         int32   writenbuf;
148         byte*   writebuf;
149         uintptr sigcode0;
150         uintptr sigcode1;
151         // uintptr      sigpc;
152         uintptr gopc;   // pc of go statement that created this goroutine
153
154         G*      dotraceback;
155
156         ucontext_t      context;
157         void*           stack_context[10];
158 };
159
160 struct  M
161 {
162         G*      g0;             // goroutine with scheduling stack
163         G*      gsignal;        // signal-handling G
164         G*      curg;           // current running goroutine
165         int32   id;
166         int32   mallocing;
167         int32   gcing;
168         int32   locks;
169         int32   nomemprof;
170         int32   waitnextg;
171         int32   dying;
172         int32   profilehz;
173         int32   helpgc;
174         uint32  fastrand;
175         Note    havenextg;
176         G*      nextg;
177         M*      alllink;        // on allm
178         M*      schedlink;
179         MCache  *mcache;
180         G*      lockedg;
181         G*      idleg;
182         uintptr createstack[32];        // Stack that created this thread.
183         M*      nextwaitm;      // next M waiting for lock
184         uintptr waitsema;       // semaphore for parking on locks
185         uint32  waitsemacount;
186         uint32  waitsemalock;
187 };
188
189 struct  SigTab
190 {
191         int32   sig;
192         int32   flags;
193 };
194 enum
195 {
196         SigNotify = 1<<0,       // let signal.Notify have signal, even if from kernel
197         SigKill = 1<<1,         // if signal.Notify doesn't take it, exit quietly
198         SigThrow = 1<<2,        // if signal.Notify doesn't take it, exit loudly
199         SigPanic = 1<<3,        // if the signal is from the kernel, panic
200         SigDefault = 1<<4,      // if the signal isn't explicitly requested, don't monitor it
201 };
202
203 #ifndef NSIG
204 #define NSIG 32
205 #endif
206
207 // NOTE(rsc): keep in sync with extern.go:/type.Func.
208 // Eventually, the loaded symbol table should be closer to this form.
209 struct  Func
210 {
211         String  name;
212         uintptr entry;  // entry pc
213 };
214
215 /* Macros.  */
216
217 #ifdef GOOS_windows
218 enum {
219    Windows = 1
220 };
221 #else
222 enum {
223    Windows = 0
224 };
225 #endif
226
227 struct  Timers
228 {
229         Lock;
230         G       *timerproc;
231         bool            sleeping;
232         bool            rescheduling;
233         Note    waitnote;
234         Timer   **t;
235         int32   len;
236         int32   cap;
237 };
238
239 // Package time knows the layout of this structure.
240 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
241 struct  Timer
242 {
243         int32   i;              // heap index
244
245         // Timer wakes up at when, and then at when+period, ... (period > 0 only)
246         // each time calling f(now, arg) in the timer goroutine, so f must be
247         // a well-behaved function and not block.
248         int64   when;
249         int64   period;
250         void    (*f)(int64, Eface);
251         Eface   arg;
252 };
253
254 /*
255  * defined macros
256  *    you need super-gopher-guru privilege
257  *    to add this list.
258  */
259 #define nelem(x)        (sizeof(x)/sizeof((x)[0]))
260 #define nil             ((void*)0)
261 #define USED(v)         ((void) v)
262
263 /*
264  * external data
265  */
266 G*      runtime_allg;
267 G*      runtime_lastg;
268 M*      runtime_allm;
269 extern  int32   runtime_gomaxprocs;
270 extern  bool    runtime_singleproc;
271 extern  uint32  runtime_panicking;
272 extern  int32   runtime_gcwaiting;              // gc is waiting to run
273 int32   runtime_ncpu;
274
275 /*
276  * common functions and data
277  */
278 int32   runtime_findnull(const byte*);
279
280 /*
281  * very low level c-called
282  */
283 void    runtime_args(int32, byte**);
284 void    runtime_osinit();
285 void    runtime_goargs(void);
286 void    runtime_goenvs(void);
287 void    runtime_goenvs_unix(void);
288 void    runtime_throw(const char*) __attribute__ ((noreturn));
289 void    runtime_panicstring(const char*) __attribute__ ((noreturn));
290 void*   runtime_mal(uintptr);
291 void    runtime_schedinit(void);
292 void    runtime_initsig(void);
293 void    runtime_sigenable(uint32 sig);
294 int32   runtime_gotraceback(void);
295 void    runtime_goroutineheader(G*);
296 void    runtime_goroutinetrailer(G*);
297 void    runtime_traceback();
298 void    runtime_tracebackothers(G*);
299 String  runtime_gostringnocopy(const byte*);
300 void*   runtime_mstart(void*);
301 G*      runtime_malg(int32, byte**, size_t*);
302 void    runtime_minit(void);
303 void    runtime_mallocinit(void);
304 void    runtime_gosched(void);
305 void    runtime_tsleep(int64);
306 M*      runtime_newm(void);
307 void    runtime_goexit(void);
308 void    runtime_entersyscall(void) __asm__("syscall.Entersyscall");
309 void    runtime_exitsyscall(void) __asm__("syscall.Exitsyscall");
310 void    siginit(void);
311 bool    __go_sigsend(int32 sig);
312 int32   runtime_callers(int32, uintptr*, int32);
313 int64   runtime_nanotime(void);
314 int64   runtime_cputicks(void);
315
316 void    runtime_stoptheworld(void);
317 void    runtime_starttheworld(bool);
318 extern uint32 runtime_worldsema;
319 G*      __go_go(void (*pfn)(void*), void*);
320
321 /*
322  * mutual exclusion locks.  in the uncontended case,
323  * as fast as spin locks (just a few user-level instructions),
324  * but on the contention path they sleep in the kernel.
325  * a zeroed Lock is unlocked (no need to initialize each lock).
326  */
327 void    runtime_lock(Lock*);
328 void    runtime_unlock(Lock*);
329
330 /*
331  * sleep and wakeup on one-time events.
332  * before any calls to notesleep or notewakeup,
333  * must call noteclear to initialize the Note.
334  * then, exactly one thread can call notesleep
335  * and exactly one thread can call notewakeup (once).
336  * once notewakeup has been called, the notesleep
337  * will return.  future notesleep will return immediately.
338  * subsequent noteclear must be called only after
339  * previous notesleep has returned, e.g. it's disallowed
340  * to call noteclear straight after notewakeup.
341  *
342  * notetsleep is like notesleep but wakes up after
343  * a given number of nanoseconds even if the event
344  * has not yet happened.  if a goroutine uses notetsleep to
345  * wake up early, it must wait to call noteclear until it
346  * can be sure that no other goroutine is calling
347  * notewakeup.
348  */
349 void    runtime_noteclear(Note*);
350 void    runtime_notesleep(Note*);
351 void    runtime_notewakeup(Note*);
352 void    runtime_notetsleep(Note*, int64);
353
354 /*
355  * low-level synchronization for implementing the above
356  */
357 uintptr runtime_semacreate(void);
358 int32   runtime_semasleep(int64);
359 void    runtime_semawakeup(M*);
360 // or
361 void    runtime_futexsleep(uint32*, uint32, int64);
362 void    runtime_futexwakeup(uint32*, uint32);
363
364 /*
365  * low level C-called
366  */
367 #define runtime_mmap mmap
368 #define runtime_munmap munmap
369 #define runtime_madvise madvise
370 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
371 #define runtime_getcallerpc(p) __builtin_return_address(0)
372
373 #ifdef __rtems__
374 void __wrap_rtems_task_variable_add(void **);
375 #endif
376
377 /*
378  * runtime go-called
379  */
380 void    runtime_panic(Eface);
381 struct __go_func_type;
382 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
383                   void **, void **)
384   asm ("reflect.call");
385
386 /* Functions.  */
387 #define runtime_panic __go_panic
388 #define runtime_printf printf
389 #define runtime_malloc(s) __go_alloc(s)
390 #define runtime_free(p) __go_free(p)
391 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
392 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
393 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
394 #define runtime_exit(s) exit(s)
395 MCache* runtime_allocmcache(void);
396 void    free(void *v);
397 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
398 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
399 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
400 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
401 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
402 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
403 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
404 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
405
406 struct __go_func_type;
407 bool    runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
408 #define runtime_getcallersp(p) __builtin_frame_address(1)
409 int32   runtime_mcount(void);
410 int32   runtime_gcount(void);
411 void    runtime_dopanic(int32) __attribute__ ((noreturn));
412 void    runtime_startpanic(void);
413 void    runtime_ready(G*);
414 const byte*     runtime_getenv(const char*);
415 int32   runtime_atoi(const byte*);
416 uint32  runtime_fastrand1(void);
417
418 void    runtime_sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
419 void    runtime_resetcpuprofiler(int32);
420 void    runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
421 void    runtime_usleep(uint32);
422
423 /*
424  * runtime c-called (but written in Go)
425  */
426 void    runtime_printany(Eface)
427      __asm__("runtime.Printany");
428 void    runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
429      __asm__("runtime.NewTypeAssertionError");
430 void    runtime_newErrorString(String, Eface*)
431      __asm__("runtime.NewErrorString");
432
433 /*
434  * wrapped for go users
435  */
436 void    runtime_semacquire(uint32 volatile *);
437 void    runtime_semrelease(uint32 volatile *);
438 int32   runtime_gomaxprocsfunc(int32 n);
439 void    runtime_procyield(uint32);
440 void    runtime_osyield(void);
441 void    runtime_LockOSThread(void) __asm__("runtime.LockOSThread");
442 void    runtime_UnlockOSThread(void) __asm__("runtime.UnlockOSThread");
443
444 bool    runtime_showframe(const unsigned char*);
445
446 uintptr runtime_memlimit(void);
447
448 // If appropriate, ask the operating system to control whether this
449 // thread should receive profiling signals.  This is only necessary on OS X.
450 // An operating system should not deliver a profiling signal to a
451 // thread that is not actually executing (what good is that?), but that's
452 // what OS X prefers to do.  When profiling is turned on, we mask
453 // away the profiling signal when threads go to sleep, so that OS X
454 // is forced to deliver the signal to a thread that's actually running.
455 // This is a no-op on other systems.
456 void    runtime_setprof(bool);
457
458 void    runtime_time_scan(void (*)(byte*, int64));
459
460 void    runtime_setsig(int32, bool, bool);
461 #define runtime_setitimer setitimer
462
463 void    runtime_check(void);
464
465 // A list of global variables that the garbage collector must scan.
466 struct root_list {
467         struct root_list *next;
468         struct root {
469                 void *decl;
470                 size_t size;
471         } roots[];
472 };
473
474 void    __go_register_gc_roots(struct root_list*);
475
476 // Size of stack space allocated using Go's allocator.
477 // This will be 0 when using split stacks, as in that case
478 // the stacks are allocated by the splitstack library.
479 extern uintptr runtime_stacks_sys;
480
481 extern _Bool __go_file_line (uintptr, String*, String*, int *);