eclipse-archived / ceylon

The Ceylon compiler, language module, and command line tools
http://ceylon-lang.org
Apache License 2.0
399 stars 62 forks source link

system.sleep() #5474

Open CeylonMigrationBot opened 9 years ago

CeylonMigrationBot commented 9 years ago

[@gavinking] I think we should have a sleep() function in the language module. This can be implemented in both JS and Java, right?

[Migrated from ceylon/ceylon.language#596]

CeylonMigrationBot commented 9 years ago

[@tombentley] It can't be system.sleep() because it's not making the system sleep. It would have to be process.sleep(). But even then it's semantics on JVM are to make the current thread sleep which isn't quite the same thing. So I'm wondering how you'd specify this.

And how would you implement this on JS?

CeylonMigrationBot commented 9 years ago

[@gavinking]

And how would you implement this on JS?

Ah, sorry, you're right, I was thinking of using setTimeout() but that doesn't quite work. It would have to be implemented using a loop. Hrm.

renatoathaydes commented 8 years ago

It seems to me the appropriate platform-neutral approach to this would have to be to use a callback, so you do not assume the platform you're running on has Threads or similar:

sleep(1k, function() {
    // after one second, we continue
});

Seems quite reasonable to me. Unless you want to add continuations to the language!

gavinking commented 8 years ago

@renatoathaydes Indeed, we could do that. WDYT, @jvasileff, I assume that could be implemented on Dart, right?

renatoathaydes commented 8 years ago

The alternative is ugly and can be implemented in pure Ceylon :)

void sleep(Integer millis) {
    value ms => system.milliseconds;
    value end = ms + millis;
    while(ms < end) {}
}
jvasileff commented 8 years ago

Yeah, I believe that's

Future.delayed(Duration duration, [dynamic computation()]) Creates a future that runs its computation after a delay.

Would the task run on a separate thread on the JVM? If not, these seem like very different functions.

jvasileff commented 8 years ago

The alternative is ugly and can be implemented in pure Ceylon :)

:-1: my CPU run's hot enough already :smile:

renatoathaydes commented 8 years ago

On the JVM, the callback must run in the same Thread, otherwise you run into synchronization issues just for calling sleep.

jvasileff commented 8 years ago

Ok, but then on JVM it's blocking & precise, and on JS/Dart it's non-blocking and imprecise.

gavinking commented 8 years ago

Ugh. That's not great.

renatoathaydes commented 8 years ago

Yes, I thought about that... it's a problem if you block.... it must be non-blocking in any platform to be consistent, but the callback must run on the same Thread... kind of a contradiction.

Probably best to leave this to a small library that provides green Threads... I did start on this with the ConcurrenCey project but has been a while I updated that. I might get back to it soon... but it was Java-only... not sure if I can make it work in JS (and Dart).

renatoathaydes commented 8 years ago

my CPU run's hot enough already @jvasileff https://xkcd.com/1172/