mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Refactor BlockWithTimer() to reduce duplicated code.
The users of Task.BlockWithTimer() are doing the same work. PiperOrigin-RevId: 610358579
This commit is contained in:
@@ -102,15 +102,23 @@ func (t *Task) BlockWithDeadline(C <-chan struct{}, haveDeadline bool, deadline
|
||||
return err
|
||||
}
|
||||
|
||||
// BlockWithTimer blocks t until an event is received from C or tchan, or t is
|
||||
// interrupted. It returns nil if an event is received from C, ETIMEDOUT if an
|
||||
// event is received from tchan, and linuxerr.ErrInterrupted if t is
|
||||
// interrupted.
|
||||
// BlockWithDeadlineFrom is similar to BlockWithDeadline, except it uses the
|
||||
// passed clock (instead of application monotonic clock).
|
||||
//
|
||||
// Most clients should use BlockWithDeadline or BlockWithTimeout instead.
|
||||
//
|
||||
// Preconditions: The caller must be running on the task goroutine.
|
||||
func (t *Task) BlockWithTimer(C <-chan struct{}, tchan <-chan struct{}) error {
|
||||
func (t *Task) BlockWithDeadlineFrom(C <-chan struct{}, clock ktime.Clock, haveDeadline bool, deadline ktime.Time) error {
|
||||
if !haveDeadline {
|
||||
return t.block(C, nil)
|
||||
}
|
||||
notifier, tchan := ktime.NewChannelNotifier()
|
||||
timer := ktime.NewTimer(clock, notifier)
|
||||
timer.Swap(ktime.Setting{
|
||||
Enabled: true,
|
||||
Next: deadline,
|
||||
})
|
||||
defer timer.Destroy()
|
||||
return t.block(C, tchan)
|
||||
}
|
||||
|
||||
|
||||
@@ -61,14 +61,7 @@ func futexWaitAbsolute(t *kernel.Task, clockRealtime bool, ts linux.Timespec, fo
|
||||
if forever {
|
||||
err = t.Block(w.C)
|
||||
} else if clockRealtime {
|
||||
notifier, tchan := ktime.NewChannelNotifier()
|
||||
timer := ktime.NewTimer(t.Kernel().RealtimeClock(), notifier)
|
||||
timer.Swap(ktime.Setting{
|
||||
Enabled: true,
|
||||
Next: ktime.FromTimespec(ts),
|
||||
})
|
||||
err = t.BlockWithTimer(w.C, tchan)
|
||||
timer.Destroy()
|
||||
err = t.BlockWithDeadlineFrom(w.C, t.Kernel().RealtimeClock(), true, ktime.FromTimespec(ts))
|
||||
} else {
|
||||
err = t.BlockWithDeadline(w.C, true, ktime.FromTimespec(ts))
|
||||
}
|
||||
@@ -138,14 +131,7 @@ func futexLockPI(t *kernel.Task, ts linux.Timespec, forever bool, addr hostarch.
|
||||
if forever {
|
||||
err = t.Block(w.C)
|
||||
} else {
|
||||
notifier, tchan := ktime.NewChannelNotifier()
|
||||
timer := ktime.NewTimer(t.Kernel().RealtimeClock(), notifier)
|
||||
timer.Swap(ktime.Setting{
|
||||
Enabled: true,
|
||||
Next: ktime.FromTimespec(ts),
|
||||
})
|
||||
err = t.BlockWithTimer(w.C, tchan)
|
||||
timer.Destroy()
|
||||
err = t.BlockWithDeadlineFrom(w.C, t.Kernel().RealtimeClock(), true, ktime.FromTimespec(ts))
|
||||
}
|
||||
|
||||
t.Futex().WaitComplete(w, t)
|
||||
|
||||
@@ -245,15 +245,7 @@ func clockNanosleepUntil(t *kernel.Task, c ktime.Clock, end ktime.Time, rem host
|
||||
if c == t.Kernel().MonotonicClock() {
|
||||
err = t.BlockWithDeadline(nil, true, end)
|
||||
} else {
|
||||
notifier, tchan := ktime.NewChannelNotifier()
|
||||
timer := ktime.NewTimer(c, notifier)
|
||||
timer.Swap(ktime.Setting{
|
||||
Period: 0,
|
||||
Enabled: true,
|
||||
Next: end,
|
||||
})
|
||||
err = t.BlockWithTimer(nil, tchan)
|
||||
timer.Destroy()
|
||||
err = t.BlockWithDeadlineFrom(nil, c, true, end)
|
||||
}
|
||||
|
||||
switch {
|
||||
|
||||
Reference in New Issue
Block a user