1
1
require 'benchmark'
2
- require 'concurrent '
2
+ require 'rbconfig '
3
3
require 'thread'
4
+ require 'concurrent'
4
5
Thread . abort_on_exception = true
5
6
6
7
$go = false # for synchronizing parallel threads
11
12
# number of threads for parallel test
12
13
M = ARGV [ 0 ] ? ARGV [ 0 ] . to_i : 100
13
14
15
+ # list of platform-specific implementations
16
+ ATOMICS = [
17
+ 'MutexAtomic' ,
18
+ 'CAtomic' ,
19
+ 'JavaAtomic' ,
20
+ 'RbxAtomic' ,
21
+ ]
22
+
23
+ puts "Testing with #{ RbConfig ::CONFIG [ 'ruby_install_name' ] } #{ RUBY_VERSION } "
14
24
15
- puts "*** Sequential updates ***"
25
+ puts
26
+ puts '*** Sequential updates ***'
16
27
Benchmark . bm ( 10 ) do |x |
17
28
value = 0
18
- x . report " no lock" do
29
+ x . report ' no lock' do
19
30
N . times do
20
31
value += 1
21
32
end
22
33
end
23
34
24
35
@lock = Mutex . new
25
- x . report " mutex" do
36
+ x . report ' mutex' do
26
37
value = 0
27
38
N . times do
28
39
@lock . synchronize do
31
42
end
32
43
end
33
44
34
- @atom = Concurrent ::Atomic . new ( 0 )
35
- x . report "atomic" do
36
- N . times do
37
- @atom . update { |x | x += 1 }
45
+ ATOMICS . each do |clazz |
46
+ if Concurrent . const_defined? clazz
47
+ @atom = Concurrent . const_get ( clazz ) . new ( 0 )
48
+ x . report clazz do
49
+ N . times do
50
+ @atom . update { |x | x += 1 }
51
+ end
52
+ end
38
53
end
39
54
end
40
55
end
41
56
42
57
def para_setup ( num_threads , count , &block )
43
58
if num_threads % 2 > 0
44
- raise ArgumentError , " num_threads must be a multiple of two"
59
+ raise ArgumentError , ' num_threads must be a multiple of two'
45
60
end
46
- raise ArgumentError , " need block" unless block_given?
61
+ raise ArgumentError , ' need block' unless block_given?
47
62
48
63
# Keep those threads together
49
64
tg = ThreadGroup . new
@@ -62,7 +77,7 @@ def para_setup(num_threads, count, &block)
62
77
end
63
78
64
79
# Make sure all threads are started
65
- while tg . list . find { |t | t . status != " run" }
80
+ while tg . list . find { |t | t . status != ' run' }
66
81
Thread . pass
67
82
end
68
83
@@ -78,15 +93,15 @@ def para_run(tg)
78
93
$go = false
79
94
end
80
95
81
- puts "*** Parallel updates ***"
96
+ puts
97
+ puts '*** Parallel updates ***'
82
98
Benchmark . bm ( 10 ) do |bm |
83
99
# This is not secure
84
100
value = 0
85
101
tg = para_setup ( M , N /M ) do |diff |
86
102
value += diff
87
103
end
88
- bm . report ( "no lock" ) { para_run ( tg ) }
89
-
104
+ bm . report ( 'no lock' ) { para_run ( tg ) }
90
105
91
106
value = 0
92
107
@lock = Mutex . new
@@ -95,15 +110,17 @@ def para_run(tg)
95
110
value += diff
96
111
end
97
112
end
98
- bm . report ( " mutex" ) { para_run ( tg ) }
113
+ bm . report ( ' mutex' ) { para_run ( tg ) }
99
114
raise unless value == 0
100
115
101
-
102
- @atom = Concurrent ::Atomic . new ( 0 )
103
- tg = para_setup ( M , N /M ) do |diff |
104
- @atom . update { |x | x + diff }
116
+ ATOMICS . each do |clazz |
117
+ if Concurrent . const_defined? clazz
118
+ @atom = Concurrent . const_get ( clazz ) . new ( 0 )
119
+ tg = para_setup ( M , N /M ) do |diff |
120
+ @atom . update { |x | x + diff }
121
+ end
122
+ bm . report ( clazz ) { para_run ( tg ) }
123
+ raise unless @atom . value == 0
124
+ end
105
125
end
106
- bm . report ( "atomic" ) { para_run ( tg ) }
107
- raise unless @atom . value == 0
108
-
109
126
end
0 commit comments