Source file src/runtime/os_wasm.go

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

View as plain text