Skip to content

Commit

Permalink
Removing the last traces of Monitor from Agent. I guess we didn't nee…
Browse files Browse the repository at this point in the history
…d re-entrant mutexes after all.
  • Loading branch information
Nathan Sutton committed Mar 21, 2012
1 parent ee27c7b commit a5ee664
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
1 change: 0 additions & 1 deletion lib/agent/all.rb
@@ -1,5 +1,4 @@
require "thread"
require "monitor"

require "agent/version"
require "agent/errors"
Expand Down
10 changes: 5 additions & 5 deletions spec/blocking_once_spec.rb
Expand Up @@ -85,28 +85,28 @@
r, s = [], Time.now.to_f

# Using condition variables to maximize potential contention
monitor = Monitor.new
condition = monitor.new_cond
mutex = Mutex.new
condition = ConditionVariable.new

waiting_channel = channel!(:type => TrueClass, :size => 2)
finished_channel = channel!(:type => TrueClass, :size => 2)

go! do
monitor.synchronize{ waiting_channel.send(true); condition.wait }
mutex.synchronize{ waiting_channel.send(true); condition.wait(mutex) }
@blocking_once.perform{ sleep 0.1; r << 1 }
finished_channel.send(true)
end

go! do
monitor.synchronize{ waiting_channel.send(true); condition.wait }
mutex.synchronize{ waiting_channel.send(true); condition.wait(mutex) }
@blocking_once.perform{ sleep 0.1; r << 1 }
finished_channel.send(true)
end

# wait for both the goroutines to be waiting
2.times{ waiting_channel.receive }

monitor.synchronize{ condition.broadcast }
mutex.synchronize{ condition.broadcast }

# wait for the finished channel to be completed
2.times{ finished_channel.receive }
Expand Down
10 changes: 5 additions & 5 deletions spec/once_spec.rb
Expand Up @@ -55,28 +55,28 @@
r, s = [], Time.now.to_f

# Using condition variables to maximize potential contention
monitor = Monitor.new
condition = monitor.new_cond
mutex = Mutex.new
condition = ConditionVariable.new

waiting_channel = channel!(:type => TrueClass, :size => 2)
finished_channel = channel!(:type => TrueClass, :size => 2)

go! do
monitor.synchronize{ waiting_channel.send(true); condition.wait }
mutex.synchronize{ waiting_channel.send(true); condition.wait(mutex) }
@once.perform{ sleep 0.1; r << 1 }
finished_channel.send(true)
end

go! do
monitor.synchronize{ waiting_channel.send(true); condition.wait }
mutex.synchronize{ waiting_channel.send(true); condition.wait(mutex) }
@once.perform{ sleep 0.1; r << 1 }
finished_channel.send(true)
end

# wait for both the goroutines to be waiting
2.times{ waiting_channel.receive }

monitor.synchronize{ condition.broadcast }
mutex.synchronize{ condition.broadcast }

# wait for the finished channel to be completed
2.times{ finished_channel.receive }
Expand Down

2 comments on commit a5ee664

@corasaurus-hex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's crazy how much slower Monitors are than Mutexes, especially on ruby 1.8. The sieve benchmark takes 79.7s in ruby 1.8 w/ Monitors. The same benchmark takes 32.5s with Mutexes. In ruby 1.9.2 it goes from 10.5s to 9.0s. A big improvement, imho.

@igrigorik
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, well that's good to know. Now I'm curious to see where else Monitors are used.. and could benefit from this observation.

Please sign in to comment.