about summary refs log tree commit homepage
path: root/lib/rainbows/worker_yield.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-27 02:43:44 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-27 02:43:44 +0000
commit94b848a8f9120bce8b0abd776b1a9b7e2f4fa30d (patch)
tree672d1f677360e849bb4bf5200dc87ec266f701dc /lib/rainbows/worker_yield.rb
parenta310302708faa19042282e94525544cfbb23eba5 (diff)
downloadrainbows-94b848a8f9120bce8b0abd776b1a9b7e2f4fa30d.tar.gz
This lets Rainbows! yield the current worker process
when busy in the hopes another worker will pick up the
slack.  We can also override this for the single worker
process case later if people care enough.
Diffstat (limited to 'lib/rainbows/worker_yield.rb')
-rw-r--r--lib/rainbows/worker_yield.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/rainbows/worker_yield.rb b/lib/rainbows/worker_yield.rb
new file mode 100644
index 0000000..ec2a289
--- /dev/null
+++ b/lib/rainbows/worker_yield.rb
@@ -0,0 +1,15 @@
+# :enddoc:
+module Rainbows::WorkerYield
+
+  # Sleep if we're busy (and let other threads run).  Another less busy
+  # worker process may take it for us if we sleep. This is gross but
+  # other options still suck because they require expensive/complicated
+  # synchronization primitives for _every_ case, not just this unlikely
+  # one.  Since this case is (or should be) uncommon, just busy wait
+  # when we have to.  We don't use Thread.pass because it needlessly
+  # spins the CPU during I/O wait, CPU cycles that can be better used by
+  # other worker _processes_.
+  def worker_yield
+    sleep(0.01)
+  end
+end