atomic-ruby 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e47f4b67bd3d4dcaecbd560ed61b8dc4c86c24882b52279a5d0e92fd6de666a
4
- data.tar.gz: 2fb299af89ccc871b7c8b3d6ef18f15924e98b90ad196143c0ea9fa3cac76419
3
+ metadata.gz: 21542683125e7a193b4500ff05f3ee74249db2e3b16e71a3b10b46ff53a52d4b
4
+ data.tar.gz: 609c1f8e8edb35ad1ad049a33c515a74522803b64a592ed49ac8666d8c06a8ba
5
5
  SHA512:
6
- metadata.gz: fb201628eedea540bea2a8436daf0bb9f9db3cf2f0cd1caa5d27a54af840b992474cf4e08496a52c16f963ab311cd14f3796977f6777140c648e38cd283a6679
7
- data.tar.gz: fe346ae21ffa67e8ed82048a1b79fe2eada3f101e2b812112eef689139f924e01b420b6e0d56f37ca2c795e597bd86631e79f8d93fa5d32f50a77ef7eb1f9734
6
+ metadata.gz: 8a3e15dee614e4455540272dba417192800fe85d41acdd0ff30babe8681f1d768b408a38fcf8acba878054fab96c94e085516299670b1643c591cc617a165919
7
+ data.tar.gz: ae7b2e2e3a03019471d741a995d95a83f2e52eeb36137942b14cea55e8e76f8fd48ae734acbad5c2ac1a98a2fc8c88ddf5192ff6a2598f53555d7c08bfcbce9e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2025-07-06
4
+
5
+ - Revert "Fix `AtomicThreadPool#<<` shutdown check race condition"
6
+ - Add `:name` to `AtomicThreadPool` initializer
7
+ - Add `AtomicCountDownLatch`
8
+
3
9
  ## [0.3.2] - 2025-06-14
4
10
 
5
11
  - Fix `AtomicThreadPool#<<` shutdown check race condition
data/README.md CHANGED
@@ -190,7 +190,7 @@ puts "Atomic Ruby Atomic Bank Account: #{results[2].real.round(6)} seconds"
190
190
 
191
191
  ruby version: ruby 3.4.4 (2025-05-14 revision a38531fd3f) +YJIT +PRISM [arm64-darwin24]
192
192
  concurrent-ruby version: 1.3.5
193
- atomic-ruby version: 0.3.2
193
+ atomic-ruby version: 0.4.0
194
194
 
195
195
  Balances:
196
196
  Synchronized Bank Account Balance: 975
@@ -198,9 +198,9 @@ Concurrent Ruby Atomic Bank Account Balance: 975
198
198
  Atomic Ruby Atomic Bank Account Balance: 975
199
199
 
200
200
  Benchmark Results:
201
- Synchronized Bank Account: 5.130792 seconds
202
- Concurrent Ruby Atomic Bank Account: 5.121400 seconds
203
- Atomic Ruby Atomic Bank Account: 5.119191 seconds
201
+ Synchronized Bank Account: 5.110062 seconds
202
+ Concurrent Ruby Atomic Bank Account: 5.107966 seconds
203
+ Atomic Ruby Atomic Bank Account: 5.107739 seconds
204
204
  ```
205
205
 
206
206
  </details>
@@ -281,27 +281,27 @@ end
281
281
 
282
282
  ruby version: ruby 3.4.4 (2025-05-14 revision a38531fd3f) +YJIT +PRISM [arm64-darwin24]
283
283
  concurrent-ruby version: 1.3.5
284
- atomic-ruby version: 0.3.2
284
+ atomic-ruby version: 0.4.0
285
285
 
286
286
  Warming up --------------------------------------
287
287
  Synchronized Boolean Toggle
288
- 87.000 i/100ms
288
+ 102.000 i/100ms
289
289
  Concurrent Ruby Atomic Boolean Toggle
290
- 81.000 i/100ms
290
+ 88.000 i/100ms
291
291
  Atomic Ruby Atomic Boolean Toggle
292
- 92.000 i/100ms
292
+ 109.000 i/100ms
293
293
  Calculating -------------------------------------
294
294
  Synchronized Boolean Toggle
295
- 969.205 (± 5.8%) i/s (1.03 ms/i) - 4.872k in 5.043520s
295
+ 1.062k2.5%) i/s (941.81 μs/i) - 5.406k in 5.094827s
296
296
  Concurrent Ruby Atomic Boolean Toggle
297
- 885.546 (± 5.2%) i/s (1.13 ms/i) - 4.455k in 5.044282s
297
+ 981.4953.5%) i/s (1.02 ms/i) - 4.928k in 5.027167s
298
298
  Atomic Ruby Atomic Boolean Toggle
299
- 1.154k (± 5.3%) i/s (866.65 μs/i) - 5.796k in 5.037272s
299
+ 1.274k1.5%) i/s (784.70 μs/i) - 6.431k in 5.047458s
300
300
 
301
301
  Comparison:
302
- Atomic Ruby Atomic Boolean Toggle: 1153.9 i/s
303
- Synchronized Boolean Toggle: 969.2 i/s - 1.19x slower
304
- Concurrent Ruby Atomic Boolean Toggle: 885.5 i/s - 1.30x slower
302
+ Atomic Ruby Atomic Boolean Toggle: 1274.4 i/s
303
+ Synchronized Boolean Toggle: 1061.8 i/s - 1.20x slower
304
+ Concurrent Ruby Atomic Boolean Toggle: 981.5 i/s - 1.30x slower
305
305
  ```
306
306
 
307
307
  </details>
@@ -359,11 +359,11 @@ puts "Atomic Ruby Atomic Thread Pool: #{results[1].real.round(6)} seconds"
359
359
 
