Refactor BlockWithTimer() to reduce duplicated code.

The users of Task.BlockWithTimer() are doing the same work.

PiperOrigin-RevId: 610358579
This commit is contained in:
Ayush Ranjan
2024-02-26 03:55:27 -08:00
committed by gVisor bot
parent 16dba7fa76
commit fa0adadccb
3 changed files with 16 additions and 30 deletions
+13 -5
View File
@@ -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)
}
+2 -16
View File
@@ -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)
+1 -9
View File
@@ -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 {