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