Debian Codegraph @

src/runtime/os_wasm.go

raw
1 // Copyright 2023 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 "internal/runtime/atomic"
9 "unsafe"
10 )
11
12 func osinit() {
13 // https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances
14 physPageSize = 64 * 1024
15 initBloc()
16 blocMax = uintptr(currentMemory()) * physPageSize // record the initial linear memory size
17 numCPUStartup = getCPUCount()
18 getg().m.procid = 2
19 }
20
21 func getCPUCount() int32 {
22 return 1
23 }
24
25 const _SIGSEGV = 0xb
26
27 func sigpanic() {
28 gp := getg()
29 if !canpanic() {
30 throw("unexpected signal during runtime execution")
31 }
32
33 // js only invokes the exception handler for memory faults.
34 gp.sig = _SIGSEGV
35 panicmem()
36 }
37
38 // func exitThread(wait *uint32)
39 // FIXME: wasm doesn't have atomic yet
40 func exitThread(wait *atomic.Uint32)
41
42 type mOS struct{}
43
44 func osyield()
45
46 //go:nosplit
47 func osyield_no_g() {
48 osyield()
49 }
50
51 type sigset struct{}
52
53 // Called to initialize a new m (including the bootstrap m).
54 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
55 func mpreinit(mp *m) {
56 mp.gsignal = malg(32 * 1024)
57 mp.gsignal.m = mp
58 }
59
60 //go:nosplit
61 func usleep_no_g(usec uint32) {
62 usleep(usec)
63 }
64
65 //go:nosplit
66 func sigsave(p *sigset) {
67 }
68
69 //go:nosplit
70 func msigrestore(sigmask sigset) {
71 }
72
73 //go:nosplit
74 //go:nowritebarrierrec
75 func clearSignalHandlers() {
76 }
77
78 //go:nosplit
79 func sigblock(exiting bool) {
80 }
81
82 // Called to initialize a new m (including the bootstrap m).
83 // Called on the new thread, cannot allocate memory.
84 func minit() {
85 }
86
87 // Called from dropm to undo the effect of an minit.
88 func unminit() {
89 }
90
91 // Called from exitm, but not from drop, to undo the effect of thread-owned
92 // resources in minit, semacreate, or elsewhere. Do not take locks after calling this.
93 func mdestroy(mp *m) {
94 }
95
96 // wasm has no signals
97 const _NSIG = 0
98
99 func signame(sig uint32) string {
100 return ""
101 }
102
103 func crash() {
104 abort()
105 }
106
107 func initsig(preinit bool) {
108 }
109
110 // May run with m.p==nil, so write barriers are not allowed.
111 //
112 //go:nowritebarrier
113 func newosproc(mp *m) {
114 throw("newosproc: not implemented")
115 }
116
117 // Do nothing on WASM platform, always return EPIPE to caller.
118 //
119 //go:linkname os_sigpipe os.sigpipe
120 func os_sigpipe() {}
121
122 //go:linkname syscall_now syscall.now
123 func syscall_now() (sec int64, nsec int32) {
124 sec, nsec, _ = time_now()
125 return
126 }
127
128 //go:nosplit
129 func cputicks() int64 {
130 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
131 return nanotime()
132 }
133
134 // gsignalStack is unused on js.
135 type gsignalStack struct{}
136
137 const preemptMSupported = false
138
139 func preemptM(mp *m) {
140 // No threads, so nothing to do.
141 }
142
143 // getfp returns the frame pointer register of its caller or 0 if not implemented.
144 // TODO: Make this a compiler intrinsic
145 //
146 //go:nosplit
147 func getfp() uintptr { return 0 }
148
149 func setProcessCPUProfiler(hz int32) {}
150 func setThreadCPUProfiler(hz int32) {}
151 func sigdisable(uint32) {}
152 func sigenable(uint32) {}
153 func sigignore(uint32) {}
154
155 // Stubs so tests can link correctly. These should never be called.
156 func open(name *byte, mode, perm int32) int32 { panic("not implemented") }
157 func closefd(fd int32) int32 { panic("not implemented") }
158 func read(fd int32, p unsafe.Pointer, n int32) int32 { panic("not implemented") }