OSDN Git Service

runtime: Remove temporary runtime_cond_wait function.
[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 #define _GNU_SOURCE
10 #include "go-assert.h"
11 #include <setjmp.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <pthread.h>
20 #include <semaphore.h>
21 #include <ucontext.h>
22
23 #ifdef HAVE_SYS_MMAN_H
24 #include <sys/mman.h>
25 #endif
26
27 #include "array.h"
28 #include "go-alloc.h"
29 #include "go-panic.h"
30 #include "go-string.h"
31
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
34    of the code.  */
35
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)));
47
48 /* Defined types.  */
49
50 typedef uint8                   bool;
51 typedef uint8                   byte;
52 typedef struct  G               G;
53 typedef union   Lock            Lock;
54 typedef struct  M               M;
55 typedef union   Note            Note;
56 typedef struct  MCache          MCache;
57 typedef struct  FixAlloc        FixAlloc;
58 typedef struct  Hchan           Hchan;
59
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;
67
68 typedef struct  __go_func_type          FuncType;
69 typedef struct  __go_map_type           MapType;
70
71 /*
72  * per-cpu declaration.
73  */
74 extern M*       runtime_m(void);
75 extern G*       runtime_g(void);
76
77 extern M        runtime_m0;
78 extern G        runtime_g0;
79
80 /*
81  * defined constants
82  */
83 enum
84 {
85         // G status
86         //
87         // If you add to this list, add to the list
88         // of "okay during garbage collection" status
89         // in mgc0.c too.
90         Gidle,
91         Grunnable,
92         Grunning,
93         Gsyscall,
94         Gwaiting,
95         Gmoribund,
96         Gdead,
97 };
98 enum
99 {
100         true    = 1,
101         false   = 0,
102 };
103
104 /*
105  * structures
106  */
107 union   Lock
108 {
109         uint32  key;    // futex-based impl
110         M*      waitm;  // linked list of waiting M's (sema-based impl)
111 };
112 union   Note
113 {
114         uint32  key;    // futex-based impl
115         M*      waitm;  // waiting M (sema-based impl)
116 };
117 struct  G
118 {
119         Defer*  defer;
120         Panic*  panic;
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;
126         void*   gcnext_sp;
127         void*   gcinitial_sp;
128         jmp_buf gcregs;
129         byte*   entry;          // initial function
130         G*      alllink;        // on allg
131         void*   param;          // passed parameter on wakeup
132         bool    fromgogo;       // reached from gogo
133         int16   status;
134         int32   goid;
135         uint32  selgen;         // valid sudog pointer
136         const char*     waitreason;     // if status==Gwaiting
137         G*      schedlink;
138         bool    readyonstop;
139         bool    ispanic;
140         M*      m;              // for debuggers, but offset not hard-coded
141         M*      lockedm;
142         M*      idlem;
143         // int32        sig;
144         // uintptr      sigcode0;
145         // uintptr      sigcode1;
146         // uintptr      sigpc;
147         uintptr gopc;   // pc of go statement that created this goroutine
148
149         ucontext_t      context;
150         void*           stack_context[10];
151 };
152
153 struct  M
154 {
155         G*      g0;             // goroutine with scheduling stack
156         G*      gsignal;        // signal-handling G
157         G*      curg;           // current running goroutine
158         int32   id;
159         int32   mallocing;
160         int32   gcing;
161         int32   locks;
162         int32   nomemprof;
163         int32   waitnextg;
164         int32   dying;
165         int32   profilehz;
166         int32   helpgc;
167         uint32  fastrand;
168         Note    havenextg;
169         G*      nextg;
170         M*      alllink;        // on allm
171         M*      schedlink;
172         MCache  *mcache;
173         G*      lockedg;
174         G*      idleg;
175         M*      nextwaitm;      // next M waiting for lock
176         uintptr waitsema;       // semaphore for parking on locks
177         uint32  waitsemacount;
178         uint32  waitsemalock;
179 };
180
181 /* Macros.  */
182
183 #ifdef __WINDOWS__
184 enum {
185    Windows = 1
186 };
187 #else
188 enum {
189    Windows = 0
190 };
191 #endif
192
193 #define nelem(x)        (sizeof(x)/sizeof((x)[0]))
194 #define nil             ((void*)0)
195 #define USED(v)         ((void) v)
196
197 /*
198  * external data
199  */
200 G*      runtime_allg;
201 G*      runtime_lastg;
202 M*      runtime_allm;
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
207 int32   runtime_ncpu;
208
209 /*
210  * common functions and data
211  */
212 int32   runtime_findnull(const byte*);
213
214 /*
215  * very low level c-called
216  */
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");
235 void    siginit(void);
236 bool    __go_sigsend(int32 sig);
237 int64   runtime_nanotime(void);
238
239 void    runtime_stoptheworld(void);
240 void    runtime_starttheworld(bool);
241 G*      __go_go(void (*pfn)(void*), void*);
242
243 /*
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).
248  */
249 void    runtime_lock(Lock*);
250 void    runtime_unlock(Lock*);
251
252 /*
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.
263  *
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
269  * notewakeup.
270  */
271 void    runtime_noteclear(Note*);
272 void    runtime_notesleep(Note*);
273 void    runtime_notewakeup(Note*);
274 void    runtime_notetsleep(Note*, int64);
275
276 /*
277  * low-level synchronization for implementing the above
278  */
279 uintptr runtime_semacreate(void);
280 int32   runtime_semasleep(int64);
281 void    runtime_semawakeup(M*);
282 // or
283 void    runtime_futexsleep(uint32*, uint32, int64);
284 void    runtime_futexwakeup(uint32*, uint32);
285
286 /*
287  * runtime go-called
288  */
289 void    runtime_panic(Eface);
290
291 /* Functions.  */
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);
302 void    free(void *v);
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)
315
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);
331
332 struct __go_func_type;
333 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
334                   void **, void **)
335   asm ("libgo_reflect.reflect.call");
336
337 #ifdef __rtems__
338 void __wrap_rtems_task_variable_add(void **);
339 #endif