interop 0.3.1 → 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: d87e7c23c67f9e2d514ff393cee8789856588ff4b399e51305008608d9e9d316
4
- data.tar.gz: bbb0a45add6f441698a5f4d07016d9613b36f3273f51c891894742090fce6769
3
+ metadata.gz: c223f2bcb37a0232a64aada70c36947472db2cb1f3f93f4c240f8ef506521244
4
+ data.tar.gz: c138b462718e569d020a416451a89b81b8a04ed8a100cba3a528bf39b03c82f8
5
5
  SHA512:
6
- metadata.gz: 0fce7c36089b584c1b2496705ff4dec7e1902db1e965327fc50fded4c26483a6fc4733021287d7c262754cae2abf4da663a464a579b848242d883c023a957a3f
7
- data.tar.gz: fc4a78d74b5af7eb5aad0ac5aaa1a8972a720a535ab233587e26c0f5f0a9e689fcb7a85450f9a6436e216c52aaaee88ddb2eac2f797328a632f28da28567e742
6
+ metadata.gz: 220ec6f89e51f1ffdc00657ff52f2a1d16a143dad472b5604c396adfb83748b3bb037203d79df524625c33b2c17232f80cf61045a05a2af6ba4a1792a41d1976
7
+ data.tar.gz: 7e24d2f42c7346248e14d04a981c0480f60746901272e9c01cbbbde442900fd2b151e02785bad850d112879d5a58abf545cf88baddc14f2f4abe39f0bc80f51c
@@ -1,15 +1,17 @@
1
1
  require 'interop/connection'
2
2
  require 'interop/rpc/dispatcher'
3
3
  require 'interop/rpc/controller'
4
+ require 'interop/task_group'
4
5
 
5
6
  module Hx
6
7
  module Interop
7
8
  module RPC
8
9
  # Base class for RPC Client and Server
9
10
  class Base
10
- def initialize(reader, writer = reader)
11
+ def initialize(reader, writer = reader, task_group: TaskGroup.new)
11
12
  @connection = Connection.build(reader, writer)
12
13
  @dispatcher = Dispatcher.new
14
+ @tasks = task_group
13
15
  yield self if block_given?
14
16
  @io_thread = Thread.new do
15
17
  run
@@ -20,7 +22,8 @@ module Hx
20
22
 
21
23
  # TODO: custom exception handler?
22
24
 
23
- # Wait for the process to finish (i.e. for the connection to close).
25
+ # Wait for the process to finish (i.e. for the connection to close). Does not wait for running requests/event
26
+ # handlers to complete.
24
27
  def wait
25
28
  @io_thread.join
26
29
  raise @error if @error # TODO: wrap in something specific, to preserve backtrace
@@ -48,7 +51,7 @@ module Hx
48
51
 
49
52
  def run
50
53
  @connection.read_all do |request|
51
- Thread.new do
54
+ @tasks.run do
52
55
  yield request
53
56
  rescue StandardError => e
54
57
  @io_thread.raise e
@@ -0,0 +1,32 @@
1
+ module Hx
2
+ module Interop
3
+ # Allows blocking on running tasks
4
+ class TaskGroup
5
+ def initialize
6
+ @count = 0
7
+ @mutex = Mutex.new
8
+ @condition = ConditionVariable.new
9
+ end
10
+
11
+ # Run the given block in a new thread. Calls to #wait will block until it has finished running.
12
+ def run
13
+ @mutex.synchronize { @count += 1 }
14
+ Thread.new do
15
+ yield
16
+ ensure
17
+ @mutex.synchronize do
18
+ @count -= 1
19
+ @condition.broadcast if @count.zero?
20
+ end
21
+ end
22
+ end
23
+
24
+ # Block until all threads created by #run have finished.
25
+ def wait
26
+ @mutex.synchronize do
27
+ @condition.wait(@mutex) while @count.positive?
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,5 @@
1
1
  module Hx
2
2
  module Interop
3
- VERSION = '0.3.1'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-27 00:00:00.000000000 Z
11
+ date: 2022-10-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby implementation of hx/interop cross-language interop abstraction
14
14
  email:
@@ -43,6 +43,7 @@ files:
43
43
  - lib/interop/stream_adapter.rb
44
44
  - lib/interop/stream_reader.rb
45
45
  - lib/interop/stream_writer.rb
46
+ - lib/interop/task_group.rb
46
47
  - lib/interop/version.rb
47
48
  - lib/interop/writer.rb
48
49
  homepage: https://github.com/hx/interop