rbs 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +28 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +15 -0
- data/BSDL +22 -0
- data/CHANGELOG.md +9 -0
- data/COPYING +56 -0
- data/Gemfile +6 -0
- data/README.md +93 -0
- data/Rakefile +142 -0
- data/bin/annotate-with-rdoc +157 -0
- data/bin/console +14 -0
- data/bin/query-rdoc +103 -0
- data/bin/setup +10 -0
- data/bin/sort +89 -0
- data/bin/test_runner.rb +16 -0
- data/docs/CONTRIBUTING.md +97 -0
- data/docs/sigs.md +148 -0
- data/docs/stdlib.md +152 -0
- data/docs/syntax.md +528 -0
- data/exe/rbs +7 -0
- data/lib/rbs.rb +64 -0
- data/lib/rbs/ast/annotation.rb +27 -0
- data/lib/rbs/ast/comment.rb +27 -0
- data/lib/rbs/ast/declarations.rb +395 -0
- data/lib/rbs/ast/members.rb +362 -0
- data/lib/rbs/buffer.rb +50 -0
- data/lib/rbs/builtin_names.rb +55 -0
- data/lib/rbs/cli.rb +558 -0
- data/lib/rbs/constant.rb +26 -0
- data/lib/rbs/constant_table.rb +150 -0
- data/lib/rbs/definition.rb +170 -0
- data/lib/rbs/definition_builder.rb +919 -0
- data/lib/rbs/environment.rb +281 -0
- data/lib/rbs/environment_loader.rb +136 -0
- data/lib/rbs/environment_walker.rb +124 -0
- data/lib/rbs/errors.rb +187 -0
- data/lib/rbs/location.rb +102 -0
- data/lib/rbs/method_type.rb +123 -0
- data/lib/rbs/namespace.rb +91 -0
- data/lib/rbs/parser.y +1344 -0
- data/lib/rbs/prototype/rb.rb +553 -0
- data/lib/rbs/prototype/rbi.rb +587 -0
- data/lib/rbs/prototype/runtime.rb +381 -0
- data/lib/rbs/substitution.rb +46 -0
- data/lib/rbs/test.rb +26 -0
- data/lib/rbs/test/errors.rb +61 -0
- data/lib/rbs/test/hook.rb +294 -0
- data/lib/rbs/test/setup.rb +58 -0
- data/lib/rbs/test/spy.rb +325 -0
- data/lib/rbs/test/test_helper.rb +183 -0
- data/lib/rbs/test/type_check.rb +254 -0
- data/lib/rbs/type_name.rb +70 -0
- data/lib/rbs/types.rb +936 -0
- data/lib/rbs/variance_calculator.rb +138 -0
- data/lib/rbs/vendorer.rb +47 -0
- data/lib/rbs/version.rb +3 -0
- data/lib/rbs/writer.rb +269 -0
- data/lib/ruby/signature.rb +7 -0
- data/rbs.gemspec +46 -0
- data/stdlib/abbrev/abbrev.rbs +60 -0
- data/stdlib/base64/base64.rbs +71 -0
- data/stdlib/benchmark/benchmark.rbs +372 -0
- data/stdlib/builtin/array.rbs +1997 -0
- data/stdlib/builtin/basic_object.rbs +280 -0
- data/stdlib/builtin/binding.rbs +177 -0
- data/stdlib/builtin/builtin.rbs +45 -0
- data/stdlib/builtin/class.rbs +145 -0
- data/stdlib/builtin/comparable.rbs +116 -0
- data/stdlib/builtin/complex.rbs +400 -0
- data/stdlib/builtin/constants.rbs +37 -0
- data/stdlib/builtin/data.rbs +5 -0
- data/stdlib/builtin/deprecated.rbs +2 -0
- data/stdlib/builtin/dir.rbs +413 -0
- data/stdlib/builtin/encoding.rbs +607 -0
- data/stdlib/builtin/enumerable.rbs +404 -0
- data/stdlib/builtin/enumerator.rbs +260 -0
- data/stdlib/builtin/errno.rbs +781 -0
- data/stdlib/builtin/errors.rbs +582 -0
- data/stdlib/builtin/exception.rbs +194 -0
- data/stdlib/builtin/false_class.rbs +40 -0
- data/stdlib/builtin/fiber.rbs +68 -0
- data/stdlib/builtin/fiber_error.rbs +12 -0
- data/stdlib/builtin/file.rbs +1076 -0
- data/stdlib/builtin/file_test.rbs +59 -0
- data/stdlib/builtin/float.rbs +696 -0
- data/stdlib/builtin/gc.rbs +243 -0
- data/stdlib/builtin/hash.rbs +1029 -0
- data/stdlib/builtin/integer.rbs +707 -0
- data/stdlib/builtin/io.rbs +683 -0
- data/stdlib/builtin/kernel.rbs +576 -0
- data/stdlib/builtin/marshal.rbs +161 -0
- data/stdlib/builtin/match_data.rbs +271 -0
- data/stdlib/builtin/math.rbs +369 -0
- data/stdlib/builtin/method.rbs +185 -0
- data/stdlib/builtin/module.rbs +1104 -0
- data/stdlib/builtin/nil_class.rbs +82 -0
- data/stdlib/builtin/numeric.rbs +409 -0
- data/stdlib/builtin/object.rbs +824 -0
- data/stdlib/builtin/proc.rbs +429 -0
- data/stdlib/builtin/process.rbs +1227 -0
- data/stdlib/builtin/random.rbs +267 -0
- data/stdlib/builtin/range.rbs +226 -0
- data/stdlib/builtin/rational.rbs +424 -0
- data/stdlib/builtin/rb_config.rbs +57 -0
- data/stdlib/builtin/regexp.rbs +1083 -0
- data/stdlib/builtin/ruby_vm.rbs +14 -0
- data/stdlib/builtin/signal.rbs +55 -0
- data/stdlib/builtin/string.rbs +1901 -0
- data/stdlib/builtin/string_io.rbs +284 -0
- data/stdlib/builtin/struct.rbs +40 -0
- data/stdlib/builtin/symbol.rbs +228 -0
- data/stdlib/builtin/thread.rbs +1108 -0
- data/stdlib/builtin/thread_group.rbs +23 -0
- data/stdlib/builtin/time.rbs +1047 -0
- data/stdlib/builtin/trace_point.rbs +290 -0
- data/stdlib/builtin/true_class.rbs +46 -0
- data/stdlib/builtin/unbound_method.rbs +153 -0
- data/stdlib/builtin/warning.rbs +17 -0
- data/stdlib/coverage/coverage.rbs +62 -0
- data/stdlib/csv/csv.rbs +773 -0
- data/stdlib/erb/erb.rbs +392 -0
- data/stdlib/find/find.rbs +40 -0
- data/stdlib/ipaddr/ipaddr.rbs +247 -0
- data/stdlib/json/json.rbs +335 -0
- data/stdlib/pathname/pathname.rbs +1093 -0
- data/stdlib/prime/integer-extension.rbs +23 -0
- data/stdlib/prime/prime.rbs +188 -0
- data/stdlib/securerandom/securerandom.rbs +9 -0
- data/stdlib/set/set.rbs +301 -0
- data/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +292 -0
@@ -0,0 +1,1108 @@
|
|
1
|
+
# Threads are the Ruby implementation for a concurrent programming model.
|
2
|
+
#
|
3
|
+
# Programs that require multiple threads of execution are a perfect
|
4
|
+
# candidate for Ruby's [Thread](Thread) class.
|
5
|
+
#
|
6
|
+
# For example, we can create a new thread separate from the main thread's
|
7
|
+
# execution using [::new](Thread#method-c-new).
|
8
|
+
#
|
9
|
+
# ```ruby
|
10
|
+
# thr = Thread.new { puts "Whats the big deal" }
|
11
|
+
# ```
|
12
|
+
#
|
13
|
+
# Then we are able to pause the execution of the main thread and allow our
|
14
|
+
# new thread to finish, using
|
15
|
+
# [join](Thread#method-i-join):
|
16
|
+
#
|
17
|
+
# ```ruby
|
18
|
+
# thr.join #=> "Whats the big deal"
|
19
|
+
# ```
|
20
|
+
#
|
21
|
+
# If we don't call `thr.join` before the main thread terminates, then all
|
22
|
+
# other threads including `thr` will be killed.
|
23
|
+
#
|
24
|
+
# Alternatively, you can use an array for handling multiple threads at
|
25
|
+
# once, like in the following example:
|
26
|
+
#
|
27
|
+
# ```ruby
|
28
|
+
# threads = []
|
29
|
+
# threads << Thread.new { puts "Whats the big deal" }
|
30
|
+
# threads << Thread.new { 3.times { puts "Threads are fun!" } }
|
31
|
+
# ```
|
32
|
+
#
|
33
|
+
# After creating a few threads we wait for them all to finish
|
34
|
+
# consecutively.
|
35
|
+
#
|
36
|
+
# ```ruby
|
37
|
+
# threads.each { |thr| thr.join }
|
38
|
+
# ```
|
39
|
+
#
|
40
|
+
#
|
41
|
+
# In order to create new threads, Ruby provides
|
42
|
+
# [::new](Thread#method-c-new),
|
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.
|
48
|
+
#
|
49
|
+
# When subclassing the [Thread](Thread) class, the
|
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.
|
54
|
+
#
|
55
|
+
#
|
56
|
+
# For terminating threads, Ruby provides a variety of ways to do this.
|
57
|
+
#
|
58
|
+
# The class method [::kill](Thread#method-c-kill), is
|
59
|
+
# meant to exit a given thread:
|
60
|
+
#
|
61
|
+
# thr = Thread.new { ... }
|
62
|
+
# Thread.kill(thr) # sends exit() to thr
|
63
|
+
#
|
64
|
+
# Alternatively, you can use the instance method
|
65
|
+
# [exit](Thread#method-i-exit), or any of its aliases
|
66
|
+
# [kill](Thread#method-i-kill) or
|
67
|
+
# [terminate](Thread#method-i-terminate).
|
68
|
+
#
|
69
|
+
# ```ruby
|
70
|
+
# thr.exit
|
71
|
+
# ```
|
72
|
+
#
|
73
|
+
#
|
74
|
+
# Ruby provides a few instance methods for querying the state of a given
|
75
|
+
# thread. To get a string with the current thread's state use
|
76
|
+
# [status](Thread#method-i-status)
|
77
|
+
#
|
78
|
+
# ```ruby
|
79
|
+
# thr = Thread.new { sleep }
|
80
|
+
# thr.status # => "sleep"
|
81
|
+
# thr.exit
|
82
|
+
# thr.status # => false
|
83
|
+
# ```
|
84
|
+
#
|
85
|
+
# You can also use [alive?](Thread#method-i-alive-3F)
|
86
|
+
# to tell if the thread is running or sleeping, and
|
87
|
+
# [stop?](Thread#method-i-stop-3F) if the thread is
|
88
|
+
# dead or sleeping.
|
89
|
+
#
|
90
|
+
#
|
91
|
+
# Since threads are created with blocks, the same rules apply to other
|
92
|
+
# Ruby blocks for variable scope. Any local variables created within this
|
93
|
+
# block are accessible to only this thread.
|
94
|
+
#
|
95
|
+
#
|
96
|
+
# Each fiber has its own bucket for
|
97
|
+
# [\#\[\]](Thread#method-i-5B-5D) storage. When you
|
98
|
+
# set a new fiber-local it is only accessible within this
|
99
|
+
# [Fiber](https://ruby-doc.org/core-2.6.3/Fiber.html). To illustrate:
|
100
|
+
#
|
101
|
+
# ```ruby
|
102
|
+
# Thread.new {
|
103
|
+
# Thread.current[:foo] = "bar"
|
104
|
+
# Fiber.new {
|
105
|
+
# p Thread.current[:foo] # => nil
|
106
|
+
# }.resume
|
107
|
+
# }.join
|
108
|
+
# ```
|
109
|
+
#
|
110
|
+
# This example uses [\[\]](Thread#method-i-5B-5D) for
|
111
|
+
# getting and [\[\]=](Thread#method-i-5B-5D-3D) for
|
112
|
+
# setting fiber-locals, you can also use
|
113
|
+
# [keys](Thread#method-i-keys) to list the
|
114
|
+
# fiber-locals for a given thread and
|
115
|
+
# [key?](Thread#method-i-key-3F) to check if a
|
116
|
+
# fiber-local exists.
|
117
|
+
#
|
118
|
+
# When it comes to thread-locals, they are accessible within the entire
|
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.
|
169
|
+
#
|
170
|
+
#
|
171
|
+
# Ruby provides a few ways to support scheduling threads in your program.
|
172
|
+
#
|
173
|
+
# The first way is by using the class method
|
174
|
+
# [::stop](Thread#method-c-stop), to put the current
|
175
|
+
# running thread to sleep and schedule the execution of another thread.
|
176
|
+
#
|
177
|
+
# Once a thread is asleep, you can use the instance method
|
178
|
+
# [wakeup](Thread#method-i-wakeup) to mark your thread
|
179
|
+
# as eligible for scheduling.
|
180
|
+
#
|
181
|
+
# You can also try [::pass](Thread#method-c-pass),
|
182
|
+
# which attempts to pass execution to another thread but is dependent on
|
183
|
+
# the OS whether a running thread will switch or not. The same goes for
|
184
|
+
# [priority](Thread#method-i-priority), which lets
|
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.
|
188
|
+
class Thread < Object
|
189
|
+
def self.current: () -> Thread
|
190
|
+
|
191
|
+
# Returns the main thread.
|
192
|
+
def self.main: () -> Thread
|
193
|
+
|
194
|
+
def []: (String | Symbol key) -> untyped
|
195
|
+
|
196
|
+
# Attribute Assignment—Sets or creates the value of a fiber-local
|
197
|
+
# variable, using either a symbol or a string.
|
198
|
+
#
|
199
|
+
# See also [\#\[\]](Thread.downloaded.ruby_doc#method-i-5B-5D).
|
200
|
+
#
|
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
|
+
def []=: (String | Symbol key, untyped value) -> untyped
|
207
|
+
|
208
|
+
def alive?: () -> bool
|
209
|
+
|
210
|
+
# Terminates `thr` and schedules another thread to be run.
|
211
|
+
#
|
212
|
+
# If this thread is already marked to be killed,
|
213
|
+
# [exit](Thread.downloaded.ruby_doc#method-i-exit) returns the
|
214
|
+
# [Thread](Thread.downloaded.ruby_doc).
|
215
|
+
#
|
216
|
+
# If this is the main thread, or the last thread, exits the process.
|
217
|
+
def kill: () -> Thread?
|
218
|
+
|
219
|
+
# Returns the status of the thread-local “abort on exception” condition
|
220
|
+
# for this `thr` .
|
221
|
+
#
|
222
|
+
# The default is `false` .
|
223
|
+
#
|
224
|
+
# See also
|
225
|
+
# [abort\_on\_exception=](Thread.downloaded.ruby_doc#method-i-abort_on_exception-3D)
|
226
|
+
# .
|
227
|
+
#
|
228
|
+
# There is also a class level method to set this for all threads, see
|
229
|
+
# [::abort\_on\_exception](Thread.downloaded.ruby_doc#method-c-abort_on_exception)
|
230
|
+
# .
|
231
|
+
def abort_on_exception: () -> bool
|
232
|
+
|
233
|
+
# When set to `true`, if this `thr` is aborted by an exception, the
|
234
|
+
# raised exception will be re-raised in the main thread.
|
235
|
+
#
|
236
|
+
# See also
|
237
|
+
# [abort\_on\_exception](Thread.downloaded.ruby_doc#method-i-abort_on_exception)
|
238
|
+
# .
|
239
|
+
#
|
240
|
+
# There is also a class level method to set this for all threads, see
|
241
|
+
# [::abort\_on\_exception=](Thread.downloaded.ruby_doc#method-c-abort_on_exception-3D)
|
242
|
+
# .
|
243
|
+
def abort_on_exception=: (bool abort_on_exception) -> untyped
|
244
|
+
|
245
|
+
# Adds *proc* as a handler for tracing.
|
246
|
+
#
|
247
|
+
# See
|
248
|
+
# [\#set\_trace\_func](Thread.downloaded.ruby_doc#method-i-set_trace_func)
|
249
|
+
# and
|
250
|
+
# [Kernel\#set\_trace\_func](https://ruby-doc.org/core-2.6.3/Kernel.html#method-i-set_trace_func)
|
251
|
+
# .
|
252
|
+
def add_trace_func: (untyped proc) -> untyped
|
253
|
+
|
254
|
+
# Returns the current backtrace of the target thread.
|
255
|
+
def backtrace: (*untyped args) -> ::Array[untyped]
|
256
|
+
|
257
|
+
# Returns the execution stack for the target thread—an array containing
|
258
|
+
# backtrace location objects.
|
259
|
+
#
|
260
|
+
# See
|
261
|
+
# [Thread::Backtrace::Location](https://ruby-doc.org/core-2.6.3/Thread/Backtrace/Location.html)
|
262
|
+
# for more information.
|
263
|
+
#
|
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
|
+
def backtrace_locations: (*untyped args) -> ::Array[untyped]?
|
268
|
+
|
269
|
+
# Terminates `thr` and schedules another thread to be run.
|
270
|
+
#
|
271
|
+
# If this thread is already marked to be killed,
|
272
|
+
# [exit](Thread.downloaded.ruby_doc#method-i-exit) returns the
|
273
|
+
# [Thread](Thread.downloaded.ruby_doc).
|
274
|
+
#
|
275
|
+
# If this is the main thread, or the last thread, exits the process.
|
276
|
+
def exit: () -> Thread?
|
277
|
+
|
278
|
+
# Returns a fiber-local for the given key. If the key can’t be found,
|
279
|
+
# there are several options: With no other arguments, it will raise a
|
280
|
+
# `KeyError` exception; if *default* is given, then that will be returned;
|
281
|
+
# if the optional code block is specified, then that will be run and its
|
282
|
+
# result returned. See [\#\[\]](Thread.downloaded.ruby_doc#method-i-5B-5D)
|
283
|
+
# and
|
284
|
+
# [Hash\#fetch](https://ruby-doc.org/core-2.6.3/Hash.html#method-i-fetch)
|
285
|
+
# .
|
286
|
+
def fetch: (*untyped sym) -> untyped
|
287
|
+
|
288
|
+
def group: () -> ThreadGroup?
|
289
|
+
|
290
|
+
def initialize: (*untyped args) -> Thread
|
291
|
+
|
292
|
+
# The calling thread will suspend execution and run this `thr` .
|
293
|
+
#
|
294
|
+
# Does not return until `thr` exits or until the given `limit` seconds
|
295
|
+
# have passed.
|
296
|
+
#
|
297
|
+
# If the time limit expires, `nil` will be returned, otherwise `thr` is
|
298
|
+
# returned.
|
299
|
+
#
|
300
|
+
# Any threads not joined will be killed when the main program exits.
|
301
|
+
#
|
302
|
+
# If `thr` had previously raised an exception and the
|
303
|
+
# [::abort\_on\_exception](Thread.downloaded.ruby_doc#method-c-abort_on_exception)
|
304
|
+
# or $DEBUG flags are not set, (so the exception has not yet been
|
305
|
+
# processed), it will be processed at this time.
|
306
|
+
#
|
307
|
+
# ```ruby
|
308
|
+
# a = Thread.new { print "a"; sleep(10); print "b"; print "c" }
|
309
|
+
# x = Thread.new { print "x"; Thread.pass; print "y"; print "z" }
|
310
|
+
# x.join # Let thread x finish, thread a will be killed on exit.
|
311
|
+
# #=> "axyz"
|
312
|
+
# ```
|
313
|
+
#
|
314
|
+
# The following example illustrates the `limit` parameter.
|
315
|
+
#
|
316
|
+
# ```ruby
|
317
|
+
# y = Thread.new { 4.times { sleep 0.1; puts 'tick... ' }}
|
318
|
+
# puts "Waiting" until y.join(0.15)
|
319
|
+
# ```
|
320
|
+
#
|
321
|
+
# This will produce:
|
322
|
+
#
|
323
|
+
# tick...
|
324
|
+
# Waiting
|
325
|
+
# tick...
|
326
|
+
# Waiting
|
327
|
+
# tick...
|
328
|
+
# tick...
|
329
|
+
def join: (*untyped limit) -> Thread
|
330
|
+
|
331
|
+
# Returns `true` if the given string (or symbol) exists as a fiber-local
|
332
|
+
# variable.
|
333
|
+
#
|
334
|
+
# ```ruby
|
335
|
+
# me = Thread.current
|
336
|
+
# me[:oliver] = "a"
|
337
|
+
# me.key?(:oliver) #=> true
|
338
|
+
# me.key?(:stanley) #=> false
|
339
|
+
# ```
|
340
|
+
def key?: (Symbol sym) -> bool
|
341
|
+
|
342
|
+
def keys: () -> ::Array[Symbol]
|
343
|
+
|
344
|
+
# show the name of the thread.
|
345
|
+
def name: () -> String
|
346
|
+
|
347
|
+
# set given name to the ruby thread. On some platform, it may set the name
|
348
|
+
# to pthread and/or kernel.
|
349
|
+
def name=: (untyped name) -> untyped
|
350
|
+
|
351
|
+
# Returns whether or not the asynchronous queue is empty for the target
|
352
|
+
# thread.
|
353
|
+
#
|
354
|
+
# If `error` is given, then check only for `error` type deferred events.
|
355
|
+
#
|
356
|
+
# See
|
357
|
+
# [::pending\_interrupt?](Thread.downloaded.ruby_doc#method-c-pending_interrupt-3F)
|
358
|
+
# for more information.
|
359
|
+
def pending_interrupt?: (*untyped args) -> bool
|
360
|
+
|
361
|
+
# Returns the priority of *thr* . Default is inherited from the current
|
362
|
+
# thread which creating the new thread, or zero for the initial main
|
363
|
+
# thread; higher-priority thread will run more frequently than
|
364
|
+
# lower-priority threads (but lower-priority threads can also run).
|
365
|
+
#
|
366
|
+
# This is just hint for Ruby thread scheduler. It may be ignored on some
|
367
|
+
# platform.
|
368
|
+
#
|
369
|
+
# ```ruby
|
370
|
+
# Thread.current.priority #=> 0
|
371
|
+
# ```
|
372
|
+
def priority: () -> Integer
|
373
|
+
|
374
|
+
# Sets the priority of *thr* to *integer* . Higher-priority threads will
|
375
|
+
# run more frequently than lower-priority threads (but lower-priority
|
376
|
+
# threads can also run).
|
377
|
+
#
|
378
|
+
# This is just hint for Ruby thread scheduler. It may be ignored on some
|
379
|
+
# platform.
|
380
|
+
#
|
381
|
+
# ```ruby
|
382
|
+
# count1 = count2 = 0
|
383
|
+
# a = Thread.new do
|
384
|
+
# loop { count1 += 1 }
|
385
|
+
# end
|
386
|
+
# a.priority = -1
|
387
|
+
#
|
388
|
+
# b = Thread.new do
|
389
|
+
# loop { count2 += 1 }
|
390
|
+
# end
|
391
|
+
# b.priority = -2
|
392
|
+
# sleep 1 #=> 1
|
393
|
+
# count1 #=> 622504
|
394
|
+
# count2 #=> 5832
|
395
|
+
# ```
|
396
|
+
def priority=: (Integer priority) -> untyped
|
397
|
+
|
398
|
+
# Returns the status of the thread-local “report on exception” condition
|
399
|
+
# for this `thr` .
|
400
|
+
#
|
401
|
+
# The default value when creating a [Thread](Thread.downloaded.ruby_doc)
|
402
|
+
# is the value of the global flag
|
403
|
+
# [::report\_on\_exception](Thread.downloaded.ruby_doc#method-c-report_on_exception)
|
404
|
+
# .
|
405
|
+
#
|
406
|
+
# See also
|
407
|
+
# [report\_on\_exception=](Thread.downloaded.ruby_doc#method-i-report_on_exception-3D)
|
408
|
+
# .
|
409
|
+
#
|
410
|
+
# There is also a class level method to set this for all new threads, see
|
411
|
+
# [::report\_on\_exception=](Thread.downloaded.ruby_doc#method-c-report_on_exception-3D)
|
412
|
+
# .
|
413
|
+
def report_on_exception: () -> bool
|
414
|
+
|
415
|
+
# When set to `true`, a message is printed on $stderr if an exception
|
416
|
+
# kills this `thr` . See
|
417
|
+
# [::report\_on\_exception](Thread.downloaded.ruby_doc#method-c-report_on_exception)
|
418
|
+
# for details.
|
419
|
+
#
|
420
|
+
# See also
|
421
|
+
# [report\_on\_exception](Thread.downloaded.ruby_doc#method-i-report_on_exception)
|
422
|
+
# .
|
423
|
+
#
|
424
|
+
# There is also a class level method to set this for all new threads, see
|
425
|
+
# [::report\_on\_exception=](Thread.downloaded.ruby_doc#method-c-report_on_exception-3D)
|
426
|
+
# .
|
427
|
+
def report_on_exception=: (bool report_on_exception) -> untyped
|
428
|
+
|
429
|
+
# Wakes up `thr`, making it eligible for scheduling.
|
430
|
+
#
|
431
|
+
# ```ruby
|
432
|
+
# a = Thread.new { puts "a"; Thread.stop; puts "c" }
|
433
|
+
# sleep 0.1 while a.status!='sleep'
|
434
|
+
# puts "Got here"
|
435
|
+
# a.run
|
436
|
+
# a.join
|
437
|
+
# ```
|
438
|
+
#
|
439
|
+
# This will produce:
|
440
|
+
#
|
441
|
+
# ```ruby
|
442
|
+
# a
|
443
|
+
# Got here
|
444
|
+
# c
|
445
|
+
# ```
|
446
|
+
#
|
447
|
+
# See also the instance method
|
448
|
+
# [wakeup](Thread.downloaded.ruby_doc#method-i-wakeup).
|
449
|
+
def run: () -> Thread
|
450
|
+
|
451
|
+
# Returns the safe level.
|
452
|
+
#
|
453
|
+
# This method is obsolete because $SAFE is a process global state. Simply
|
454
|
+
# check $SAFE.
|
455
|
+
def safe_level: () -> Integer
|
456
|
+
|
457
|
+
def status: () -> (String | bool)?
|
458
|
+
|
459
|
+
# Returns `true` if `thr` is dead or sleeping.
|
460
|
+
#
|
461
|
+
# ```ruby
|
462
|
+
# a = Thread.new { Thread.stop }
|
463
|
+
# b = Thread.current
|
464
|
+
# a.stop? #=> true
|
465
|
+
# b.stop? #=> false
|
466
|
+
# ```
|
467
|
+
#
|
468
|
+
# See also [alive?](Thread.downloaded.ruby_doc#method-i-alive-3F) and
|
469
|
+
# [status](Thread.downloaded.ruby_doc#method-i-status).
|
470
|
+
def `stop?`: () -> bool
|
471
|
+
|
472
|
+
# Terminates `thr` and schedules another thread to be run.
|
473
|
+
#
|
474
|
+
# If this thread is already marked to be killed,
|
475
|
+
# [exit](Thread.downloaded.ruby_doc#method-i-exit) returns the
|
476
|
+
# [Thread](Thread.downloaded.ruby_doc).
|
477
|
+
#
|
478
|
+
# If this is the main thread, or the last thread, exits the process.
|
479
|
+
def terminate: () -> Thread?
|
480
|
+
|
481
|
+
# Returns `true` if the given string (or symbol) exists as a thread-local
|
482
|
+
# variable.
|
483
|
+
#
|
484
|
+
# ```ruby
|
485
|
+
# me = Thread.current
|
486
|
+
# me.thread_variable_set(:oliver, "a")
|
487
|
+
# me.thread_variable?(:oliver) #=> true
|
488
|
+
# me.thread_variable?(:stanley) #=> false
|
489
|
+
# ```
|
490
|
+
#
|
491
|
+
# Note that these are not fiber local variables. Please see
|
492
|
+
# [\#\[\]](Thread.downloaded.ruby_doc#method-i-5B-5D) and
|
493
|
+
# [\#thread\_variable\_get](Thread.downloaded.ruby_doc#method-i-thread_variable_get)
|
494
|
+
# for more details.
|
495
|
+
def thread_variable?: (String | Symbol key) -> bool
|
496
|
+
|
497
|
+
# Returns the value of a thread local variable that has been set. Note
|
498
|
+
# that these are different than fiber local values. For fiber local
|
499
|
+
# values, please see [\#\[\]](Thread.downloaded.ruby_doc#method-i-5B-5D)
|
500
|
+
# and [\#\[\]=](Thread.downloaded.ruby_doc#method-i-5B-5D-3D).
|
501
|
+
#
|
502
|
+
# [Thread](Thread.downloaded.ruby_doc) local values are carried along with
|
503
|
+
# threads, and do not respect fibers. For example:
|
504
|
+
#
|
505
|
+
# ```ruby
|
506
|
+
# Thread.new {
|
507
|
+
# Thread.current.thread_variable_set("foo", "bar") # set a thread local
|
508
|
+
# Thread.current["foo"] = "bar" # set a fiber local
|
509
|
+
#
|
510
|
+
# Fiber.new {
|
511
|
+
# Fiber.yield [
|
512
|
+
# Thread.current.thread_variable_get("foo"), # get the thread local
|
513
|
+
# Thread.current["foo"], # get the fiber local
|
514
|
+
# ]
|
515
|
+
# }.resume
|
516
|
+
# }.join.value # => ['bar', nil]
|
517
|
+
# ```
|
518
|
+
#
|
519
|
+
# The value “bar” is returned for the thread local, where nil is returned
|
520
|
+
# for the fiber local. The fiber is executed in the same thread, so the
|
521
|
+
# thread local values are available.
|
522
|
+
def thread_variable_get: (untyped key) -> untyped
|
523
|
+
|
524
|
+
# Sets a thread local with `key` to `value` . Note that these are local to
|
525
|
+
# threads, and not to fibers. Please see
|
526
|
+
# [\#thread\_variable\_get](Thread.downloaded.ruby_doc#method-i-thread_variable_get)
|
527
|
+
# and [\#\[\]](Thread.downloaded.ruby_doc#method-i-5B-5D) for more
|
528
|
+
# information.
|
529
|
+
def thread_variable_set: (untyped key, untyped value) -> untyped
|
530
|
+
|
531
|
+
def thread_variables: () -> ::Array[Symbol]
|
532
|
+
|
533
|
+
# Waits for `thr` to complete, using
|
534
|
+
# [join](Thread.downloaded.ruby_doc#method-i-join), and returns its value
|
535
|
+
# or raises the exception which terminated the thread.
|
536
|
+
#
|
537
|
+
# ```ruby
|
538
|
+
# a = Thread.new { 2 + 2 }
|
539
|
+
# a.value #=> 4
|
540
|
+
#
|
541
|
+
# b = Thread.new { raise 'something went wrong' }
|
542
|
+
# b.value #=> RuntimeError: something went wrong
|
543
|
+
# ```
|
544
|
+
def value: () -> Object
|
545
|
+
|
546
|
+
# Marks a given thread as eligible for scheduling, however it may still
|
547
|
+
# remain blocked on I/O.
|
548
|
+
#
|
549
|
+
# **Note:** This does not invoke the scheduler, see
|
550
|
+
# [run](Thread.downloaded.ruby_doc#method-i-run) for more information.
|
551
|
+
#
|
552
|
+
# ```ruby
|
553
|
+
# c = Thread.new { Thread.stop; puts "hey!" }
|
554
|
+
# sleep 0.1 while c.status!='sleep'
|
555
|
+
# c.wakeup
|
556
|
+
# c.join
|
557
|
+
# #=> "hey!"
|
558
|
+
# ```
|
559
|
+
def wakeup: () -> Thread
|
560
|
+
|
561
|
+
# Returns the status of the global “abort on exception” condition.
|
562
|
+
#
|
563
|
+
# The default is `false` .
|
564
|
+
#
|
565
|
+
# When set to `true`, if any thread is aborted by an exception, the
|
566
|
+
# raised exception will be re-raised in the main thread.
|
567
|
+
#
|
568
|
+
# Can also be specified by the global $DEBUG flag or command line option
|
569
|
+
# `-d` .
|
570
|
+
#
|
571
|
+
# See also
|
572
|
+
# [::abort\_on\_exception=](Thread.downloaded.ruby_doc#method-c-abort_on_exception-3D)
|
573
|
+
# .
|
574
|
+
#
|
575
|
+
# There is also an instance level method to set this for a specific
|
576
|
+
# thread, see
|
577
|
+
# [abort\_on\_exception](Thread.downloaded.ruby_doc#method-i-abort_on_exception)
|
578
|
+
# .
|
579
|
+
def self.abort_on_exception: () -> untyped
|
580
|
+
|
581
|
+
# When set to `true`, if any thread is aborted by an exception, the
|
582
|
+
# raised exception will be re-raised in the main thread. Returns the new
|
583
|
+
# state.
|
584
|
+
#
|
585
|
+
# ```ruby
|
586
|
+
# Thread.abort_on_exception = true
|
587
|
+
# t1 = Thread.new do
|
588
|
+
# puts "In new thread"
|
589
|
+
# raise "Exception from thread"
|
590
|
+
# end
|
591
|
+
# sleep(1)
|
592
|
+
# puts "not reached"
|
593
|
+
# ```
|
594
|
+
#
|
595
|
+
# This will produce:
|
596
|
+
#
|
597
|
+
# In new thread
|
598
|
+
# prog.rb:4: Exception from thread (RuntimeError)
|
599
|
+
# from prog.rb:2:in `initialize'
|
600
|
+
# from prog.rb:2:in `new'
|
601
|
+
# from prog.rb:2
|
602
|
+
#
|
603
|
+
# See also
|
604
|
+
# [::abort\_on\_exception](Thread.downloaded.ruby_doc#method-c-abort_on_exception)
|
605
|
+
# .
|
606
|
+
#
|
607
|
+
# There is also an instance level method to set this for a specific
|
608
|
+
# thread, see
|
609
|
+
# [abort\_on\_exception=](Thread.downloaded.ruby_doc#method-i-abort_on_exception-3D)
|
610
|
+
# .
|
611
|
+
def self.abort_on_exception=: (untyped abort_on_exception) -> untyped
|
612
|
+
|
613
|
+
# Wraps the block in a single, VM-global
|
614
|
+
# [Mutex\#synchronize](https://ruby-doc.org/core-2.6.3/Mutex.html#method-i-synchronize)
|
615
|
+
# , returning the value of the block. A thread executing inside the
|
616
|
+
# exclusive section will only block other threads which also use the
|
617
|
+
# [::exclusive](Thread.downloaded.ruby_doc#method-c-exclusive) mechanism.
|
618
|
+
def self.exclusive: () { () -> untyped } -> untyped
|
619
|
+
|
620
|
+
# Terminates the currently running thread and schedules another thread to
|
621
|
+
# be run.
|
622
|
+
#
|
623
|
+
# If this thread is already marked to be killed,
|
624
|
+
# [::exit](Thread.downloaded.ruby_doc#method-c-exit) returns the
|
625
|
+
# [Thread](Thread.downloaded.ruby_doc).
|
626
|
+
#
|
627
|
+
# If this is the main thread, or the last thread, exit the process.
|
628
|
+
def self.exit: () -> untyped
|
629
|
+
|
630
|
+
# Basically the same as [::new](Thread.downloaded.ruby_doc#method-c-new).
|
631
|
+
# However, if class [Thread](Thread.downloaded.ruby_doc) is subclassed,
|
632
|
+
# then calling `start` in that subclass will not invoke the subclass’s
|
633
|
+
# `initialize` method.
|
634
|
+
def self.fork: (*untyped args) -> untyped
|
635
|
+
|
636
|
+
# Changes asynchronous interrupt timing.
|
637
|
+
#
|
638
|
+
# *interrupt* means asynchronous event and corresponding procedure by
|
639
|
+
# [\#raise](Thread.downloaded.ruby_doc#method-i-raise),
|
640
|
+
# [\#kill](Thread.downloaded.ruby_doc#method-i-kill), signal trap (not
|
641
|
+
# supported yet) and main thread termination (if main thread terminates,
|
642
|
+
# then all other thread will be killed).
|
643
|
+
#
|
644
|
+
# The given `hash` has pairs like `ExceptionClass => :TimingSymbol` .
|
645
|
+
# Where the ExceptionClass is the interrupt handled by the given block.
|
646
|
+
# The TimingSymbol can be one of the following symbols:
|
647
|
+
#
|
648
|
+
# - `:immediate`
|
649
|
+
# Invoke interrupts immediately.
|
650
|
+
#
|
651
|
+
# - `:on_blocking`
|
652
|
+
# Invoke interrupts while *BlockingOperation* .
|
653
|
+
#
|
654
|
+
# - `:never`
|
655
|
+
# Never invoke all interrupts.
|
656
|
+
#
|
657
|
+
# *BlockingOperation* means that the operation will block the calling
|
658
|
+
# thread, such as read and write. On CRuby implementation,
|
659
|
+
# *BlockingOperation* is any operation executed without GVL.
|
660
|
+
#
|
661
|
+
# Masked asynchronous interrupts are delayed until they are enabled. This
|
662
|
+
# method is similar to sigprocmask(3).
|
663
|
+
#
|
664
|
+
#
|
665
|
+
# Asynchronous interrupts are difficult to use.
|
666
|
+
#
|
667
|
+
# If you need to communicate between threads, please consider to use
|
668
|
+
# another way such as [Queue](https://ruby-doc.org/core-2.6.3/Queue.html)
|
669
|
+
# .
|
670
|
+
#
|
671
|
+
# Or use them with deep understanding about this method.
|
672
|
+
#
|
673
|
+
#
|
674
|
+
# In this example, we can guard from
|
675
|
+
# [\#raise](Thread.downloaded.ruby_doc#method-i-raise) exceptions.
|
676
|
+
#
|
677
|
+
# Using the `:never` TimingSymbol the
|
678
|
+
# [RuntimeError](https://ruby-doc.org/core-2.6.3/RuntimeError.html)
|
679
|
+
# exception will always be ignored in the first block of the main thread.
|
680
|
+
# In the second
|
681
|
+
# [::handle\_interrupt](Thread.downloaded.ruby_doc#method-c-handle_interrupt)
|
682
|
+
# block we can purposefully handle
|
683
|
+
# [RuntimeError](https://ruby-doc.org/core-2.6.3/RuntimeError.html)
|
684
|
+
# exceptions.
|
685
|
+
#
|
686
|
+
# ```ruby
|
687
|
+
# th = Thread.new do
|
688
|
+
# Thread.handle_interrupt(RuntimeError => :never) {
|
689
|
+
# begin
|
690
|
+
# # You can write resource allocation code safely.
|
691
|
+
# Thread.handle_interrupt(RuntimeError => :immediate) {
|
692
|
+
# # ...
|
693
|
+
# }
|
694
|
+
# ensure
|
695
|
+
# # You can write resource deallocation code safely.
|
696
|
+
# end
|
697
|
+
# }
|
698
|
+
# end
|
699
|
+
# Thread.pass
|
700
|
+
# # ...
|
701
|
+
# th.raise "stop"
|
702
|
+
# ```
|
703
|
+
#
|
704
|
+
# While we are ignoring the
|
705
|
+
# [RuntimeError](https://ruby-doc.org/core-2.6.3/RuntimeError.html)
|
706
|
+
# exception, it’s safe to write our resource allocation code. Then, the
|
707
|
+
# ensure block is where we can safely deallocate your resources.
|
708
|
+
#
|
709
|
+
#
|
710
|
+
# In the next example, we will guard from the Timeout::Error exception.
|
711
|
+
# This will help prevent from leaking resources when Timeout::Error
|
712
|
+
# exceptions occur during normal ensure clause. For this example we use
|
713
|
+
# the help of the standard library Timeout, from lib/timeout.rb
|
714
|
+
#
|
715
|
+
# ```ruby
|
716
|
+
# require 'timeout'
|
717
|
+
# Thread.handle_interrupt(Timeout::Error => :never) {
|
718
|
+
# timeout(10){
|
719
|
+
# # Timeout::Error doesn't occur here
|
720
|
+
# Thread.handle_interrupt(Timeout::Error => :on_blocking) {
|
721
|
+
# # possible to be killed by Timeout::Error
|
722
|
+
# # while blocking operation
|
723
|
+
# }
|
724
|
+
# # Timeout::Error doesn't occur here
|
725
|
+
# }
|
726
|
+
# }
|
727
|
+
# ```
|
728
|
+
#
|
729
|
+
# In the first part of the `timeout` block, we can rely on Timeout::Error
|
730
|
+
# being ignored. Then in the `Timeout::Error => :on_blocking` block, any
|
731
|
+
# operation that will block the calling thread is susceptible to a
|
732
|
+
# Timeout::Error exception being raised.
|
733
|
+
#
|
734
|
+
#
|
735
|
+
# It’s possible to stack multiple levels of
|
736
|
+
# [::handle\_interrupt](Thread.downloaded.ruby_doc#method-c-handle_interrupt)
|
737
|
+
# blocks in order to control more than one ExceptionClass and TimingSymbol
|
738
|
+
# at a time.
|
739
|
+
#
|
740
|
+
# ```ruby
|
741
|
+
# Thread.handle_interrupt(FooError => :never) {
|
742
|
+
# Thread.handle_interrupt(BarError => :never) {
|
743
|
+
# # FooError and BarError are prohibited.
|
744
|
+
# }
|
745
|
+
# }
|
746
|
+
# ```
|
747
|
+
#
|
748
|
+
#
|
749
|
+
# All exceptions inherited from the ExceptionClass parameter will be
|
750
|
+
# considered.
|
751
|
+
#
|
752
|
+
# ```ruby
|
753
|
+
# Thread.handle_interrupt(Exception => :never) {
|
754
|
+
# # all exceptions inherited from Exception are prohibited.
|
755
|
+
# }
|
756
|
+
# ```
|
757
|
+
def self.handle_interrupt: (untyped hash) -> untyped
|
758
|
+
|
759
|
+
def self.kill: (Thread thread) -> untyped
|
760
|
+
|
761
|
+
def self.list: () -> untyped
|
762
|
+
|
763
|
+
# Give the thread scheduler a hint to pass execution to another thread. A
|
764
|
+
# running thread may or may not switch, it depends on OS and processor.
|
765
|
+
def self.pass: () -> untyped
|
766
|
+
|
767
|
+
# Returns whether or not the asynchronous queue is empty.
|
768
|
+
#
|
769
|
+
# Since
|
770
|
+
# [::handle\_interrupt](Thread.downloaded.ruby_doc#method-c-handle_interrupt)
|
771
|
+
# can be used to defer asynchronous events, this method can be used to
|
772
|
+
# determine if there are any deferred events.
|
773
|
+
#
|
774
|
+
# If you find this method returns true, then you may finish `:never`
|
775
|
+
# blocks.
|
776
|
+
#
|
777
|
+
# For example, the following method processes deferred asynchronous events
|
778
|
+
# immediately.
|
779
|
+
#
|
780
|
+
# ```ruby
|
781
|
+
# def Thread.kick_interrupt_immediately
|
782
|
+
# Thread.handle_interrupt(Object => :immediate) {
|
783
|
+
# Thread.pass
|
784
|
+
# }
|
785
|
+
# end
|
786
|
+
# ```
|
787
|
+
#
|
788
|
+
# If `error` is given, then check only for `error` type deferred events.
|
789
|
+
#
|
790
|
+
#
|
791
|
+
# th = Thread.new{
|
792
|
+
# Thread.handle_interrupt(RuntimeError => :on_blocking){
|
793
|
+
# while true
|
794
|
+
# ...
|
795
|
+
# # reach safe point to invoke interrupt
|
796
|
+
# if Thread.pending_interrupt?
|
797
|
+
# Thread.handle_interrupt(Object => :immediate){}
|
798
|
+
# end
|
799
|
+
# ...
|
800
|
+
# end
|
801
|
+
# }
|
802
|
+
# }
|
803
|
+
# ...
|
804
|
+
# th.raise # stop thread
|
805
|
+
#
|
806
|
+
# This example can also be written as the following, which you should use
|
807
|
+
# to avoid asynchronous interrupts.
|
808
|
+
#
|
809
|
+
# flag = true
|
810
|
+
# th = Thread.new{
|
811
|
+
# Thread.handle_interrupt(RuntimeError => :on_blocking){
|
812
|
+
# while true
|
813
|
+
# ...
|
814
|
+
# # reach safe point to invoke interrupt
|
815
|
+
# break if flag == false
|
816
|
+
# ...
|
817
|
+
# end
|
818
|
+
# }
|
819
|
+
# }
|
820
|
+
# ...
|
821
|
+
# flag = false # stop thread
|
822
|
+
def self.pending_interrupt?: (*untyped args) -> bool
|
823
|
+
|
824
|
+
def self.report_on_exception: () -> untyped
|
825
|
+
|
826
|
+
def self.report_on_exception=: (untyped report_on_exception) -> untyped
|
827
|
+
|
828
|
+
# Basically the same as [::new](Thread.downloaded.ruby_doc#method-c-new).
|
829
|
+
# However, if class [Thread](Thread.downloaded.ruby_doc) is subclassed,
|
830
|
+
# then calling `start` in that subclass will not invoke the subclass’s
|
831
|
+
# `initialize` method.
|
832
|
+
def self.start: (*untyped args) -> untyped
|
833
|
+
|
834
|
+
# Stops execution of the current thread, putting it into a “sleep” state,
|
835
|
+
# and schedules execution of another thread.
|
836
|
+
#
|
837
|
+
# ```ruby
|
838
|
+
# a = Thread.new { print "a"; Thread.stop; print "c" }
|
839
|
+
# sleep 0.1 while a.status!='sleep'
|
840
|
+
# print "b"
|
841
|
+
# a.run
|
842
|
+
# a.join
|
843
|
+
# #=> "abc"
|
844
|
+
# ```
|
845
|
+
def self.`stop`: () -> untyped
|
846
|
+
end
|
847
|
+
|
848
|
+
class Thread::Backtrace < Object
|
849
|
+
end
|
850
|
+
|
851
|
+
class Thread::Backtrace::Location
|
852
|
+
def absolute_path: () -> String?
|
853
|
+
|
854
|
+
def base_label: () -> String?
|
855
|
+
|
856
|
+
def label: () -> String?
|
857
|
+
|
858
|
+
def lineno: () -> Integer
|
859
|
+
|
860
|
+
def path: () -> String?
|
861
|
+
end
|
862
|
+
|
863
|
+
# [ConditionVariable](ConditionVariable) objects
|
864
|
+
# augment class [Mutex](https://ruby-doc.org/core-2.6.3/Mutex.html).
|
865
|
+
# Using condition variables, it is possible to suspend while in the middle
|
866
|
+
# of a critical section until a resource becomes available.
|
867
|
+
#
|
868
|
+
# Example:
|
869
|
+
#
|
870
|
+
# ```ruby
|
871
|
+
# mutex = Mutex.new
|
872
|
+
# resource = ConditionVariable.new
|
873
|
+
#
|
874
|
+
# a = Thread.new {
|
875
|
+
# mutex.synchronize {
|
876
|
+
# # Thread 'a' now needs the resource
|
877
|
+
# resource.wait(mutex)
|
878
|
+
# # 'a' can now have the resource
|
879
|
+
# }
|
880
|
+
# }
|
881
|
+
#
|
882
|
+
# b = Thread.new {
|
883
|
+
# mutex.synchronize {
|
884
|
+
# # Thread 'b' has finished using the resource
|
885
|
+
# resource.signal
|
886
|
+
# }
|
887
|
+
# }
|
888
|
+
# ```
|
889
|
+
class Thread::ConditionVariable < Object
|
890
|
+
# Wakes up all threads waiting for this lock.
|
891
|
+
def broadcast: () -> self
|
892
|
+
|
893
|
+
# Wakes up the first thread in line waiting for this lock.
|
894
|
+
def signal: () -> self
|
895
|
+
|
896
|
+
# Releases the lock held in `mutex` and waits; reacquires the lock on
|
897
|
+
# wakeup.
|
898
|
+
#
|
899
|
+
# If `timeout` is given, this method returns after `timeout` seconds
|
900
|
+
# passed, even if no other thread doesn't signal.
|
901
|
+
def wait: (Mutex mutex, ?Integer timeout) -> self
|
902
|
+
end
|
903
|
+
|
904
|
+
# [Mutex](Mutex) implements a simple semaphore that
|
905
|
+
# can be used to coordinate access to shared data from multiple concurrent
|
906
|
+
# threads.
|
907
|
+
#
|
908
|
+
# Example:
|
909
|
+
#
|
910
|
+
# ```ruby
|
911
|
+
# semaphore = Mutex.new
|
912
|
+
#
|
913
|
+
# a = Thread.new {
|
914
|
+
# semaphore.synchronize {
|
915
|
+
# # access shared resource
|
916
|
+
# }
|
917
|
+
# }
|
918
|
+
#
|
919
|
+
# b = Thread.new {
|
920
|
+
# semaphore.synchronize {
|
921
|
+
# # access shared resource
|
922
|
+
# }
|
923
|
+
# }
|
924
|
+
# ```
|
925
|
+
class Thread::Mutex < Object
|
926
|
+
# Attempts to grab the lock and waits if it isn’t available. Raises
|
927
|
+
# `ThreadError` if `mutex` was locked by the current thread.
|
928
|
+
def lock: () -> self
|
929
|
+
|
930
|
+
# Returns `true` if this lock is currently held by some thread.
|
931
|
+
def locked?: () -> bool
|
932
|
+
|
933
|
+
# Returns `true` if this lock is currently held by current thread.
|
934
|
+
def owned?: () -> bool
|
935
|
+
|
936
|
+
# Obtains a lock, runs the block, and releases the lock when the block
|
937
|
+
# completes. See the example under `Mutex` .
|
938
|
+
def synchronize: [X] () { () -> X } -> X
|
939
|
+
|
940
|
+
# Attempts to obtain the lock and returns immediately. Returns `true` if
|
941
|
+
# the lock was granted.
|
942
|
+
def try_lock: () -> bool
|
943
|
+
|
944
|
+
# Releases the lock. Raises `ThreadError` if `mutex` wasn’t locked by the
|
945
|
+
# current thread.
|
946
|
+
def unlock: () -> self
|
947
|
+
end
|
948
|
+
|
949
|
+
# The [Queue](Queue) class implements multi-producer,
|
950
|
+
# multi-consumer queues. It is especially useful in threaded programming
|
951
|
+
# when information must be exchanged safely between multiple threads. The
|
952
|
+
# [Queue](Queue) class implements all the required
|
953
|
+
# locking semantics.
|
954
|
+
#
|
955
|
+
# The class implements FIFO type of queue. In a FIFO queue, the first
|
956
|
+
# tasks added are the first retrieved.
|
957
|
+
#
|
958
|
+
# Example:
|
959
|
+
#
|
960
|
+
# ```ruby
|
961
|
+
# queue = Queue.new
|
962
|
+
#
|
963
|
+
# producer = Thread.new do
|
964
|
+
# 5.times do |i|
|
965
|
+
# sleep rand(i) # simulate expense
|
966
|
+
# queue << i
|
967
|
+
# puts "#{i} produced"
|
968
|
+
# end
|
969
|
+
# end
|
970
|
+
#
|
971
|
+
# consumer = Thread.new do
|
972
|
+
# 5.times do |i|
|
973
|
+
# value = queue.pop
|
974
|
+
# sleep rand(i/2) # simulate expense
|
975
|
+
# puts "consumed #{value}"
|
976
|
+
# end
|
977
|
+
# end
|
978
|
+
#
|
979
|
+
# consumer.join
|
980
|
+
# ```
|
981
|
+
class Thread::Queue < Object
|
982
|
+
# Alias for: [push](Queue.downloaded.ruby_doc#method-i-push)
|
983
|
+
alias << push
|
984
|
+
|
985
|
+
# Removes all objects from the queue.
|
986
|
+
def clear: () -> void
|
987
|
+
|
988
|
+
# Closes the queue. A closed queue cannot be re-opened.
|
989
|
+
#
|
990
|
+
# After the call to close completes, the following are true:
|
991
|
+
#
|
992
|
+
# - `closed?` will return true
|
993
|
+
#
|
994
|
+
# - `close` will be ignored.
|
995
|
+
#
|
996
|
+
# - calling enq/push/\<\< will raise a `ClosedQueueError` .
|
997
|
+
#
|
998
|
+
# - when `empty?` is false, calling deq/pop/shift will return an object
|
999
|
+
# from the queue as usual.
|
1000
|
+
#
|
1001
|
+
# - when `empty?` is true, deq(false) will not suspend the thread and
|
1002
|
+
# will return nil. deq(true) will raise a `ThreadError` .
|
1003
|
+
#
|
1004
|
+
# [ClosedQueueError](https://ruby-doc.org/core-2.6.3/ClosedQueueError.html)
|
1005
|
+
# is inherited from
|
1006
|
+
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html), so
|
1007
|
+
# that you can break loop block.
|
1008
|
+
#
|
1009
|
+
# Example:
|
1010
|
+
#
|
1011
|
+
# q = Queue.new
|
1012
|
+
# Thread.new{
|
1013
|
+
# while e = q.deq # wait for nil to break loop
|
1014
|
+
# # ...
|
1015
|
+
# end
|
1016
|
+
# }
|
1017
|
+
# q.close
|
1018
|
+
def close: () -> self
|
1019
|
+
|
1020
|
+
# Returns `true` if the queue is closed.
|
1021
|
+
def closed?: () -> bool
|
1022
|
+
|
1023
|
+
# Alias for: [pop](Queue.downloaded.ruby_doc#method-i-pop)
|
1024
|
+
alias deq pop
|
1025
|
+
|
1026
|
+
# Returns `true` if the queue is empty.
|
1027
|
+
def empty?: () -> bool
|
1028
|
+
|
1029
|
+
# Alias for: [push](Queue.downloaded.ruby_doc#method-i-push)
|
1030
|
+
alias enq push
|
1031
|
+
|
1032
|
+
# Returns the length of the queue.
|
1033
|
+
#
|
1034
|
+
#
|
1035
|
+
#
|
1036
|
+
# Also aliased as: [size](Queue.downloaded.ruby_doc#method-i-size)
|
1037
|
+
def length: () -> Integer
|
1038
|
+
|
1039
|
+
# Returns the number of threads waiting on the queue.
|
1040
|
+
def num_waiting: () -> Integer
|
1041
|
+
|
1042
|
+
# Retrieves data from the queue.
|
1043
|
+
#
|
1044
|
+
# If the queue is empty, the calling thread is suspended until data is
|
1045
|
+
# pushed onto the queue. If `non_block` is true, the thread isn't
|
1046
|
+
# suspended, and `ThreadError` is raised.
|
1047
|
+
#
|
1048
|
+
#
|
1049
|
+
#
|
1050
|
+
# Also aliased as: [deq](Queue.downloaded.ruby_doc#method-i-deq),
|
1051
|
+
# [shift](Queue.downloaded.ruby_doc#method-i-shift)
|
1052
|
+
def pop: (?bool non_block) -> untyped
|
1053
|
+
|
1054
|
+
# Pushes the given `object` to the queue.
|
1055
|
+
#
|
1056
|
+
#
|
1057
|
+
#
|
1058
|
+
# Also aliased as: [enq](Queue.downloaded.ruby_doc#method-i-enq),
|
1059
|
+
# [\<\<](Queue.downloaded.ruby_doc#method-i-3C-3C)
|
1060
|
+
def push: (untyped obj) -> void
|
1061
|
+
|
1062
|
+
# Alias for: [pop](Queue.downloaded.ruby_doc#method-i-pop)
|
1063
|
+
alias shift pop
|
1064
|
+
|
1065
|
+
# Alias for: [length](Queue.downloaded.ruby_doc#method-i-length)
|
1066
|
+
alias size length
|
1067
|
+
end
|
1068
|
+
|
1069
|
+
# This class represents queues of specified size capacity. The push
|
1070
|
+
# operation may be blocked if the capacity is full.
|
1071
|
+
#
|
1072
|
+
# See [Queue](https://ruby-doc.org/core-2.6.3/Queue.html) for an example
|
1073
|
+
# of how a [SizedQueue](SizedQueue) works.
|
1074
|
+
class Thread::SizedQueue < Thread::Queue
|
1075
|
+
# Alias for: [push](SizedQueue.downloaded.ruby_doc#method-i-push)
|
1076
|
+
alias << push
|
1077
|
+
|
1078
|
+
# Alias for: [push](SizedQueue.downloaded.ruby_doc#method-i-push)
|
1079
|
+
alias enq push
|
1080
|
+
|
1081
|
+
def initialize: (Integer max) -> SizedQueue
|
1082
|
+
|
1083
|
+
# Returns the maximum size of the queue.
|
1084
|
+
def max: () -> Integer
|
1085
|
+
|
1086
|
+
# Sets the maximum size of the queue to the given `number` .
|
1087
|
+
def max=: (Integer max) -> void
|
1088
|
+
|
1089
|
+
# Pushes `object` to the queue.
|
1090
|
+
#
|
1091
|
+
# If there is no space left in the queue, waits until space becomes
|
1092
|
+
# available, unless `non_block` is true. If `non_block` is true, the
|
1093
|
+
# thread isn't suspended, and `ThreadError` is raised.
|
1094
|
+
#
|
1095
|
+
#
|
1096
|
+
#
|
1097
|
+
# Also aliased as: [enq](SizedQueue.downloaded.ruby_doc#method-i-enq),
|
1098
|
+
# [\<\<](SizedQueue.downloaded.ruby_doc#method-i-3C-3C)
|
1099
|
+
def push: (untyped obj, ?bool non_block) -> void
|
1100
|
+
end
|
1101
|
+
|
1102
|
+
ConditionVariable: singleton(Thread::ConditionVariable)
|
1103
|
+
|
1104
|
+
Mutex: singleton(Thread::Mutex)
|
1105
|
+
|
1106
|
+
Queue: singleton(Thread::Queue)
|
1107
|
+
|
1108
|
+
SizedQueue: singleton(Thread::SizedQueue)
|