tsafe 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/include/monhash.rb +2 -2
- data/include/mrswlock.rb +30 -9
- data/tsafe.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/include/monhash.rb
CHANGED
@@ -5,8 +5,8 @@
|
|
5
5
|
# h['test'] = 'trala'
|
6
6
|
# ret = h['test']
|
7
7
|
class Tsafe::MonHash < ::Hash
|
8
|
-
@@
|
9
|
-
@@
|
8
|
+
@@tsafe_mrswlock_w_methods = [:[]=, :clear, :delete, :delete_if, :keep_if, :merge!, :rehash, :reject!, :replace, :select!, :shift, :store, :update, :values_at]
|
9
|
+
@@tsafe_mrswlock_r_methods = [:each, :each_key, :each_pair, :each_value]
|
10
10
|
|
11
11
|
include Tsafe::Mrswlock::SynModule
|
12
12
|
end
|
data/include/mrswlock.rb
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
class Tsafe::Mrswlock
|
3
3
|
@@debug = false
|
4
4
|
|
5
|
-
#Sets various variables.
|
5
|
+
# Sets various variables.
|
6
6
|
def initialize
|
7
7
|
@reads = 0
|
8
8
|
@w_mutex = Mutex.new
|
9
9
|
end
|
10
10
|
|
11
|
-
#Runs the given block through the read-synchronization.
|
11
|
+
# Runs the given block through the read-synchronization.
|
12
12
|
def rsync
|
13
13
|
begin
|
14
14
|
while @w_mutex.locked?
|
@@ -24,7 +24,11 @@ class Tsafe::Mrswlock
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
#Runs the given block through the write-synchronization (locks both reading and writing).
|
27
|
+
# Runs the given block through the write-synchronization (locks both reading and writing).
|
28
|
+
# ===Examples
|
29
|
+
# lock.wsync do
|
30
|
+
# #do something within lock.
|
31
|
+
# end
|
28
32
|
def wsync
|
29
33
|
@w_mutex.synchronize do
|
30
34
|
#Wait for any reads to finish that might have started while we were getting the lock.
|
@@ -37,30 +41,46 @@ class Tsafe::Mrswlock
|
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
44
|
+
#This module can be included in order to painlessly make a thread-safe multi-reader-single-writer thread-safe copy of a class.
|
45
|
+
#===Examples
|
46
|
+
# class Tsafe::MonHash < ::Hash
|
47
|
+
# @@tsafe_mrswlock_w_methods = [:[]=, :clear, :delete, :delete_if, :keep_if, :merge!, :rehash, :reject!, :replace, :select!, :shift, :store, :update, :values_at]
|
48
|
+
# @@tsafe_mrswlock_r_methods = [:each, :each_key, :each_pair, :each_value]
|
49
|
+
# include Tsafe::Mrswlock::SynModule
|
50
|
+
# end
|
40
51
|
module SynModule
|
41
52
|
def self.included(base)
|
42
53
|
base.to_s.split("::").inject(Object, :const_get).class_eval do
|
43
54
|
#Yields the given block within the read-lock.
|
55
|
+
#===Examples
|
56
|
+
# obj._tsafe_rsync do
|
57
|
+
# #do something within read-lock.
|
58
|
+
# end
|
44
59
|
def _tsafe_rsync(&block)
|
45
60
|
@tsafe_mrswlock.rsync(&block)
|
46
61
|
end
|
47
62
|
|
48
63
|
#Yields the given block within the write-lock (and read-lock).
|
64
|
+
#===Examples
|
65
|
+
# obj._tsafe_wsync do
|
66
|
+
# #do something within write-lock.
|
67
|
+
# end
|
49
68
|
def _tsafe_wsync(&block)
|
50
69
|
@tsafe_mrswlock.rsync(&block)
|
51
70
|
end
|
52
71
|
|
53
72
|
#Rename initialize.
|
54
|
-
alias_method(:
|
73
|
+
alias_method(:initialize_mrswlock, :initialize)
|
55
74
|
|
56
75
|
#Make another initialize-method that spawns the lock and then calls the original initialize.
|
57
76
|
define_method(:initialize) do |*args, &block|
|
58
77
|
@tsafe_mrswlock = Tsafe::Mrswlock.new
|
59
|
-
return
|
78
|
+
return initialize_mrswlock(*args, &block)
|
60
79
|
end
|
61
80
|
|
62
|
-
|
63
|
-
|
81
|
+
#Makes reader methods go through reader-lock.
|
82
|
+
base.class_variable_get(:@@tsafe_mrswlock_r_methods).each do |mname|
|
83
|
+
newmname = "tsafe_mrswlock_#{mname}".to_sym
|
64
84
|
alias_method(newmname, mname)
|
65
85
|
|
66
86
|
define_method(mname) do |*args, &block|
|
@@ -70,8 +90,9 @@ class Tsafe::Mrswlock
|
|
70
90
|
end
|
71
91
|
end
|
72
92
|
|
73
|
-
|
74
|
-
|
93
|
+
#Makes writer methods go through writer-lock.
|
94
|
+
base.class_variable_get(:@@tsafe_mrswlock_w_methods).each do |mname|
|
95
|
+
newmname = "tsafe_mrswlock_#{mname}".to_sym
|
75
96
|
alias_method(newmname, mname)
|
76
97
|
|
77
98
|
define_method(mname) do |*args, &block|
|
data/tsafe.gemspec
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: tsafe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
hash:
|
103
|
+
hash: 2392481904128949132
|
104
104
|
segments:
|
105
105
|
- 0
|
106
106
|
version: "0"
|