synchronizable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,8 @@ Synchronizable is available as a RubyGem:
15
15
 
16
16
  == Examples
17
17
 
18
+ === Making an instance synchronizable
19
+
18
20
  class Foo
19
21
  def bar
20
22
  end
@@ -27,6 +29,8 @@ Synchronizable is available as a RubyGem:
27
29
  # the foo instance now has all methods synchronized / thread-safe
28
30
  foo.extend(Synchronizable)
29
31
 
32
+ === Making a class synchronizable
33
+
30
34
  # note that classes are objects in Ruby and can also be synchronized
31
35
  class Bar
32
36
  def self.foo
@@ -36,6 +40,17 @@ Synchronizable is available as a RubyGem:
36
40
  # only class/singleton methods will become synchronized
37
41
  Bar.extend(Synchronizable)
38
42
 
43
+ === Utilizing the #synchronize method to protect a block
44
+
45
+ # the synchronize method takes a block and executes it
46
+ # in a thread-safe manner
47
+ s = "this is a test"
48
+ s.extend(Synchronizable)
49
+ s.synchronize do
50
+ s.gsub!('i', 'x')
51
+ s.slice!(0, 5)
52
+ end
53
+
39
54
  == Note on Patches/Pull Requests
40
55
 
41
56
  * Fork the project.
@@ -1,4 +1,4 @@
1
- require 'thread'
1
+ require 'monitor'
2
2
  require 'synchronizable/version'
3
3
 
4
4
  # Synchronizable is intended to be injected into objects via Object#extend.
@@ -20,11 +20,19 @@ module Synchronizable
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ # define synchronize method that executes a block
25
+ # protected with the internal instance lock
26
+ obj.define_singleton_method(:synchronize) do |&block|
27
+ __lock.synchronize(&block)
28
+ end
23
29
  end
24
30
 
25
31
  private
26
32
 
27
33
  def __lock
28
- @__lock ||= Mutex.new
34
+ # Monitor is used instead of Mutex in order to support
35
+ # re-entrant locking
36
+ @__lock ||= Monitor.new
29
37
  end
30
38
  end
@@ -1,3 +1,3 @@
1
1
  module Synchronizable
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -35,5 +35,31 @@ describe Synchronizable do
35
35
  object.size
36
36
  lock.sync_invoked.should == true
37
37
  end
38
+
39
+ it "allows re-entrant methods" do
40
+ class Test
41
+ def m1
42
+ 10
43
+ end
44
+
45
+ def m2
46
+ m1
47
+ end
48
+ end
49
+
50
+ t = Test.new
51
+ t.extend(Synchronizable)
52
+ t.m2.should == 10
53
+ end
54
+ end
55
+
56
+ it "protects a block via #synchronize" do
57
+ s = ""
58
+ s.methods.should_not include(:synchronize)
59
+ s.extend(Synchronizable)
60
+ s.methods.should include(:synchronize)
61
+ s.synchronize do
62
+ s.split
63
+ end
38
64
  end
39
65
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ryan LeCompte
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-28 00:00:00 -04:00
17
+ date: 2011-07-29 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency