Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created September 2, 2008 20:42
Show Gist options
  • Save tmm1/8461 to your computer and use it in GitHub Desktop.
Save tmm1/8461 to your computer and use it in GitHub Desktop.
Patch for MRI to disable thread timer (and improve performance) when no threads are running
--- eval.c.orig 2008-08-03 20:24:26.000000000 -0700
+++ eval.c 2008-09-02 14:03:15.000000000 -0700
@@ -10761,6 +10761,8 @@
#endif
}
+static int thread_init;
+
static void
rb_thread_restore_context(th, exit)
rb_thread_t th;
@@ -10801,6 +10803,12 @@
rb_thread_die(th);
th->prev->next = th->next;
th->next->prev = th->prev;
+
+ /* if this is the last ruby thread, stop timer signals */
+ if (th->next == th->prev && th->next == main_thread) {
+ rb_thread_stop_timer();
+ thread_init = 0;
+ }
}
static int
@@ -12090,8 +12098,6 @@
return th;
}
-static int thread_init;
-
#if defined(_THREAD_SAFE)
static void
catch_timer(sig)
$ strace -ttT ruby -rubygems -e "require 'eventmachine'; EM.run{ Thread.new{ 500_000.times{1} } }" 2>&1 | egrep '(sigret|setitimer)'
14:03:03.967429 setitimer(ITIMER_VIRTUAL, {it_interval={0, 10000}, it_value={0, 10000}}, NULL) = 0 <0.000006>
14:03:03.980404 rt_sigreturn(0x1a) = 139788857670280 <0.000005>
14:03:03.992394 rt_sigreturn(0x1a) = -1 EPERM (Operation not permitted) <0.000005>
14:03:04.004394 rt_sigreturn(0x1a) = 139788855520512 <0.000008>
14:03:04.016393 rt_sigreturn(0x1a) = 643135 <0.000007>
14:03:04.019187 setitimer(ITIMER_VIRTUAL, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0 <0.000007>
^C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment