github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/timer/sleep.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package timer 4 5 import ( 6 "unsafe" 7 8 "../protocol" 9 "../scheduler" 10 ) 11 12 // Sleep pauses the execution of the current goroutine for at least the duration d. 13 // A negative or zero duration causes Sleep to return immediately. 14 func Sleep(d protocol.Duration) { 15 if d <= 0 { 16 return 17 } 18 19 var thread = scheduler.ActiveThread() 20 var timer Timer 21 timer.Init(goroutineReady, thread) 22 timer.Start(d) 23 24 // TODO::: Decide to park or sleep?? 25 // gopark(t.resetForSleep, unsafe.Pointer(t), waitReasonSleep, traceEvGoSleep, 1) 26 thread.Sleep(scheduler.ThreadWaitReason_Sleep) 27 } 28 29 // resetForSleep is called after the goroutine is parked for timeSleep. 30 // We can't call resettimer in timeSleep itself because if this is a short 31 // sleep and there are many goroutines then the P can wind up running the 32 // timer function, goroutineReady, before the goroutine has been parked. 33 func resetForSleep(gp *g, ut unsafe.Pointer) bool { 34 var t = (*Timer)(ut) 35 resettimer(t, t.when) 36 return true 37 } 38 39 // Ready the goroutine arg. 40 func goroutineReady(arg any) { 41 var thread = arg.(*scheduler.Thread) 42 thread.Ready(0) 43 }