steep 0.11.1 → 0.12.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 +27 -0
- data/.gitmodules +3 -0
- data/CHANGELOG.md +5 -0
- data/README.md +48 -90
- data/Rakefile +10 -6
- data/Steepfile +1 -0
- data/bin/setup +1 -0
- data/bin/smoke_runner.rb +9 -14
- data/exe/rbs +3 -0
- data/exe/ruby-signature +3 -0
- data/exe/steep +1 -0
- data/lib/steep.rb +32 -26
- data/lib/steep/annotation_parser.rb +167 -0
- data/lib/steep/ast/annotation/collection.rb +7 -7
- data/lib/steep/ast/types.rb +60 -0
- data/lib/steep/ast/types/any.rb +1 -1
- data/lib/steep/ast/types/factory.rb +535 -0
- data/lib/steep/ast/types/name.rb +3 -3
- data/lib/steep/ast/types/var.rb +1 -1
- data/lib/steep/cli.rb +56 -240
- data/lib/steep/drivers/annotations.rb +36 -19
- data/lib/steep/drivers/check.rb +55 -91
- data/lib/steep/drivers/init.rb +54 -0
- data/lib/steep/drivers/langserver.rb +241 -150
- data/lib/steep/drivers/print_project.rb +56 -0
- data/lib/steep/drivers/signature_error_printer.rb +25 -0
- data/lib/steep/drivers/trace_printer.rb +25 -0
- data/lib/steep/drivers/utils/driver_helper.rb +26 -0
- data/lib/steep/drivers/validate.rb +18 -38
- data/lib/steep/drivers/vendor.rb +46 -0
- data/lib/steep/drivers/watch.rb +78 -140
- data/lib/steep/errors.rb +22 -13
- data/lib/steep/interface/interface.rb +91 -0
- data/lib/steep/interface/method.rb +0 -4
- data/lib/steep/interface/method_type.rb +362 -2
- data/lib/steep/interface/substitution.rb +22 -0
- data/lib/steep/project.rb +25 -233
- data/lib/steep/project/dsl.rb +132 -0
- data/lib/steep/project/file.rb +93 -76
- data/lib/steep/project/file_loader.rb +63 -0
- data/lib/steep/project/options.rb +7 -0
- data/lib/steep/project/target.rb +190 -0
- data/lib/steep/signature/errors.rb +25 -77
- data/lib/steep/signature/validator.rb +122 -0
- data/lib/steep/source.rb +12 -7
- data/lib/steep/subtyping/check.rb +357 -633
- data/lib/steep/subtyping/constraints.rb +2 -2
- data/lib/steep/subtyping/trace.rb +23 -0
- data/lib/steep/type_construction.rb +509 -455
- data/lib/steep/type_inference/constant_env.rb +16 -24
- data/lib/steep/type_inference/type_env.rb +26 -18
- data/lib/steep/version.rb +1 -1
- data/sample/Steepfile +6 -0
- data/sample/lib/conference.rb +12 -0
- data/sample/sig/conference.rbs +6 -0
- data/smoke/alias/Steepfile +4 -0
- data/smoke/alias/a.rb +2 -2
- data/smoke/alias/{a.rbi → a.rbs} +1 -1
- data/smoke/and/Steepfile +4 -0
- data/smoke/array/Steepfile +4 -0
- data/smoke/array/a.rb +2 -2
- data/smoke/array/b.rb +4 -4
- data/smoke/array/c.rb +2 -2
- data/smoke/block/Steepfile +5 -0
- data/smoke/block/{a.rbi → a.rbs} +1 -1
- data/smoke/block/{c.rbi → c.rbs} +0 -0
- data/smoke/block/d.rb +6 -6
- data/smoke/case/Steepfile +4 -0
- data/smoke/case/a.rb +4 -3
- data/smoke/class/Steepfile +4 -0
- data/smoke/class/a.rb +1 -4
- data/smoke/class/a.rbs +24 -0
- data/smoke/class/h.rb +6 -2
- data/smoke/class/{h.rbi → h.rbs} +1 -2
- data/smoke/class/i.rb +1 -2
- data/smoke/class/i.rbs +9 -0
- data/smoke/const/Steepfile +4 -0
- data/smoke/dstr/Steepfile +4 -0
- data/smoke/ensure/Steepfile +4 -0
- data/smoke/ensure/a.rb +1 -1
- data/smoke/enumerator/Steepfile +4 -0
- data/smoke/enumerator/a.rb +7 -7
- data/smoke/enumerator/b.rb +6 -6
- data/smoke/extension/Steepfile +4 -0
- data/smoke/extension/{a.rbi → a.rbs} +2 -2
- data/smoke/extension/{e.rbi → e.rbs} +2 -2
- data/smoke/hash/Steepfile +4 -0
- data/smoke/hash/{a.rbi → a.rbs} +0 -0
- data/smoke/hash/b.rb +2 -2
- data/smoke/hash/c.rb +1 -1
- data/smoke/hash/e.rbs +3 -0
- data/smoke/hash/f.rb +1 -1
- data/smoke/hello/Steepfile +4 -0
- data/smoke/hello/hello.rbs +7 -0
- data/smoke/if/Steepfile +4 -0
- data/smoke/implements/Steepfile +4 -0
- data/smoke/implements/a.rbs +6 -0
- data/smoke/initialize/Steepfile +4 -0
- data/smoke/initialize/a.rbs +3 -0
- data/smoke/integer/Steepfile +4 -0
- data/smoke/integer/a.rb +5 -3
- data/smoke/interface/Steepfile +4 -0
- data/smoke/interface/{a.rbi → a.rbs} +0 -0
- data/smoke/kwbegin/Steepfile +4 -0
- data/smoke/lambda/Steepfile +4 -0
- data/smoke/lambda/a.rb +9 -2
- data/smoke/literal/Steepfile +4 -0
- data/smoke/literal/{literal_methods.rbi → literal_methods.rbs} +0 -0
- data/smoke/map/Steepfile +4 -0
- data/smoke/map/a.rb +1 -1
- data/smoke/method/Steepfile +4 -0
- data/smoke/method/{a.rbi → a.rbs} +0 -0
- data/smoke/method/b.rb +1 -4
- data/smoke/method/d.rb +1 -0
- data/smoke/method/d.rbs +3 -0
- data/smoke/module/Steepfile +4 -0
- data/smoke/module/a.rb +1 -1
- data/smoke/module/a.rbs +16 -0
- data/smoke/module/c.rb +1 -1
- data/smoke/regexp/Steepfile +4 -0
- data/smoke/regexp/a.rb +2 -2
- data/smoke/regexp/b.rb +16 -16
- data/smoke/regression/Steepfile +5 -0
- data/smoke/regression/array.rb +2 -2
- data/smoke/regression/hash.rb +2 -2
- data/smoke/regression/poly_new.rb +2 -0
- data/smoke/regression/poly_new.rbs +4 -0
- data/smoke/regression/set_divide.rb +2 -2
- data/smoke/rescue/Steepfile +4 -0
- data/smoke/rescue/a.rb +1 -1
- data/smoke/self/Steepfile +4 -0
- data/smoke/self/a.rbs +4 -0
- data/smoke/skip/Steepfile +4 -0
- data/smoke/stdout/Steepfile +4 -0
- data/smoke/stdout/{a.rbi → a.rbs} +1 -1
- data/smoke/super/Steepfile +4 -0
- data/smoke/super/a.rbs +10 -0
- data/smoke/type_case/Steepfile +4 -0
- data/smoke/type_case/a.rb +1 -1
- data/smoke/yield/Steepfile +4 -0
- data/smoke/yield/a.rb +2 -2
- data/steep.gemspec +14 -7
- data/vendor/ruby-signature/.github/workflows/ruby.yml +27 -0
- data/vendor/ruby-signature/.gitignore +12 -0
- data/vendor/ruby-signature/.rubocop.yml +15 -0
- data/vendor/ruby-signature/BSDL +22 -0
- data/vendor/ruby-signature/COPYING +56 -0
- data/vendor/ruby-signature/Gemfile +6 -0
- data/vendor/ruby-signature/README.md +93 -0
- data/vendor/ruby-signature/Rakefile +66 -0
- data/vendor/ruby-signature/bin/annotate-with-rdoc +156 -0
- data/vendor/ruby-signature/bin/console +14 -0
- data/vendor/ruby-signature/bin/query-rdoc +103 -0
- data/vendor/ruby-signature/bin/setup +10 -0
- data/vendor/ruby-signature/bin/sort +88 -0
- data/vendor/ruby-signature/bin/test_runner.rb +17 -0
- data/vendor/ruby-signature/docs/CONTRIBUTING.md +97 -0
- data/vendor/ruby-signature/docs/sigs.md +148 -0
- data/vendor/ruby-signature/docs/stdlib.md +152 -0
- data/vendor/ruby-signature/docs/syntax.md +528 -0
- data/vendor/ruby-signature/exe/rbs +3 -0
- data/vendor/ruby-signature/exe/ruby-signature +7 -0
- data/vendor/ruby-signature/lib/ruby/signature.rb +64 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +29 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +391 -0
- data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +364 -0
- data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +52 -0
- data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +54 -0
- data/vendor/ruby-signature/lib/ruby/signature/cli.rb +534 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +152 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition.rb +172 -0
- data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +921 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment.rb +283 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +138 -0
- data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +126 -0
- data/vendor/ruby-signature/lib/ruby/signature/errors.rb +189 -0
- data/vendor/ruby-signature/lib/ruby/signature/location.rb +104 -0
- data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +125 -0
- data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +93 -0
- data/vendor/ruby-signature/lib/ruby/signature/parser.y +1343 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +441 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +579 -0
- data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +383 -0
- data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +48 -0
- data/vendor/ruby-signature/lib/ruby/signature/test.rb +28 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +63 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +290 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +58 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +324 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +185 -0
- data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +256 -0
- data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +72 -0
- data/vendor/ruby-signature/lib/ruby/signature/types.rb +932 -0
- data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +140 -0
- data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +49 -0
- data/vendor/ruby-signature/lib/ruby/signature/version.rb +5 -0
- data/vendor/ruby-signature/lib/ruby/signature/writer.rb +271 -0
- data/vendor/ruby-signature/ruby-signature.gemspec +45 -0
- data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +3 -0
- data/vendor/ruby-signature/stdlib/base64/base64.rbs +15 -0
- data/vendor/ruby-signature/stdlib/builtin/array.rbs +1997 -0
- data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +280 -0
- data/vendor/ruby-signature/stdlib/builtin/binding.rbs +177 -0
- data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +35 -0
- data/vendor/ruby-signature/stdlib/builtin/class.rbs +145 -0
- data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +116 -0
- data/vendor/ruby-signature/stdlib/builtin/complex.rbs +400 -0
- data/vendor/ruby-signature/stdlib/builtin/constants.rbs +37 -0
- data/vendor/ruby-signature/stdlib/builtin/data.rbs +5 -0
- data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +2 -0
- data/vendor/ruby-signature/stdlib/builtin/dir.rbs +419 -0
- data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +606 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +404 -0
- data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +260 -0
- data/vendor/ruby-signature/stdlib/builtin/errno.rbs +781 -0
- data/vendor/ruby-signature/stdlib/builtin/errors.rbs +582 -0
- data/vendor/ruby-signature/stdlib/builtin/exception.rbs +193 -0
- data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +68 -0
- data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +12 -0
- data/vendor/ruby-signature/stdlib/builtin/file.rbs +476 -0
- data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +59 -0
- data/vendor/ruby-signature/stdlib/builtin/float.rbs +696 -0
- data/vendor/ruby-signature/stdlib/builtin/gc.rbs +121 -0
- data/vendor/ruby-signature/stdlib/builtin/hash.rbs +1029 -0
- data/vendor/ruby-signature/stdlib/builtin/integer.rbs +710 -0
- data/vendor/ruby-signature/stdlib/builtin/io.rbs +683 -0
- data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +574 -0
- data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +135 -0
- data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +141 -0
- data/vendor/ruby-signature/stdlib/builtin/math.rbs +66 -0
- data/vendor/ruby-signature/stdlib/builtin/method.rbs +182 -0
- data/vendor/ruby-signature/stdlib/builtin/module.rbs +248 -0
- data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +82 -0
- data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +409 -0
- data/vendor/ruby-signature/stdlib/builtin/object.rbs +824 -0
- data/vendor/ruby-signature/stdlib/builtin/proc.rbs +426 -0
- data/vendor/ruby-signature/stdlib/builtin/process.rbs +354 -0
- data/vendor/ruby-signature/stdlib/builtin/random.rbs +93 -0
- data/vendor/ruby-signature/stdlib/builtin/range.rbs +226 -0
- data/vendor/ruby-signature/stdlib/builtin/rational.rbs +424 -0
- data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +10 -0
- data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +131 -0
- data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +14 -0
- data/vendor/ruby-signature/stdlib/builtin/signal.rbs +55 -0
- data/vendor/ruby-signature/stdlib/builtin/string.rbs +770 -0
- data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +13 -0
- data/vendor/ruby-signature/stdlib/builtin/struct.rbs +40 -0
- data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +230 -0
- data/vendor/ruby-signature/stdlib/builtin/thread.rbs +1112 -0
- data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +23 -0
- data/vendor/ruby-signature/stdlib/builtin/time.rbs +739 -0
- data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +91 -0
- data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +46 -0
- data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +159 -0
- data/vendor/ruby-signature/stdlib/builtin/warning.rbs +17 -0
- data/vendor/ruby-signature/stdlib/erb/erb.rbs +18 -0
- data/vendor/ruby-signature/stdlib/find/find.rbs +44 -0
- data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +21 -0
- data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +23 -0
- data/vendor/ruby-signature/stdlib/prime/prime.rbs +188 -0
- data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +9 -0
- data/vendor/ruby-signature/stdlib/set/set.rbs +77 -0
- data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +53 -0
- metadata +244 -54
- data/.travis.yml +0 -7
- data/lib/steep/ast/signature/alias.rb +0 -19
- data/lib/steep/ast/signature/class.rb +0 -33
- data/lib/steep/ast/signature/const.rb +0 -17
- data/lib/steep/ast/signature/env.rb +0 -138
- data/lib/steep/ast/signature/extension.rb +0 -21
- data/lib/steep/ast/signature/gvar.rb +0 -17
- data/lib/steep/ast/signature/interface.rb +0 -31
- data/lib/steep/ast/signature/members.rb +0 -115
- data/lib/steep/ast/signature/module.rb +0 -21
- data/lib/steep/drivers/print_interface.rb +0 -94
- data/lib/steep/drivers/scaffold.rb +0 -321
- data/lib/steep/drivers/utils/each_signature.rb +0 -31
- data/lib/steep/interface/abstract.rb +0 -68
- data/lib/steep/interface/builder.rb +0 -637
- data/lib/steep/interface/instantiated.rb +0 -163
- data/lib/steep/interface/ivar_chain.rb +0 -26
- data/lib/steep/parser.y +0 -1278
- data/lib/steep/project/listener.rb +0 -53
- data/smoke/class/a.rbi +0 -24
- data/smoke/class/d.rb +0 -9
- data/smoke/class/e.rb +0 -12
- data/smoke/class/i.rbi +0 -9
- data/smoke/hash/e.rbi +0 -3
- data/smoke/hello/hello.rbi +0 -7
- data/smoke/implements/a.rbi +0 -6
- data/smoke/initialize/a.rbi +0 -3
- data/smoke/module/a.rbi +0 -16
- data/smoke/self/a.rbi +0 -4
- data/smoke/super/a.rbi +0 -10
- data/stdlib/builtin.rbi +0 -787
@@ -0,0 +1,13 @@
|
|
1
|
+
class StringIO
|
2
|
+
def initialize: (?String, ?String) -> untyped
|
3
|
+
def puts: (*untyped) -> void
|
4
|
+
def readline: () -> String
|
5
|
+
| (String) -> String
|
6
|
+
def write: (String) -> void
|
7
|
+
def flush: () -> void
|
8
|
+
def string: -> String
|
9
|
+
def close_read: -> nil
|
10
|
+
def closed_read?: -> bool
|
11
|
+
def close_write: -> nil
|
12
|
+
def closed_write?: -> bool
|
13
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# A [Struct](Struct) is a convenient way to bundle a
|
2
|
+
# number of attributes together, using accessor methods, without having to
|
3
|
+
# write an explicit class.
|
4
|
+
#
|
5
|
+
# The [Struct](Struct) class generates new subclasses
|
6
|
+
# that hold a set of members and their values. For each member a reader
|
7
|
+
# and writer method is created similar to
|
8
|
+
# [Module\#attr\_accessor](https://ruby-doc.org/core-2.6.3/Module.html#method-i-attr_accessor)
|
9
|
+
# .
|
10
|
+
#
|
11
|
+
# ```ruby
|
12
|
+
# Customer = Struct.new(:name, :address) do
|
13
|
+
# def greeting
|
14
|
+
# "Hello #{name}!"
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# dave = Customer.new("Dave", "123 Main")
|
19
|
+
# dave.name #=> "Dave"
|
20
|
+
# dave.greeting #=> "Hello Dave!"
|
21
|
+
# ```
|
22
|
+
#
|
23
|
+
# See [::new](Struct#method-c-new) for further
|
24
|
+
# examples of creating struct subclasses and instances.
|
25
|
+
#
|
26
|
+
# In the method descriptions that follow, a "member" parameter refers to a
|
27
|
+
# struct member which is either a quoted string ( `"name"` ) or a
|
28
|
+
# [Symbol](https://ruby-doc.org/core-2.6.3/Symbol.html) ( `:name` ).
|
29
|
+
class Struct[Elem] < Object
|
30
|
+
include Enumerable[Elem, Struct[Elem]]
|
31
|
+
|
32
|
+
def initialize: (Symbol | String arg0, *Symbol | String arg1, ?keyword_init: bool keyword_init) -> void
|
33
|
+
|
34
|
+
def each: () { (Elem arg0) -> untyped } -> untyped
|
35
|
+
| () -> self
|
36
|
+
|
37
|
+
def self.members: () -> ::Array[Symbol]
|
38
|
+
|
39
|
+
def new: (*untyped args) -> Struct[untyped]
|
40
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
# Symbol objects represent names inside the Ruby interpreter. They are generated
|
2
|
+
# using the `:name` and `:"string"` literals syntax, and by the various `to_sym`
|
3
|
+
# methods. The same Symbol object will be created for a given name or string for
|
4
|
+
# the duration of a program's execution, regardless of the context or meaning of
|
5
|
+
# that name. Thus if `Fred` is a constant in one context, a method in another,
|
6
|
+
# and a class in a third, the Symbol `:Fred` will be the same object in all
|
7
|
+
# three contexts.
|
8
|
+
#
|
9
|
+
# module One
|
10
|
+
# class Fred
|
11
|
+
# end
|
12
|
+
# $f1 = :Fred
|
13
|
+
# end
|
14
|
+
# module Two
|
15
|
+
# Fred = 1
|
16
|
+
# $f2 = :Fred
|
17
|
+
# end
|
18
|
+
# def Fred()
|
19
|
+
# end
|
20
|
+
# $f3 = :Fred
|
21
|
+
# $f1.object_id #=> 2514190
|
22
|
+
# $f2.object_id #=> 2514190
|
23
|
+
# $f3.object_id #=> 2514190
|
24
|
+
#
|
25
|
+
class Symbol
|
26
|
+
include Comparable
|
27
|
+
|
28
|
+
# Returns an array of all the symbols currently in Ruby's symbol table.
|
29
|
+
#
|
30
|
+
# Symbol.all_symbols.size #=> 903
|
31
|
+
# Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink,
|
32
|
+
# :chown, :EOFError, :$;, :String,
|
33
|
+
# :LOCK_SH, :"setuid?", :$<,
|
34
|
+
# :default_proc, :compact, :extend,
|
35
|
+
# :Tms, :getwd, :$=, :ThreadGroup,
|
36
|
+
# :wait2, :$>]
|
37
|
+
#
|
38
|
+
def self.all_symbols: () -> ::Array[Symbol]
|
39
|
+
|
40
|
+
public
|
41
|
+
|
42
|
+
# Compares `symbol` with `other_symbol` after calling #to_s on each of the
|
43
|
+
# symbols. Returns -1, 0, +1, or `nil` depending on whether `symbol` is less
|
44
|
+
# than, equal to, or greater than `other_symbol`.
|
45
|
+
#
|
46
|
+
# `nil` is returned if the two values are incomparable.
|
47
|
+
#
|
48
|
+
# See String#<=> for more information.
|
49
|
+
#
|
50
|
+
def <=>: (Symbol other) -> Integer
|
51
|
+
| (untyped other) -> Integer?
|
52
|
+
|
53
|
+
# Equality---If *sym* and *obj* are exactly the same symbol, returns `true`.
|
54
|
+
#
|
55
|
+
def ==: (untyped obj) -> bool
|
56
|
+
|
57
|
+
# Equality---If *sym* and *obj* are exactly the same symbol, returns `true`.
|
58
|
+
#
|
59
|
+
def ===: (untyped obj) -> bool
|
60
|
+
|
61
|
+
# Returns `sym.to_s =~ obj`.
|
62
|
+
#
|
63
|
+
def =~: (untyped obj) -> Integer?
|
64
|
+
|
65
|
+
# Returns `sym.to_s[]`.
|
66
|
+
#
|
67
|
+
def []: (Integer index) -> String?
|
68
|
+
| (Integer start, Integer length) -> String?
|
69
|
+
| (Range[Integer?] range) -> String?
|
70
|
+
| (Regexp regexp) -> String?
|
71
|
+
| (Regexp regexp, Integer | String capture) -> String?
|
72
|
+
| (String match_str) -> String?
|
73
|
+
|
74
|
+
# Same as `sym.to_s.capitalize.intern`.
|
75
|
+
#
|
76
|
+
def capitalize: () -> Symbol
|
77
|
+
| (:ascii | :lithuanian | :turkic) -> Symbol
|
78
|
+
| (:lithuanian, :turkic) -> Symbol
|
79
|
+
| (:turkic, :lithuanian) -> Symbol
|
80
|
+
|
81
|
+
# Case-insensitive version of Symbol#<=>. Currently, case-insensitivity only
|
82
|
+
# works on characters A-Z/a-z, not all of Unicode. This is different from
|
83
|
+
# Symbol#casecmp?.
|
84
|
+
#
|
85
|
+
# :aBcDeF.casecmp(:abcde) #=> 1
|
86
|
+
# :aBcDeF.casecmp(:abcdef) #=> 0
|
87
|
+
# :aBcDeF.casecmp(:abcdefg) #=> -1
|
88
|
+
# :abcdef.casecmp(:ABCDEF) #=> 0
|
89
|
+
#
|
90
|
+
# `nil` is returned if the two symbols have incompatible encodings, or if
|
91
|
+
# `other_symbol` is not a symbol.
|
92
|
+
#
|
93
|
+
# :foo.casecmp(2) #=> nil
|
94
|
+
# "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp(:"\u{c4 d6 dc}") #=> nil
|
95
|
+
#
|
96
|
+
def casecmp: (Symbol other) -> Integer?
|
97
|
+
| (untyped other) -> Integer?
|
98
|
+
|
99
|
+
# Returns `true` if `sym` and `other_symbol` are equal after Unicode case
|
100
|
+
# folding, `false` if they are not equal.
|
101
|
+
#
|
102
|
+
# :aBcDeF.casecmp?(:abcde) #=> false
|
103
|
+
# :aBcDeF.casecmp?(:abcdef) #=> true
|
104
|
+
# :aBcDeF.casecmp?(:abcdefg) #=> false
|
105
|
+
# :abcdef.casecmp?(:ABCDEF) #=> true
|
106
|
+
# :"\u{e4 f6 fc}".casecmp?(:"\u{c4 d6 dc}") #=> true
|
107
|
+
#
|
108
|
+
# `nil` is returned if the two symbols have incompatible encodings, or if
|
109
|
+
# `other_symbol` is not a symbol.
|
110
|
+
#
|
111
|
+
# :foo.casecmp?(2) #=> nil
|
112
|
+
# "\u{e4 f6 fc}".encode("ISO-8859-1").to_sym.casecmp?(:"\u{c4 d6 dc}") #=> nil
|
113
|
+
#
|
114
|
+
def casecmp?: (Symbol other) -> bool
|
115
|
+
| (untyped other) -> bool
|
116
|
+
|
117
|
+
# Same as `sym.to_s.downcase.intern`.
|
118
|
+
#
|
119
|
+
def downcase: () -> Symbol
|
120
|
+
| (:ascii | :fold | :lithuanian | :turkic) -> Symbol
|
121
|
+
| (:lithuanian, :turkic) -> Symbol
|
122
|
+
| (:turkic, :lithuanian) -> Symbol
|
123
|
+
|
124
|
+
# Returns whether *sym* is :"" or not.
|
125
|
+
#
|
126
|
+
def empty?: () -> bool
|
127
|
+
|
128
|
+
# Returns the Encoding object that represents the encoding of *sym*.
|
129
|
+
#
|
130
|
+
def encoding: () -> Encoding
|
131
|
+
|
132
|
+
# Returns true if `sym` ends with one of the `suffixes` given.
|
133
|
+
#
|
134
|
+
# :hello.end_with?("ello") #=> true
|
135
|
+
#
|
136
|
+
# # returns true if one of the +suffixes+ matches.
|
137
|
+
# :hello.end_with?("heaven", "ello") #=> true
|
138
|
+
# :hello.end_with?("heaven", "paradise") #=> false
|
139
|
+
#
|
140
|
+
def end_with?: (String str, *String str) -> bool
|
141
|
+
|
142
|
+
# Returns the name or string corresponding to *sym*.
|
143
|
+
#
|
144
|
+
# :fred.id2name #=> "fred"
|
145
|
+
# :ginger.to_s #=> "ginger"
|
146
|
+
#
|
147
|
+
def id2name: () -> String
|
148
|
+
|
149
|
+
# Returns the representation of *sym* as a symbol literal.
|
150
|
+
#
|
151
|
+
# :fred.inspect #=> ":fred"
|
152
|
+
#
|
153
|
+
def inspect: () -> String
|
154
|
+
|
155
|
+
# In general, `to_sym` returns the Symbol corresponding to an object. As *sym*
|
156
|
+
# is already a symbol, `self` is returned in this case.
|
157
|
+
#
|
158
|
+
def intern: () -> self
|
159
|
+
|
160
|
+
# Same as `sym.to_s.length`.
|
161
|
+
#
|
162
|
+
def length: () -> Integer
|
163
|
+
|
164
|
+
# Returns `sym.to_s.match`.
|
165
|
+
#
|
166
|
+
def match: (Regexp | String pattern, ?Integer pos) ?{ (MatchData) -> void } -> MatchData?
|
167
|
+
|
168
|
+
# Returns `sym.to_s.match?`.
|
169
|
+
#
|
170
|
+
def match?: (Regexp | String pattern, ?Integer pos) -> bool
|
171
|
+
|
172
|
+
# Same as `sym.to_s.succ.intern`.
|
173
|
+
#
|
174
|
+
def next: () -> Symbol
|
175
|
+
|
176
|
+
# Same as `sym.to_s.length`.
|
177
|
+
#
|
178
|
+
alias size length
|
179
|
+
|
180
|
+
# Returns `sym.to_s[]`.
|
181
|
+
#
|
182
|
+
alias slice `[]`
|
183
|
+
|
184
|
+
# Returns true if `sym` starts with one of the `prefixes` given. Each of the
|
185
|
+
# `prefixes` should be a String or a Regexp.
|
186
|
+
#
|
187
|
+
# :hello.start_with?("hell") #=> true
|
188
|
+
# :hello.start_with?(/H/i) #=> true
|
189
|
+
#
|
190
|
+
# # returns true if one of the prefixes matches.
|
191
|
+
# :hello.start_with?("heaven", "hell") #=> true
|
192
|
+
# :hello.start_with?("heaven", "paradise") #=> false
|
193
|
+
def start_with?: (String str, *String str) -> bool
|
194
|
+
|
195
|
+
# Same as `sym.to_s.succ.intern`.
|
196
|
+
#
|
197
|
+
alias succ next
|
198
|
+
|
199
|
+
# Same as `sym.to_s.swapcase.intern`.
|
200
|
+
#
|
201
|
+
def swapcase: () -> Symbol
|
202
|
+
| (:ascii | :lithuanian | :turkic) -> Symbol
|
203
|
+
| (:lithuanian, :turkic) -> Symbol
|
204
|
+
| (:turkic, :lithuanian) -> Symbol
|
205
|
+
|
206
|
+
# Returns a *Proc* object which responds to the given method by *sym*.
|
207
|
+
#
|
208
|
+
# (1..3).collect(&:to_s) #=> ["1", "2", "3"]
|
209
|
+
#
|
210
|
+
def to_proc: () -> Proc
|
211
|
+
|
212
|
+
# Returns the name or string corresponding to *sym*.
|
213
|
+
#
|
214
|
+
# :fred.id2name #=> "fred"
|
215
|
+
# :ginger.to_s #=> "ginger"
|
216
|
+
#
|
217
|
+
alias to_s id2name
|
218
|
+
|
219
|
+
# In general, `to_sym` returns the Symbol corresponding to an object. As *sym*
|
220
|
+
# is already a symbol, `self` is returned in this case.
|
221
|
+
#
|
222
|
+
alias to_sym intern
|
223
|
+
|
224
|
+
# Same as `sym.to_s.upcase.intern`.
|
225
|
+
#
|
226
|
+
def upcase: () -> Symbol
|
227
|
+
| (:ascii | :lithuanian | :turkic) -> Symbol
|
228
|
+
| (:lithuanian, :turkic) -> Symbol
|
229
|
+
| (:turkic, :lithuanian) -> Symbol
|
230
|
+
end
|
@@ -0,0 +1,1112 @@
|
|
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: () -> untyped
|
892
|
+
|
893
|
+
def marshal_dump: () -> untyped
|
894
|
+
|
895
|
+
# Wakes up the first thread in line waiting for this lock.
|
896
|
+
def signal: () -> untyped
|
897
|
+
|
898
|
+
# Releases the lock held in `mutex` and waits; reacquires the lock on
|
899
|
+
# wakeup.
|
900
|
+
#
|
901
|
+
# If `timeout` is given, this method returns after `timeout` seconds
|
902
|
+
# passed, even if no other thread doesn't signal.
|
903
|
+
def wait: (*untyped _) -> untyped
|
904
|
+
end
|
905
|
+
|
906
|
+
# [Mutex](Mutex) implements a simple semaphore that
|
907
|
+
# can be used to coordinate access to shared data from multiple concurrent
|
908
|
+
# threads.
|
909
|
+
#
|
910
|
+
# Example:
|
911
|
+
#
|
912
|
+
# ```ruby
|
913
|
+
# semaphore = Mutex.new
|
914
|
+
#
|
915
|
+
# a = Thread.new {
|
916
|
+
# semaphore.synchronize {
|
917
|
+
# # access shared resource
|
918
|
+
# }
|
919
|
+
# }
|
920
|
+
#
|
921
|
+
# b = Thread.new {
|
922
|
+
# semaphore.synchronize {
|
923
|
+
# # access shared resource
|
924
|
+
# }
|
925
|
+
# }
|
926
|
+
# ```
|
927
|
+
class Thread::Mutex < Object
|
928
|
+
# Attempts to grab the lock and waits if it isn’t available. Raises
|
929
|
+
# `ThreadError` if `mutex` was locked by the current thread.
|
930
|
+
def lock: () -> untyped
|
931
|
+
|
932
|
+
# Returns `true` if this lock is currently held by some thread.
|
933
|
+
def locked?: () -> bool
|
934
|
+
|
935
|
+
# Returns `true` if this lock is currently held by current thread.
|
936
|
+
def owned?: () -> bool
|
937
|
+
|
938
|
+
# Obtains a lock, runs the block, and releases the lock when the block
|
939
|
+
# completes. See the example under `Mutex` .
|
940
|
+
def synchronize: () -> untyped
|
941
|
+
|
942
|
+
# Attempts to obtain the lock and returns immediately. Returns `true` if
|
943
|
+
# the lock was granted.
|
944
|
+
def try_lock: () -> bool
|
945
|
+
|
946
|
+
# Releases the lock. Raises `ThreadError` if `mutex` wasn’t locked by the
|
947
|
+
# current thread.
|
948
|
+
def unlock: () -> untyped
|
949
|
+
end
|
950
|
+
|
951
|
+
# The [Queue](Queue) class implements multi-producer,
|
952
|
+
# multi-consumer queues. It is especially useful in threaded programming
|
953
|
+
# when information must be exchanged safely between multiple threads. The
|
954
|
+
# [Queue](Queue) class implements all the required
|
955
|
+
# locking semantics.
|
956
|
+
#
|
957
|
+
# The class implements FIFO type of queue. In a FIFO queue, the first
|
958
|
+
# tasks added are the first retrieved.
|
959
|
+
#
|
960
|
+
# Example:
|
961
|
+
#
|
962
|
+
# ```ruby
|
963
|
+
# queue = Queue.new
|
964
|
+
#
|
965
|
+
# producer = Thread.new do
|
966
|
+
# 5.times do |i|
|
967
|
+
# sleep rand(i) # simulate expense
|
968
|
+
# queue << i
|
969
|
+
# puts "#{i} produced"
|
970
|
+
# end
|
971
|
+
# end
|
972
|
+
#
|
973
|
+
# consumer = Thread.new do
|
974
|
+
# 5.times do |i|
|
975
|
+
# value = queue.pop
|
976
|
+
# sleep rand(i/2) # simulate expense
|
977
|
+
# puts "consumed #{value}"
|
978
|
+
# end
|
979
|
+
# end
|
980
|
+
#
|
981
|
+
# consumer.join
|
982
|
+
# ```
|
983
|
+
class Thread::Queue < Object
|
984
|
+
# Alias for: [push](Queue.downloaded.ruby_doc#method-i-push)
|
985
|
+
def <<: (untyped obj) -> untyped
|
986
|
+
|
987
|
+
# Removes all objects from the queue.
|
988
|
+
def clear: () -> untyped
|
989
|
+
|
990
|
+
# Closes the queue. A closed queue cannot be re-opened.
|
991
|
+
#
|
992
|
+
# After the call to close completes, the following are true:
|
993
|
+
#
|
994
|
+
# - `closed?` will return true
|
995
|
+
#
|
996
|
+
# - `close` will be ignored.
|
997
|
+
#
|
998
|
+
# - calling enq/push/\<\< will raise a `ClosedQueueError` .
|
999
|
+
#
|
1000
|
+
# - when `empty?` is false, calling deq/pop/shift will return an object
|
1001
|
+
# from the queue as usual.
|
1002
|
+
#
|
1003
|
+
# - when `empty?` is true, deq(false) will not suspend the thread and
|
1004
|
+
# will return nil. deq(true) will raise a `ThreadError` .
|
1005
|
+
#
|
1006
|
+
# [ClosedQueueError](https://ruby-doc.org/core-2.6.3/ClosedQueueError.html)
|
1007
|
+
# is inherited from
|
1008
|
+
# [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html), so
|
1009
|
+
# that you can break loop block.
|
1010
|
+
#
|
1011
|
+
# Example:
|
1012
|
+
#
|
1013
|
+
# q = Queue.new
|
1014
|
+
# Thread.new{
|
1015
|
+
# while e = q.deq # wait for nil to break loop
|
1016
|
+
# # ...
|
1017
|
+
# end
|
1018
|
+
# }
|
1019
|
+
# q.close
|
1020
|
+
def close: () -> untyped
|
1021
|
+
|
1022
|
+
# Returns `true` if the queue is closed.
|
1023
|
+
def closed?: () -> bool
|
1024
|
+
|
1025
|
+
# Alias for: [pop](Queue.downloaded.ruby_doc#method-i-pop)
|
1026
|
+
def deq: (*untyped args) -> untyped
|
1027
|
+
|
1028
|
+
# Returns `true` if the queue is empty.
|
1029
|
+
def empty?: () -> bool
|
1030
|
+
|
1031
|
+
# Alias for: [push](Queue.downloaded.ruby_doc#method-i-push)
|
1032
|
+
def enq: (untyped obj) -> untyped
|
1033
|
+
|
1034
|
+
# Returns the length of the queue.
|
1035
|
+
#
|
1036
|
+
#
|
1037
|
+
#
|
1038
|
+
# Also aliased as: [size](Queue.downloaded.ruby_doc#method-i-size)
|
1039
|
+
def length: () -> Integer
|
1040
|
+
|
1041
|
+
def marshal_dump: () -> untyped
|
1042
|
+
|
1043
|
+
# Returns the number of threads waiting on the queue.
|
1044
|
+
def num_waiting: () -> untyped
|
1045
|
+
|
1046
|
+
# Retrieves data from the queue.
|
1047
|
+
#
|
1048
|
+
# If the queue is empty, the calling thread is suspended until data is
|
1049
|
+
# pushed onto the queue. If `non_block` is true, the thread isn't
|
1050
|
+
# suspended, and `ThreadError` is raised.
|
1051
|
+
#
|
1052
|
+
#
|
1053
|
+
#
|
1054
|
+
# Also aliased as: [deq](Queue.downloaded.ruby_doc#method-i-deq),
|
1055
|
+
# [shift](Queue.downloaded.ruby_doc#method-i-shift)
|
1056
|
+
def pop: (*untyped args) -> untyped
|
1057
|
+
|
1058
|
+
# Pushes the given `object` to the queue.
|
1059
|
+
#
|
1060
|
+
#
|
1061
|
+
#
|
1062
|
+
# Also aliased as: [enq](Queue.downloaded.ruby_doc#method-i-enq),
|
1063
|
+
# [\<\<](Queue.downloaded.ruby_doc#method-i-3C-3C)
|
1064
|
+
def push: (untyped obj) -> untyped
|
1065
|
+
|
1066
|
+
# Alias for: [pop](Queue.downloaded.ruby_doc#method-i-pop)
|
1067
|
+
def shift: (*untyped args) -> untyped
|
1068
|
+
|
1069
|
+
# Alias for: [length](Queue.downloaded.ruby_doc#method-i-length)
|
1070
|
+
def size: () -> Integer
|
1071
|
+
end
|
1072
|
+
|
1073
|
+
# This class represents queues of specified size capacity. The push
|
1074
|
+
# operation may be blocked if the capacity is full.
|
1075
|
+
#
|
1076
|
+
# See [Queue](https://ruby-doc.org/core-2.6.3/Queue.html) for an example
|
1077
|
+
# of how a [SizedQueue](SizedQueue) works.
|
1078
|
+
class Thread::SizedQueue < Thread::Queue
|
1079
|
+
# Alias for: [push](SizedQueue.downloaded.ruby_doc#method-i-push)
|
1080
|
+
def <<: (*untyped args) -> untyped
|
1081
|
+
|
1082
|
+
# Alias for: [push](SizedQueue.downloaded.ruby_doc#method-i-push)
|
1083
|
+
def enq: (*untyped args) -> untyped
|
1084
|
+
|
1085
|
+
def initialize: (untyped max) -> SizedQueue
|
1086
|
+
|
1087
|
+
# Returns the maximum size of the queue.
|
1088
|
+
def max: () -> Integer
|
1089
|
+
|
1090
|
+
# Sets the maximum size of the queue to the given `number` .
|
1091
|
+
def max=: (Integer max) -> untyped
|
1092
|
+
|
1093
|
+
# Pushes `object` to the queue.
|
1094
|
+
#
|
1095
|
+
# If there is no space left in the queue, waits until space becomes
|
1096
|
+
# available, unless `non_block` is true. If `non_block` is true, the
|
1097
|
+
# thread isn't suspended, and `ThreadError` is raised.
|
1098
|
+
#
|
1099
|
+
#
|
1100
|
+
#
|
1101
|
+
# Also aliased as: [enq](SizedQueue.downloaded.ruby_doc#method-i-enq),
|
1102
|
+
# [\<\<](SizedQueue.downloaded.ruby_doc#method-i-3C-3C)
|
1103
|
+
def push: (*untyped args) -> untyped
|
1104
|
+
end
|
1105
|
+
|
1106
|
+
ConditionVariable: untyped
|
1107
|
+
|
1108
|
+
Mutex: untyped
|
1109
|
+
|
1110
|
+
Queue: untyped
|
1111
|
+
|
1112
|
+
SizedQueue: untyped
|