fiber_recycling 0.1.0 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 630db3827d3b2a636f707e11f6595dc987627718
4
- data.tar.gz: 33a112a7bdfbfc4e08daf71e45bf5ca2e35bed44
3
+ metadata.gz: 76a3c74c75354a54f3438062cac901748454d8ec
4
+ data.tar.gz: b0675b0eddcac298676339f6d4885abaab149ef3
5
5
  SHA512:
6
- metadata.gz: 39a32ddb2a25f409eb60350a1dcdc1b56f6e70a92e12ea2fd1f8358989f10b8a663f2bdf325ea848d274c9187e350da3ef5a860b03e66166095fc3ae62c89529
7
- data.tar.gz: 8eee84fc7780c79be520918267a1a3992fcaf06b2bedcda4662624c6e157d7c756d14d1be21860c5f1ddafcd728e21004cdf12fb20483995a2048157e1f3d52c
6
+ metadata.gz: 530a5354a7352191f3017f281f685e7063ccad0aef27008b7c4eb537a3967dcfeb55f4217d2132b7b3c5818f2308b49dd0c635b2982f445738cf00fbbab166c2
7
+ data.tar.gz: b4d63ad3bd907eba5c2811d5a0d5317505616bf78541203a3d927334b68e1ca35aeae1800705ec8e3bef0c477013865b1f576e9d5160669b2ac37be368ed0097
data/README.md CHANGED
@@ -43,7 +43,7 @@ module A
43
43
  fiber # => FiberRecycling::Fiber
44
44
  fiber.resume # => nil
45
45
  fiber.resume # => nil
46
- fiber.resume # => FiberRecycling::FiberError, dead fiber resumed
46
+ fiber.resume # => FiberRecycling::FiberError, dead fiber called
47
47
  end
48
48
 
49
49
  end
@@ -10,9 +10,11 @@ require 'fiber_recycling/duck_types'
10
10
  require 'fiber_recycling/normal_fiber_backend'
11
11
  require 'fiber_recycling/recycled_fiber'
12
12
  require 'fiber_recycling/recycled_fiber_pool'
13
+ require 'fiber_recycling/recycled_fiber_pool_borrower'
13
14
  require 'fiber_recycling/root_fiber_backend'
14
15
  require 'fiber_recycling/thread_extensions'
15
- require_relative 'thread'
16
+ require 'fiber_recycling/monkey_patches/thread'
17
+
16
18
 
17
19
  module FiberRecycling
18
20
  end
@@ -1,6 +1,6 @@
1
1
  module FiberRecycling
2
2
 
3
- # this class only exists so we can type check backends
3
+ # this class only exists so we can verify backend type
4
4
  class FiberBackend
5
5
 
6
6
  end
@@ -1,6 +1,10 @@
1
1
  module FiberRecycling
2
2
  class NormalFiberBackend < FiberBackend
3
3
 
4
+ def self.finalizer(recycled_fiber_borrower)
5
+ Proc.new { recycled_fiber_borrower.return }
6
+ end
7
+
4
8
  def self.yield(*args)
5
9
  @state = 'suspended'
6
10
  return_value = RecycledFiber.yield(*args)
@@ -13,8 +17,10 @@ module FiberRecycling
13
17
  def initialize(fiber, block)
14
18
  @state = 'created'
15
19
  @variables = {}
16
- @recycled_fiber = RecycledFiberPool.local.release_recycled_fiber
20
+ @recycled_fiber_borrower = RecycledFiberPool.local.borrower
21
+ @recycled_fiber = @recycled_fiber_borrower.retrieve
17
22
  @recycled_fiber.run { execute(fiber, block) }
23
+ ObjectSpace.define_finalizer(self, self.class.finalizer(@recycled_fiber_borrower))
18
24
  end
19
25
 
20
26
  def alive?
@@ -47,7 +53,8 @@ module FiberRecycling
47
53
  return_value = block.call(*args)
48
54
  Thread.current[:fiber_recycling__fiber] = nil
49
55
  @state = 'terminated'
50
- RecycledFiberPool.local.absorb_recycled_fiber(@recycled_fiber)
56
+ @recycled_fiber = nil
57
+ @recycled_fiber_borrower.return
51
58
  return_value
52
59
  end
53
60
 
@@ -12,13 +12,17 @@ module FiberRecycling
12
12
  super(resource_class: RecycledFiber)
13
13
  end
14
14
 
15
- def release_recycled_fiber
16
- release_resource
17
- end
18
-
19
15
  def absorb_recycled_fiber(recycled_fiber)
20
16
  absorb_resource(recycled_fiber)
21
17
  end
18
+
19
+ def borrower
20
+ RecycledFiberPoolBorrower.new(self)
21
+ end
22
+
23
+ def release_recycled_fiber
24
+ release_resource
25
+ end
22
26
 
23
27
  end
24
28
  end
@@ -0,0 +1,24 @@
1
+ module FiberRecycling
2
+ class RecycledFiberPoolBorrower
3
+
4
+ def initialize(pool)
5
+ @pool = pool
6
+ @recycled_fiber = nil
7
+ end
8
+
9
+ def retrieve
10
+ @recycled_fiber ||= @pool.release_recycled_fiber
11
+ @recycled_fiber
12
+ end
13
+
14
+ def return
15
+ @pool.absorb_recycled_fiber(@recycled_fiber) if @recycled_fiber
16
+ nil
17
+ end
18
+
19
+ def returned?
20
+ @recycled_fiber
21
+ end
22
+
23
+ end
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fiber_recycling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Fors
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2018-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: quack_pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: thread_root_fiber
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,12 +53,13 @@ files:
39
53
  - lib/fiber_recycling/fiber.rb
40
54
  - lib/fiber_recycling/fiber_backend.rb
41
55
  - lib/fiber_recycling/fiber_error.rb
56
+ - lib/fiber_recycling/monkey_patches/thread.rb
42
57
  - lib/fiber_recycling/normal_fiber_backend.rb
43
58
  - lib/fiber_recycling/recycled_fiber.rb
44
59
  - lib/fiber_recycling/recycled_fiber_pool.rb
60
+ - lib/fiber_recycling/recycled_fiber_pool_borrower.rb
45
61
  - lib/fiber_recycling/root_fiber_backend.rb
46
62
  - lib/fiber_recycling/thread_extensions.rb
47
- - lib/thread.rb
48
63
  - spec/duck_types_spec.rb
49
64
  - spec/fiber_spec.rb
50
65
  - spec/spec_helper.rb