summaryrefslogtreecommitdiff
path: root/lib/oxidized
diff options
context:
space:
mode:
authorSaku Ytti <saku@ytti.fi>2014-02-25 11:21:43 +0200
committerSaku Ytti <saku@ytti.fi>2014-02-25 11:21:43 +0200
commit596607c8bad948f605fdece2829e24eac3411279 (patch)
treefc45568982ec2f5fd3a80722fd4a5be1575d6f2e /lib/oxidized
parentd750967f1abd8748b48ac3172fe4016361c3d97e (diff)
Remove Mutex#owned? It is incompatible with 1.9.3
I didn't like that solution anyhow, but wasn't sure my locked methods are not calling other external locked methods. That shouldn't be the case so synchronize should work.
Diffstat (limited to 'lib/oxidized')
-rw-r--r--lib/oxidized/nodes.rb88
1 files changed, 46 insertions, 42 deletions
diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb
index 981e082..a6aa2f9 100644
--- a/lib/oxidized/nodes.rb
+++ b/lib/oxidized/nodes.rb
@@ -5,70 +5,70 @@ module Oxidized
class Nodes < Array
attr_accessor :source
alias :put :unshift
- def initialize *args
- super
- @mutex= Mutex.new # we compete for the nodes with webapi thread
- load if args.empty?
- end
def load
- lock
- new = []
- @source = CFG.source[:default]
- Oxidized.mgr.source = @source
- Oxidized.mgr.source[@source].new.load.each do |node|
- new.push Node.new node
+ with_lock do
+ new = []
+ @source = CFG.source[:default]
+ Oxidized.mgr.source = @source
+ Oxidized.mgr.source[@source].new.load.each do |node|
+ new.push Node.new node
+ end
+ replace new
end
- unlock(replace new)
end
+
def list
- lock
- unlock(map { |e| e.serialize })
+ with_lock do
+ map { |e| e.serialize }
+ end
end
+
def show node
- lock
- i = find_node_index node
- unlock(self[i].serialize)
+ with_lock do
+ i = find_node_index node
+ self[i].serialize
+ end
end
+
def fetch node, group
- lock
- i = find_node_index node
- output = self[i].output.new
- unlock
- raise Oxidized::NotSupported unless output.respond_to? :fetch
- output.fetch node, group
- end
- def del node
- lock
- unlock(delete_at find_node_index(node))
+ with_lock do
+ i = find_node_index node
+ output = self[i].output.new
+ raise Oxidized::NotSupported unless output.respond_to? :fetch
+ output.fetch node, group
+ end
end
+
# @param node [String] name of the node moved into the head of array
def next node, opt={}
- lock
- n = del node
- if n
- n.user = opt['user']
- n.msg = opt['msg']
- n.from = opt['from']
- put n
+ with_lock do
+ n = del node
+ if n
+ n.user = opt['user']
+ n.msg = opt['msg']
+ n.from = opt['from']
+ put n
+ end
end
- unlock
end
alias :top :next
# @return [String] node from the head of the array
def get
- lock
- unlock((self << shift).last)
+ with_lock do
+ (self << shift).last
+ end
end
private
- def lock
- @mutex.lock unless @mutex.owned?
+ def initialize *args
+ super
+ @mutex= Mutex.new # we compete for the nodes with webapi thread
+ load if args.empty?
end
- def unlock arg=nil
- @mutex.unlock if @mutex.owned?
- arg
+ def with_lock &block
+ @mutex.synchronize(&block)
end
def find_index node
@@ -78,5 +78,9 @@ module Oxidized
def find_node_index node
find_index node or raise Oxidized::NodeNotFound, "unable to find '#{node}'"
end
+
+ def del node
+ delete_at find_node_index(node)
+ end
end
end