uringmachine 1.0.8 → 1.0.9

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: cf67592c033a0106bdd648ba5ead31df0d9eeea94ddb489466fd033f51d8fa4d
4
- data.tar.gz: 17d54c7e04ff3d854f087fdaa4072ae2be772f661e62b47397e21a06aa8a0491
3
+ metadata.gz: ebeda6a23e02e6ea2325264cf90d9fe2ac3977086995936328dea34f201f5fda
4
+ data.tar.gz: b3540e9219257a54f770b97a1ec15c2d9102f09312a4416db98b9d6b38cbfde7
5
5
  SHA512:
6
- metadata.gz: 401116f03c5e8ce75fa1702a01599a50ca60f8329501aeba677728f5742a5cc24724a90dc273afd468ce4e6028c9e3925836190f71951dce39ff70bba1d1c2f8
7
- data.tar.gz: 944d8b62e828b452b339e97235f010f4f383b0480deee4073db84e1c5db252785498cce46e2c25a56342207064b75869ccea2fd3f15992d809a56b1fe1aca08a
6
+ metadata.gz: bf61e20da3015b218f0298a431578747b1eadef6269c77a2765daec5e77379231e0cd9e58e18f9d6276b5872f134662805b2900f8ffcdec4666856d36b92b21e
7
+ data.tar.gz: c167e10850043e7c80bcafa1eb768a484b2a353672e5965da69c81c6c6a4d709654658e59b483d83e8de0c03e56f7134bb6a31b824fdcf89130d9ca5c885e76d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 1.0.9 2026-11-20
2
+
3
+ - Add `UM::Broadcaster` class, `UM#broadcaster` method
4
+
1
5
  # 1.0.8 2026-11-20
2
6
 
3
7
  - Increase buffer pool initial buffer size to 64KB
data/TODO.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Immediate
2
+
3
+ - `UM::IO`: Implement automatic failover to `IO_MODE` mode when doing a multishot read (in
4
+ `IO_FD`) and getting a `-EBADFD` error.
5
+
1
6
  ## CSP
2
7
 
3
8
  How amenable is UringMachine to CSP-style programming?
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UringMachine
4
+ # A Broadcaster implements a broadcast pattern where multiple fibers may be
5
+ # notified of an event or some arbitrary value. Listeners listen for events
6
+ # using the #each method.
7
+ class Broadcaster
8
+ # Initializes a broadcaster instance.
9
+ #
10
+ # @param machine [UringMachine]
11
+ # @return [void]
12
+ def initialize(machine)
13
+ @machine = machine
14
+ @listeners = Set.new
15
+ end
16
+
17
+ # Returns the number of currently listening fibers.
18
+ #
19
+ # @return [Integer]
20
+ def listener_count
21
+ @listeners.size
22
+ end
23
+
24
+ # Broadcasts the given value to all listeners.
25
+ #
26
+ # @param value [any]
27
+ # @return [void]
28
+ def broadcast(value)
29
+ @listeners.each { @machine.push(it, value) }
30
+ end
31
+
32
+ # Listens for broadcast values in an infinite loop, yielding each broadcast
33
+ # value to the given block.
34
+ #
35
+ # @return [void]
36
+ def each
37
+ mailbox = Fiber.current.mailbox
38
+ @listeners << mailbox
39
+ loop { yield @machine.shift(mailbox) }
40
+ ensure
41
+ @listeners.delete(mailbox)
42
+ end
43
+ end
44
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class UringMachine
4
- VERSION = '1.0.8'
4
+ VERSION = '1.0.9'
5
5
  end
data/lib/uringmachine.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'um_ext'
4
4
  require 'uringmachine/version'
5
5
  require 'uringmachine/dns_resolver'
6
+ require 'uringmachine/broadcaster'
6
7
 
7
8
  UM = UringMachine
8
9
 
@@ -221,6 +222,11 @@ class UringMachine
221
222
  res
222
223
  end
223
224
 
225
+ # @return [UringMachine::Broadcaster]
226
+ def broadcaster
227
+ Broadcaster.new(self)
228
+ end
229
+
224
230
  # call-seq:
225
231
  # machine.file_read(fn) -> data
