OSDN Git Service

7f061cbbe3130ff417e52eefb98c439f51e31fb5
[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 <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
21 #ifdef HAVE_SYS_MMAN_H
22 #include <sys/mman.h>
23 #endif
24
25 #include "go-alloc.h"
26 #include "go-panic.h"
27 #include "go-string.h"
28
29 typedef struct __go_string String;
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  G               G;
52 typedef struct  M               M;
53 typedef struct  MCache          MCache;
54 typedef struct  FixAlloc        FixAlloc;
55 typedef struct  Lock            Lock;
56
57 typedef struct  __go_defer_stack        Defer;
58 typedef struct  __go_panic_stack        Panic;
59
60 /* We use mutexes for locks.  6g uses futexes directly, and perhaps
61    someday we will do that too.  */
62
63 struct  Lock
64 {
65         uint32 key;
66         sem_t sem;
67 };
68
69 /* A Note.  */
70
71 typedef struct  Note            Note;
72
73 struct Note {
74         int32 woken;
75 };
76
77 /* Per CPU declarations.  */
78
79 #ifdef __rtems__
80 #define __thread
81 #endif
82
83 extern __thread         G*      g;
84 extern __thread         M*      m;
85
86 extern M        runtime_m0;
87 extern G        runtime_g0;
88
89 #ifdef __rtems__
90 #undef __thread
91 #endif
92
93 /* Constants.  */
94
95 enum
96 {
97         true    = 1,
98         false   = 0,
99 };
100
101 /* Structures.  */
102
103 struct  G
104 {
105         Defer*  defer;
106         Panic*  panic;
107         void*   exception;      // current exception being thrown
108         bool    is_foreign;     // whether current exception from other language
109         byte*   entry;          // initial function
110         G*      alllink;        // on allg
111         void*   param;          // passed parameter on wakeup
112         int16   status;
113         int32   goid;
114         int8*   waitreason;     // if status==Gwaiting
115         G*      schedlink;
116         bool    readyonstop;
117         bool    ispanic;
118         M*      m;              // for debuggers, but offset not hard-coded
119         M*      lockedm;
120         M*      idlem;
121         // int32        sig;
122         // uintptr      sigcode0;
123         // uintptr      sigcode1;
124         // uintptr      sigpc;
125         // uintptr      gopc;   // pc of go statement that created this goroutine
126 };
127
128 struct  M
129 {
130         G*      curg;           // current running goroutine
131         int32   id;
132         int32   mallocing;
133         int32   gcing;
134         int32   locks;
135         int32   nomemprof;
136         int32   gcing_for_prof;
137         int32   holds_finlock;
138         int32   gcing_for_finlock;
139         int32   profilehz;
140         uint32  fastrand;
141         MCache  *mcache;
142
143         /* For the list of all threads.  */
144         struct __go_thread_id *list_entry;
145
146         /* For the garbage collector.  */
147         void    *gc_sp;
148         size_t  gc_len;
149         void    *gc_next_segment;
150         void    *gc_next_sp;
151         void    *gc_initial_sp;
152 };
153
154 /* Macros.  */
155 #define nelem(x)        (sizeof(x)/sizeof((x)[0]))
156 #define nil             ((void*)0)
157 #define USED(v)         ((void) v)
158
159 /* We map throw to assert.  */
160 #define runtime_throw(s) __go_assert(s == 0)
161
162 void*   runtime_mal(uintptr);
163 void    runtime_mallocinit(void);
164 void    runtime_initfintab(void);
165 void    siginit(void);
166 bool    __go_sigsend(int32 sig);
167 int64   runtime_nanotime(void);
168
169 void    runtime_stoptheworld(void);
170 void    runtime_starttheworld(bool);
171 void    __go_go(void (*pfn)(void*), void*);
172 void    __go_gc_goroutine_init(void*);
173 void    __go_enable_gc(void);
174 int     __go_run_goroutine_gc(int);
175 void    __go_scanstacks(void (*scan)(byte *, int64));
176 void    __go_stealcache(void);
177 void    __go_cachestats(void);
178
179 /*
180  * mutual exclusion locks.  in the uncontended case,
181  * as fast as spin locks (just a few user-level instructions),
182  * but on the contention path they sleep in the kernel.
183  */
184 void    runtime_initlock(Lock*);
185 void    runtime_lock(Lock*);
186 void    runtime_unlock(Lock*);
187 void    runtime_destroylock(Lock*);
188
189 void runtime_semacquire (uint32 *) asm ("libgo_runtime.runtime.Semacquire");
190 void runtime_semrelease (uint32 *) asm ("libgo_runtime.runtime.Semrelease");
191
192 /*
193  * sleep and wakeup on one-time events.
194  * before any calls to notesleep or notewakeup,
195  * must call noteclear to initialize the Note.
196  * then, any number of threads can call notesleep
197  * and exactly one thread can call notewakeup (once).
198  * once notewakeup has been called, all the notesleeps
199  * will return.  future notesleeps will return immediately.
200  */
201 void    runtime_noteclear(Note*);
202 void    runtime_notesleep(Note*);
203 void    runtime_notewakeup(Note*);
204
205 /* Functions.  */
206 #define runtime_printf printf
207 #define runtime_malloc(s) __go_alloc(s)
208 #define runtime_free(p) __go_free(p)
209 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
210 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
211 #define runtime_getenv(s) getenv(s)
212 #define runtime_atoi(s) atoi(s)
213 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
214 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
215 MCache* runtime_allocmcache(void);
216 void    free(void *v);
217 struct __go_func_type;
218 bool    runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
219 #define runtime_mmap mmap
220 #define runtime_munmap(p, s) munmap((p), (s))
221 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
222 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
223 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
224
225 void    runtime_sigprof(uint8 *pc, uint8 *sp, uint8 *lr);
226 void    runtime_cpuprofinit(void);
227 void    runtime_resetcpuprofiler(int32);
228 void    runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
229 uint32  runtime_fastrand1(void);
230 void    runtime_procyield(uint32);
231 void    runtime_osyield(void);
232 void    runtime_usleep(uint32);
233
234 struct __go_func_type;
235 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
236                   void **, void **)
237   asm ("libgo_reflect.reflect.call");
238
239 #ifdef __rtems__
240 void __wrap_rtems_task_variable_add(void **);
241 #endif