1 |
747 |
jeremybenn |
// 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 |
|
|
/*
|
6 |
|
|
Package runtime contains operations that interact with Go's runtime system,
|
7 |
|
|
such as functions to control goroutines. It also includes the low-level type information
|
8 |
|
|
used by the reflect package; see reflect's documentation for the programmable
|
9 |
|
|
interface to the run-time type system.
|
10 |
|
|
*/
|
11 |
|
|
package runtime
|
12 |
|
|
|
13 |
|
|
// Gosched yields the processor, allowing other goroutines to run. It does not
|
14 |
|
|
// suspend the current goroutine, so execution resumes automatically.
|
15 |
|
|
func Gosched()
|
16 |
|
|
|
17 |
|
|
// Goexit terminates the goroutine that calls it. No other goroutine is affected.
|
18 |
|
|
// Goexit runs all deferred calls before terminating the goroutine.
|
19 |
|
|
func Goexit()
|
20 |
|
|
|
21 |
|
|
// Caller reports file and line number information about function invocations on
|
22 |
|
|
// the calling goroutine's stack. The argument skip is the number of stack frames
|
23 |
|
|
// to ascend, with 0 identifying the caller of Caller. The return values report the
|
24 |
|
|
// program counter, file name, and line number within the file of the corresponding
|
25 |
|
|
// call. The boolean ok is false if it was not possible to recover the information.
|
26 |
|
|
func Caller(skip int) (pc uintptr, file string, line int, ok bool)
|
27 |
|
|
|
28 |
|
|
// Callers fills the slice pc with the program counters of function invocations
|
29 |
|
|
// on the calling goroutine's stack. The argument skip is the number of stack frames
|
30 |
|
|
// to skip before recording in pc, with 0 starting at the caller of Callers.
|
31 |
|
|
// It returns the number of entries written to pc.
|
32 |
|
|
func Callers(skip int, pc []uintptr) int
|
33 |
|
|
|
34 |
|
|
type Func struct { // Keep in sync with runtime.h:struct Func
|
35 |
|
|
name string
|
36 |
|
|
typ string // go type string
|
37 |
|
|
src string // src file name
|
38 |
|
|
pcln []byte // pc/ln tab for this func
|
39 |
|
|
entry uintptr // entry pc
|
40 |
|
|
pc0 uintptr // starting pc, ln for table
|
41 |
|
|
ln0 int32
|
42 |
|
|
frame int32 // stack frame size
|
43 |
|
|
args int32 // number of 32-bit in/out args
|
44 |
|
|
locals int32 // number of 32-bit locals
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
// FuncForPC returns a *Func describing the function that contains the
|
48 |
|
|
// given program counter address, or else nil.
|
49 |
|
|
func FuncForPC(pc uintptr) *Func
|
50 |
|
|
|
51 |
|
|
// Name returns the name of the function.
|
52 |
|
|
func (f *Func) Name() string { return f.name }
|
53 |
|
|
|
54 |
|
|
// Entry returns the entry address of the function.
|
55 |
|
|
func (f *Func) Entry() uintptr { return f.entry }
|
56 |
|
|
|
57 |
|
|
// FileLine returns the file name and line number of the
|
58 |
|
|
// source code corresponding to the program counter pc.
|
59 |
|
|
// The result will not be accurate if pc is not a program
|
60 |
|
|
// counter within f.
|
61 |
|
|
func (f *Func) FileLine(pc uintptr) (file string, line int) {
|
62 |
|
|
return funcline_go(f, pc)
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
// implemented in symtab.c
|
66 |
|
|
func funcline_go(*Func, uintptr) (string, int)
|
67 |
|
|
|
68 |
|
|
// mid returns the current os thread (m) id.
|
69 |
|
|
func mid() uint32
|
70 |
|
|
|
71 |
|
|
// Semacquire waits until *s > 0 and then atomically decrements it.
|
72 |
|
|
// It is intended as a simple sleep primitive for use by the synchronization
|
73 |
|
|
// library and should not be used directly.
|
74 |
|
|
func Semacquire(s *uint32)
|
75 |
|
|
|
76 |
|
|
// Semrelease atomically increments *s and notifies a waiting goroutine
|
77 |
|
|
// if one is blocked in Semacquire.
|
78 |
|
|
// It is intended as a simple wakeup primitive for use by the synchronization
|
79 |
|
|
// library and should not be used directly.
|
80 |
|
|
func Semrelease(s *uint32)
|
81 |
|
|
|
82 |
|
|
// SetFinalizer sets the finalizer associated with x to f.
|
83 |
|
|
// When the garbage collector finds an unreachable block
|
84 |
|
|
// with an associated finalizer, it clears the association and runs
|
85 |
|
|
// f(x) in a separate goroutine. This makes x reachable again, but
|
86 |
|
|
// now without an associated finalizer. Assuming that SetFinalizer
|
87 |
|
|
// is not called again, the next time the garbage collector sees
|
88 |
|
|
// that x is unreachable, it will free x.
|
89 |
|
|
//
|
90 |
|
|
// SetFinalizer(x, nil) clears any finalizer associated with x.
|
91 |
|
|
//
|
92 |
|
|
// The argument x must be a pointer to an object allocated by
|
93 |
|
|
// calling new or by taking the address of a composite literal.
|
94 |
|
|
// The argument f must be a function that takes a single argument
|
95 |
|
|
// of x's type and can have arbitrary ignored return values.
|
96 |
|
|
// If either of these is not true, SetFinalizer aborts the program.
|
97 |
|
|
//
|
98 |
|
|
// Finalizers are run in dependency order: if A points at B, both have
|
99 |
|
|
// finalizers, and they are otherwise unreachable, only the finalizer
|
100 |
|
|
// for A runs; once A is freed, the finalizer for B can run.
|
101 |
|
|
// If a cyclic structure includes a block with a finalizer, that
|
102 |
|
|
// cycle is not guaranteed to be garbage collected and the finalizer
|
103 |
|
|
// is not guaranteed to run, because there is no ordering that
|
104 |
|
|
// respects the dependencies.
|
105 |
|
|
//
|
106 |
|
|
// The finalizer for x is scheduled to run at some arbitrary time after
|
107 |
|
|
// x becomes unreachable.
|
108 |
|
|
// There is no guarantee that finalizers will run before a program exits,
|
109 |
|
|
// so typically they are useful only for releasing non-memory resources
|
110 |
|
|
// associated with an object during a long-running program.
|
111 |
|
|
// For example, an os.File object could use a finalizer to close the
|
112 |
|
|
// associated operating system file descriptor when a program discards
|
113 |
|
|
// an os.File without calling Close, but it would be a mistake
|
114 |
|
|
// to depend on a finalizer to flush an in-memory I/O buffer such as a
|
115 |
|
|
// bufio.Writer, because the buffer would not be flushed at program exit.
|
116 |
|
|
//
|
117 |
|
|
// A single goroutine runs all finalizers for a program, sequentially.
|
118 |
|
|
// If a finalizer must run for a long time, it should do so by starting
|
119 |
|
|
// a new goroutine.
|
120 |
|
|
func SetFinalizer(x, f interface{})
|
121 |
|
|
|
122 |
|
|
func getgoroot() string
|
123 |
|
|
|
124 |
|
|
// GOROOT returns the root of the Go tree.
|
125 |
|
|
// It uses the GOROOT environment variable, if set,
|
126 |
|
|
// or else the root used during the Go build.
|
127 |
|
|
func GOROOT() string {
|
128 |
|
|
s := getgoroot()
|
129 |
|
|
if s != "" {
|
130 |
|
|
return s
|
131 |
|
|
}
|
132 |
|
|
return defaultGoroot
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
// Version returns the Go tree's version string.
|
136 |
|
|
// It is either a sequence number or, when possible,
|
137 |
|
|
// a release tag like "release.2010-03-04".
|
138 |
|
|
// A trailing + indicates that the tree had local modifications
|
139 |
|
|
// at the time of the build.
|
140 |
|
|
func Version() string {
|
141 |
|
|
return theVersion
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
// GOOS is the Go tree's operating system target:
|
145 |
|
|
// one of darwin, freebsd, linux, and so on.
|
146 |
|
|
const GOOS string = theGoos
|
147 |
|
|
|
148 |
|
|
// GOARCH is the Go tree's architecture target:
|
149 |
|
|
// 386, amd64, or arm.
|
150 |
|
|
const GOARCH string = theGoarch
|