...

Source file src/runtime/os_wasm.go

Documentation: runtime

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

View as plain text