]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/runtime.go
internal/godebug: define more efficient API
[gostls13.git] / src / runtime / runtime.go
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 package runtime
6
7 import (
8         "runtime/internal/atomic"
9         "unsafe"
10 )
11
12 //go:generate go run wincallback.go
13 //go:generate go run mkduff.go
14 //go:generate go run mkfastlog2table.go
15 //go:generate go run mklockrank.go -o lockrank.go
16
17 var ticks ticksType
18
19 type ticksType struct {
20         lock mutex
21         val  atomic.Int64
22 }
23
24 // Note: Called by runtime/pprof in addition to runtime code.
25 func tickspersecond() int64 {
26         r := ticks.val.Load()
27         if r != 0 {
28                 return r
29         }
30         lock(&ticks.lock)
31         r = ticks.val.Load()
32         if r == 0 {
33                 t0 := nanotime()
34                 c0 := cputicks()
35                 usleep(100 * 1000)
36                 t1 := nanotime()
37                 c1 := cputicks()
38                 if t1 == t0 {
39                         t1++
40                 }
41                 r = (c1 - c0) * 1000 * 1000 * 1000 / (t1 - t0)
42                 if r == 0 {
43                         r++
44                 }
45                 ticks.val.Store(r)
46         }
47         unlock(&ticks.lock)
48         return r
49 }
50
51 var envs []string
52 var argslice []string
53
54 //go:linkname syscall_runtime_envs syscall.runtime_envs
55 func syscall_runtime_envs() []string { return append([]string{}, envs...) }
56
57 //go:linkname syscall_Getpagesize syscall.Getpagesize
58 func syscall_Getpagesize() int { return int(physPageSize) }
59
60 //go:linkname os_runtime_args os.runtime_args
61 func os_runtime_args() []string { return append([]string{}, argslice...) }
62
63 //go:linkname syscall_Exit syscall.Exit
64 //go:nosplit
65 func syscall_Exit(code int) {
66         exit(int32(code))
67 }
68
69 var godebugDefault string
70 var godebugUpdate atomic.Pointer[func(string, string)]
71 var godebugEnv atomic.Pointer[string] // set by parsedebugvars
72
73 //go:linkname godebug_setUpdate internal/godebug.setUpdate
74 func godebug_setUpdate(update func(string, string)) {
75         p := new(func(string, string))
76         *p = update
77         godebugUpdate.Store(p)
78         godebugNotify()
79 }
80
81 func godebugNotify() {
82         if update := godebugUpdate.Load(); update != nil {
83                 var env string
84                 if p := godebugEnv.Load(); p != nil {
85                         env = *p
86                 }
87                 (*update)(godebugDefault, env)
88         }
89 }
90
91 //go:linkname syscall_runtimeSetenv syscall.runtimeSetenv
92 func syscall_runtimeSetenv(key, value string) {
93         setenv_c(key, value)
94         if key == "GODEBUG" {
95                 p := new(string)
96                 *p = value
97                 godebugEnv.Store(p)
98                 godebugNotify()
99         }
100 }
101
102 //go:linkname syscall_runtimeUnsetenv syscall.runtimeUnsetenv
103 func syscall_runtimeUnsetenv(key string) {
104         unsetenv_c(key)
105         if key == "GODEBUG" {
106                 godebugEnv.Store(nil)
107                 godebugNotify()
108         }
109 }
110
111 // writeErrStr writes a string to descriptor 2.
112 //
113 //go:nosplit
114 func writeErrStr(s string) {
115         write(2, unsafe.Pointer(unsafe.StringData(s)), int32(len(s)))
116 }