github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/kernel/time/util.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package time 16 17 import ( 18 "sync" 19 "time" 20 ) 21 22 // AfterFunc waits for duration to elapse according to clock then runs fn. 23 // The timer is started immediately and will fire exactly once. 24 func AfterFunc(clock Clock, duration time.Duration, fn func()) *VariableTimer { 25 timer := &VariableTimer{ 26 clock: clock, 27 } 28 timer.notifier = functionNotifier{ 29 fn: func() { 30 // tcpip.Timer.Stop() explicitly states that the function is called in a 31 // separate goroutine that Stop() does not synchronize with. 32 // Timer.Destroy() synchronizes with calls to Listener.NotifyTimer(). 33 // This is semantically meaningful because, in the former case, it's 34 // legal to call tcpip.Timer.Stop() while holding locks that may also be 35 // taken by the function, but this isn't so in the latter case. Most 36 // immediately, Timer calls Listener.NotifyTimer() while holding 37 // Timer.mu. A deadlock occurs without spawning a goroutine: 38 // T1: (Timer expires) 39 // => Timer.Tick() <- Timer.mu.Lock() called 40 // => Listener.NotifyTimer() 41 // => Timer.Stop() 42 // => Timer.Destroy() <- Timer.mu.Lock() called, deadlock! 43 // 44 // Spawning a goroutine avoids the deadlock: 45 // T1: (Timer expires) 46 // => Timer.Tick() <- Timer.mu.Lock() called 47 // => Listener.NotifyTimer() <- Launches T2 48 // T2: 49 // => Timer.Stop() 50 // => Timer.Destroy() <- Timer.mu.Lock() called, blocks 51 // T1: 52 // => (returns) <- Timer.mu.Unlock() called 53 // T2: 54 // => (continues) <- No deadlock! 55 go func() { 56 timer.Stop() 57 fn() 58 }() 59 }, 60 } 61 timer.Reset(duration) 62 return timer 63 } 64 65 // VariableTimer is a resettable timer with variable duration expirations. 66 // Implements tcpip.Timer, which does not define a Destroy method; instead, all 67 // resources are released after timer expiration and calls to Timer.Stop. 68 // 69 // Must be created by AfterFunc. 70 type VariableTimer struct { 71 // clock is the time source. clock is immutable. 72 clock Clock 73 74 // notifier is called when the Timer expires. notifier is immutable. 75 notifier functionNotifier 76 77 // mu protects t. 78 mu sync.Mutex 79 80 // t stores the latest running Timer. This is replaced whenever Reset is 81 // called since Timer cannot be restarted once it has been Destroyed by Stop. 82 // 83 // This field is nil iff Stop has been called. 84 t *Timer 85 } 86 87 // Stop implements tcpip.Timer.Stop. 88 func (r *VariableTimer) Stop() bool { 89 r.mu.Lock() 90 defer r.mu.Unlock() 91 92 if r.t == nil { 93 return false 94 } 95 _, lastSetting := r.t.Swap(Setting{}) 96 r.t.Destroy() 97 r.t = nil 98 return lastSetting.Enabled 99 } 100 101 // Reset implements tcpip.Timer.Reset. 102 func (r *VariableTimer) Reset(d time.Duration) { 103 r.mu.Lock() 104 defer r.mu.Unlock() 105 106 if r.t == nil { 107 r.t = NewTimer(r.clock, &r.notifier) 108 } 109 110 r.t.Swap(Setting{ 111 Enabled: true, 112 Period: 0, 113 Next: r.clock.Now().Add(d), 114 }) 115 } 116 117 // functionNotifier is a TimerListener that runs a function. 118 // 119 // functionNotifier cannot be saved or loaded. 120 type functionNotifier struct { 121 fn func() 122 } 123 124 // NotifyTimer implements ktime.TimerListener.NotifyTimer. 125 func (f *functionNotifier) NotifyTimer(uint64, Setting) (Setting, bool) { 126 f.fn() 127 return Setting{}, false 128 }