1 // Copyright 2018 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.
9 import _ "unsafe" // for go:linkname
11 // js/wasm has no support for threads yet. There is no preemption.
27 lockWithRank(l, getLockRank(l))
30 func lock2(l *mutex) {
31 if l.key == mutex_locked {
32 // js/wasm is single-threaded so we should never
34 throw("self deadlock")
44 func unlock(l *mutex) {
48 func unlock2(l *mutex) {
49 if l.key == mutex_unlocked {
50 throw("unlock of unlocked lock")
57 l.key = mutex_unlocked
60 // One-time notifications.
62 type noteWithTimeout struct {
68 notes = make(map[*note]*g)
69 notesWithTimeout = make(map[*note]noteWithTimeout)
72 func noteclear(n *note) {
76 func notewakeup(n *note) {
78 if n.key == note_woken {
79 throw("notewakeup - double wakeup")
81 cleared := n.key == note_cleared
88 func notesleep(n *note) {
89 throw("notesleep not supported by js")
92 func notetsleep(n *note, ns int64) bool {
93 throw("notetsleep not supported by js")
97 // same as runtimeĀ·notetsleep, but called on user g (not g0)
98 func notetsleepg(n *note, ns int64) bool {
101 throw("notetsleepg on g0")
105 deadline := nanotime() + ns
106 delay := ns/1000000 + 1 // round up
108 delay = 1<<31 - 1 // cap to max int32
111 id := scheduleTimeoutEvent(delay)
114 notesWithTimeout[n] = noteWithTimeout{gp: gp, deadline: deadline}
117 gopark(nil, nil, waitReasonSleep, traceBlockSleep, 1)
119 clearTimeoutEvent(id) // note might have woken early, clear timeout
124 delete(notesWithTimeout, n)
127 return n.key == note_woken
130 for n.key != note_woken {
135 gopark(nil, nil, waitReasonZero, traceBlockGeneric, 1)
144 // checkTimeouts resumes goroutines that are waiting on a note which has reached its deadline.
145 // TODO(drchase): need to understand if write barriers are really okay in this context.
147 //go:yeswritebarrierrec
148 func checkTimeouts() {
150 // TODO: map iteration has the write barriers in it; is that okay?
151 for n, nt := range notesWithTimeout {
152 if n.key == note_cleared && now >= nt.deadline {
159 // events is a stack of calls from JavaScript into Go.
163 // g was the active goroutine when the call from JavaScript occurred.
164 // It needs to be active when returning to JavaScript.
166 // returned reports whether the event handler has returned.
167 // When all goroutines are idle and the event handler has returned,
168 // then g gets resumed and returns the execution to JavaScript.
172 // The timeout event started by beforeIdle.
175 // beforeIdle gets called by the scheduler if no goroutine is awake.
176 // If we are not already handling an event, then we pause for an async event.
177 // If an event handler returned, we resume it and it will pause the execution.
178 // beforeIdle either returns the specific goroutine to schedule next or
179 // indicates with otherReady that some goroutine became ready.
180 // TODO(drchase): need to understand if write barriers are really okay in this context.
182 //go:yeswritebarrierrec
183 func beforeIdle(now, pollUntil int64) (gp *g, otherReady bool) {
186 delay = pollUntil - now
193 } else if delay < 1e15 {
196 // An arbitrary cap on how long to wait for a timer.
197 // 1e9 ms == ~11.5 days.
200 idleID = scheduleTimeoutEvent(delay)
203 if len(events) == 0 {
204 // TODO: this is the line that requires the yeswritebarrierrec
205 go handleAsyncEvent()
209 e := events[len(events)-1]
216 func handleAsyncEvent() {
217 pause(getcallersp() - 16)
220 // clearIdleID clears our record of the timeout started by beforeIdle.
223 clearTimeoutEvent(idleID)
228 // pause sets SP to newsp and pauses the execution of Go's WebAssembly code until an event is triggered.
229 func pause(newsp uintptr)
231 // scheduleTimeoutEvent tells the WebAssembly environment to trigger an event after ms milliseconds.
232 // It returns a timer id that can be used with clearTimeoutEvent.
234 //go:wasmimport gojs runtime.scheduleTimeoutEvent
235 func scheduleTimeoutEvent(ms int64) int32
237 // clearTimeoutEvent clears a timeout event scheduled by scheduleTimeoutEvent.
239 //go:wasmimport gojs runtime.clearTimeoutEvent
240 func clearTimeoutEvent(id int32)
242 // handleEvent gets invoked on a call from JavaScript into Go. It calls the event handler of the syscall/js package
243 // and then parks the handler goroutine to allow other goroutines to run before giving execution back to JavaScript.
244 // When no other goroutine is awake any more, beforeIdle resumes the handler goroutine. Now that the same goroutine
245 // is running as was running when the call came in from JavaScript, execution can be safely passed back to JavaScript.
251 events = append(events, e)
257 // wait until all goroutines are idle
259 gopark(nil, nil, waitReasonZero, traceBlockGeneric, 1)
261 events[len(events)-1] = nil
262 events = events[:len(events)-1]
264 // return execution to JavaScript
265 pause(getcallersp() - 16)
268 var eventHandler func()
270 //go:linkname setEventHandler syscall/js.setEventHandler
271 func setEventHandler(fn func()) {