-
Notifications
You must be signed in to change notification settings - Fork 952
Implement time.NewTimer and time.NewTicker #1402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package runtime | ||
|
||
// timerNode is an element in a linked list of timers. | ||
type timerNode struct { | ||
next *timerNode | ||
timer *timer | ||
callback func(*timerNode) | ||
} | ||
|
||
// whenTicks returns the (absolute) time when this timer should trigger next. | ||
func (t *timerNode) whenTicks() timeUnit { | ||
return nanosecondsToTicks(t.timer.when) | ||
} | ||
|
||
// Defined in the time package, implemented here in the runtime. | ||
//go:linkname startTimer time.startTimer | ||
func startTimer(tim *timer) { | ||
addTimer(&timerNode{ | ||
timer: tim, | ||
callback: timerCallback, | ||
}) | ||
scheduleLog("adding timer") | ||
} | ||
|
||
// timerCallback is called when a timer expires. It makes sure to call the | ||
// callback in the time package and to re-add the timer to the queue if this is | ||
// a ticker (repeating timer). | ||
// This is intentionally used as a callback and not a direct call (even though a | ||
// direct call would be trivial), because otherwise a circular dependency | ||
// between scheduler, addTimer and timerQueue would form. Such a circular | ||
// dependency causes timerQueue not to get optimized away. | ||
// If timerQueue doesn't get optimized away, small programs (that don't call | ||
// time.NewTimer etc) would still pay the cost of these timers. | ||
func timerCallback(tn *timerNode) { | ||
// Run timer function (implemented in the time package). | ||
// The seq parameter to the f function is not used in the time | ||
// package so is left zero. | ||
tn.timer.f(tn.timer.arg, 0) | ||
|
||
// If this is a periodic timer (a ticker), re-add it to the queue. | ||
if tn.timer.period != 0 { | ||
tn.timer.when += tn.timer.period | ||
addTimer(tn) | ||
} | ||
} | ||
|
||
//go:linkname stopTimer time.stopTimer | ||
func stopTimer(tim *timer) bool { | ||
return removeTimer(tim) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// +build !go1.14 | ||
|
||
package runtime | ||
|
||
// Runtime timer, must be kept in sync with src/time/sleep.go of the Go stdlib. | ||
// The layout changed in Go 1.14, so this is only supported on Go 1.13 and | ||
// below. | ||
// | ||
// The fields used by the time package are: | ||
// when: time to wake up (in nanoseconds) | ||
// period: if not 0, a repeating time (of the given nanoseconds) | ||
// f: the function called when the timer expires | ||
// arg: parameter to f | ||
type timer struct { | ||
tb uintptr | ||
i int | ||
|
||
when int64 | ||
period int64 | ||
f func(interface{}, uintptr) | ||
arg interface{} | ||
seq uintptr | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// +build go1.14 | ||
|
||
package runtime | ||
|
||
// Runtime timer, must be kept in sync with src/time/sleep.go of the Go stdlib. | ||
// The layout changed in Go 1.14, so this is only supported on Go 1.14 and | ||
// above. | ||
// | ||
// The fields used by the time package are: | ||
// when: time to wake up (in nanoseconds) | ||
// period: if not 0, a repeating time (of the given nanoseconds) | ||
// f: the function called when the timer expires | ||
// arg: parameter to f | ||
type timer struct { | ||
pp uintptr | ||
when int64 | ||
period int64 | ||
f func(interface{}, uintptr) | ||
arg interface{} | ||
seq uintptr | ||
nextwhen int64 | ||
status uint32 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is really safe with coroutine lowering right now?
Any user code can define a function value of the type
func(interface{}, uintptr)
which I think is sufficient to break this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid you're right. I'll have to think a bit about how to fix this. It's not trivial, as the signature is set by the time package.
Thank you for taking a look. I hadn't thought of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So on coroutines we should do
go tn.callback(tn)
. I dunno, there is probably a way to improve this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, nevermind, you fixed it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I didn't, I haven't really thought about how to do this in a way that works with the coroutines scheduler.