Skip to content

Instantly share code, notes, and snippets.

View tmm1's full-sized avatar

Aman Karmani tmm1

View GitHub Profile
@tmm1
tmm1 / gist:48802
Created January 18, 2009 23:12
Continuation based Fiber implementation for Ruby 1.8
class Fiber
def initialize &blk
unless @yield = callcc{|c| c }
@ret = blk.call(@ret)
@resume.call
end
end
def resume *args
if @resume = callcc{|c| c }
$ cat fbbench.rb
FIBER_NUM = 503
number = ARGV.first.to_i
threads = []
for i in 1..FIBER_NUM
threads << Fiber.new do |num|
while true
Fiber.yield
if number > 0
@tmm1
tmm1 / gist:52671
Created January 26, 2009 02:56
Fiber implementation for Ruby 1.8.7
diff --git a/eval.c b/eval.c
index 11264f7..01d127b 100644
--- a/eval.c
+++ b/eval.c
@@ -1026,6 +1026,7 @@ static struct tag *prot_tag;
#define PROT_LOOP INT2FIX(1) /* 3 */
#define PROT_LAMBDA INT2FIX(2) /* 5 */
#define PROT_YIELD INT2FIX(3) /* 7 */
+#define PROT_FIBER INT2FIX(4) /* 9 */
git clone git://github.com/tmm1/ruby187
cd ruby187
git checkout -b fibers origin/fibers
CFLAGS="-O2 -ggdb" ./configure --disable-pthread --prefix=/tmp/ruby-with-fibers
make && sudo make install
/tmp/ruby-with-fibers/bin/ruby test/test_fiber.rb
cd /tmp
wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
tar zxvf rubygems-1.3.1.tgz
require 'rubygems'
require 'sinatra'
# eventmachine/thin
require 'eventmachine'
require 'thin'
# mysql
require 'mysqlplus'
##
# sync api: return values and exceptions
begin
images = []
results = RestClient.get('http://google.com/search?q=ruby')
Hpricot(results).find('a').each{ |link|
page = RestClient.get(link)
begin
diff --git a/lib/sequel_core/adapters/mysql.rb b/lib/sequel_core/adapters/mysql.rb
index 89684cf..e7fdd77 100644
--- a/lib/sequel_core/adapters/mysql.rb
+++ b/lib/sequel_core/adapters/mysql.rb
@@ -58,6 +58,10 @@ module Sequel
opts = server_opts(server)
conn = Mysql.init
conn.options(Mysql::OPT_LOCAL_INFILE, "client")
+ if encoding = opts[:encoding] || opts[:charset]
+ # set charset _before_ the connect. using an option instead of "SET (NAMES|CHARACTER_SET_*)" works across reconnects
class FiberedMutex
#
# Creates a new FiberedMutex
#
def initialize
@waiting = []
@locked = false
end
#
@tmm1
tmm1 / gist:61762
Created February 11, 2009 01:55
FAQ about MRI internals
> - In ruby 1.8.x, what is the functional difference between rb_thread_schedule and rb_thread_select?
rb_thread_schedule() is the guts of the thread scheduler, it traverses
over the linked list of threads (several times) to find the next one
to switch into. The function is long (250 lines) and messy, and covers
all the combinations of thread status (RUNNABLE, TO_KILL, STOPPED,
KILLED) and wait state (FD, SELECT, TIME, JOIN, PID).
If there are no threads doing i/o or waiting on a timeout,
rb_thread_schedule() picks another thread from the list (considering
class FiberedQueue
def initialize
@queue = []
@busy = false
end
def busy?
!!@busy
end