rbs 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +23 -0
- data/README.md +6 -1
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +472 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1059 -139
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2315 -316
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/cli.rb +45 -1
- data/lib/rbs/definition_builder.rb +5 -1
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +59 -0
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1146 -144
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/net-http.rbs +1492 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +420 -106
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +556 -327
- data/stdlib/uri/0/http.rbs +26 -115
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +17 -5
- data/lib/rbs/location.rb +0 -221
data/core/thread.rbs
CHANGED
@@ -1,292 +1,373 @@
|
|
1
|
+
# <!-- rdoc-file=vm.c -->
|
1
2
|
# Threads are the Ruby implementation for a concurrent programming model.
|
2
3
|
#
|
3
|
-
# Programs that require multiple threads of execution are a perfect
|
4
|
-
#
|
4
|
+
# Programs that require multiple threads of execution are a perfect candidate
|
5
|
+
# for Ruby's Thread class.
|
5
6
|
#
|
6
7
|
# For example, we can create a new thread separate from the main thread's
|
7
|
-
# execution using
|
8
|
+
# execution using ::new.
|
8
9
|
#
|
9
|
-
#
|
10
|
-
# thr = Thread.new { puts "Whats the big deal" }
|
11
|
-
# ```
|
10
|
+
# thr = Thread.new { puts "What's the big deal" }
|
12
11
|
#
|
13
|
-
# Then we are able to pause the execution of the main thread and allow our
|
14
|
-
#
|
15
|
-
# [join](Thread#method-i-join):
|
12
|
+
# Then we are able to pause the execution of the main thread and allow our new
|
13
|
+
# thread to finish, using #join:
|
16
14
|
#
|
17
|
-
#
|
18
|
-
# thr.join #=> "Whats the big deal"
|
19
|
-
# ```
|
15
|
+
# thr.join #=> "What's the big deal"
|
20
16
|
#
|
21
|
-
# If we don't call `thr.join` before the main thread terminates, then all
|
22
|
-
#
|
17
|
+
# If we don't call `thr.join` before the main thread terminates, then all other
|
18
|
+
# threads including `thr` will be killed.
|
23
19
|
#
|
24
|
-
# Alternatively, you can use an array for handling multiple threads at
|
25
|
-
#
|
20
|
+
# Alternatively, you can use an array for handling multiple threads at once,
|
21
|
+
# like in the following example:
|
26
22
|
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
# threads << Thread.new { 3.times { puts "Threads are fun!" } }
|
31
|
-
# ```
|
23
|
+
# threads = []
|
24
|
+
# threads << Thread.new { puts "What's the big deal" }
|
25
|
+
# threads << Thread.new { 3.times { puts "Threads are fun!" } }
|
32
26
|
#
|
33
|
-
# After creating a few threads we wait for them all to finish
|
34
|
-
# consecutively.
|
27
|
+
# After creating a few threads we wait for them all to finish consecutively.
|
35
28
|
#
|
36
|
-
#
|
37
|
-
# threads.each { |thr| thr.join }
|
38
|
-
# ```
|
29
|
+
# threads.each { |thr| thr.join }
|
39
30
|
#
|
31
|
+
# To retrieve the last value of a thread, use #value
|
40
32
|
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# [::start](Thread#method-c-start), and
|
44
|
-
# [::fork](Thread#method-c-fork). A block must be
|
45
|
-
# provided with each of these methods, otherwise a
|
46
|
-
# [ThreadError](https://ruby-doc.org/core-2.6.3/ThreadError.html) will be
|
47
|
-
# raised.
|
33
|
+
# thr = Thread.new { sleep 1; "Useful value" }
|
34
|
+
# thr.value #=> "Useful value"
|
48
35
|
#
|
49
|
-
#
|
50
|
-
# `initialize` method of your subclass will be ignored by
|
51
|
-
# [::start](Thread#method-c-start) and
|
52
|
-
# [::fork](Thread#method-c-fork). Otherwise, be sure
|
53
|
-
# to call super in your `initialize` method.
|
36
|
+
# ### Thread initialization
|
54
37
|
#
|
38
|
+
# In order to create new threads, Ruby provides ::new, ::start, and ::fork. A
|
39
|
+
# block must be provided with each of these methods, otherwise a ThreadError
|
40
|
+
# will be raised.
|
41
|
+
#
|
42
|
+
# When subclassing the Thread class, the `initialize` method of your subclass
|
43
|
+
# will be ignored by ::start and ::fork. Otherwise, be sure to call super in
|
44
|
+
# your `initialize` method.
|
45
|
+
#
|
46
|
+
# ### Thread termination
|
55
47
|
#
|
56
48
|
# For terminating threads, Ruby provides a variety of ways to do this.
|
57
49
|
#
|
58
|
-
# The class method
|
59
|
-
# meant to exit a given thread:
|
50
|
+
# The class method ::kill, is meant to exit a given thread:
|
60
51
|
#
|
61
|
-
# thr = Thread.new {
|
52
|
+
# thr = Thread.new { sleep }
|
62
53
|
# Thread.kill(thr) # sends exit() to thr
|
63
54
|
#
|
64
|
-
# Alternatively, you can use the instance method
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
# to
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
# scope of the thread. Given the following example:
|
120
|
-
#
|
121
|
-
# ```ruby
|
122
|
-
# Thread.new{
|
123
|
-
# Thread.current.thread_variable_set(:foo, 1)
|
124
|
-
# p Thread.current.thread_variable_get(:foo) # => 1
|
125
|
-
# Fiber.new{
|
126
|
-
# Thread.current.thread_variable_set(:foo, 2)
|
127
|
-
# p Thread.current.thread_variable_get(:foo) # => 2
|
128
|
-
# }.resume
|
129
|
-
# p Thread.current.thread_variable_get(:foo) # => 2
|
130
|
-
# }.join
|
131
|
-
# ```
|
132
|
-
#
|
133
|
-
# You can see that the thread-local `:foo` carried over into the fiber and
|
134
|
-
# was changed to `2` by the end of the thread.
|
135
|
-
#
|
136
|
-
# This example makes use of
|
137
|
-
# [thread\_variable\_set](Thread#method-i-thread_variable_set)
|
138
|
-
# to create new thread-locals, and
|
139
|
-
# [thread\_variable\_get](Thread#method-i-thread_variable_get)
|
140
|
-
# to reference them.
|
141
|
-
#
|
142
|
-
# There is also
|
143
|
-
# [thread\_variables](Thread#method-i-thread_variables)
|
144
|
-
# to list all thread-locals, and
|
145
|
-
# [thread\_variable?](Thread#method-i-thread_variable-3F)
|
146
|
-
# to check if a given thread-local exists.
|
147
|
-
#
|
148
|
-
#
|
149
|
-
# Any thread can raise an exception using the
|
150
|
-
# [raise](Thread#method-i-raise) instance method,
|
151
|
-
# which operates similarly to
|
152
|
-
# [Kernel\#raise](https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-raise)
|
153
|
-
# .
|
154
|
-
#
|
155
|
-
# However, it's important to note that an exception that occurs in any
|
156
|
-
# thread except the main thread depends on
|
157
|
-
# [abort\_on\_exception](Thread#method-i-abort_on_exception)
|
158
|
-
# . This option is `false` by default, meaning that any unhandled
|
159
|
-
# exception will cause the thread to terminate silently when waited on by
|
160
|
-
# either [join](Thread#method-i-join) or
|
161
|
-
# [value](Thread#method-i-value). You can change this
|
162
|
-
# default by either
|
163
|
-
# [abort\_on\_exception=](Thread#method-i-abort_on_exception-3D)
|
164
|
-
# `true` or setting $DEBUG to `true` .
|
165
|
-
#
|
166
|
-
# With the addition of the class method
|
167
|
-
# [::handle\_interrupt](Thread#method-c-handle_interrupt)
|
168
|
-
# , you can now handle exceptions asynchronously with threads.
|
55
|
+
# Alternatively, you can use the instance method #exit, or any of its aliases
|
56
|
+
# #kill or #terminate.
|
57
|
+
#
|
58
|
+
# thr.exit
|
59
|
+
#
|
60
|
+
# ### Thread status
|
61
|
+
#
|
62
|
+
# Ruby provides a few instance methods for querying the state of a given thread.
|
63
|
+
# To get a string with the current thread's state use #status
|
64
|
+
#
|
65
|
+
# thr = Thread.new { sleep }
|
66
|
+
# thr.status # => "sleep"
|
67
|
+
# thr.exit
|
68
|
+
# thr.status # => false
|
69
|
+
#
|
70
|
+
# You can also use #alive? to tell if the thread is running or sleeping, and
|
71
|
+
# #stop? if the thread is dead or sleeping.
|
72
|
+
#
|
73
|
+
# ### Thread variables and scope
|
74
|
+
#
|
75
|
+
# Since threads are created with blocks, the same rules apply to other Ruby
|
76
|
+
# blocks for variable scope. Any local variables created within this block are
|
77
|
+
# accessible to only this thread.
|
78
|
+
#
|
79
|
+
# #### Fiber-local vs. Thread-local
|
80
|
+
#
|
81
|
+
# Each fiber has its own bucket for Thread#[] storage. When you set a new
|
82
|
+
# fiber-local it is only accessible within this Fiber. To illustrate:
|
83
|
+
#
|
84
|
+
# Thread.new {
|
85
|
+
# Thread.current[:foo] = "bar"
|
86
|
+
# Fiber.new {
|
87
|
+
# p Thread.current[:foo] # => nil
|
88
|
+
# }.resume
|
89
|
+
# }.join
|
90
|
+
#
|
91
|
+
# This example uses #[] for getting and #[]= for setting fiber-locals, you can
|
92
|
+
# also use #keys to list the fiber-locals for a given thread and #key? to check
|
93
|
+
# if a fiber-local exists.
|
94
|
+
#
|
95
|
+
# When it comes to thread-locals, they are accessible within the entire scope of
|
96
|
+
# the thread. Given the following example:
|
97
|
+
#
|
98
|
+
# Thread.new{
|
99
|
+
# Thread.current.thread_variable_set(:foo, 1)
|
100
|
+
# p Thread.current.thread_variable_get(:foo) # => 1
|
101
|
+
# Fiber.new{
|
102
|
+
# Thread.current.thread_variable_set(:foo, 2)
|
103
|
+
# p Thread.current.thread_variable_get(:foo) # => 2
|
104
|
+
# }.resume
|
105
|
+
# p Thread.current.thread_variable_get(:foo) # => 2
|
106
|
+
# }.join
|
107
|
+
#
|
108
|
+
# You can see that the thread-local `:foo` carried over into the fiber and was
|
109
|
+
# changed to `2` by the end of the thread.
|
169
110
|
#
|
111
|
+
# This example makes use of #thread_variable_set to create new thread-locals,
|
112
|
+
# and #thread_variable_get to reference them.
|
113
|
+
#
|
114
|
+
# There is also #thread_variables to list all thread-locals, and
|
115
|
+
# #thread_variable? to check if a given thread-local exists.
|
116
|
+
#
|
117
|
+
# ### Exception handling
|
118
|
+
#
|
119
|
+
# When an unhandled exception is raised inside a thread, it will terminate. By
|
120
|
+
# default, this exception will not propagate to other threads. The exception is
|
121
|
+
# stored and when another thread calls #value or #join, the exception will be
|
122
|
+
# re-raised in that thread.
|
123
|
+
#
|
124
|
+
# t = Thread.new{ raise 'something went wrong' }
|
125
|
+
# t.value #=> RuntimeError: something went wrong
|
126
|
+
#
|
127
|
+
# An exception can be raised from outside the thread using the Thread#raise
|
128
|
+
# instance method, which takes the same parameters as Kernel#raise.
|
129
|
+
#
|
130
|
+
# Setting Thread.abort_on_exception = true, Thread#abort_on_exception = true, or
|
131
|
+
# $DEBUG = true will cause a subsequent unhandled exception raised in a thread
|
132
|
+
# to be automatically re-raised in the main thread.
|
133
|
+
#
|
134
|
+
# With the addition of the class method ::handle_interrupt, you can now handle
|
135
|
+
# exceptions asynchronously with threads.
|
136
|
+
#
|
137
|
+
# ### Scheduling
|
170
138
|
#
|
171
139
|
# Ruby provides a few ways to support scheduling threads in your program.
|
172
140
|
#
|
173
|
-
# The first way is by using the class method
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
# the OS
|
184
|
-
#
|
185
|
-
# you hint to the thread scheduler which threads you want to take
|
186
|
-
# precedence when passing execution. This method is also dependent on the
|
187
|
-
# OS and may be ignored on some platforms.
|
141
|
+
# The first way is by using the class method ::stop, to put the current running
|
142
|
+
# thread to sleep and schedule the execution of another thread.
|
143
|
+
#
|
144
|
+
# Once a thread is asleep, you can use the instance method #wakeup to mark your
|
145
|
+
# thread as eligible for scheduling.
|
146
|
+
#
|
147
|
+
# You can also try ::pass, which attempts to pass execution to another thread
|
148
|
+
# but is dependent on the OS whether a running thread will switch or not. The
|
149
|
+
# same goes for #priority, which lets you hint to the thread scheduler which
|
150
|
+
# threads you want to take precedence when passing execution. This method is
|
151
|
+
# also dependent on the OS and may be ignored on some platforms.
|
152
|
+
#
|
188
153
|
class Thread < Object
|
154
|
+
# <!--
|
155
|
+
# rdoc-file=thread.c
|
156
|
+
# - Thread.current -> thread
|
157
|
+
# -->
|
158
|
+
# Returns the currently executing thread.
|
159
|
+
#
|
160
|
+
# Thread.current #=> #<Thread:0x401bdf4c run>
|
161
|
+
#
|
189
162
|
def self.current: () -> Thread
|
190
163
|
|
164
|
+
# <!--
|
165
|
+
# rdoc-file=thread.c
|
166
|
+
# - Thread.main -> thread
|
167
|
+
# -->
|
191
168
|
# Returns the main thread.
|
169
|
+
#
|
192
170
|
def self.main: () -> Thread
|
193
171
|
|
172
|
+
# <!--
|
173
|
+
# rdoc-file=thread.c
|
174
|
+
# - thr[sym] -> obj or nil
|
175
|
+
# -->
|
176
|
+
# Attribute Reference---Returns the value of a fiber-local variable (current
|
177
|
+
# thread's root fiber if not explicitly inside a Fiber), using either a symbol
|
178
|
+
# or a string name. If the specified variable does not exist, returns `nil`.
|
179
|
+
#
|
180
|
+
# [
|
181
|
+
# Thread.new { Thread.current["name"] = "A" },
|
182
|
+
# Thread.new { Thread.current[:name] = "B" },
|
183
|
+
# Thread.new { Thread.current["name"] = "C" }
|
184
|
+
# ].each do |th|
|
185
|
+
# th.join
|
186
|
+
# puts "#{th.inspect}: #{th[:name]}"
|
187
|
+
# end
|
188
|
+
#
|
189
|
+
# This will produce:
|
190
|
+
#
|
191
|
+
# #<Thread:0x00000002a54220 dead>: A
|
192
|
+
# #<Thread:0x00000002a541a8 dead>: B
|
193
|
+
# #<Thread:0x00000002a54130 dead>: C
|
194
|
+
#
|
195
|
+
# Thread#[] and Thread#[]= are not thread-local but fiber-local. This confusion
|
196
|
+
# did not exist in Ruby 1.8 because fibers are only available since Ruby 1.9.
|
197
|
+
# Ruby 1.9 chooses that the methods behaves fiber-local to save following idiom
|
198
|
+
# for dynamic scope.
|
199
|
+
#
|
200
|
+
# def meth(newvalue)
|
201
|
+
# begin
|
202
|
+
# oldvalue = Thread.current[:name]
|
203
|
+
# Thread.current[:name] = newvalue
|
204
|
+
# yield
|
205
|
+
# ensure
|
206
|
+
# Thread.current[:name] = oldvalue
|
207
|
+
# end
|
208
|
+
# end
|
209
|
+
#
|
210
|
+
# The idiom may not work as dynamic scope if the methods are thread-local and a
|
211
|
+
# given block switches fiber.
|
212
|
+
#
|
213
|
+
# f = Fiber.new {
|
214
|
+
# meth(1) {
|
215
|
+
# Fiber.yield
|
216
|
+
# }
|
217
|
+
# }
|
218
|
+
# meth(2) {
|
219
|
+
# f.resume
|
220
|
+
# }
|
221
|
+
# f.resume
|
222
|
+
# p Thread.current[:name]
|
223
|
+
# #=> nil if fiber-local
|
224
|
+
# #=> 2 if thread-local (The value 2 is leaked to outside of meth method.)
|
225
|
+
#
|
226
|
+
# For thread-local variables, please see #thread_variable_get and
|
227
|
+
# #thread_variable_set.
|
228
|
+
#
|
194
229
|
def []: (String | Symbol key) -> untyped
|
195
230
|
|
196
|
-
#
|
197
|
-
#
|
231
|
+
# <!--
|
232
|
+
# rdoc-file=thread.c
|
233
|
+
# - thr[sym] = obj -> obj
|
234
|
+
# -->
|
235
|
+
# Attribute Assignment---Sets or creates the value of a fiber-local variable,
|
236
|
+
# using either a symbol or a string.
|
237
|
+
#
|
238
|
+
# See also Thread#[].
|
198
239
|
#
|
199
|
-
#
|
240
|
+
# For thread-local variables, please see #thread_variable_set and
|
241
|
+
# #thread_variable_get.
|
200
242
|
#
|
201
|
-
# For thread-local variables, please see
|
202
|
-
# [thread\_variable\_set](Thread.downloaded.ruby_doc#method-i-thread_variable_set)
|
203
|
-
# and
|
204
|
-
# [thread\_variable\_get](Thread.downloaded.ruby_doc#method-i-thread_variable_get)
|
205
|
-
# .
|
206
243
|
def []=: (String | Symbol key, untyped value) -> untyped
|
207
244
|
|
245
|
+
# <!--
|
246
|
+
# rdoc-file=thread.c
|
247
|
+
# - thr.alive? -> true or false
|
248
|
+
# -->
|
249
|
+
# Returns `true` if `thr` is running or sleeping.
|
250
|
+
#
|
251
|
+
# thr = Thread.new { }
|
252
|
+
# thr.join #=> #<Thread:0x401b3fb0 dead>
|
253
|
+
# Thread.current.alive? #=> true
|
254
|
+
# thr.alive? #=> false
|
255
|
+
#
|
256
|
+
# See also #stop? and #status.
|
257
|
+
#
|
208
258
|
def alive?: () -> bool
|
209
259
|
|
210
|
-
#
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
260
|
+
# <!--
|
261
|
+
# rdoc-file=thread.c
|
262
|
+
# - thr.exit -> thr
|
263
|
+
# - thr.kill -> thr
|
264
|
+
# - thr.terminate -> thr
|
265
|
+
# -->
|
266
|
+
# Terminates `thr` and schedules another thread to be run, returning the
|
267
|
+
# terminated Thread. If this is the main thread, or the last thread, exits the
|
268
|
+
# process.
|
215
269
|
#
|
216
|
-
# If this is the main thread, or the last thread, exits the process.
|
217
270
|
def kill: () -> Thread?
|
218
271
|
|
219
|
-
#
|
220
|
-
#
|
272
|
+
# <!--
|
273
|
+
# rdoc-file=thread.c
|
274
|
+
# - thr.abort_on_exception -> true or false
|
275
|
+
# -->
|
276
|
+
# Returns the status of the thread-local ``abort on exception'' condition for
|
277
|
+
# this `thr`.
|
221
278
|
#
|
222
|
-
# The default is `false
|
279
|
+
# The default is `false`.
|
223
280
|
#
|
224
|
-
# See also
|
225
|
-
# [abort\_on\_exception=](Thread.downloaded.ruby_doc#method-i-abort_on_exception-3D)
|
226
|
-
# .
|
281
|
+
# See also #abort_on_exception=.
|
227
282
|
#
|
228
283
|
# There is also a class level method to set this for all threads, see
|
229
|
-
#
|
230
|
-
#
|
284
|
+
# ::abort_on_exception.
|
285
|
+
#
|
231
286
|
def abort_on_exception: () -> bool
|
232
287
|
|
233
|
-
#
|
234
|
-
#
|
288
|
+
# <!--
|
289
|
+
# rdoc-file=thread.c
|
290
|
+
# - thr.abort_on_exception= boolean -> true or false
|
291
|
+
# -->
|
292
|
+
# When set to `true`, if this `thr` is aborted by an exception, the raised
|
293
|
+
# exception will be re-raised in the main thread.
|
235
294
|
#
|
236
|
-
# See also
|
237
|
-
# [abort\_on\_exception](Thread.downloaded.ruby_doc#method-i-abort_on_exception)
|
238
|
-
# .
|
295
|
+
# See also #abort_on_exception.
|
239
296
|
#
|
240
297
|
# There is also a class level method to set this for all threads, see
|
241
|
-
#
|
242
|
-
#
|
298
|
+
# ::abort_on_exception=.
|
299
|
+
#
|
243
300
|
def abort_on_exception=: (boolish abort_on_exception) -> untyped
|
244
301
|
|
302
|
+
# <!--
|
303
|
+
# rdoc-file=vm_trace.c
|
304
|
+
# - thr.add_trace_func(proc) -> proc
|
305
|
+
# -->
|
245
306
|
# Adds *proc* as a handler for tracing.
|
246
307
|
#
|
247
|
-
# See
|
248
|
-
#
|
249
|
-
# and
|
250
|
-
# [Kernel\#set\_trace\_func](https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-set_trace_func)
|
251
|
-
# .
|
308
|
+
# See Thread#set_trace_func and Kernel#set_trace_func.
|
309
|
+
#
|
252
310
|
def add_trace_func: (untyped proc) -> untyped
|
253
311
|
|
312
|
+
# <!--
|
313
|
+
# rdoc-file=thread.c
|
314
|
+
# - thread.backtrace -> array or nil
|
315
|
+
# -->
|
254
316
|
# Returns the current backtrace of the target thread.
|
317
|
+
#
|
255
318
|
def backtrace: (*untyped args) -> ::Array[untyped]
|
256
319
|
|
257
|
-
#
|
320
|
+
# <!--
|
321
|
+
# rdoc-file=thread.c
|
322
|
+
# - thread.backtrace_locations(*args) -> array or nil
|
323
|
+
# -->
|
324
|
+
# Returns the execution stack for the target thread---an array containing
|
258
325
|
# backtrace location objects.
|
259
326
|
#
|
260
|
-
# See
|
261
|
-
#
|
262
|
-
#
|
327
|
+
# See Thread::Backtrace::Location for more information.
|
328
|
+
#
|
329
|
+
# This method behaves similarly to Kernel#caller_locations except it applies to
|
330
|
+
# a specific thread.
|
263
331
|
#
|
264
|
-
# This method behaves similarly to
|
265
|
-
# [Kernel\#caller\_locations](https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-caller_locations)
|
266
|
-
# except it applies to a specific thread.
|
267
332
|
def backtrace_locations: (*untyped args) -> ::Array[untyped]?
|
268
333
|
|
269
|
-
#
|
270
|
-
#
|
271
|
-
# If this thread
|
272
|
-
#
|
273
|
-
# [Thread](Thread.downloaded.ruby_doc).
|
334
|
+
# <!-- rdoc-file=thread.c -->
|
335
|
+
# Terminates `thr` and schedules another thread to be run, returning the
|
336
|
+
# terminated Thread. If this is the main thread, or the last thread, exits the
|
337
|
+
# process.
|
274
338
|
#
|
275
|
-
# If this is the main thread, or the last thread, exits the process.
|
276
339
|
def exit: () -> Thread?
|
277
340
|
|
278
|
-
#
|
279
|
-
#
|
280
|
-
#
|
281
|
-
#
|
282
|
-
#
|
283
|
-
#
|
284
|
-
#
|
285
|
-
#
|
341
|
+
# <!--
|
342
|
+
# rdoc-file=thread.c
|
343
|
+
# - thr.fetch(sym) -> obj
|
344
|
+
# - thr.fetch(sym) { } -> obj
|
345
|
+
# - thr.fetch(sym, default) -> obj
|
346
|
+
# -->
|
347
|
+
# Returns a fiber-local for the given key. If the key can't be found, there are
|
348
|
+
# several options: With no other arguments, it will raise a KeyError exception;
|
349
|
+
# if *default* is given, then that will be returned; if the optional code block
|
350
|
+
# is specified, then that will be run and its result returned. See Thread#[]
|
351
|
+
# and Hash#fetch.
|
352
|
+
#
|
286
353
|
def fetch: (*untyped sym) -> untyped
|
287
354
|
|
355
|
+
# <!--
|
356
|
+
# rdoc-file=thread.c
|
357
|
+
# - thr.group -> thgrp or nil
|
358
|
+
# -->
|
359
|
+
# Returns the ThreadGroup which contains the given thread.
|
360
|
+
#
|
361
|
+
# Thread.main.group #=> #<ThreadGroup:0x4029d914>
|
362
|
+
#
|
288
363
|
def group: () -> ThreadGroup?
|
289
364
|
|
365
|
+
# <!--
|
366
|
+
# rdoc-file=thread.c
|
367
|
+
# - Thread.new { ... } -> thread
|
368
|
+
# - Thread.new(*args, &proc) -> thread
|
369
|
+
# - Thread.new(*args) { |args| ... } -> thread
|
370
|
+
# -->
|
290
371
|
# Creates a new thread executing the given block.
|
291
372
|
#
|
292
373
|
# Any `args` given to ::new will be passed to the block:
|
@@ -300,36 +381,37 @@ class Thread < Object
|
|
300
381
|
#
|
301
382
|
# If you're going to subclass Thread, be sure to call super in your `initialize`
|
302
383
|
# method, otherwise a ThreadError will be raised.
|
384
|
+
#
|
303
385
|
def initialize: (*untyped) { (*untyped) -> void } -> void
|
304
386
|
|
305
|
-
#
|
387
|
+
# <!--
|
388
|
+
# rdoc-file=thread.c
|
389
|
+
# - thr.join -> thr
|
390
|
+
# - thr.join(limit) -> thr
|
391
|
+
# -->
|
392
|
+
# The calling thread will suspend execution and run this `thr`.
|
306
393
|
#
|
307
|
-
# Does not return until `thr` exits or until the given `limit` seconds
|
308
|
-
#
|
394
|
+
# Does not return until `thr` exits or until the given `limit` seconds have
|
395
|
+
# passed.
|
309
396
|
#
|
310
397
|
# If the time limit expires, `nil` will be returned, otherwise `thr` is
|
311
398
|
# returned.
|
312
399
|
#
|
313
400
|
# Any threads not joined will be killed when the main program exits.
|
314
401
|
#
|
315
|
-
# If `thr` had previously raised an exception and the
|
316
|
-
#
|
317
|
-
#
|
318
|
-
# processed), it will be processed at this time.
|
402
|
+
# If `thr` had previously raised an exception and the ::abort_on_exception or
|
403
|
+
# $DEBUG flags are not set, (so the exception has not yet been processed), it
|
404
|
+
# will be processed at this time.
|
319
405
|
#
|
320
|
-
#
|
321
|
-
#
|
322
|
-
#
|
323
|
-
#
|
324
|
-
# #=> "axyz"
|
325
|
-
# ```
|
406
|
+
# a = Thread.new { print "a"; sleep(10); print "b"; print "c" }
|
407
|
+
# x = Thread.new { print "x"; Thread.pass; print "y"; print "z" }
|
408
|
+
# x.join # Let thread x finish, thread a will be killed on exit.
|
409
|
+
# #=> "axyz"
|
326
410
|
#
|
327
411
|
# The following example illustrates the `limit` parameter.
|
328
412
|
#
|
329
|
-
#
|
330
|
-
#
|
331
|
-
# puts "Waiting" until y.join(0.15)
|
332
|
-
# ```
|
413
|
+
# y = Thread.new { 4.times { sleep 0.1; puts 'tick... ' }}
|
414
|
+
# puts "Waiting" until y.join(0.15)
|
333
415
|
#
|
334
416
|
# This will produce:
|
335
417
|
#
|
@@ -339,126 +421,183 @@ class Thread < Object
|
|
339
421
|
# Waiting
|
340
422
|
# tick...
|
341
423
|
# tick...
|
424
|
+
#
|
342
425
|
def join: (*untyped limit) -> Thread
|
343
426
|
|
427
|
+
# <!--
|
428
|
+
# rdoc-file=thread.c
|
429
|
+
# - thr.key?(sym) -> true or false
|
430
|
+
# -->
|
344
431
|
# Returns `true` if the given string (or symbol) exists as a fiber-local
|
345
432
|
# variable.
|
346
433
|
#
|
347
|
-
#
|
348
|
-
#
|
349
|
-
#
|
350
|
-
#
|
351
|
-
#
|
352
|
-
# ```
|
434
|
+
# me = Thread.current
|
435
|
+
# me[:oliver] = "a"
|
436
|
+
# me.key?(:oliver) #=> true
|
437
|
+
# me.key?(:stanley) #=> false
|
438
|
+
#
|
353
439
|
def key?: (Symbol sym) -> bool
|
354
440
|
|
441
|
+
# <!--
|
442
|
+
# rdoc-file=thread.c
|
443
|
+
# - thr.keys -> array
|
444
|
+
# -->
|
445
|
+
# Returns an array of the names of the fiber-local variables (as Symbols).
|
446
|
+
#
|
447
|
+
# thr = Thread.new do
|
448
|
+
# Thread.current[:cat] = 'meow'
|
449
|
+
# Thread.current["dog"] = 'woof'
|
450
|
+
# end
|
451
|
+
# thr.join #=> #<Thread:0x401b3f10 dead>
|
452
|
+
# thr.keys #=> [:dog, :cat]
|
453
|
+
#
|
355
454
|
def keys: () -> ::Array[Symbol]
|
356
455
|
|
456
|
+
# <!--
|
457
|
+
# rdoc-file=thread.c
|
458
|
+
# - thr.name -> string
|
459
|
+
# -->
|
357
460
|
# show the name of the thread.
|
461
|
+
#
|
358
462
|
def name: () -> String
|
359
463
|
|
360
|
-
#
|
361
|
-
#
|
464
|
+
# <!--
|
465
|
+
# rdoc-file=thread.c
|
466
|
+
# - thr.name=(name) -> string
|
467
|
+
# -->
|
468
|
+
# set given name to the ruby thread. On some platform, it may set the name to
|
469
|
+
# pthread and/or kernel.
|
470
|
+
#
|
362
471
|
def name=: (untyped name) -> untyped
|
363
472
|
|
364
|
-
#
|
365
|
-
#
|
473
|
+
# <!--
|
474
|
+
# rdoc-file=thread.c
|
475
|
+
# - thr.native_thread_id -> integer
|
476
|
+
# -->
|
477
|
+
# Return the native thread ID which is used by the Ruby thread.
|
478
|
+
#
|
479
|
+
# The ID depends on the OS. (not POSIX thread ID returned by pthread_self(3))
|
480
|
+
# * On Linux it is TID returned by gettid(2).
|
481
|
+
# * On macOS it is the system-wide unique integral ID of thread returned by
|
482
|
+
# pthread_threadid_np(3).
|
483
|
+
# * On FreeBSD it is the unique integral ID of the thread returned by
|
484
|
+
# pthread_getthreadid_np(3).
|
485
|
+
# * On Windows it is the thread identifier returned by GetThreadId().
|
486
|
+
# * On other platforms, it raises NotImplementedError.
|
487
|
+
#
|
488
|
+
#
|
489
|
+
# NOTE: If the thread is not associated yet or already deassociated with a
|
490
|
+
# native thread, it returns *nil*. If the Ruby implementation uses M:N thread
|
491
|
+
# model, the ID may change depending on the timing.
|
492
|
+
#
|
493
|
+
def native_thread_id: () -> Integer
|
494
|
+
|
495
|
+
# <!--
|
496
|
+
# rdoc-file=thread.c
|
497
|
+
# - target_thread.pending_interrupt?(error = nil) -> true/false
|
498
|
+
# -->
|
499
|
+
# Returns whether or not the asynchronous queue is empty for the target thread.
|
366
500
|
#
|
367
501
|
# If `error` is given, then check only for `error` type deferred events.
|
368
502
|
#
|
369
|
-
# See
|
370
|
-
#
|
371
|
-
# for more information.
|
503
|
+
# See ::pending_interrupt? for more information.
|
504
|
+
#
|
372
505
|
def pending_interrupt?: (*untyped args) -> bool
|
373
506
|
|
374
|
-
#
|
375
|
-
#
|
376
|
-
#
|
377
|
-
#
|
507
|
+
# <!--
|
508
|
+
# rdoc-file=thread.c
|
509
|
+
# - thr.priority -> integer
|
510
|
+
# -->
|
511
|
+
# Returns the priority of *thr*. Default is inherited from the current thread
|
512
|
+
# which creating the new thread, or zero for the initial main thread;
|
513
|
+
# higher-priority thread will run more frequently than lower-priority threads
|
514
|
+
# (but lower-priority threads can also run).
|
378
515
|
#
|
379
|
-
# This is just hint for Ruby thread scheduler.
|
516
|
+
# This is just hint for Ruby thread scheduler. It may be ignored on some
|
380
517
|
# platform.
|
381
518
|
#
|
382
|
-
#
|
383
|
-
#
|
384
|
-
# ```
|
519
|
+
# Thread.current.priority #=> 0
|
520
|
+
#
|
385
521
|
def priority: () -> Integer
|
386
522
|
|
387
|
-
#
|
388
|
-
#
|
389
|
-
#
|
523
|
+
# <!--
|
524
|
+
# rdoc-file=thread.c
|
525
|
+
# - thr.priority= integer -> thr
|
526
|
+
# -->
|
527
|
+
# Sets the priority of *thr* to *integer*. Higher-priority threads will run more
|
528
|
+
# frequently than lower-priority threads (but lower-priority threads can also
|
529
|
+
# run).
|
390
530
|
#
|
391
|
-
# This is just hint for Ruby thread scheduler.
|
531
|
+
# This is just hint for Ruby thread scheduler. It may be ignored on some
|
392
532
|
# platform.
|
393
533
|
#
|
394
|
-
#
|
395
|
-
#
|
396
|
-
#
|
397
|
-
#
|
398
|
-
#
|
399
|
-
#
|
534
|
+
# count1 = count2 = 0
|
535
|
+
# a = Thread.new do
|
536
|
+
# loop { count1 += 1 }
|
537
|
+
# end
|
538
|
+
# a.priority = -1
|
539
|
+
#
|
540
|
+
# b = Thread.new do
|
541
|
+
# loop { count2 += 1 }
|
542
|
+
# end
|
543
|
+
# b.priority = -2
|
544
|
+
# sleep 1 #=> 1
|
545
|
+
# count1 #=> 622504
|
546
|
+
# count2 #=> 5832
|
400
547
|
#
|
401
|
-
# b = Thread.new do
|
402
|
-
# loop { count2 += 1 }
|
403
|
-
# end
|
404
|
-
# b.priority = -2
|
405
|
-
# sleep 1 #=> 1
|
406
|
-
# count1 #=> 622504
|
407
|
-
# count2 #=> 5832
|
408
|
-
# ```
|
409
548
|
def priority=: (Integer priority) -> untyped
|
410
549
|
|
411
|
-
#
|
412
|
-
#
|
550
|
+
# <!--
|
551
|
+
# rdoc-file=thread.c
|
552
|
+
# - thr.report_on_exception -> true or false
|
553
|
+
# -->
|
554
|
+
# Returns the status of the thread-local ``report on exception'' condition for
|
555
|
+
# this `thr`.
|
413
556
|
#
|
414
|
-
# The default value when creating a
|
415
|
-
#
|
416
|
-
# [::report\_on\_exception](Thread.downloaded.ruby_doc#method-c-report_on_exception)
|
417
|
-
# .
|
557
|
+
# The default value when creating a Thread is the value of the global flag
|
558
|
+
# Thread.report_on_exception.
|
418
559
|
#
|
419
|
-
# See also
|
420
|
-
# [report\_on\_exception=](Thread.downloaded.ruby_doc#method-i-report_on_exception-3D)
|
421
|
-
# .
|
560
|
+
# See also #report_on_exception=.
|
422
561
|
#
|
423
562
|
# There is also a class level method to set this for all new threads, see
|
424
|
-
#
|
425
|
-
#
|
563
|
+
# ::report_on_exception=.
|
564
|
+
#
|
426
565
|
def report_on_exception: () -> bool
|
427
566
|
|
428
|
-
#
|
429
|
-
#
|
430
|
-
#
|
431
|
-
#
|
567
|
+
# <!--
|
568
|
+
# rdoc-file=thread.c
|
569
|
+
# - thr.report_on_exception= boolean -> true or false
|
570
|
+
# -->
|
571
|
+
# When set to `true`, a message is printed on $stderr if an exception kills this
|
572
|
+
# `thr`. See ::report_on_exception for details.
|
432
573
|
#
|
433
|
-
# See also
|
434
|
-
# [report\_on\_exception](Thread.downloaded.ruby_doc#method-i-report_on_exception)
|
435
|
-
# .
|
574
|
+
# See also #report_on_exception.
|
436
575
|
#
|
437
576
|
# There is also a class level method to set this for all new threads, see
|
438
|
-
#
|
439
|
-
#
|
577
|
+
# ::report_on_exception=.
|
578
|
+
#
|
440
579
|
def report_on_exception=: (boolish report_on_exception) -> untyped
|
441
580
|
|
581
|
+
# <!--
|
582
|
+
# rdoc-file=thread.c
|
583
|
+
# - thr.run -> thr
|
584
|
+
# -->
|
442
585
|
# Wakes up `thr`, making it eligible for scheduling.
|
443
586
|
#
|
444
|
-
#
|
445
|
-
#
|
446
|
-
#
|
447
|
-
#
|
448
|
-
#
|
449
|
-
# a.join
|
450
|
-
# ```
|
587
|
+
# a = Thread.new { puts "a"; Thread.stop; puts "c" }
|
588
|
+
# sleep 0.1 while a.status!='sleep'
|
589
|
+
# puts "Got here"
|
590
|
+
# a.run
|
591
|
+
# a.join
|
451
592
|
#
|
452
593
|
# This will produce:
|
453
594
|
#
|
454
|
-
#
|
455
|
-
#
|
456
|
-
#
|
457
|
-
#
|
458
|
-
#
|
595
|
+
# a
|
596
|
+
# Got here
|
597
|
+
# c
|
598
|
+
#
|
599
|
+
# See also the instance method #wakeup.
|
459
600
|
#
|
460
|
-
# See also the instance method
|
461
|
-
# [wakeup](Thread.downloaded.ruby_doc#method-i-wakeup).
|
462
601
|
def run: () -> Thread
|
463
602
|
|
464
603
|
# Returns the safe level.
|
@@ -467,143 +606,201 @@ class Thread < Object
|
|
467
606
|
# check $SAFE.
|
468
607
|
def safe_level: () -> Integer
|
469
608
|
|
609
|
+
# <!--
|
610
|
+
# rdoc-file=thread.c
|
611
|
+
# - thr.status -> string, false or nil
|
612
|
+
# -->
|
613
|
+
# Returns the status of `thr`.
|
614
|
+
#
|
615
|
+
# `"sleep"`
|
616
|
+
# : Returned if this thread is sleeping or waiting on I/O
|
617
|
+
# `"run"`
|
618
|
+
# : When this thread is executing
|
619
|
+
# `"aborting"`
|
620
|
+
# : If this thread is aborting
|
621
|
+
# `false`
|
622
|
+
# : When this thread is terminated normally
|
623
|
+
# `nil`
|
624
|
+
# : If terminated with an exception.
|
625
|
+
#
|
626
|
+
#
|
627
|
+
# a = Thread.new { raise("die now") }
|
628
|
+
# b = Thread.new { Thread.stop }
|
629
|
+
# c = Thread.new { Thread.exit }
|
630
|
+
# d = Thread.new { sleep }
|
631
|
+
# d.kill #=> #<Thread:0x401b3678 aborting>
|
632
|
+
# a.status #=> nil
|
633
|
+
# b.status #=> "sleep"
|
634
|
+
# c.status #=> false
|
635
|
+
# d.status #=> "aborting"
|
636
|
+
# Thread.current.status #=> "run"
|
637
|
+
#
|
638
|
+
# See also the instance methods #alive? and #stop?
|
639
|
+
#
|
470
640
|
def status: () -> (String | bool)?
|
471
641
|
|
642
|
+
# <!--
|
643
|
+
# rdoc-file=thread.c
|
644
|
+
# - thr.stop? -> true or false
|
645
|
+
# -->
|
472
646
|
# Returns `true` if `thr` is dead or sleeping.
|
473
647
|
#
|
474
|
-
#
|
475
|
-
#
|
476
|
-
#
|
477
|
-
#
|
478
|
-
# b.stop? #=> false
|
479
|
-
# ```
|
648
|
+
# a = Thread.new { Thread.stop }
|
649
|
+
# b = Thread.current
|
650
|
+
# a.stop? #=> true
|
651
|
+
# b.stop? #=> false
|
480
652
|
#
|
481
|
-
# See also
|
482
|
-
# [status](Thread.downloaded.ruby_doc#method-i-status).
|
483
|
-
def `stop?`: () -> bool
|
484
|
-
|
485
|
-
# Terminates `thr` and schedules another thread to be run.
|
653
|
+
# See also #alive? and #status.
|
486
654
|
#
|
487
|
-
|
488
|
-
|
489
|
-
#
|
655
|
+
def stop?: () -> bool
|
656
|
+
|
657
|
+
# <!-- rdoc-file=thread.c -->
|
658
|
+
# Terminates `thr` and schedules another thread to be run, returning the
|
659
|
+
# terminated Thread. If this is the main thread, or the last thread, exits the
|
660
|
+
# process.
|
490
661
|
#
|
491
|
-
# If this is the main thread, or the last thread, exits the process.
|
492
662
|
def terminate: () -> Thread?
|
493
663
|
|
664
|
+
# <!--
|
665
|
+
# rdoc-file=thread.c
|
666
|
+
# - thr.thread_variable?(key) -> true or false
|
667
|
+
# -->
|
494
668
|
# Returns `true` if the given string (or symbol) exists as a thread-local
|
495
669
|
# variable.
|
496
670
|
#
|
497
|
-
#
|
498
|
-
#
|
499
|
-
#
|
500
|
-
#
|
501
|
-
#
|
502
|
-
#
|
503
|
-
#
|
504
|
-
#
|
505
|
-
# [\#\[\]](Thread.downloaded.ruby_doc#method-i-5B-5D) and
|
506
|
-
# [\#thread\_variable\_get](Thread.downloaded.ruby_doc#method-i-thread_variable_get)
|
507
|
-
# for more details.
|
671
|
+
# me = Thread.current
|
672
|
+
# me.thread_variable_set(:oliver, "a")
|
673
|
+
# me.thread_variable?(:oliver) #=> true
|
674
|
+
# me.thread_variable?(:stanley) #=> false
|
675
|
+
#
|
676
|
+
# Note that these are not fiber local variables. Please see Thread#[] and
|
677
|
+
# Thread#thread_variable_get for more details.
|
678
|
+
#
|
508
679
|
def thread_variable?: (String | Symbol key) -> bool
|
509
680
|
|
510
|
-
#
|
511
|
-
#
|
512
|
-
#
|
513
|
-
#
|
514
|
-
#
|
515
|
-
#
|
516
|
-
#
|
517
|
-
#
|
518
|
-
#
|
519
|
-
#
|
520
|
-
#
|
521
|
-
#
|
522
|
-
#
|
523
|
-
#
|
524
|
-
#
|
525
|
-
#
|
526
|
-
#
|
527
|
-
#
|
528
|
-
#
|
529
|
-
#
|
530
|
-
#
|
531
|
-
#
|
532
|
-
#
|
533
|
-
#
|
534
|
-
#
|
681
|
+
# <!--
|
682
|
+
# rdoc-file=thread.c
|
683
|
+
# - thr.thread_variable_get(key) -> obj or nil
|
684
|
+
# -->
|
685
|
+
# Returns the value of a thread local variable that has been set. Note that
|
686
|
+
# these are different than fiber local values. For fiber local values, please
|
687
|
+
# see Thread#[] and Thread#[]=.
|
688
|
+
#
|
689
|
+
# Thread local values are carried along with threads, and do not respect fibers.
|
690
|
+
# For example:
|
691
|
+
#
|
692
|
+
# Thread.new {
|
693
|
+
# Thread.current.thread_variable_set("foo", "bar") # set a thread local
|
694
|
+
# Thread.current["foo"] = "bar" # set a fiber local
|
695
|
+
#
|
696
|
+
# Fiber.new {
|
697
|
+
# Fiber.yield [
|
698
|
+
# Thread.current.thread_variable_get("foo"), # get the thread local
|
699
|
+
# Thread.current["foo"], # get the fiber local
|
700
|
+
# ]
|
701
|
+
# }.resume
|
702
|
+
# }.join.value # => ['bar', nil]
|
703
|
+
#
|
704
|
+
# The value "bar" is returned for the thread local, where nil is returned for
|
705
|
+
# the fiber local. The fiber is executed in the same thread, so the thread
|
706
|
+
# local values are available.
|
707
|
+
#
|
535
708
|
def thread_variable_get: (untyped key) -> untyped
|
536
709
|
|
537
|
-
#
|
538
|
-
#
|
539
|
-
#
|
540
|
-
#
|
541
|
-
#
|
710
|
+
# <!--
|
711
|
+
# rdoc-file=thread.c
|
712
|
+
# - thr.thread_variable_set(key, value)
|
713
|
+
# -->
|
714
|
+
# Sets a thread local with `key` to `value`. Note that these are local to
|
715
|
+
# threads, and not to fibers. Please see Thread#thread_variable_get and
|
716
|
+
# Thread#[] for more information.
|
717
|
+
#
|
542
718
|
def thread_variable_set: (untyped key, untyped value) -> untyped
|
543
719
|
|
720
|
+
# <!--
|
721
|
+
# rdoc-file=thread.c
|
722
|
+
# - thr.thread_variables -> array
|
723
|
+
# -->
|
724
|
+
# Returns an array of the names of the thread-local variables (as Symbols).
|
725
|
+
#
|
726
|
+
# thr = Thread.new do
|
727
|
+
# Thread.current.thread_variable_set(:cat, 'meow')
|
728
|
+
# Thread.current.thread_variable_set("dog", 'woof')
|
729
|
+
# end
|
730
|
+
# thr.join #=> #<Thread:0x401b3f10 dead>
|
731
|
+
# thr.thread_variables #=> [:dog, :cat]
|
732
|
+
#
|
733
|
+
# Note that these are not fiber local variables. Please see Thread#[] and
|
734
|
+
# Thread#thread_variable_get for more details.
|
735
|
+
#
|
544
736
|
def thread_variables: () -> ::Array[Symbol]
|
545
737
|
|
546
|
-
#
|
547
|
-
#
|
548
|
-
#
|
738
|
+
# <!--
|
739
|
+
# rdoc-file=thread.c
|
740
|
+
# - thr.value -> obj
|
741
|
+
# -->
|
742
|
+
# Waits for `thr` to complete, using #join, and returns its value or raises the
|
743
|
+
# exception which terminated the thread.
|
549
744
|
#
|
550
|
-
#
|
551
|
-
#
|
552
|
-
#
|
745
|
+
# a = Thread.new { 2 + 2 }
|
746
|
+
# a.value #=> 4
|
747
|
+
#
|
748
|
+
# b = Thread.new { raise 'something went wrong' }
|
749
|
+
# b.value #=> RuntimeError: something went wrong
|
553
750
|
#
|
554
|
-
# b = Thread.new { raise 'something went wrong' }
|
555
|
-
# b.value #=> RuntimeError: something went wrong
|
556
|
-
# ```
|
557
751
|
def value: () -> untyped
|
558
752
|
|
559
|
-
#
|
560
|
-
#
|
753
|
+
# <!--
|
754
|
+
# rdoc-file=thread.c
|
755
|
+
# - thr.wakeup -> thr
|
756
|
+
# -->
|
757
|
+
# Marks a given thread as eligible for scheduling, however it may still remain
|
758
|
+
# blocked on I/O.
|
759
|
+
#
|
760
|
+
# **Note:** This does not invoke the scheduler, see #run for more information.
|
561
761
|
#
|
562
|
-
#
|
563
|
-
#
|
762
|
+
# c = Thread.new { Thread.stop; puts "hey!" }
|
763
|
+
# sleep 0.1 while c.status!='sleep'
|
764
|
+
# c.wakeup
|
765
|
+
# c.join
|
766
|
+
# #=> "hey!"
|
564
767
|
#
|
565
|
-
# ```ruby
|
566
|
-
# c = Thread.new { Thread.stop; puts "hey!" }
|
567
|
-
# sleep 0.1 while c.status!='sleep'
|
568
|
-
# c.wakeup
|
569
|
-
# c.join
|
570
|
-
# #=> "hey!"
|
571
|
-
# ```
|
572
768
|
def wakeup: () -> Thread
|
573
769
|
|
574
|
-
#
|
770
|
+
# <!--
|
771
|
+
# rdoc-file=thread.c
|
772
|
+
# - Thread.abort_on_exception -> true or false
|
773
|
+
# -->
|
774
|
+
# Returns the status of the global ``abort on exception'' condition.
|
775
|
+
#
|
776
|
+
# The default is `false`.
|
575
777
|
#
|
576
|
-
#
|
778
|
+
# When set to `true`, if any thread is aborted by an exception, the raised
|
779
|
+
# exception will be re-raised in the main thread.
|
577
780
|
#
|
578
|
-
#
|
579
|
-
# raised exception will be re-raised in the main thread.
|
781
|
+
# Can also be specified by the global $DEBUG flag or command line option `-d`.
|
580
782
|
#
|
581
|
-
#
|
582
|
-
# `-d` .
|
783
|
+
# See also ::abort_on_exception=.
|
583
784
|
#
|
584
|
-
#
|
585
|
-
#
|
586
|
-
# .
|
785
|
+
# There is also an instance level method to set this for a specific thread, see
|
786
|
+
# #abort_on_exception.
|
587
787
|
#
|
588
|
-
# There is also an instance level method to set this for a specific
|
589
|
-
# thread, see
|
590
|
-
# [abort\_on\_exception](Thread.downloaded.ruby_doc#method-i-abort_on_exception)
|
591
|
-
# .
|
592
788
|
def self.abort_on_exception: () -> untyped
|
593
789
|
|
594
|
-
#
|
595
|
-
#
|
596
|
-
#
|
597
|
-
#
|
598
|
-
#
|
599
|
-
#
|
600
|
-
#
|
601
|
-
#
|
602
|
-
#
|
603
|
-
#
|
604
|
-
#
|
605
|
-
#
|
606
|
-
#
|
790
|
+
# <!--
|
791
|
+
# rdoc-file=thread.c
|
792
|
+
# - Thread.abort_on_exception= boolean -> true or false
|
793
|
+
# -->
|
794
|
+
# When set to `true`, if any thread is aborted by an exception, the raised
|
795
|
+
# exception will be re-raised in the main thread. Returns the new state.
|
796
|
+
#
|
797
|
+
# Thread.abort_on_exception = true
|
798
|
+
# t1 = Thread.new do
|
799
|
+
# puts "In new thread"
|
800
|
+
# raise "Exception from thread"
|
801
|
+
# end
|
802
|
+
# sleep(1)
|
803
|
+
# puts "not reached"
|
607
804
|
#
|
608
805
|
# This will produce:
|
609
806
|
#
|
@@ -613,14 +810,11 @@ class Thread < Object
|
|
613
810
|
# from prog.rb:2:in `new'
|
614
811
|
# from prog.rb:2
|
615
812
|
#
|
616
|
-
# See also
|
617
|
-
#
|
618
|
-
#
|
813
|
+
# See also ::abort_on_exception.
|
814
|
+
#
|
815
|
+
# There is also an instance level method to set this for a specific thread, see
|
816
|
+
# #abort_on_exception=.
|
619
817
|
#
|
620
|
-
# There is also an instance level method to set this for a specific
|
621
|
-
# thread, see
|
622
|
-
# [abort\_on\_exception=](Thread.downloaded.ruby_doc#method-i-abort_on_exception-3D)
|
623
|
-
# .
|
624
818
|
def self.abort_on_exception=: (untyped abort_on_exception) -> untyped
|
625
819
|
|
626
820
|
# Wraps the block in a single, VM-global
|
@@ -630,176 +824,212 @@ class Thread < Object
|
|
630
824
|
# [::exclusive](Thread.downloaded.ruby_doc#method-c-exclusive) mechanism.
|
631
825
|
def self.exclusive: () { () -> untyped } -> untyped
|
632
826
|
|
633
|
-
#
|
634
|
-
#
|
827
|
+
# <!--
|
828
|
+
# rdoc-file=thread.c
|
829
|
+
# - Thread.exit -> thread
|
830
|
+
# -->
|
831
|
+
# Terminates the currently running thread and schedules another thread to be
|
832
|
+
# run.
|
833
|
+
#
|
834
|
+
# If this thread is already marked to be killed, ::exit returns the Thread.
|
635
835
|
#
|
636
|
-
# If this thread
|
637
|
-
# [::exit](Thread.downloaded.ruby_doc#method-c-exit) returns the
|
638
|
-
# [Thread](Thread.downloaded.ruby_doc).
|
836
|
+
# If this is the main thread, or the last thread, exit the process.
|
639
837
|
#
|
640
|
-
# If this is the main thread, or the last thread, exit the process.
|
641
838
|
def self.exit: () -> untyped
|
642
839
|
|
643
|
-
#
|
644
|
-
#
|
645
|
-
#
|
646
|
-
#
|
840
|
+
# <!--
|
841
|
+
# rdoc-file=thread.c
|
842
|
+
# - Thread.start([args]*) {|args| block } -> thread
|
843
|
+
# - Thread.fork([args]*) {|args| block } -> thread
|
844
|
+
# -->
|
845
|
+
# Basically the same as ::new. However, if class Thread is subclassed, then
|
846
|
+
# calling `start` in that subclass will not invoke the subclass's `initialize`
|
847
|
+
# method.
|
848
|
+
#
|
647
849
|
def self.fork: (*untyped args) -> untyped
|
648
850
|
|
851
|
+
# <!--
|
852
|
+
# rdoc-file=thread.c
|
853
|
+
# - Thread.handle_interrupt(hash) { ... } -> result of the block
|
854
|
+
# -->
|
649
855
|
# Changes asynchronous interrupt timing.
|
650
856
|
#
|
651
857
|
# *interrupt* means asynchronous event and corresponding procedure by
|
652
|
-
#
|
653
|
-
#
|
654
|
-
# supported yet) and main thread termination (if main thread terminates,
|
655
|
-
# then all other thread will be killed).
|
656
|
-
#
|
657
|
-
# The given `hash` has pairs like `ExceptionClass => :TimingSymbol` .
|
658
|
-
# Where the ExceptionClass is the interrupt handled by the given block.
|
659
|
-
# The TimingSymbol can be one of the following symbols:
|
858
|
+
# Thread#raise, Thread#kill, signal trap (not supported yet) and main thread
|
859
|
+
# termination (if main thread terminates, then all other thread will be killed).
|
660
860
|
#
|
661
|
-
#
|
662
|
-
#
|
861
|
+
# The given `hash` has pairs like `ExceptionClass => :TimingSymbol`. Where the
|
862
|
+
# ExceptionClass is the interrupt handled by the given block. The TimingSymbol
|
863
|
+
# can be one of the following symbols:
|
663
864
|
#
|
664
|
-
#
|
665
|
-
#
|
865
|
+
# `:immediate`
|
866
|
+
# : Invoke interrupts immediately.
|
867
|
+
# `:on_blocking`
|
868
|
+
# : Invoke interrupts while *BlockingOperation*.
|
869
|
+
# `:never`
|
870
|
+
# : Never invoke all interrupts.
|
666
871
|
#
|
667
|
-
# - `:never`
|
668
|
-
# Never invoke all interrupts.
|
669
872
|
#
|
670
|
-
# *BlockingOperation* means that the operation will block the calling
|
671
|
-
#
|
672
|
-
#
|
873
|
+
# *BlockingOperation* means that the operation will block the calling thread,
|
874
|
+
# such as read and write. On CRuby implementation, *BlockingOperation* is any
|
875
|
+
# operation executed without GVL.
|
673
876
|
#
|
674
|
-
# Masked asynchronous interrupts are delayed until they are enabled. This
|
675
|
-
#
|
877
|
+
# Masked asynchronous interrupts are delayed until they are enabled. This method
|
878
|
+
# is similar to sigprocmask(3).
|
676
879
|
#
|
880
|
+
# ### NOTE
|
677
881
|
#
|
678
882
|
# Asynchronous interrupts are difficult to use.
|
679
883
|
#
|
680
|
-
# If you need to communicate between threads, please consider to use
|
681
|
-
#
|
682
|
-
# .
|
884
|
+
# If you need to communicate between threads, please consider to use another way
|
885
|
+
# such as Queue.
|
683
886
|
#
|
684
887
|
# Or use them with deep understanding about this method.
|
685
888
|
#
|
889
|
+
# ### Usage
|
686
890
|
#
|
687
|
-
# In this example, we can guard from
|
688
|
-
#
|
689
|
-
#
|
690
|
-
#
|
691
|
-
#
|
692
|
-
#
|
693
|
-
#
|
694
|
-
#
|
695
|
-
#
|
696
|
-
#
|
697
|
-
#
|
698
|
-
#
|
699
|
-
#
|
700
|
-
#
|
701
|
-
#
|
702
|
-
#
|
703
|
-
# # You can write resource allocation code safely.
|
704
|
-
# Thread.handle_interrupt(RuntimeError => :immediate) {
|
705
|
-
# # ...
|
891
|
+
# In this example, we can guard from Thread#raise exceptions.
|
892
|
+
#
|
893
|
+
# Using the `:never` TimingSymbol the RuntimeError exception will always be
|
894
|
+
# ignored in the first block of the main thread. In the second
|
895
|
+
# ::handle_interrupt block we can purposefully handle RuntimeError exceptions.
|
896
|
+
#
|
897
|
+
# th = Thread.new do
|
898
|
+
# Thread.handle_interrupt(RuntimeError => :never) {
|
899
|
+
# begin
|
900
|
+
# # You can write resource allocation code safely.
|
901
|
+
# Thread.handle_interrupt(RuntimeError => :immediate) {
|
902
|
+
# # ...
|
903
|
+
# }
|
904
|
+
# ensure
|
905
|
+
# # You can write resource deallocation code safely.
|
906
|
+
# end
|
706
907
|
# }
|
707
|
-
# ensure
|
708
|
-
# # You can write resource deallocation code safely.
|
709
908
|
# end
|
710
|
-
#
|
711
|
-
#
|
712
|
-
#
|
713
|
-
#
|
714
|
-
#
|
715
|
-
#
|
716
|
-
#
|
717
|
-
#
|
718
|
-
#
|
719
|
-
#
|
720
|
-
#
|
721
|
-
#
|
722
|
-
#
|
723
|
-
#
|
724
|
-
#
|
725
|
-
#
|
726
|
-
#
|
727
|
-
#
|
728
|
-
#
|
729
|
-
#
|
730
|
-
#
|
731
|
-
#
|
732
|
-
#
|
733
|
-
#
|
734
|
-
#
|
735
|
-
# # while blocking operation
|
909
|
+
# Thread.pass
|
910
|
+
# # ...
|
911
|
+
# th.raise "stop"
|
912
|
+
#
|
913
|
+
# While we are ignoring the RuntimeError exception, it's safe to write our
|
914
|
+
# resource allocation code. Then, the ensure block is where we can safely
|
915
|
+
# deallocate your resources.
|
916
|
+
#
|
917
|
+
# #### Guarding from Timeout::Error
|
918
|
+
#
|
919
|
+
# In the next example, we will guard from the Timeout::Error exception. This
|
920
|
+
# will help prevent from leaking resources when Timeout::Error exceptions occur
|
921
|
+
# during normal ensure clause. For this example we use the help of the standard
|
922
|
+
# library Timeout, from lib/timeout.rb
|
923
|
+
#
|
924
|
+
# require 'timeout'
|
925
|
+
# Thread.handle_interrupt(Timeout::Error => :never) {
|
926
|
+
# timeout(10){
|
927
|
+
# # Timeout::Error doesn't occur here
|
928
|
+
# Thread.handle_interrupt(Timeout::Error => :on_blocking) {
|
929
|
+
# # possible to be killed by Timeout::Error
|
930
|
+
# # while blocking operation
|
931
|
+
# }
|
932
|
+
# # Timeout::Error doesn't occur here
|
933
|
+
# }
|
736
934
|
# }
|
737
|
-
#
|
738
|
-
#
|
739
|
-
#
|
740
|
-
#
|
741
|
-
#
|
742
|
-
#
|
743
|
-
#
|
744
|
-
#
|
745
|
-
#
|
746
|
-
#
|
747
|
-
#
|
748
|
-
#
|
749
|
-
#
|
750
|
-
#
|
751
|
-
#
|
752
|
-
#
|
753
|
-
#
|
754
|
-
#
|
755
|
-
#
|
756
|
-
#
|
757
|
-
#
|
758
|
-
#
|
759
|
-
#
|
760
|
-
#
|
761
|
-
#
|
762
|
-
#
|
763
|
-
#
|
764
|
-
#
|
765
|
-
# ```ruby
|
766
|
-
# Thread.handle_interrupt(Exception => :never) {
|
767
|
-
# # all exceptions inherited from Exception are prohibited.
|
768
|
-
# }
|
769
|
-
# ```
|
935
|
+
#
|
936
|
+
# In the first part of the `timeout` block, we can rely on Timeout::Error being
|
937
|
+
# ignored. Then in the `Timeout::Error => :on_blocking` block, any operation
|
938
|
+
# that will block the calling thread is susceptible to a Timeout::Error
|
939
|
+
# exception being raised.
|
940
|
+
#
|
941
|
+
# #### Stack control settings
|
942
|
+
#
|
943
|
+
# It's possible to stack multiple levels of ::handle_interrupt blocks in order
|
944
|
+
# to control more than one ExceptionClass and TimingSymbol at a time.
|
945
|
+
#
|
946
|
+
# Thread.handle_interrupt(FooError => :never) {
|
947
|
+
# Thread.handle_interrupt(BarError => :never) {
|
948
|
+
# # FooError and BarError are prohibited.
|
949
|
+
# }
|
950
|
+
# }
|
951
|
+
#
|
952
|
+
# #### Inheritance with ExceptionClass
|
953
|
+
#
|
954
|
+
# All exceptions inherited from the ExceptionClass parameter will be considered.
|
955
|
+
#
|
956
|
+
# Thread.handle_interrupt(Exception => :never) {
|
957
|
+
# # all exceptions inherited from Exception are prohibited.
|
958
|
+
# }
|
959
|
+
#
|
960
|
+
# For handling all interrupts, use `Object` and not `Exception` as the
|
961
|
+
# ExceptionClass, as kill/terminate interrupts are not handled by `Exception`.
|
962
|
+
#
|
770
963
|
def self.handle_interrupt: (untyped hash) -> untyped
|
771
964
|
|
965
|
+
# <!--
|
966
|
+
# rdoc-file=thread.c
|
967
|
+
# - Thread.kill(thread) -> thread
|
968
|
+
# -->
|
969
|
+
# Causes the given `thread` to exit, see also Thread::exit.
|
970
|
+
#
|
971
|
+
# count = 0
|
972
|
+
# a = Thread.new { loop { count += 1 } }
|
973
|
+
# sleep(0.1) #=> 0
|
974
|
+
# Thread.kill(a) #=> #<Thread:0x401b3d30 dead>
|
975
|
+
# count #=> 93947
|
976
|
+
# a.alive? #=> false
|
977
|
+
#
|
772
978
|
def self.kill: (Thread thread) -> untyped
|
773
979
|
|
980
|
+
# <!--
|
981
|
+
# rdoc-file=thread.c
|
982
|
+
# - Thread.list -> array
|
983
|
+
# -->
|
984
|
+
# Returns an array of Thread objects for all threads that are either runnable or
|
985
|
+
# stopped.
|
986
|
+
#
|
987
|
+
# Thread.new { sleep(200) }
|
988
|
+
# Thread.new { 1000000.times {|i| i*i } }
|
989
|
+
# Thread.new { Thread.stop }
|
990
|
+
# Thread.list.each {|t| p t}
|
991
|
+
#
|
992
|
+
# This will produce:
|
993
|
+
#
|
994
|
+
# #<Thread:0x401b3e84 sleep>
|
995
|
+
# #<Thread:0x401b3f38 run>
|
996
|
+
# #<Thread:0x401b3fb0 sleep>
|
997
|
+
# #<Thread:0x401bdf4c run>
|
998
|
+
#
|
774
999
|
def self.list: () -> untyped
|
775
1000
|
|
1001
|
+
# <!--
|
1002
|
+
# rdoc-file=thread.c
|
1003
|
+
# - Thread.pass -> nil
|
1004
|
+
# -->
|
776
1005
|
# Give the thread scheduler a hint to pass execution to another thread. A
|
777
1006
|
# running thread may or may not switch, it depends on OS and processor.
|
1007
|
+
#
|
778
1008
|
def self.pass: () -> untyped
|
779
1009
|
|
1010
|
+
# <!--
|
1011
|
+
# rdoc-file=thread.c
|
1012
|
+
# - Thread.pending_interrupt?(error = nil) -> true/false
|
1013
|
+
# -->
|
780
1014
|
# Returns whether or not the asynchronous queue is empty.
|
781
1015
|
#
|
782
|
-
# Since
|
783
|
-
#
|
784
|
-
# can be used to defer asynchronous events, this method can be used to
|
785
|
-
# determine if there are any deferred events.
|
1016
|
+
# Since Thread::handle_interrupt can be used to defer asynchronous events, this
|
1017
|
+
# method can be used to determine if there are any deferred events.
|
786
1018
|
#
|
787
|
-
# If you find this method returns true, then you may finish `:never`
|
788
|
-
# blocks.
|
1019
|
+
# If you find this method returns true, then you may finish `:never` blocks.
|
789
1020
|
#
|
790
1021
|
# For example, the following method processes deferred asynchronous events
|
791
1022
|
# immediately.
|
792
1023
|
#
|
793
|
-
#
|
794
|
-
#
|
795
|
-
#
|
796
|
-
#
|
797
|
-
#
|
798
|
-
# end
|
799
|
-
# ```
|
1024
|
+
# def Thread.kick_interrupt_immediately
|
1025
|
+
# Thread.handle_interrupt(Object => :immediate) {
|
1026
|
+
# Thread.pass
|
1027
|
+
# }
|
1028
|
+
# end
|
800
1029
|
#
|
801
1030
|
# If `error` is given, then check only for `error` type deferred events.
|
802
1031
|
#
|
1032
|
+
# ### Usage
|
803
1033
|
#
|
804
1034
|
# th = Thread.new{
|
805
1035
|
# Thread.handle_interrupt(RuntimeError => :on_blocking){
|
@@ -816,8 +1046,8 @@ class Thread < Object
|
|
816
1046
|
# ...
|
817
1047
|
# th.raise # stop thread
|
818
1048
|
#
|
819
|
-
# This example can also be written as the following, which you should use
|
820
|
-
#
|
1049
|
+
# This example can also be written as the following, which you should use to
|
1050
|
+
# avoid asynchronous interrupts.
|
821
1051
|
#
|
822
1052
|
# flag = true
|
823
1053
|
# th = Thread.new{
|
@@ -832,283 +1062,653 @@ class Thread < Object
|
|
832
1062
|
# }
|
833
1063
|
# ...
|
834
1064
|
# flag = false # stop thread
|
1065
|
+
#
|
835
1066
|
def self.pending_interrupt?: (*untyped args) -> bool
|
836
1067
|
|
1068
|
+
# <!--
|
1069
|
+
# rdoc-file=thread.c
|
1070
|
+
# - Thread.report_on_exception -> true or false
|
1071
|
+
# -->
|
1072
|
+
# Returns the status of the global ``report on exception'' condition.
|
1073
|
+
#
|
1074
|
+
# The default is `true` since Ruby 2.5.
|
1075
|
+
#
|
1076
|
+
# All threads created when this flag is true will report a message on $stderr if
|
1077
|
+
# an exception kills the thread.
|
1078
|
+
#
|
1079
|
+
# Thread.new { 1.times { raise } }
|
1080
|
+
#
|
1081
|
+
# will produce this output on $stderr:
|
1082
|
+
#
|
1083
|
+
# #<Thread:...> terminated with exception (report_on_exception is true):
|
1084
|
+
# Traceback (most recent call last):
|
1085
|
+
# 2: from -e:1:in `block in <main>'
|
1086
|
+
# 1: from -e:1:in `times'
|
1087
|
+
#
|
1088
|
+
# This is done to catch errors in threads early. In some cases, you might not
|
1089
|
+
# want this output. There are multiple ways to avoid the extra output:
|
1090
|
+
#
|
1091
|
+
# * If the exception is not intended, the best is to fix the cause of the
|
1092
|
+
# exception so it does not happen anymore.
|
1093
|
+
# * If the exception is intended, it might be better to rescue it closer to
|
1094
|
+
# where it is raised rather then let it kill the Thread.
|
1095
|
+
# * If it is guaranteed the Thread will be joined with Thread#join or
|
1096
|
+
# Thread#value, then it is safe to disable this report with
|
1097
|
+
# `Thread.current.report_on_exception = false` when starting the Thread.
|
1098
|
+
# However, this might handle the exception much later, or not at all if the
|
1099
|
+
# Thread is never joined due to the parent thread being blocked, etc.
|
1100
|
+
#
|
1101
|
+
#
|
1102
|
+
# See also ::report_on_exception=.
|
1103
|
+
#
|
1104
|
+
# There is also an instance level method to set this for a specific thread, see
|
1105
|
+
# #report_on_exception=.
|
1106
|
+
#
|
837
1107
|
def self.report_on_exception: () -> untyped
|
838
1108
|
|
1109
|
+
# <!--
|
1110
|
+
# rdoc-file=thread.c
|
1111
|
+
# - Thread.report_on_exception= boolean -> true or false
|
1112
|
+
# -->
|
1113
|
+
# Returns the new state. When set to `true`, all threads created afterwards will
|
1114
|
+
# inherit the condition and report a message on $stderr if an exception kills a
|
1115
|
+
# thread:
|
1116
|
+
#
|
1117
|
+
# Thread.report_on_exception = true
|
1118
|
+
# t1 = Thread.new do
|
1119
|
+
# puts "In new thread"
|
1120
|
+
# raise "Exception from thread"
|
1121
|
+
# end
|
1122
|
+
# sleep(1)
|
1123
|
+
# puts "In the main thread"
|
1124
|
+
#
|
1125
|
+
# This will produce:
|
1126
|
+
#
|
1127
|
+
# In new thread
|
1128
|
+
# #<Thread:...prog.rb:2> terminated with exception (report_on_exception is true):
|
1129
|
+
# Traceback (most recent call last):
|
1130
|
+
# prog.rb:4:in `block in <main>': Exception from thread (RuntimeError)
|
1131
|
+
# In the main thread
|
1132
|
+
#
|
1133
|
+
# See also ::report_on_exception.
|
1134
|
+
#
|
1135
|
+
# There is also an instance level method to set this for a specific thread, see
|
1136
|
+
# #report_on_exception=.
|
1137
|
+
#
|
839
1138
|
def self.report_on_exception=: (untyped report_on_exception) -> untyped
|
840
1139
|
|
841
|
-
#
|
842
|
-
#
|
843
|
-
#
|
844
|
-
#
|
1140
|
+
# <!--
|
1141
|
+
# rdoc-file=thread.c
|
1142
|
+
# - Thread.start([args]*) {|args| block } -> thread
|
1143
|
+
# - Thread.fork([args]*) {|args| block } -> thread
|
1144
|
+
# -->
|
1145
|
+
# Basically the same as ::new. However, if class Thread is subclassed, then
|
1146
|
+
# calling `start` in that subclass will not invoke the subclass's `initialize`
|
1147
|
+
# method.
|
1148
|
+
#
|
845
1149
|
def self.start: (*untyped args) { (*untyped) -> void } -> instance
|
846
1150
|
|
847
|
-
#
|
848
|
-
#
|
849
|
-
#
|
850
|
-
#
|
851
|
-
#
|
852
|
-
#
|
853
|
-
#
|
854
|
-
# a.
|
855
|
-
# a.
|
856
|
-
#
|
857
|
-
#
|
858
|
-
|
1151
|
+
# <!--
|
1152
|
+
# rdoc-file=thread.c
|
1153
|
+
# - Thread.stop -> nil
|
1154
|
+
# -->
|
1155
|
+
# Stops execution of the current thread, putting it into a ``sleep'' state, and
|
1156
|
+
# schedules execution of another thread.
|
1157
|
+
#
|
1158
|
+
# a = Thread.new { print "a"; Thread.stop; print "c" }
|
1159
|
+
# sleep 0.1 while a.status!='sleep'
|
1160
|
+
# print "b"
|
1161
|
+
# a.run
|
1162
|
+
# a.join
|
1163
|
+
# #=> "abc"
|
1164
|
+
#
|
1165
|
+
def self.stop: () -> untyped
|
859
1166
|
end
|
860
1167
|
|
1168
|
+
# <!-- rdoc-file=vm_backtrace.c -->
|
1169
|
+
# An internal representation of the backtrace. The user will never interact with
|
1170
|
+
# objects of this class directly, but class methods can be used to get backtrace
|
1171
|
+
# settings of the current session.
|
1172
|
+
#
|
861
1173
|
class Thread::Backtrace < Object
|
1174
|
+
# <!--
|
1175
|
+
# rdoc-file=vm_backtrace.c
|
1176
|
+
# - Threade::Backtrace::limit -> integer
|
1177
|
+
# -->
|
1178
|
+
# Returns maximum backtrace length set by `--backtrace-limit` command-line
|
1179
|
+
# option. The defalt is `-1` which means unlimited backtraces. If the value is
|
1180
|
+
# zero or positive, the error backtraces, produced by Exception#full_message,
|
1181
|
+
# are abbreviated and the extra lines are replaced by `... 3 levels... `
|
1182
|
+
#
|
1183
|
+
# $ ruby -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
|
1184
|
+
# - 1
|
1185
|
+
# .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
|
1186
|
+
# from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
|
1187
|
+
# from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
|
1188
|
+
# from .../lib/ruby/3.1.0/net/http.rb:998:in `connect'
|
1189
|
+
# from .../lib/ruby/3.1.0/net/http.rb:976:in `do_start'
|
1190
|
+
# from .../lib/ruby/3.1.0/net/http.rb:965:in `start'
|
1191
|
+
# from .../lib/ruby/3.1.0/net/http.rb:627:in `start'
|
1192
|
+
# from .../lib/ruby/3.1.0/net/http.rb:503:in `get_response'
|
1193
|
+
# from .../lib/ruby/3.1.0/net/http.rb:474:in `get'
|
1194
|
+
# .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
|
1195
|
+
# from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
|
1196
|
+
# from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
|
1197
|
+
# from .../lib/ruby/3.1.0/net/http.rb:998:in `connect'
|
1198
|
+
# from .../lib/ruby/3.1.0/net/http.rb:976:in `do_start'
|
1199
|
+
# from .../lib/ruby/3.1.0/net/http.rb:965:in `start'
|
1200
|
+
# from .../lib/ruby/3.1.0/net/http.rb:627:in `start'
|
1201
|
+
# from .../lib/ruby/3.1.0/net/http.rb:503:in `get_response'
|
1202
|
+
# from .../lib/ruby/3.1.0/net/http.rb:474:in `get'
|
1203
|
+
# from -e:1:in `<main>'
|
1204
|
+
#
|
1205
|
+
# $ ruby --backtrace-limit 2 -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
|
1206
|
+
# 2
|
1207
|
+
# .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
|
1208
|
+
# from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
|
1209
|
+
# from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
|
1210
|
+
# ... 7 levels...
|
1211
|
+
# .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
|
1212
|
+
# from .../lib/ruby/3.1.0/socket.rb:227:in `foreach'
|
1213
|
+
# from .../lib/ruby/3.1.0/socket.rb:632:in `tcp'
|
1214
|
+
# ... 7 levels...
|
1215
|
+
#
|
1216
|
+
# $ ruby --backtrace-limit 0 -r net/http -e "p Thread::Backtrace.limit; Net::HTTP.get(URI('http://wrong.address'))"
|
1217
|
+
# 0
|
1218
|
+
# .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': Failed to open TCP connection to wrong.address:80 (getaddrinfo: Name or service not known) (SocketError)
|
1219
|
+
# ... 9 levels...
|
1220
|
+
# .../lib/ruby/3.1.0/socket.rb:227:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
|
1221
|
+
# ... 9 levels...
|
1222
|
+
#
|
1223
|
+
def self.limit: () -> Integer
|
862
1224
|
end
|
863
1225
|
|
1226
|
+
# <!-- rdoc-file=vm_backtrace.c -->
|
1227
|
+
# An object representation of a stack frame, initialized by
|
1228
|
+
# Kernel#caller_locations.
|
1229
|
+
#
|
1230
|
+
# For example:
|
1231
|
+
#
|
1232
|
+
# # caller_locations.rb
|
1233
|
+
# def a(skip)
|
1234
|
+
# caller_locations(skip)
|
1235
|
+
# end
|
1236
|
+
# def b(skip)
|
1237
|
+
# a(skip)
|
1238
|
+
# end
|
1239
|
+
# def c(skip)
|
1240
|
+
# b(skip)
|
1241
|
+
# end
|
1242
|
+
#
|
1243
|
+
# c(0..2).map do |call|
|
1244
|
+
# puts call.to_s
|
1245
|
+
# end
|
1246
|
+
#
|
1247
|
+
# Running `ruby caller_locations.rb` will produce:
|
1248
|
+
#
|
1249
|
+
# caller_locations.rb:2:in `a'
|
1250
|
+
# caller_locations.rb:5:in `b'
|
1251
|
+
# caller_locations.rb:8:in `c'
|
1252
|
+
#
|
1253
|
+
# Here's another example with a slightly different result:
|
1254
|
+
#
|
1255
|
+
# # foo.rb
|
1256
|
+
# class Foo
|
1257
|
+
# attr_accessor :locations
|
1258
|
+
# def initialize(skip)
|
1259
|
+
# @locations = caller_locations(skip)
|
1260
|
+
# end
|
1261
|
+
# end
|
1262
|
+
#
|
1263
|
+
# Foo.new(0..2).locations.map do |call|
|
1264
|
+
# puts call.to_s
|
1265
|
+
# end
|
1266
|
+
#
|
1267
|
+
# Now run `ruby foo.rb` and you should see:
|
1268
|
+
#
|
1269
|
+
# init.rb:4:in `initialize'
|
1270
|
+
# init.rb:8:in `new'
|
1271
|
+
# init.rb:8:in `<main>'
|
1272
|
+
#
|
864
1273
|
class Thread::Backtrace::Location
|
1274
|
+
# <!--
|
1275
|
+
# rdoc-file=vm_backtrace.c
|
1276
|
+
# - absolute_path()
|
1277
|
+
# -->
|
1278
|
+
# Returns the full file path of this frame.
|
1279
|
+
#
|
1280
|
+
# Same as #path, except that it will return absolute path even if the frame is
|
1281
|
+
# in the main script.
|
1282
|
+
#
|
865
1283
|
def absolute_path: () -> String?
|
866
1284
|
|
1285
|
+
# <!--
|
1286
|
+
# rdoc-file=vm_backtrace.c
|
1287
|
+
# - base_label()
|
1288
|
+
# -->
|
1289
|
+
# Returns the base label of this frame.
|
1290
|
+
#
|
1291
|
+
# Usually same as #label, without decoration.
|
1292
|
+
#
|
867
1293
|
def base_label: () -> String?
|
868
1294
|
|
1295
|
+
# <!--
|
1296
|
+
# rdoc-file=vm_backtrace.c
|
1297
|
+
# - label()
|
1298
|
+
# -->
|
1299
|
+
# Returns the label of this frame.
|
1300
|
+
#
|
1301
|
+
# Usually consists of method, class, module, etc names with decoration.
|
1302
|
+
#
|
1303
|
+
# Consider the following example:
|
1304
|
+
#
|
1305
|
+
# def foo
|
1306
|
+
# puts caller_locations(0).first.label
|
1307
|
+
#
|
1308
|
+
# 1.times do
|
1309
|
+
# puts caller_locations(0).first.label
|
1310
|
+
#
|
1311
|
+
# 1.times do
|
1312
|
+
# puts caller_locations(0).first.label
|
1313
|
+
# end
|
1314
|
+
#
|
1315
|
+
# end
|
1316
|
+
# end
|
1317
|
+
#
|
1318
|
+
# The result of calling `foo` is this:
|
1319
|
+
#
|
1320
|
+
# label: foo
|
1321
|
+
# label: block in foo
|
1322
|
+
# label: block (2 levels) in foo
|
1323
|
+
#
|
869
1324
|
def label: () -> String?
|
870
1325
|
|
1326
|
+
# <!--
|
1327
|
+
# rdoc-file=vm_backtrace.c
|
1328
|
+
# - lineno()
|
1329
|
+
# -->
|
1330
|
+
# Returns the line number of this frame.
|
1331
|
+
#
|
1332
|
+
# For example, using `caller_locations.rb` from Thread::Backtrace::Location
|
1333
|
+
#
|
1334
|
+
# loc = c(0..1).first
|
1335
|
+
# loc.lineno #=> 2
|
1336
|
+
#
|
871
1337
|
def lineno: () -> Integer
|
872
1338
|
|
1339
|
+
# <!--
|
1340
|
+
# rdoc-file=vm_backtrace.c
|
1341
|
+
# - path()
|
1342
|
+
# -->
|
1343
|
+
# Returns the file name of this frame. This will generally be an absolute path,
|
1344
|
+
# unless the frame is in the main script, in which case it will be the script
|
1345
|
+
# location passed on the command line.
|
1346
|
+
#
|
1347
|
+
# For example, using `caller_locations.rb` from Thread::Backtrace::Location
|
1348
|
+
#
|
1349
|
+
# loc = c(0..1).first
|
1350
|
+
# loc.path #=> caller_locations.rb
|
1351
|
+
#
|
873
1352
|
def path: () -> String?
|
874
1353
|
end
|
875
1354
|
|
876
|
-
#
|
877
|
-
# augment class
|
878
|
-
#
|
879
|
-
#
|
1355
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1356
|
+
# ConditionVariable objects augment class Mutex. Using condition variables, it
|
1357
|
+
# is possible to suspend while in the middle of a critical section until a
|
1358
|
+
# resource becomes available.
|
880
1359
|
#
|
881
1360
|
# Example:
|
882
1361
|
#
|
883
|
-
#
|
884
|
-
#
|
885
|
-
#
|
886
|
-
#
|
887
|
-
#
|
888
|
-
#
|
889
|
-
#
|
890
|
-
#
|
891
|
-
#
|
892
|
-
#
|
893
|
-
#
|
894
|
-
#
|
895
|
-
#
|
896
|
-
#
|
897
|
-
#
|
898
|
-
#
|
899
|
-
#
|
900
|
-
#
|
901
|
-
# ```
|
1362
|
+
# mutex = Thread::Mutex.new
|
1363
|
+
# resource = Thread::ConditionVariable.new
|
1364
|
+
#
|
1365
|
+
# a = Thread.new {
|
1366
|
+
# mutex.synchronize {
|
1367
|
+
# # Thread 'a' now needs the resource
|
1368
|
+
# resource.wait(mutex)
|
1369
|
+
# # 'a' can now have the resource
|
1370
|
+
# }
|
1371
|
+
# }
|
1372
|
+
#
|
1373
|
+
# b = Thread.new {
|
1374
|
+
# mutex.synchronize {
|
1375
|
+
# # Thread 'b' has finished using the resource
|
1376
|
+
# resource.signal
|
1377
|
+
# }
|
1378
|
+
# }
|
1379
|
+
#
|
902
1380
|
class Thread::ConditionVariable < Object
|
1381
|
+
# <!--
|
1382
|
+
# rdoc-file=thread_sync.c
|
1383
|
+
# - broadcast()
|
1384
|
+
# -->
|
903
1385
|
# Wakes up all threads waiting for this lock.
|
1386
|
+
#
|
904
1387
|
def broadcast: () -> self
|
905
1388
|
|
1389
|
+
# <!--
|
1390
|
+
# rdoc-file=thread_sync.c
|
1391
|
+
# - signal()
|
1392
|
+
# -->
|
906
1393
|
# Wakes up the first thread in line waiting for this lock.
|
1394
|
+
#
|
907
1395
|
def signal: () -> self
|
908
1396
|
|
909
|
-
#
|
910
|
-
#
|
1397
|
+
# <!--
|
1398
|
+
# rdoc-file=thread_sync.c
|
1399
|
+
# - wait(mutex, timeout=nil)
|
1400
|
+
# -->
|
1401
|
+
# Releases the lock held in `mutex` and waits; reacquires the lock on wakeup.
|
1402
|
+
#
|
1403
|
+
# If `timeout` is given, this method returns after `timeout` seconds passed,
|
1404
|
+
# even if no other thread doesn't signal.
|
1405
|
+
#
|
1406
|
+
# Returns the slept result on `mutex`.
|
911
1407
|
#
|
912
|
-
# If `timeout` is given, this method returns after `timeout` seconds
|
913
|
-
# passed, even if no other thread doesn't signal.
|
914
1408
|
def wait: (Thread::Mutex mutex, ?Integer timeout) -> self
|
915
1409
|
end
|
916
1410
|
|
917
|
-
#
|
918
|
-
#
|
919
|
-
# threads.
|
1411
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1412
|
+
# Thread::Mutex implements a simple semaphore that can be used to coordinate
|
1413
|
+
# access to shared data from multiple concurrent threads.
|
920
1414
|
#
|
921
1415
|
# Example:
|
922
1416
|
#
|
923
|
-
#
|
924
|
-
#
|
925
|
-
#
|
926
|
-
#
|
927
|
-
#
|
928
|
-
#
|
929
|
-
#
|
930
|
-
#
|
931
|
-
#
|
932
|
-
#
|
933
|
-
#
|
934
|
-
#
|
935
|
-
#
|
936
|
-
#
|
937
|
-
# ```
|
1417
|
+
# semaphore = Thread::Mutex.new
|
1418
|
+
#
|
1419
|
+
# a = Thread.new {
|
1420
|
+
# semaphore.synchronize {
|
1421
|
+
# # access shared resource
|
1422
|
+
# }
|
1423
|
+
# }
|
1424
|
+
#
|
1425
|
+
# b = Thread.new {
|
1426
|
+
# semaphore.synchronize {
|
1427
|
+
# # access shared resource
|
1428
|
+
# }
|
1429
|
+
# }
|
1430
|
+
#
|
938
1431
|
class Thread::Mutex < Object
|
939
|
-
#
|
1432
|
+
# <!--
|
1433
|
+
# rdoc-file=thread_sync.c
|
1434
|
+
# - mutex.lock -> self
|
1435
|
+
# -->
|
1436
|
+
# Attempts to grab the lock and waits if it isn't available. Raises
|
940
1437
|
# `ThreadError` if `mutex` was locked by the current thread.
|
1438
|
+
#
|
941
1439
|
def lock: () -> self
|
942
1440
|
|
1441
|
+
# <!--
|
1442
|
+
# rdoc-file=thread_sync.c
|
1443
|
+
# - mutex.locked? -> true or false
|
1444
|
+
# -->
|
943
1445
|
# Returns `true` if this lock is currently held by some thread.
|
1446
|
+
#
|
944
1447
|
def locked?: () -> bool
|
945
1448
|
|
1449
|
+
# <!--
|
1450
|
+
# rdoc-file=thread_sync.c
|
1451
|
+
# - mutex.owned? -> true or false
|
1452
|
+
# -->
|
946
1453
|
# Returns `true` if this lock is currently held by current thread.
|
1454
|
+
#
|
947
1455
|
def owned?: () -> bool
|
948
1456
|
|
1457
|
+
# <!--
|
1458
|
+
# rdoc-file=thread_sync.c
|
1459
|
+
# - mutex.synchronize { ... } -> result of the block
|
1460
|
+
# -->
|
949
1461
|
# Obtains a lock, runs the block, and releases the lock when the block
|
950
|
-
# completes.
|
1462
|
+
# completes. See the example under Thread::Mutex.
|
1463
|
+
#
|
951
1464
|
def synchronize: [X] () { () -> X } -> X
|
952
1465
|
|
953
|
-
#
|
954
|
-
#
|
1466
|
+
# <!--
|
1467
|
+
# rdoc-file=thread_sync.c
|
1468
|
+
# - mutex.try_lock -> true or false
|
1469
|
+
# -->
|
1470
|
+
# Attempts to obtain the lock and returns immediately. Returns `true` if the
|
1471
|
+
# lock was granted.
|
1472
|
+
#
|
955
1473
|
def try_lock: () -> bool
|
956
1474
|
|
957
|
-
#
|
1475
|
+
# <!--
|
1476
|
+
# rdoc-file=thread_sync.c
|
1477
|
+
# - mutex.unlock -> self
|
1478
|
+
# -->
|
1479
|
+
# Releases the lock. Raises `ThreadError` if `mutex` wasn't locked by the
|
958
1480
|
# current thread.
|
1481
|
+
#
|
959
1482
|
def unlock: () -> self
|
960
1483
|
end
|
961
1484
|
|
962
|
-
#
|
963
|
-
# multi-consumer queues.
|
964
|
-
#
|
965
|
-
#
|
966
|
-
# locking semantics.
|
1485
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1486
|
+
# The Thread::Queue class implements multi-producer, multi-consumer queues. It
|
1487
|
+
# is especially useful in threaded programming when information must be
|
1488
|
+
# exchanged safely between multiple threads. The Thread::Queue class implements
|
1489
|
+
# all the required locking semantics.
|
967
1490
|
#
|
968
|
-
# The class implements FIFO type of queue. In a FIFO queue, the first
|
969
|
-
#
|
1491
|
+
# The class implements FIFO type of queue. In a FIFO queue, the first tasks
|
1492
|
+
# added are the first retrieved.
|
970
1493
|
#
|
971
1494
|
# Example:
|
972
1495
|
#
|
973
|
-
#
|
974
|
-
#
|
975
|
-
#
|
976
|
-
#
|
977
|
-
#
|
978
|
-
#
|
979
|
-
#
|
980
|
-
#
|
981
|
-
#
|
982
|
-
#
|
983
|
-
#
|
984
|
-
#
|
985
|
-
#
|
986
|
-
#
|
987
|
-
#
|
988
|
-
#
|
989
|
-
#
|
990
|
-
#
|
991
|
-
#
|
992
|
-
#
|
993
|
-
# ```
|
1496
|
+
# queue = Thread::Queue.new
|
1497
|
+
#
|
1498
|
+
# producer = Thread.new do
|
1499
|
+
# 5.times do |i|
|
1500
|
+
# sleep rand(i) # simulate expense
|
1501
|
+
# queue << i
|
1502
|
+
# puts "#{i} produced"
|
1503
|
+
# end
|
1504
|
+
# end
|
1505
|
+
#
|
1506
|
+
# consumer = Thread.new do
|
1507
|
+
# 5.times do |i|
|
1508
|
+
# value = queue.pop
|
1509
|
+
# sleep rand(i/2) # simulate expense
|
1510
|
+
# puts "consumed #{value}"
|
1511
|
+
# end
|
1512
|
+
# end
|
1513
|
+
#
|
1514
|
+
# consumer.join
|
1515
|
+
#
|
994
1516
|
class Thread::Queue < Object
|
995
|
-
#
|
1517
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1518
|
+
# Pushes the given `object` to the queue.
|
1519
|
+
#
|
996
1520
|
alias << push
|
997
1521
|
|
1522
|
+
# <!--
|
1523
|
+
# rdoc-file=thread_sync.c
|
1524
|
+
# - clear()
|
1525
|
+
# -->
|
998
1526
|
# Removes all objects from the queue.
|
1527
|
+
#
|
999
1528
|
def clear: () -> void
|
1000
1529
|
|
1530
|
+
# <!--
|
1531
|
+
# rdoc-file=thread_sync.c
|
1532
|
+
# - close
|
1533
|
+
# -->
|
1001
1534
|
# Closes the queue. A closed queue cannot be re-opened.
|
1002
1535
|
#
|
1003
1536
|
# After the call to close completes, the following are true:
|
1004
1537
|
#
|
1005
|
-
#
|
1538
|
+
# * `closed?` will return true
|
1006
1539
|
#
|
1007
|
-
#
|
1540
|
+
# * `close` will be ignored.
|
1008
1541
|
#
|
1009
|
-
#
|
1542
|
+
# * calling enq/push/<< will raise a `ClosedQueueError`.
|
1010
1543
|
#
|
1011
|
-
#
|
1012
|
-
#
|
1544
|
+
# * when `empty?` is false, calling deq/pop/shift will return an object from
|
1545
|
+
# the queue as usual.
|
1546
|
+
# * when `empty?` is true, deq(false) will not suspend the thread and will
|
1547
|
+
# return nil. deq(true) will raise a `ThreadError`.
|
1013
1548
|
#
|
1014
|
-
# - when `empty?` is true, deq(false) will not suspend the thread and
|
1015
|
-
# will return nil. deq(true) will raise a `ThreadError` .
|
1016
1549
|
#
|
1017
|
-
#
|
1018
|
-
#
|
1019
|
-
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html), so
|
1020
|
-
# that you can break loop block.
|
1550
|
+
# ClosedQueueError is inherited from StopIteration, so that you can break loop
|
1551
|
+
# block.
|
1021
1552
|
#
|
1022
|
-
#
|
1553
|
+
# Example:
|
1554
|
+
#
|
1555
|
+
# q = Thread::Queue.new
|
1556
|
+
# Thread.new{
|
1557
|
+
# while e = q.deq # wait for nil to break loop
|
1558
|
+
# # ...
|
1559
|
+
# end
|
1560
|
+
# }
|
1561
|
+
# q.close
|
1023
1562
|
#
|
1024
|
-
# q = Queue.new
|
1025
|
-
# Thread.new{
|
1026
|
-
# while e = q.deq # wait for nil to break loop
|
1027
|
-
# # ...
|
1028
|
-
# end
|
1029
|
-
# }
|
1030
|
-
# q.close
|
1031
1563
|
def close: () -> self
|
1032
1564
|
|
1565
|
+
# <!--
|
1566
|
+
# rdoc-file=thread_sync.c
|
1567
|
+
# - closed?
|
1568
|
+
# -->
|
1033
1569
|
# Returns `true` if the queue is closed.
|
1570
|
+
#
|
1034
1571
|
def closed?: () -> bool
|
1035
1572
|
|
1036
|
-
#
|
1573
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1574
|
+
# Retrieves data from the queue.
|
1575
|
+
#
|
1576
|
+
# If the queue is empty, the calling thread is suspended until data is pushed
|
1577
|
+
# onto the queue. If `non_block` is true, the thread isn't suspended, and
|
1578
|
+
# `ThreadError` is raised.
|
1579
|
+
#
|
1037
1580
|
alias deq pop
|
1038
1581
|
|
1582
|
+
# <!--
|
1583
|
+
# rdoc-file=thread_sync.c
|
1584
|
+
# - empty?
|
1585
|
+
# -->
|
1039
1586
|
# Returns `true` if the queue is empty.
|
1587
|
+
#
|
1040
1588
|
def empty?: () -> bool
|
1041
1589
|
|
1042
|
-
#
|
1590
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1591
|
+
# Pushes the given `object` to the queue.
|
1592
|
+
#
|
1043
1593
|
alias enq push
|
1044
1594
|
|
1595
|
+
# <!--
|
1596
|
+
# rdoc-file=thread_sync.c
|
1597
|
+
# - length
|
1598
|
+
# - size
|
1599
|
+
# -->
|
1045
1600
|
# Returns the length of the queue.
|
1046
1601
|
#
|
1047
|
-
#
|
1048
|
-
#
|
1049
|
-
# Also aliased as: [size](Queue.downloaded.ruby_doc#method-i-size)
|
1050
1602
|
def length: () -> Integer
|
1051
1603
|
|
1604
|
+
# <!--
|
1605
|
+
# rdoc-file=thread_sync.c
|
1606
|
+
# - num_waiting()
|
1607
|
+
# -->
|
1052
1608
|
# Returns the number of threads waiting on the queue.
|
1609
|
+
#
|
1053
1610
|
def num_waiting: () -> Integer
|
1054
1611
|
|
1612
|
+
# <!--
|
1613
|
+
# rdoc-file=thread_sync.c
|
1614
|
+
# - pop(non_block=false)
|
1615
|
+
# - deq(non_block=false)
|
1616
|
+
# - shift(non_block=false)
|
1617
|
+
# -->
|
1055
1618
|
# Retrieves data from the queue.
|
1056
1619
|
#
|
1057
|
-
# If the queue is empty, the calling thread is suspended until data is
|
1058
|
-
#
|
1059
|
-
#
|
1060
|
-
#
|
1620
|
+
# If the queue is empty, the calling thread is suspended until data is pushed
|
1621
|
+
# onto the queue. If `non_block` is true, the thread isn't suspended, and
|
1622
|
+
# `ThreadError` is raised.
|
1061
1623
|
#
|
1062
|
-
#
|
1063
|
-
# Also aliased as: [deq](Queue.downloaded.ruby_doc#method-i-deq),
|
1064
|
-
# [shift](Queue.downloaded.ruby_doc#method-i-shift)
|
1065
1624
|
def pop: (?boolish non_block) -> untyped
|
1066
1625
|
|
1626
|
+
# <!--
|
1627
|
+
# rdoc-file=thread_sync.c
|
1628
|
+
# - push(object)
|
1629
|
+
# - enq(object)
|
1630
|
+
# - <<(object)
|
1631
|
+
# -->
|
1067
1632
|
# Pushes the given `object` to the queue.
|
1068
1633
|
#
|
1069
|
-
#
|
1070
|
-
#
|
1071
|
-
# Also aliased as: [enq](Queue.downloaded.ruby_doc#method-i-enq),
|
1072
|
-
# [\<\<](Queue.downloaded.ruby_doc#method-i-3C-3C)
|
1073
1634
|
def push: (untyped obj) -> void
|
1074
1635
|
|
1075
|
-
#
|
1636
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1637
|
+
# Retrieves data from the queue.
|
1638
|
+
#
|
1639
|
+
# If the queue is empty, the calling thread is suspended until data is pushed
|
1640
|
+
# onto the queue. If `non_block` is true, the thread isn't suspended, and
|
1641
|
+
# `ThreadError` is raised.
|
1642
|
+
#
|
1076
1643
|
alias shift pop
|
1077
1644
|
|
1078
|
-
#
|
1645
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1646
|
+
# Returns the length of the queue.
|
1647
|
+
#
|
1079
1648
|
alias size length
|
1080
1649
|
end
|
1081
1650
|
|
1082
|
-
#
|
1083
|
-
#
|
1651
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1652
|
+
# This class represents queues of specified size capacity. The push operation
|
1653
|
+
# may be blocked if the capacity is full.
|
1654
|
+
#
|
1655
|
+
# See Thread::Queue for an example of how a Thread::SizedQueue works.
|
1084
1656
|
#
|
1085
|
-
# See [Queue](https://ruby-doc.org/core-2.6.3/Queue.html) for an example
|
1086
|
-
# of how a [SizedQueue](SizedQueue) works.
|
1087
1657
|
class Thread::SizedQueue < Thread::Queue
|
1088
|
-
#
|
1658
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1659
|
+
# Pushes `object` to the queue.
|
1660
|
+
#
|
1661
|
+
# If there is no space left in the queue, waits until space becomes available,
|
1662
|
+
# unless `non_block` is true. If `non_block` is true, the thread isn't
|
1663
|
+
# suspended, and `ThreadError` is raised.
|
1664
|
+
#
|
1089
1665
|
alias << push
|
1090
1666
|
|
1091
|
-
#
|
1667
|
+
# <!-- rdoc-file=thread_sync.c -->
|
1668
|
+
# Pushes `object` to the queue.
|
1669
|
+
#
|
1670
|
+
# If there is no space left in the queue, waits until space becomes available,
|
1671
|
+
# unless `non_block` is true. If `non_block` is true, the thread isn't
|
1672
|
+
# suspended, and `ThreadError` is raised.
|
1673
|
+
#
|
1092
1674
|
alias enq push
|
1093
1675
|
|
1676
|
+
# <!--
|
1677
|
+
# rdoc-file=thread_sync.c
|
1678
|
+
# - new(max)
|
1679
|
+
# -->
|
1680
|
+
# Creates a fixed-length queue with a maximum size of `max`.
|
1681
|
+
#
|
1094
1682
|
def initialize: (Integer max) -> void
|
1095
1683
|
|
1684
|
+
# <!--
|
1685
|
+
# rdoc-file=thread_sync.c
|
1686
|
+
# - max()
|
1687
|
+
# -->
|
1096
1688
|
# Returns the maximum size of the queue.
|
1689
|
+
#
|
1097
1690
|
def max: () -> Integer
|
1098
1691
|
|
1099
|
-
#
|
1692
|
+
# <!--
|
1693
|
+
# rdoc-file=thread_sync.c
|
1694
|
+
# - max=(number)
|
1695
|
+
# -->
|
1696
|
+
# Sets the maximum size of the queue to the given `number`.
|
1697
|
+
#
|
1100
1698
|
def max=: (Integer max) -> void
|
1101
1699
|
|
1700
|
+
# <!--
|
1701
|
+
# rdoc-file=thread_sync.c
|
1702
|
+
# - push(object, non_block=false)
|
1703
|
+
# - enq(object, non_block=false)
|
1704
|
+
# - <<(object)
|
1705
|
+
# -->
|
1102
1706
|
# Pushes `object` to the queue.
|
1103
1707
|
#
|
1104
|
-
# If there is no space left in the queue, waits until space becomes
|
1105
|
-
#
|
1106
|
-
#
|
1107
|
-
#
|
1108
|
-
#
|
1708
|
+
# If there is no space left in the queue, waits until space becomes available,
|
1709
|
+
# unless `non_block` is true. If `non_block` is true, the thread isn't
|
1710
|
+
# suspended, and `ThreadError` is raised.
|
1109
1711
|
#
|
1110
|
-
# Also aliased as: [enq](SizedQueue.downloaded.ruby_doc#method-i-enq),
|
1111
|
-
# [\<\<](SizedQueue.downloaded.ruby_doc#method-i-3C-3C)
|
1112
1712
|
def push: (untyped obj, ?boolish non_block) -> void
|
1113
1713
|
end
|
1114
1714
|
|