runtime: copy Go 1.7 runtime semaphore code
[gcc.git] / libgo / runtime / runtime.h
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include "config.h"
6
7 #include "go-assert.h"
8 #include <complex.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <pthread.h>
18 #include <semaphore.h>
19 #include <ucontext.h>
20
21 #ifdef HAVE_SYS_MMAN_H
22 #include <sys/mman.h>
23 #endif
24
25 #include "interface.h"
26 #include "go-alloc.h"
27
28 #define _STRINGIFY2_(x) #x
29 #define _STRINGIFY_(x) _STRINGIFY2_(x)
30 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
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 signed int intptr __attribute__ ((mode (pointer)));
47 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
48
49 typedef intptr intgo; // Go's int
50 typedef uintptr uintgo; // Go's uint
51
52 typedef uintptr uintreg;
53
54 /* Defined types. */
55
56 typedef uint8 bool;
57 typedef uint8 byte;
58 typedef struct Func Func;
59 typedef struct g G;
60 typedef struct mutex Lock;
61 typedef struct m M;
62 typedef struct p P;
63 typedef struct note Note;
64 typedef struct String String;
65 typedef struct FuncVal FuncVal;
66 typedef struct SigTab SigTab;
67 typedef struct mcache MCache;
68 typedef struct FixAlloc FixAlloc;
69 typedef struct hchan Hchan;
70 typedef struct Timers Timers;
71 typedef struct Timer Timer;
72 typedef struct gcstats GCStats;
73 typedef struct LFNode LFNode;
74 typedef struct ParFor ParFor;
75 typedef struct ParForThread ParForThread;
76 typedef struct cgoMal CgoMal;
77 typedef struct PollDesc PollDesc;
78 typedef struct sudog SudoG;
79
80 typedef struct __go_open_array Slice;
81 typedef struct __go_interface Iface;
82 typedef struct __go_empty_interface Eface;
83 typedef struct __go_type_descriptor Type;
84 typedef struct _defer Defer;
85 typedef struct _panic Panic;
86
87 typedef struct __go_ptr_type PtrType;
88 typedef struct __go_func_type FuncType;
89 typedef struct __go_interface_type InterfaceType;
90 typedef struct __go_map_type MapType;
91 typedef struct __go_channel_type ChanType;
92
93 typedef struct traceback Traceback;
94
95 typedef struct location Location;
96
97 struct String
98 {
99 const byte* str;
100 intgo len;
101 };
102
103 struct FuncVal
104 {
105 void (*fn)(void);
106 // variable-size, fn-specific data here
107 };
108
109 #include "array.h"
110 #include "interface.h"
111
112 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
113 // the name conflict.
114 #define timeval go_timeval
115 #define timespec go_timespec
116
117 #include "runtime.inc"
118
119 #undef timeval
120 #undef timespec
121
122 /*
123 * Per-CPU declaration.
124 */
125 extern M* runtime_m(void);
126 extern G* runtime_g(void)
127 __asm__(GOSYM_PREFIX "runtime.getg");
128
129 extern M runtime_m0;
130 extern G runtime_g0;
131
132 enum
133 {
134 true = 1,
135 false = 0,
136 };
137 enum
138 {
139 PtrSize = sizeof(void*),
140 };
141 enum
142 {
143 // Per-M stack segment cache size.
144 StackCacheSize = 32,
145 // Global <-> per-M stack segment cache transfer batch size.
146 StackCacheBatch = 16,
147 };
148
149 struct SigTab
150 {
151 int32 sig;
152 int32 flags;
153 void* fwdsig;
154 };
155
156 // Layout of in-memory per-function information prepared by linker
157 // See http://golang.org/s/go12symtab.
158 // Keep in sync with linker and with ../../libmach/sym.c
159 // and with package debug/gosym.
160 struct Func
161 {
162 String name;
163 uintptr entry; // entry pc
164 };
165
166 #ifdef GOOS_nacl
167 enum {
168 NaCl = 1,
169 };
170 #else
171 enum {
172 NaCl = 0,
173 };
174 #endif
175
176 #ifdef GOOS_windows
177 enum {
178 Windows = 1
179 };
180 #else
181 enum {
182 Windows = 0
183 };
184 #endif
185 #ifdef GOOS_solaris
186 enum {
187 Solaris = 1
188 };
189 #else
190 enum {
191 Solaris = 0
192 };
193 #endif
194
195 struct Timers
196 {
197 Lock;
198 G *timerproc;
199 bool sleeping;
200 bool rescheduling;
201 Note waitnote;
202 Timer **t;
203 int32 len;
204 int32 cap;
205 };
206
207 // Package time knows the layout of this structure.
208 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
209 // For GOOS=nacl, package syscall knows the layout of this structure.
210 // If this struct changes, adjust ../syscall/net_nacl.go:/runtimeTimer.
211 struct Timer
212 {
213 intgo i; // heap index
214
215 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
216 // each time calling f(now, arg) in the timer goroutine, so f must be
217 // a well-behaved function and not block.
218 int64 when;
219 int64 period;
220 FuncVal *fv;
221 Eface arg;
222 uintptr seq;
223 };
224
225 // Lock-free stack node.
226 struct LFNode
227 {
228 LFNode *next;
229 uintptr pushcnt;
230 };
231
232 // Parallel for descriptor.
233 struct ParFor
234 {
235 const FuncVal *body; // executed for each element
236 uint32 done; // number of idle threads
237 uint32 nthr; // total number of threads
238 uint32 nthrmax; // maximum number of threads
239 uint32 thrseq; // thread id sequencer
240 uint32 cnt; // iteration space [0, cnt)
241 bool wait; // if true, wait while all threads finish processing,
242 // otherwise parfor may return while other threads are still working
243 ParForThread *thr; // array of thread descriptors
244 // stats
245 uint64 nsteal;
246 uint64 nstealcnt;
247 uint64 nprocyield;
248 uint64 nosyield;
249 uint64 nsleep;
250 };
251
252 extern bool runtime_precisestack;
253 extern bool runtime_copystack;
254
255 /*
256 * defined macros
257 * you need super-gopher-guru privilege
258 * to add this list.
259 */
260 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
261 #define nil ((void*)0)
262 #define USED(v) ((void) v)
263 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
264
265 byte* runtime_startup_random_data;
266 uint32 runtime_startup_random_data_len;
267 void runtime_get_random_data(byte**, int32*);
268
269 enum {
270 // hashinit wants this many random bytes
271 HashRandomBytes = 32
272 };
273 void runtime_hashinit(void);
274
275 void runtime_traceback(void);
276 void runtime_tracebackothers(G*);
277 enum
278 {
279 // The maximum number of frames we print for a traceback
280 TracebackMaxFrames = 100,
281 };
282
283 /*
284 * external data
285 */
286 extern uintptr runtime_zerobase;
287 extern G** runtime_allg;
288 extern uintptr runtime_allglen;
289 extern G* runtime_lastg;
290 extern M* runtime_allm;
291 extern P** runtime_allp;
292 extern int32 runtime_gomaxprocs;
293 extern uint32 runtime_needextram;
294 extern uint32 runtime_panicking;
295 extern int8* runtime_goos;
296 extern int32 runtime_ncpu;
297 extern void (*runtime_sysargs)(int32, uint8**);
298 extern struct debugVars runtime_debug;
299 extern uintptr runtime_maxstacksize;
300
301 extern bool runtime_isstarted;
302 extern bool runtime_isarchive;
303
304 /*
305 * common functions and data
306 */
307 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
308 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
309 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
310 intgo runtime_findnull(const byte*);
311 intgo runtime_findnullw(const uint16*);
312
313 void runtime_gogo(G*);
314 struct __go_func_type;
315 void runtime_args(int32, byte**)
316 __asm__ (GOSYM_PREFIX "runtime.args");
317 void runtime_osinit();
318 void runtime_goargs(void)
319 __asm__ (GOSYM_PREFIX "runtime.goargs");
320 void runtime_goenvs(void);
321 void runtime_goenvs_unix(void)
322 __asm__ (GOSYM_PREFIX "runtime.goenvs_unix");
323 void runtime_throw(const char*) __attribute__ ((noreturn));
324 void runtime_panicstring(const char*) __attribute__ ((noreturn));
325 bool runtime_canpanic(G*);
326 void runtime_printf(const char*, ...);
327 int32 runtime_snprintf(byte*, int32, const char*, ...);
328 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
329 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
330 void* runtime_mal(uintptr);
331 String runtime_gostring(const byte*);
332 String runtime_gostringnocopy(const byte*);
333 void runtime_schedinit(void);
334 void runtime_initsig(bool);
335 void runtime_sigenable(uint32 sig);
336 void runtime_sigdisable(uint32 sig);
337 void runtime_sigignore(uint32 sig);
338 int32 runtime_gotraceback(bool *crash);
339 void runtime_goroutineheader(G*);
340 void runtime_printtrace(Location*, int32, bool);
341 #define runtime_open(p, f, m) open((p), (f), (m))
342 #define runtime_read(d, v, n) read((d), (v), (n))
343 #define runtime_write(d, v, n) write((d), (v), (n))
344 #define runtime_close(d) close(d)
345 void runtime_ready(G*);
346 String runtime_getenv(const char*);
347 int32 runtime_atoi(const byte*, intgo);
348 void* runtime_mstart(void*);
349 G* runtime_malg(int32, byte**, uintptr*);
350 void runtime_mpreinit(M*);
351 void runtime_minit(void);
352 void runtime_unminit(void);
353 void runtime_needm(void);
354 void runtime_dropm(void);
355 void runtime_signalstack(byte*, int32);
356 MCache* runtime_allocmcache(void);
357 void runtime_freemcache(MCache*);
358 void runtime_mallocinit(void);
359 void runtime_mprofinit(void);
360 #define runtime_malloc(s) __go_alloc(s)
361 #define runtime_free(p) __go_free(p)
362 #define runtime_getcallersp(p) __builtin_frame_address(1)
363 int32 runtime_mcount(void);
364 int32 runtime_gcount(void);
365 void runtime_mcall(void(*)(G*));
366 uint32 runtime_fastrand1(void) __asm__ (GOSYM_PREFIX "runtime.fastrand1");
367 int32 runtime_timediv(int64, int32, int32*)
368 __asm__ (GOSYM_PREFIX "runtime.timediv");
369 int32 runtime_round2(int32 x); // round x up to a power of 2.
370
371 // atomic operations
372 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
373 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
374 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
375 // Don't confuse with XADD x86 instruction,
376 // this one is actually 'addx', that is, add-and-fetch.
377 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
378 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
379 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
380 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
381 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
382 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
383 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
384 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
385 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
386 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
387 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
388
389 void runtime_setg(G*);
390 void runtime_newextram(void);
391 #define runtime_exit(s) exit(s)
392 #define runtime_breakpoint() __builtin_trap()
393 void runtime_gosched(void);
394 void runtime_gosched0(G*);
395 void runtime_schedtrace(bool);
396 void runtime_park(bool(*)(G*, void*), void*, const char*);
397 void runtime_parkunlock(Lock*, const char*);
398 void runtime_tsleep(int64, const char*);
399 M* runtime_newm(void);
400 void runtime_goexit(void);
401 void runtime_entersyscall(int32)
402 __asm__ (GOSYM_PREFIX "runtime.entersyscall");
403 void runtime_entersyscallblock(int32)
404 __asm__ (GOSYM_PREFIX "runtime.entersyscallblock");
405 void runtime_exitsyscall(int32)
406 __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
407 G* __go_go(void (*pfn)(void*), void*);
408 void siginit(void);
409 bool __go_sigsend(int32 sig);
410 int32 runtime_callers(int32, Location*, int32, bool keep_callers);
411 int64 runtime_nanotime(void) // monotonic time
412 __asm__(GOSYM_PREFIX "runtime.nanotime");
413 int64 runtime_unixnanotime(void); // real time, can skip
414 void runtime_dopanic(int32) __attribute__ ((noreturn));
415 void runtime_startpanic(void);
416 void runtime_freezetheworld(void);
417 void runtime_unwindstack(G*, byte*);
418 void runtime_sigprof();
419 void runtime_resetcpuprofiler(int32);
420 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
421 void runtime_usleep(uint32)
422 __asm__ (GOSYM_PREFIX "runtime.usleep");
423 int64 runtime_cputicks(void)
424 __asm__ (GOSYM_PREFIX "runtime.cputicks");
425 int64 runtime_tickspersecond(void)
426 __asm__ (GOSYM_PREFIX "runtime.tickspersecond");
427 void runtime_blockevent(int64, int32);
428 extern int64 runtime_blockprofilerate;
429 void runtime_addtimer(Timer*);
430 bool runtime_deltimer(Timer*);
431 G* runtime_netpoll(bool);
432 void runtime_netpollinit(void);
433 int32 runtime_netpollopen(uintptr, PollDesc*);
434 int32 runtime_netpollclose(uintptr);
435 void runtime_netpollready(G**, PollDesc*, int32);
436 uintptr runtime_netpollfd(PollDesc*);
437 void runtime_netpollarm(PollDesc*, int32);
438 void** runtime_netpolluser(PollDesc*);
439 bool runtime_netpollclosing(PollDesc*);
440 void runtime_netpolllock(PollDesc*);
441 void runtime_netpollunlock(PollDesc*);
442 void runtime_crash(void);
443 void runtime_parsedebugvars(void)
444 __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
445 void _rt0_go(void);
446 void* runtime_funcdata(Func*, int32);
447 int32 runtime_setmaxthreads(int32);
448 G* runtime_timejump(void);
449 void runtime_iterate_finq(void (*callback)(FuncVal*, void*, const FuncType*, const PtrType*));
450
451 void runtime_stoptheworld(void);
452 void runtime_starttheworld(void);
453 extern uint32 runtime_worldsema;
454
455 /*
456 * mutual exclusion locks. in the uncontended case,
457 * as fast as spin locks (just a few user-level instructions),
458 * but on the contention path they sleep in the kernel.
459 * a zeroed Lock is unlocked (no need to initialize each lock).
460 */
461 void runtime_lock(Lock*)
462 __asm__(GOSYM_PREFIX "runtime.lock");
463 void runtime_unlock(Lock*)
464 __asm__(GOSYM_PREFIX "runtime.unlock");
465
466 /*
467 * sleep and wakeup on one-time events.
468 * before any calls to notesleep or notewakeup,
469 * must call noteclear to initialize the Note.
470 * then, exactly one thread can call notesleep
471 * and exactly one thread can call notewakeup (once).
472 * once notewakeup has been called, the notesleep
473 * will return. future notesleep will return immediately.
474 * subsequent noteclear must be called only after
475 * previous notesleep has returned, e.g. it's disallowed
476 * to call noteclear straight after notewakeup.
477 *
478 * notetsleep is like notesleep but wakes up after
479 * a given number of nanoseconds even if the event
480 * has not yet happened. if a goroutine uses notetsleep to
481 * wake up early, it must wait to call noteclear until it
482 * can be sure that no other goroutine is calling
483 * notewakeup.
484 *
485 * notesleep/notetsleep are generally called on g0,
486 * notetsleepg is similar to notetsleep but is called on user g.
487 */
488 void runtime_noteclear(Note*)
489 __asm__ (GOSYM_PREFIX "runtime.noteclear");
490 void runtime_notesleep(Note*)
491 __asm__ (GOSYM_PREFIX "runtime.notesleep");
492 void runtime_notewakeup(Note*)
493 __asm__ (GOSYM_PREFIX "runtime.notewakeup");
494 bool runtime_notetsleep(Note*, int64) // false - timeout
495 __asm__ (GOSYM_PREFIX "runtime.notetsleep");
496 bool runtime_notetsleepg(Note*, int64) // false - timeout
497 __asm__ (GOSYM_PREFIX "runtime.notetsleepg");
498
499 /*
500 * Lock-free stack.
501 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
502 * The stack does not keep pointers to nodes,
503 * so they can be garbage collected if there are no other pointers to nodes.
504 */
505 void runtime_lfstackpush(uint64 *head, LFNode *node)
506 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
507 LFNode* runtime_lfstackpop(uint64 *head);
508
509 /*
510 * Parallel for over [0, n).
511 * body() is executed for each iteration.
512 * nthr - total number of worker threads.
513 * if wait=true, threads return from parfor() when all work is done;
514 * otherwise, threads can return while other threads are still finishing processing.
515 */
516 ParFor* runtime_parforalloc(uint32 nthrmax);
517 void runtime_parforsetup(ParFor *desc, uint32 nthr, uint32 n, bool wait, const FuncVal *body);
518 void runtime_parfordo(ParFor *desc);
519 void runtime_parforiters(ParFor*, uintptr, uintptr*, uintptr*);
520
521 /*
522 * low level C-called
523 */
524 #define runtime_mmap mmap
525 #define runtime_munmap munmap
526 #define runtime_madvise madvise
527 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
528 #define runtime_getcallerpc(p) __builtin_return_address(0)
529
530 #ifdef __rtems__
531 void __wrap_rtems_task_variable_add(void **);
532 #endif
533
534 /*
535 * runtime go-called
536 */
537 void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool,
538 void **, void **)
539 __asm__ (GOSYM_PREFIX "reflect.call");
540 #define runtime_panic __go_panic
541
542 /*
543 * runtime c-called (but written in Go)
544 */
545 void runtime_printany(Eface)
546 __asm__ (GOSYM_PREFIX "runtime.Printany");
547 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
548 __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
549 void runtime_newErrorCString(const char*, Eface*)
550 __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
551
552 /*
553 * wrapped for go users
554 */
555 void runtime_semacquire(uint32 volatile *, bool)
556 __asm__ (GOSYM_PREFIX "runtime.semacquire");
557 void runtime_semrelease(uint32 volatile *)
558 __asm__ (GOSYM_PREFIX "runtime.semrelease");
559 int32 runtime_gomaxprocsfunc(int32 n);
560 void runtime_procyield(uint32)
561 __asm__(GOSYM_PREFIX "runtime.procyield");
562 void runtime_osyield(void)
563 __asm__(GOSYM_PREFIX "runtime.osyield");
564 void runtime_lockOSThread(void);
565 void runtime_unlockOSThread(void);
566 bool runtime_lockedOSThread(void);
567
568 bool runtime_showframe(String, bool);
569 void runtime_printcreatedby(G*);
570
571 uintptr runtime_memlimit(void);
572
573 #define ISNAN(f) __builtin_isnan(f)
574
575 enum
576 {
577 UseSpanType = 1,
578 };
579
580 #define runtime_setitimer setitimer
581
582 void runtime_check(void)
583 __asm__ (GOSYM_PREFIX "runtime.check");
584
585 // A list of global variables that the garbage collector must scan.
586 struct root_list {
587 struct root_list *next;
588 struct root {
589 void *decl;
590 size_t size;
591 } roots[];
592 };
593
594 void __go_register_gc_roots(struct root_list*);
595
596 // Size of stack space allocated using Go's allocator.
597 // This will be 0 when using split stacks, as in that case
598 // the stacks are allocated by the splitstack library.
599 extern uintptr runtime_stacks_sys;
600
601 struct backtrace_state;
602 extern struct backtrace_state *__go_get_backtrace_state(void);
603 extern _Bool __go_file_line(uintptr, int, String*, String*, intgo *);
604 extern void runtime_main(void*);
605 extern uint32 runtime_in_callers;
606
607 int32 getproccount(void);
608
609 #define PREFETCH(p) __builtin_prefetch(p)
610
611 bool runtime_gcwaiting(void);
612 void runtime_badsignal(int);
613 Defer* runtime_newdefer(void);
614 void runtime_freedefer(Defer*);
615
616 struct time_now_ret
617 {
618 int64_t sec;
619 int32_t nsec;
620 };
621
622 struct time_now_ret now() __asm__ (GOSYM_PREFIX "time.now")
623 __attribute__ ((no_split_stack));
624
625 extern void _cgo_wait_runtime_init_done (void);
626 extern void _cgo_notify_runtime_init_done (void);
627 extern _Bool runtime_iscgo;
628 extern _Bool runtime_cgoHasExtraM;
629 extern Hchan *runtime_main_init_done;
630 extern uintptr __go_end __attribute__ ((weak));