226
232
  #
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+ require 'uringmachine/broadcaster'
5
+
6
+ class BroadcasterTest < UMBaseTest
7
+ def test_broadcaster_basic_usage
8
+ buf = []
9
+ broadcaster = UM::Broadcaster.new(machine)
10
+ f1 = machine.spin do
11
+ broadcaster.each { |v| buf << [:f1, v] }
12
+ end
13
+ f2 = machine.spin do
14
+ broadcaster.each { |v| buf << [:f2, v] }
15
+ end
16
+
17
+ assert_equal 0, broadcaster.listener_count
18
+ 3.times { machine.snooze }
19
+ assert_equal 2, broadcaster.listener_count
20
+ broadcaster.broadcast(:foo)
21
+ 3.times { machine.snooze }
22
+ assert_equal [[:f1, :foo], [:f2, :foo]], buf
23
+
24
+ broadcaster.broadcast(:bar)
25
+ 3.times { machine.snooze }
26
+ assert_equal [[:f1, :foo], [:f2, :foo], [:f1, :bar], [:f2, :bar]], buf
27
+ end
28
+
29
+ def test_broadcaster_listener_break
30
+ buf = []
31
+ broadcaster = UM::Broadcaster.new(machine)
32
+ f1 = machine.spin do
33
+ broadcaster.each { |v| buf << [:f1, v]; break }
34
+ end
35
+ f2 = machine.spin do
36
+ broadcaster.each { |v| buf << [:f2, v] }
37
+ end
38
+
39
+ 3.times { machine.snooze }
40
+ assert_equal 2, broadcaster.listener_count
41
+ broadcaster.broadcast(:foo)
42
+ 3.times { machine.snooze }
43
+ assert_equal [[:f1, :foo], [:f2, :foo]], buf
44
+
45
+ assert_equal 1, broadcaster.listener_count
46
+ broadcaster.broadcast(:bar)
47
+ 3.times { machine.snooze }
48
+ assert_equal [[:f1, :foo], [:f2, :foo], [:f2, :bar]], buf
49
+ end
50
+
51
+ def test_broadcaster_listener_exception
52
+ buf = []
53
+ broadcaster = UM::Broadcaster.new(machine)
54
+ f1 = machine.spin do
55
+ broadcaster.each { |v| buf << [:f1, v]; raise }
56
+ rescue => e
57
+ buf << [:f1, :exception]
58
+ end
59
+ f2 = machine.spin do
60
+ broadcaster.each { |v| buf << [:f2, v] }
61
+ end
62
+
63
+ 3.times { machine.snooze }
64
+ assert_equal 2, broadcaster.listener_count
65
+ broadcaster.broadcast(:foo)
66
+ 3.times { machine.snooze }
67
+ assert_equal [[:f1, :foo], [:f1, :exception], [:f2, :foo]], buf
68
+
69
+ assert_equal 1, broadcaster.listener_count
70
+ broadcaster.broadcast(:bar)
71
+ 3.times { machine.snooze }
72
+ assert_equal [[:f1, :foo], [:f1, :exception], [:f2, :foo], [:f2, :bar]], buf
73
+ end
74
+
75
+ def test_broadcaster_helper_method
76
+ buf = []
77
+ broadcaster = machine.broadcaster
78
+ assert_kind_of UM::Broadcaster, broadcaster
79
+ f1 = machine.spin do
80
+ broadcaster.each { |v| buf << [:f1, v] }
81
+ end
82
+ f2 = machine.spin do
83
+ broadcaster.each { |v| buf << [:f2, v] }
84
+ end
85
+
86
+ assert_equal 0, broadcaster.listener_count
87
+ 3.times { machine.snooze }
88
+ assert_equal 2, broadcaster.listener_count
89
+ broadcaster.broadcast(:foo)
90
+ 3.times { machine.snooze }
91
+ assert_equal [[:f1, :foo], [:f2, :foo]], buf
92
+
93
+ broadcaster.broadcast(:bar)
94
+ 3.times { machine.snooze }
95
+ assert_equal [[:f1, :foo], [:f2, :foo], [:f1, :bar], [:f2, :bar]], buf
96
+ end
97
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uringmachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -39,6 +39,7 @@ extra_rdoc_files:
39
39
  - ext/um/um_utils.c
40
40
  - lib/uringmachine.rb
41
41
  - lib/uringmachine/actor.rb
42
+ - lib/uringmachine/broadcaster.rb
42
43
  - lib/uringmachine/dns_resolver.rb
43
44
  - lib/uringmachine/fiber_scheduler.rb
44
45
  - lib/uringmachine/version.rb
@@ -141,6 +142,7 @@ files:
141
142
  - grant-2025/tasks.md
142
143
  - lib/uringmachine.rb
143
144
  - lib/uringmachine/actor.rb
145
+ - lib/uringmachine/broadcaster.rb
144
146
  - lib/uringmachine/dns_resolver.rb
145
147
  - lib/uringmachine/fiber_scheduler.rb
146
148
  - lib/uringmachine/version.rb
@@ -150,6 +152,7 @@ files:
150
152
  - test/run.rb
151
153
  - test/test_actor.rb
152
154
  - test/test_async_op.rb
155
+ - test/test_broadcaster.rb
153
156
  - test/test_fiber.rb
154
157
  - test/test_fiber_scheduler.rb
155
158
  - test/test_io.rb