360
360
  ruby version: ruby 3.4.4 (2025-05-14 revision a38531fd3f) +YJIT +PRISM [arm64-darwin24]
361
361
  concurrent-ruby version: 1.3.5
362
- atomic-ruby version: 0.3.2
362
+ atomic-ruby version: 0.4.0
363
363
 
364
364
  Benchmark Results:
365
- Concurrent Ruby Thread Pool: 5.032940 seconds
366
- Atomic Ruby Atomic Thread Pool: 4.508259 seconds
365
+ Concurrent Ruby Thread Pool: 5.02207 seconds
366
+ Atomic Ruby Atomic Thread Pool: 4.503302 seconds
367
367
  ```
368
368
 
369
369
  </details>
@@ -11,11 +11,11 @@ module AtomicRuby
11
11
  raise InvalidBooleanError, "expected boolean to be a `TrueClass` or `FalseClass`, got #{value.class}"
12
12
  end
13
13
 
14
- @atom = Atom.new(value)
14
+ @boolean = Atom.new(value)
15
15
  end
16
16
 
17
17
  def value
18
- @atom.value
18
+ @boolean.value
19
19
  end
20
20
 
21
21
  def true?
@@ -27,15 +27,15 @@ module AtomicRuby
27
27
  end
28
28
 
29
29
  def make_true
30
- @atom.swap { true }
30
+ @boolean.swap { true }
31
31
  end
32
32
 
33
33
  def make_false
34
- @atom.swap { false }
34
+ @boolean.swap { false }
35
35
  end
36
36
 
37
37
  def toggle
38
- @atom.swap { |current_value| !current_value }
38
+ @boolean.swap { |current_value| !current_value }
39
39
  end
40
40
  end
41
41
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "atom"
4
+
5
+ module AtomicRuby
6
+ class AtomicCountDownLatch
7
+ class InvalidCountError < StandardError; end
8
+ class AlreadyCountedDownError < StandardError; end
9
+
10
+ def initialize(count)
11
+ unless count.is_a?(Integer)
12
+ raise InvalidCountError, "expected count to be an `Integer`, got #{count.class}"
13
+ end
14
+
15
+ @count = Atom.new(count)
16
+ end
17
+
18
+ def count
19
+ @count.value
20
+ end
21
+
22
+ def count_down
23
+ unless @count.value > 0
24
+ raise AlreadyCountedDownError, "count has already reached zero"
25
+ end
26
+
27
+ @count.swap { |current_value| current_value - 1 }
28
+ end
29
+
30
+ def wait
31
+ Thread.pass while @count.value > 0
32
+ end
33
+ end
34
+ end
Binary file
@@ -8,8 +8,9 @@ module AtomicRuby
8
8
  class UnsupportedWorkTypeError < StandardError; end
9
9
  class InvalidWorkQueueingError < StandardError; end
10
10
 
11
- def initialize(size:)
11
+ def initialize(size:, name: nil)
12
12
  @size = size
13
+ @name = name
13
14
  @queue = Atom.new([])
14
15
  @threads = []
15
16
  @started_threads = Atom.new(0)
@@ -23,13 +24,11 @@ module AtomicRuby
23
24
  raise UnsupportedWorkTypeError, "expected work to be a `Proc`, got #{work.class}"
24
25
  end
25
26
 
26
- @queue.swap do |current_queue|
27
- if @stopping.true?
28
- raise InvalidWorkQueueingError, "cannot queue work during or after pool shutdown"
29
- end
30
-
31
- current_queue += [work]
27
+ if @stopping.true?
28
+ raise InvalidWorkQueueingError, "cannot queue work during or after pool shutdown"
32
29
  end
30
+
31
+ @queue.swap { |current_queue| current_queue += [work] }
33
32
  true
34
33
  end
35
34
 
@@ -52,8 +51,9 @@ module AtomicRuby
52
51
  def start
53
52
  @threads = @size.times.map do |num|
54
53
  Thread.new(num) do |idx|
55
- name = "AtomicRuby::AtomicThreadPool thread #{idx}"
56
- Thread.current.name = name
54
+ thread_name = String.new("AtomicRuby::AtomicThreadPool thread #{idx}")
55
+ thread_name << " for #{@name}" if @name
56
+ Thread.current.name = thread_name
57
57
 
58
58
  @started_threads.swap { |current_count| current_count + 1 }
59
59
 
@@ -65,7 +65,7 @@ module AtomicRuby
65
65
  begin
66
66
  work.call
67
67
  rescue => err
68
- puts "#{name} rescued:"
68
+ puts "#{thread_name} rescued:"
69
69
  puts "#{err.class}: #{err.message}"
70
70
  puts err.backtrace.join("\n")
71
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AtomicRuby
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/atomic-ruby.rb CHANGED
@@ -3,7 +3,9 @@
3
3
  require_relative "atomic-ruby/version"
4
4
  require_relative "atomic-ruby/atomic_ruby"
5
5
  require_relative "atomic-ruby/atom"
6
+ require_relative "atomic-ruby/atomic_boolean"
6
7
  require_relative "atomic-ruby/atomic_thread_pool"
8
+ require_relative "atomic-ruby/atomic_count_down_latch"
7
9
 
8
10
  module AtomicRuby
9
11
  class Error < StandardError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atomic-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Young
@@ -27,6 +27,7 @@ files:
27
27
  - lib/atomic-ruby.rb
28
28
  - lib/atomic-ruby/atom.rb
29
29
  - lib/atomic-ruby/atomic_boolean.rb
30
+ - lib/atomic-ruby/atomic_count_down_latch.rb
30
31
  - lib/atomic-ruby/atomic_ruby.bundle
31
32
  - lib/atomic-ruby/atomic_thread_pool.rb
32
33
  - lib/atomic-ruby/version.rb