mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 03:27:48 +00:00
runtime: add cheap atomic condition variable
This commit is contained in:
Vendored
+44
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -71,6 +72,8 @@ func main() {
|
||||
startSimpleFunc(emptyFunc)
|
||||
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
|
||||
testCond()
|
||||
}
|
||||
|
||||
func acquire(m *sync.Mutex) {
|
||||
@@ -127,3 +130,44 @@ type simpleFunc func()
|
||||
|
||||
func emptyFunc() {
|
||||
}
|
||||
|
||||
func testCond() {
|
||||
var cond runtime.Cond
|
||||
go func() {
|
||||
// Wait for the caller to wait on the cond.
|
||||
time.Sleep(time.Millisecond)
|
||||
|
||||
// Notify the caller.
|
||||
ok := cond.Notify()
|
||||
if !ok {
|
||||
panic("notification not sent")
|
||||
}
|
||||
|
||||
// This notification will be buffered inside the cond.
|
||||
ok = cond.Notify()
|
||||
if !ok {
|
||||
panic("notification not queued")
|
||||
}
|
||||
|
||||
// This notification should fail, since there is already one buffered.
|
||||
ok = cond.Notify()
|
||||
if ok {
|
||||
panic("notification double-sent")
|
||||
}
|
||||
}()
|
||||
|
||||
// Verify that the cond has no pending notifications.
|
||||
ok := cond.Poll()
|
||||
if ok {
|
||||
panic("unexpected early notification")
|
||||
}
|
||||
|
||||
// Wait for the goroutine spawned earlier to send a notification.
|
||||
cond.Wait()
|
||||
|
||||
// The goroutine should have also queued a notification in the cond.
|
||||
ok = cond.Poll()
|
||||
if !ok {
|
||||
panic("missing queued notification")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user