Skip to content

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
wants to merge 2 commits into from
Closed

Implement time.NewTimer and time.NewTicker #1402

wants to merge 2 commits into from

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Sep 22, 2020

This has been requested before in #1037 and it is necessary for using the stdlib testing package. I have compared the code size before and after and except for very nontrivial cases the code size is the same (even with the scheduler additions). All examples in the drivers repo remain the same.

This solves a few issues:

 1. It avoids the nondeterministic order that was previously used to
    declare these types, as they were declared while iterating through a
    map.
 2. The next commit adds a runtime type that also includes standard Go
    types such as an interface and func value. If this type
    (runtime.timer) was found before runtime._interface for example, the
    runtime._interface type wasn't found triggering a panic.
 3. It might improve the performance of getLLVMRuntimeType as it does
    not involve a CGo call anymore.
timerQueue = tn.next
tn.next = nil
// Run the callback stored in this timer node.
tn.callback(tn)
Copy link
Member

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?

Copy link
Member Author

@aykevl aykevl Sep 22, 2020

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.

Copy link
Member

@niaow niaow Oct 2, 2020

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.

Copy link
Member

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?

Copy link
Member Author

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.

This commit adds support for time.NewTimer and time.NewTicker. It also
adds support for the Stop() method on time.Timer, but doesn't (yet) add
support for the Reset() method.

The implementation has been carefully written so that programs that
don't use these timers will normally not see an increase in RAM or
binary size. None of the examples in the drivers repo change as a result
of this commit. This comes at the cost of slightly more complex code and
possibly slower execution of the timers when they are used.
@dkegel-fastly
Copy link
Contributor

When asyncify lands might be a good time to revisit this...?

@deadprogram
Copy link
Member

When asyncify lands might be a good time to revisit this...?

@dkegel-fastly that is a very good question. @aykevl what do you think?

kenbell added a commit to kenbell/tinygo that referenced this pull request Jul 3, 2022
@sago35
Copy link
Member

sago35 commented Jul 22, 2022

@aykevl
Any progress on this?

Implementing time.startTimer may result in some of the following tests being ok
https://tinygo.org/docs/reference/lang-support/stdlib/

  • bufio
  • compress/gzip
  • encoding/base64
  • go/printer
  • image/jpeg
  • io
  • mime/quotedprintable

It would also be nice to be able to use time.After, time.Tick, etc.

@kenbell
Copy link
Member

kenbell commented Jul 22, 2022

@sago35 my port of @aykevl code seems to be working well to me. It's pretty much Ayke's code, so not sure if he wants to take it forward. If not, I think my PR is ready to review: #2954

@aykevl
Copy link
Member Author

aykevl commented Aug 20, 2022

Closing in favor of #2954

@aykevl aykevl closed this Aug 20, 2022
@aykevl aykevl deleted the timer-2 branch August 20, 2022 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants