diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-12-03 17:27:53 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-12-03 17:27:53 -0800 |
commit | c6c2f7782e2270ee4684d2405376a186aa806fcd (patch) | |
tree | 14efa83fc750b24fa2213dd64f6ddc9fd1b3b915 /lib/tdb/mt.rb | |
parent | 2a31a29a3398b645722b9ce77a2987948289706e (diff) | |
download | ruby-tdb-c6c2f7782e2270ee4684d2405376a186aa806fcd.tar.gz |
Thread-safety is useful sometimes and needless overhead otherwise. Default to whatever TDB upstream defaults to.
Diffstat (limited to 'lib/tdb/mt.rb')
-rw-r--r-- | lib/tdb/mt.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/tdb/mt.rb b/lib/tdb/mt.rb new file mode 100644 index 0000000..6742cc4 --- /dev/null +++ b/lib/tdb/mt.rb @@ -0,0 +1,38 @@ +# -*- encoding: binary -*- +module TDB::MT + def initialize + super + @lock = Mutex.new + end + + %w( + close closed? fetch [] store []= insert! modify! insert modify + key? has_key? include? member? + nuke! delete + lockall trylockall unlockall + lockall_read trylockall_read unlockall_read + lockall_mark lockall_unmark + ).each do |meth| + eval "def #{meth}(*args); @lock.synchronize { super }; end" + end + + def each(&block) + @lock.synchronize do + super { |k,v| @lock.exclusive_unlock { yield(k,v) } } + end + end + + def threadsafe? + true + end + + def self.extended(obj) + obj.instance_eval { @lock = Mutex.new unless defined?(@lock) } + end + + def self.included(klass) + ObjectSpace.each_object(klass) { |obj| + obj.instance_eval { @lock = Mutex.new unless defined?(@lock) } + } + end +end |