threadpuddle 0.1.1 → 0.2.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.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/forkingthread.rb +83 -0
  3. data/lib/threadpuddle.rb +20 -2
  4. metadata +11 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 269beb85fcb74159c1858a1dcec11d97b3b83195
4
- data.tar.gz: 74cdbf0130525d434fb45ba9dcc596f2b84ce3ca
2
+ SHA256:
3
+ metadata.gz: 0d27472beb80012887636388fc8bbd0b6ed36be4f7738475b9993d58377293d9
4
+ data.tar.gz: 9208b03f84e4c1453d5a4bb98a96853033113083fac805dd12d04201f1ec286c
5
5
  SHA512:
6
- metadata.gz: be373d8b0c50e0598e5fc245b969054f6c67d0ecdd977d225a1fa7fa459593b6acdffb97ee2a1bb42b5ce6311a6199f2c38a3100441e54219aec5e2ebb83377d
7
- data.tar.gz: a8118a8d59dbcbfea7a88a4d064bd14b833073a2c9f71202880d945fdd0272c1507636f26cb6cca235d751d07f2229034743296069266a65b6286402d659e175
6
+ metadata.gz: 0c27705dcdc33f30a6f5a5923215d02caeaa9a75af9a9fa7ee239bfea97c55050d184ed2ebbfe220183ab5d08e870f3a2eae8c87820b1ccb44f12ebeaf8b961e
7
+ data.tar.gz: 006b353b4423400a2250ee66b7cbf5683bbff4f81072f2dc7bd52457309e288413f96013bf8c33675a284a0365a4cf1d50eb8453297b4acbc7f51e059a0671c8
@@ -0,0 +1,83 @@
1
+
2
+ require 'timeout'
3
+
4
+ #
5
+ # A wrapper for forked processes that imitates some of the
6
+ # Thread class's interface.
7
+ #
8
+ class ForkingThread
9
+ def initialize *args, &blk
10
+ warn "ForkingThread#new doesn't support args" unless args.empty?
11
+
12
+ @pid = fork(&blk)
13
+ @dead = false
14
+ @status = nil
15
+ @name = nil
16
+ end
17
+
18
+ attr_accessor :name
19
+
20
+ def join limit=nil
21
+ return self if @dead
22
+ if limit.nil?
23
+ # wait indefinitely
24
+ result = Process.wait2(@pid, 0) rescue nil
25
+ elsif limit > 0
26
+ # wait for up to some number of seconds
27
+ result = Timeout::timeout(limit) { Process.wait2(@pid, 0) } rescue nil
28
+ else
29
+ # return immediately
30
+ result = Process.wait2(@pid, Process::WNOHANG) rescue nil
31
+ end
32
+ if result
33
+ @dead = true
34
+ @status = result[1]
35
+ return self
36
+ end
37
+ end
38
+
39
+ def kill
40
+ Process.kill('KILL', @pid) rescue nil
41
+ self
42
+ end
43
+
44
+ def alive?
45
+ return false if @dead
46
+ result = Process.wait2(@pid, Process::WNOHANG) rescue nil
47
+ if result
48
+ @dead = true
49
+ @status = result[1]
50
+ return false
51
+ end
52
+ true
53
+ end
54
+
55
+ def status
56
+ alive? ? "run" : (@status&.success? ? false : nil)
57
+ end
58
+
59
+ def stop?
60
+ !alive?
61
+ end
62
+
63
+ def native_thread_id
64
+ alive? ? @pid : nil
65
+ end
66
+ end
67
+
68
+ =begin
69
+ Copyright (c) 2026, Matthew Kerwin <matthew@kerwin.net.au>
70
+
71
+ Permission to use, copy, modify, and/or distribute this software for any
72
+ purpose with or without fee is hereby granted, provided that the above
73
+ copyright notice and this permission notice appear in all copies.
74
+
75
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
76
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
77
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
78
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
79
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
80
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
81
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
82
+ =end
83
+
data/lib/threadpuddle.rb CHANGED
@@ -1,10 +1,12 @@
1
+
1
2
  #
2
3
  # Like a smaller, lamer thread pool.
3
4
  #
4
5
  class ThreadPuddle
5
- def initialize capacity
6
+ def initialize capacity, klass=Thread
6
7
  @capacity = capacity
7
8
  @threads = []
9
+ @klass = klass
8
10
  end
9
11
 
10
12
  attr_reader :capacity
@@ -44,7 +46,7 @@ class ThreadPuddle
44
46
  # wait for a slot to open
45
47
  block
46
48
  # add the new thread
47
- @threads << (t = Thread.new(*args, &blk))
49
+ @threads << (t = @klass.new(*args, &blk))
48
50
  t
49
51
  end
50
52
 
@@ -81,3 +83,19 @@ private
81
83
 
82
84
  end
83
85
 
86
+ =begin
87
+ Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
88
+
89
+ Permission to use, copy, modify, and/or distribute this software for any
90
+ purpose with or without fee is hereby granted, provided that the above
91
+ copyright notice and this permission notice appear in all copies.
92
+
93
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
94
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
95
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
96
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
97
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
98
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
99
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
100
+ =end
101
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: threadpuddle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Kerwin
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-17 00:00:00.000000000 Z
11
+ date: 2026-04-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Create threads, with an upper bound on how many can exist at the same
14
14
  time.
@@ -18,29 +18,29 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - lib/forkingthread.rb
21
22
  - lib/threadpuddle.rb
22
- homepage: http://rubygems.org/gems/threadpuddle
23
+ homepage: https://rubygems.org/gems/threadpuddle
23
24
  licenses:
24
- - Simplified BSD License
25
+ - ISC
25
26
  metadata: {}
26
- post_install_message:
27
+ post_install_message:
27
28
  rdoc_options: []
28
29
  require_paths:
29
30
  - lib
30
31
  required_ruby_version: !ruby/object:Gem::Requirement
31
32
  requirements:
32
- - - '>='
33
+ - - ">="
33
34
  - !ruby/object:Gem::Version
34
35
  version: '0'
35
36
  required_rubygems_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
- - - '>='
38
+ - - ">="
38
39
  - !ruby/object:Gem::Version
39
40
  version: '0'
40
41
  requirements: []
41
- rubyforge_project:
42
- rubygems_version: 2.0.2
43
- signing_key:
42
+ rubygems_version: 3.3.15
43
+ signing_key:
44
44
  specification_version: 4
45
45
  summary: Like a smaller, lamer thread pool.
46
46
  test_files: []