]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/runtime/runtime.h
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.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 <pthread.h>
16
17 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/mman.h>
19 #endif
20
21 #include "go-alloc.h"
22 #include "go-panic.h"
23 #include "go-string.h"
24
25 typedef struct __go_string String;
26
27 /* This file supports C files copied from the 6g runtime library.
28 This is a version of the 6g runtime.h rewritten for gccgo's version
29 of the code. */
30
31 typedef signed int int8 __attribute__ ((mode (QI)));
32 typedef unsigned int uint8 __attribute__ ((mode (QI)));
33 typedef signed int int16 __attribute__ ((mode (HI)));
34 typedef unsigned int uint16 __attribute__ ((mode (HI)));
35 typedef signed int int32 __attribute__ ((mode (SI)));
36 typedef unsigned int uint32 __attribute__ ((mode (SI)));
37 typedef signed int int64 __attribute__ ((mode (DI)));
38 typedef unsigned int uint64 __attribute__ ((mode (DI)));
39 typedef float float32 __attribute__ ((mode (SF)));
40 typedef double float64 __attribute__ ((mode (DF)));
41 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
42
43 /* Defined types. */
44
45 typedef uint8 bool;
46 typedef uint8 byte;
47 typedef struct M M;
48 typedef struct MCache MCache;
49 typedef struct Lock Lock;
50
51 /* We use mutexes for locks. 6g uses futexes directly, and perhaps
52 someday we will do that too. */
53
54 struct Lock
55 {
56 pthread_mutex_t mutex;
57 };
58
59 /* A Note. */
60
61 typedef struct Note Note;
62
63 struct Note {
64 int32 woken;
65 };
66
67 /* Per CPU declarations. */
68
69 #ifdef __rtems__
70 #define __thread
71 #endif
72
73 extern __thread M* m;
74
75 extern M m0;
76
77 #ifdef __rtems__
78 #undef __thread
79 #endif
80
81 /* Constants. */
82
83 enum
84 {
85 true = 1,
86 false = 0,
87 };
88
89 /* Structures. */
90
91 struct M
92 {
93 int32 mallocing;
94 int32 gcing;
95 int32 locks;
96 int32 nomemprof;
97 int32 gcing_for_prof;
98 MCache *mcache;
99
100 /* For the list of all threads. */
101 struct __go_thread_id *list_entry;
102
103 /* For the garbage collector. */
104 void *gc_sp;
105 size_t gc_len;
106 void *gc_next_segment;
107 void *gc_next_sp;
108 void *gc_initial_sp;
109 struct __go_panic_defer_struct *gc_panic_defer;
110 };
111
112 /* Macros. */
113 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
114 #define nil ((void*)0)
115 #define USED(v) ((void) v)
116
117 /* We map throw to assert. */
118 #define runtime_throw(s) __go_assert(s == 0)
119
120 void* runtime_mal(uintptr);
121 void runtime_mallocinit(void);
122 void siginit(void);
123 bool __go_sigsend(int32 sig);
124 int64 runtime_nanotime(void);
125
126 void runtime_stoptheworld(void);
127 void runtime_starttheworld(void);
128 void __go_go(void (*pfn)(void*), void*);
129 void __go_gc_goroutine_init(void*);
130 void __go_enable_gc(void);
131 int __go_run_goroutine_gc(int);
132 void __go_scanstacks(void (*scan)(byte *, int64));
133 void __go_stealcache(void);
134 void __go_cachestats(void);
135
136 /*
137 * mutual exclusion locks. in the uncontended case,
138 * as fast as spin locks (just a few user-level instructions),
139 * but on the contention path they sleep in the kernel.
140 */
141 #define LOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
142 void runtime_initlock(Lock*);
143 void runtime_lock(Lock*);
144 void runtime_unlock(Lock*);
145 void runtime_destroylock(Lock*);
146 bool runtime_trylock(Lock*);
147
148 void semacquire (uint32 *) asm ("libgo_runtime.runtime.Semacquire");
149 void semrelease (uint32 *) asm ("libgo_runtime.runtime.Semrelease");
150
151 /*
152 * sleep and wakeup on one-time events.
153 * before any calls to notesleep or notewakeup,
154 * must call noteclear to initialize the Note.
155 * then, any number of threads can call notesleep
156 * and exactly one thread can call notewakeup (once).
157 * once notewakeup has been called, all the notesleeps
158 * will return. future notesleeps will return immediately.
159 */
160 void noteclear(Note*);
161 void notesleep(Note*);
162 void notewakeup(Note*);
163
164 /* Functions. */
165 #define runtime_printf printf
166 #define runtime_malloc(s) __go_alloc(s)
167 #define runtime_free(p) __go_free(p)
168 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
169 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
170 #define runtime_getenv(s) getenv(s)
171 #define runtime_atoi(s) atoi(s)
172 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
173 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
174 MCache* runtime_allocmcache(void);
175 void free(void *v);
176 struct __go_func_type;
177 void runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
178 void runtime_walkfintab(void (*fn)(void*), void (*scan)(byte *, int64));
179 #define runtime_mmap mmap
180 #define runtime_munmap(p, s) munmap((p), (s))
181 #define cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
182
183 struct __go_func_type;
184 void reflect_call(const struct __go_func_type *, const void *, _Bool, void **,
185 void **)
186 asm ("libgo_reflect.reflect.call");
187
188 #ifdef __rtems__
189 void __wrap_rtems_task_variable_add(void **);
190 #endif