Skip to content

Commit

Permalink
Only use idle callback when we have pending code
Browse files Browse the repository at this point in the history
While difficult to find hard documentation, I have seen a few different
places stating that "g_idle_add" is threadsafe. Operating on this
assumption, we add one idle callbacks for every piece of code we need to run,
and no more.

This fixes #5 and drops our idle CPU usage from 100% down to 0%
  • Loading branch information
retupmoca committed Mar 12, 2015
1 parent 55f2965 commit 8b2c996
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/GTK/Simple.pm6
Expand Up @@ -216,6 +216,8 @@ class GTK::Simple::Scheduler does Scheduler {
my class Queue is repr('ConcBlockingQueue') { }
my $queue := nqp::create(Queue);

my &idle_cb = sub ($a) { GTK::Simple::Scheduler.process_queue; return 0 };

method cue(&code, :$at, :$in, :$every, :$times, :&catch ) {
die "GTK::Simple::Scheduler does not support at" if $at;
die "GTK::Simple::Scheduler does not support in" if $in;
Expand All @@ -225,6 +227,7 @@ class GTK::Simple::Scheduler does Scheduler {
?? -> { code(); CATCH { default { catch($_) } } }
!! &code;
nqp::push($queue, &run);
g_idle_add(&idle_cb, OpaquePointer);
return Nil;
}

Expand Down Expand Up @@ -340,9 +343,6 @@ class GTK::Simple::App does GTK::Simple::Widget

method run() {
gtk_widget_show($!gtk_widget);
g_idle_add(
sub ($a) { GTK::Simple::Scheduler.process_queue; return 1 },
OpaquePointer);
gtk_main();
}

Expand Down

0 comments on commit 8b2c996

Please sign in to comment.