github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/timer/sleep.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package timer
     4  
     5  import (
     6  	"github.com/GeniusesGroup/libgo/protocol"
     7  	"github.com/GeniusesGroup/libgo/scheduler"
     8  )
     9  
    10  // Sleep pauses the execution of the current goroutine for at least the duration d.
    11  // A negative or zero duration causes Sleep to return immediately.
    12  func Sleep(d protocol.Duration) {
    13  	if d <= 0 {
    14  		return
    15  	}
    16  
    17  	var thread = scheduler.ActiveThread()
    18  	var timer Async
    19  	timer.Init(thread)
    20  	timer.Start(d)
    21  
    22  	// TODO::: if timer is fire before we yield its thread??
    23  	thread.Yield(scheduler.Thread_WaitReason_Sleep)
    24  }