-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.rbs
More file actions
1754 lines (1663 loc) · 51.1 KB
/
thread.rbs
File metadata and controls
1754 lines (1663 loc) · 51.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# <!-- rdoc-file=vm.c -->
# Threads are the Ruby implementation for a concurrent programming model.
#
# Programs that require multiple threads of execution are a perfect candidate
# for Ruby's Thread class.
#
# For example, we can create a new thread separate from the main thread's
# execution using ::new.
#
# thr = Thread.new { puts "What's the big deal" }
#
# Then we are able to pause the execution of the main thread and allow our new
# thread to finish, using #join:
#
# thr.join #=> "What's the big deal"
#
# If we don't call `thr.join` before the main thread terminates, then all other
# threads including `thr` will be killed.
#
# Alternatively, you can use an array for handling multiple threads at once,
# like in the following example:
#
# threads = []
# threads << Thread.new { puts "What's the big deal" }
# threads << Thread.new { 3.times { puts "Threads are fun!" } }
#
# After creating a few threads we wait for them all to finish consecutively.
#
# threads.each { |thr| thr.join }
#
# To retrieve the last value of a thread, use #value
#
# thr = Thread.new { sleep 1; "Useful value" }
# thr.value #=> "Useful value"
#
# ### Thread initialization
#
# In order to create new threads, Ruby provides ::new, ::start, and ::fork. A
# block must be provided with each of these methods, otherwise a ThreadError
# will be raised.
#
# When subclassing the Thread class, the `initialize` method of your subclass
# will be ignored by ::start and ::fork. Otherwise, be sure to call super in
# your `initialize` method.
#
# ### Thread termination
#
# For terminating threads, Ruby provides a variety of ways to do this.
#
# The class method ::kill, is meant to exit a given thread:
#
# thr = Thread.new { sleep }
# Thread.kill(thr) # sends exit() to thr
#
# Alternatively, you can use the instance method #exit, or any of its aliases
# #kill or #terminate.
#
# thr.exit
#
# ### Thread status
#
# Ruby provides a few instance methods for querying the state of a given thread.
# To get a string with the current thread's state use #status
#
# thr = Thread.new { sleep }
# thr.status # => "sleep"
# thr.exit
# thr.status # => false
#
# You can also use #alive? to tell if the thread is running or sleeping, and
# #stop? if the thread is dead or sleeping.
#
# ### Thread variables and scope
#
# Since threads are created with blocks, the same rules apply to other Ruby
# blocks for variable scope. Any local variables created within this block are
# accessible to only this thread.
#
# #### Fiber-local vs. Thread-local
#
# Each fiber has its own bucket for Thread#[] storage. When you set a new
# fiber-local it is only accessible within this Fiber. To illustrate:
#
# Thread.new {
# Thread.current[:foo] = "bar"
# Fiber.new {
# p Thread.current[:foo] # => nil
# }.resume
# }.join
#
# This example uses #[] for getting and #[]= for setting fiber-locals, you can
# also use #keys to list the fiber-locals for a given thread and #key? to check
# if a fiber-local exists.
#
# When it comes to thread-locals, they are accessible within the entire scope of
# the thread. Given the following example:
#
# Thread.new{
# Thread.current.thread_variable_set(:foo, 1)
# p Thread.current.thread_variable_get(:foo) # => 1
# Fiber.new{
# Thread.current.thread_variable_set(:foo, 2)
# p Thread.current.thread_variable_get(:foo) # => 2
# }.resume
# p Thread.current.thread_variable_get(:foo) # => 2
# }.join
#
# You can see that the thread-local `:foo` carried over into the fiber and was
# changed to `2` by the end of the thread.
#
# This example makes use of #thread_variable_set to create new thread-locals,
# and #thread_variable_get to reference them.
#
# There is also #thread_variables to list all thread-locals, and
# #thread_variable? to check if a given thread-local exists.
#
# ### Exception handling
#
# When an unhandled exception is raised inside a thread, it will terminate. By
# default, this exception will not propagate to other threads. The exception is
# stored and when another thread calls #value or #join, the exception will be
# re-raised in that thread.
#
# t = Thread.new{ raise 'something went wrong' }
# t.value #=> RuntimeError: something went wrong
#
# An exception can be raised from outside the thread using the Thread#raise
# instance method, which takes the same parameters as Kernel#raise.
#
# Setting Thread.abort_on_exception = true, Thread#abort_on_exception = true, or
# $DEBUG = true will cause a subsequent unhandled exception raised in a thread
# to be automatically re-raised in the main thread.
#
# With the addition of the class method ::handle_interrupt, you can now handle
# exceptions asynchronously with threads.
#
# ### Scheduling
#
# Ruby provides a few ways to support scheduling threads in your program.
#
# The first way is by using the class method ::stop, to put the current running
# thread to sleep and schedule the execution of another thread.
#
# Once a thread is asleep, you can use the instance method #wakeup to mark your
# thread as eligible for scheduling.
#
# You can also try ::pass, which attempts to pass execution to another thread
# but is dependent on the OS whether a running thread will switch or not. The
# same goes for #priority, which lets you hint to the thread scheduler which
# threads you want to take precedence when passing execution. This method is
# also dependent on the OS and may be ignored on some platforms.
#
class Thread < Object
# <!--
# rdoc-file=thread.c
# - Thread.current -> thread
# -->
# Returns the currently executing thread.
#
# Thread.current #=> #<Thread:0x401bdf4c run>
#
def self.current: () -> Thread
# <!--
# rdoc-file=thread.c
# - Thread.main -> thread
# -->
# Returns the main thread.
#
def self.main: () -> Thread
# <!--
# rdoc-file=thread.c
# - thr[sym] -> obj or nil
# -->
# Attribute Reference---Returns the value of a fiber-local variable (current
# thread's root fiber if not explicitly inside a Fiber), using either a symbol
# or a string name. If the specified variable does not exist, returns `nil`.
#
# [
# Thread.new { Thread.current["name"] = "A" },
# Thread.new { Thread.current[:name] = "B" },
# Thread.new { Thread.current["name"] = "C" }
# ].each do |th|
# th.join
# puts "#{th.inspect}: #{th[:name]}"
# end
#
# This will produce:
#
# #<Thread:0x00000002a54220 dead>: A
# #<Thread:0x00000002a541a8 dead>: B
# #<Thread:0x00000002a54130 dead>: C
#
# Thread#[] and Thread#[]= are not thread-local but fiber-local. This confusion
# did not exist in Ruby 1.8 because fibers are only available since Ruby 1.9.
# Ruby 1.9 chooses that the methods behaves fiber-local to save following idiom
# for dynamic scope.
#
# def meth(newvalue)
# begin
# oldvalue = Thread.current[:name]
# Thread.current[:name] = newvalue
# yield
# ensure
# Thread.current[:name] = oldvalue
# end
# end
#
# The idiom may not work as dynamic scope if the methods are thread-local and a
# given block switches fiber.
#
# f = Fiber.new {
# meth(1) {
# Fiber.yield
# }
# }
# meth(2) {
# f.resume
# }
# f.resume
# p Thread.current[:name]
# #=> nil if fiber-local
# #=> 2 if thread-local (The value 2 is leaked to outside of meth method.)
#
# For thread-local variables, please see #thread_variable_get and
# #thread_variable_set.
#
def []: (interned key) -> untyped
# <!--
# rdoc-file=thread.c
# - thr[sym] = obj -> obj
# -->
# Attribute Assignment---Sets or creates the value of a fiber-local variable,
# using either a symbol or a string.
#
# See also Thread#[].
#
# For thread-local variables, please see #thread_variable_set and
# #thread_variable_get.
#
def []=: (interned key, untyped value) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.alive? -> true or false
# -->
# Returns `true` if `thr` is running or sleeping.
#
# thr = Thread.new { }
# thr.join #=> #<Thread:0x401b3fb0 dead>
# Thread.current.alive? #=> true
# thr.alive? #=> false
#
# See also #stop? and #status.
#
def alive?: () -> bool
# <!--
# rdoc-file=thread.c
# - thr.exit -> thr
# - thr.kill -> thr
# - thr.terminate -> thr
# -->
# Terminates `thr` and schedules another thread to be run, returning the
# terminated Thread. If this is the main thread, or the last thread, exits the
# process.
#
def kill: () -> Thread?
# <!--
# rdoc-file=thread.c
# - thr.abort_on_exception -> true or false
# -->
# Returns the status of the thread-local ``abort on exception'' condition for
# this `thr`.
#
# The default is `false`.
#
# See also #abort_on_exception=.
#
# There is also a class level method to set this for all threads, see
# ::abort_on_exception.
#
def abort_on_exception: () -> bool
# <!--
# rdoc-file=thread.c
# - thr.abort_on_exception= boolean -> true or false
# -->
# When set to `true`, if this `thr` is aborted by an exception, the raised
# exception will be re-raised in the main thread.
#
# See also #abort_on_exception.
#
# There is also a class level method to set this for all threads, see
# ::abort_on_exception=.
#
def abort_on_exception=: (boolish abort_on_exception) -> untyped
# <!--
# rdoc-file=vm_trace.c
# - thr.add_trace_func(proc) -> proc
# -->
# Adds *proc* as a handler for tracing.
#
# See Thread#set_trace_func and Kernel#set_trace_func.
#
def add_trace_func: (untyped proc) -> untyped
# <!--
# rdoc-file=thread.c
# - thread.backtrace -> array or nil
# -->
# Returns the current backtrace of the target thread.
#
def backtrace: (*untyped args) -> ::Array[untyped]
# <!--
# rdoc-file=thread.c
# - thread.backtrace_locations(*args) -> array or nil
# -->
# Returns the execution stack for the target thread---an array containing
# backtrace location objects.
#
# See Thread::Backtrace::Location for more information.
#
# This method behaves similarly to Kernel#caller_locations except it applies to
# a specific thread.
#
def backtrace_locations: (*untyped args) -> ::Array[untyped]?
# <!-- rdoc-file=thread.c -->
# Terminates `thr` and schedules another thread to be run, returning the
# terminated Thread. If this is the main thread, or the last thread, exits the
# process.
#
def exit: () -> Thread?
# <!--
# rdoc-file=thread.c
# - thr.fetch(sym) -> obj
# - thr.fetch(sym) { } -> obj
# - thr.fetch(sym, default) -> obj
# -->
# Returns a fiber-local for the given key. If the key can't be found, there are
# several options: With no other arguments, it will raise a KeyError exception;
# if *default* is given, then that will be returned; if the optional code block
# is specified, then that will be run and its result returned. See Thread#[]
# and Hash#fetch.
#
def fetch: (*untyped sym) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.group -> thgrp or nil
# -->
# Returns the ThreadGroup which contains the given thread.
#
# Thread.main.group #=> #<ThreadGroup:0x4029d914>
#
def group: () -> ThreadGroup?
# <!--
# rdoc-file=thread.c
# - Thread.new { ... } -> thread
# - Thread.new(*args, &proc) -> thread
# - Thread.new(*args) { |args| ... } -> thread
# - Creates a new thread executing the given block.
# - Any +args+ given to ::new will be passed to the block:
# - arr = []
# - a, b, c = 1, 2, 3
# - Thread.new(a,b,c) { |d,e,f| arr << d << e << f }.join
# - arr #=> [1, 2, 3]
# - A ThreadError exception is raised if ::new is called without a block.
# - If you're going to subclass Thread, be sure to call super in your
# - +initialize+ method, otherwise a ThreadError will be raised.
# -->
#
def initialize: (*untyped) { (?) -> void } -> void
# <!--
# rdoc-file=thread.c
# - thr.join -> thr
# - thr.join(limit) -> thr
# -->
# The calling thread will suspend execution and run this `thr`.
#
# Does not return until `thr` exits or until the given `limit` seconds have
# passed.
#
# If the time limit expires, `nil` will be returned, otherwise `thr` is
# returned.
#
# Any threads not joined will be killed when the main program exits.
#
# If `thr` had previously raised an exception and the ::abort_on_exception or
# $DEBUG flags are not set, (so the exception has not yet been processed), it
# will be processed at this time.
#
# a = Thread.new { print "a"; sleep(10); print "b"; print "c" }
# x = Thread.new { print "x"; Thread.pass; print "y"; print "z" }
# x.join # Let thread x finish, thread a will be killed on exit.
# #=> "axyz"
#
# The following example illustrates the `limit` parameter.
#
# y = Thread.new { 4.times { sleep 0.1; puts 'tick... ' }}
# puts "Waiting" until y.join(0.15)
#
# This will produce:
#
# tick...
# Waiting
# tick...
# Waiting
# tick...
# tick...
#
def join: (*untyped limit) -> Thread
# <!--
# rdoc-file=thread.c
# - thr.key?(sym) -> true or false
# -->
# Returns `true` if the given string (or symbol) exists as a fiber-local
# variable.
#
# me = Thread.current
# me[:oliver] = "a"
# me.key?(:oliver) #=> true
# me.key?(:stanley) #=> false
#
def key?: (Symbol sym) -> bool
# <!--
# rdoc-file=thread.c
# - thr.keys -> array
# -->
# Returns an array of the names of the fiber-local variables (as Symbols).
#
# thr = Thread.new do
# Thread.current[:cat] = 'meow'
# Thread.current["dog"] = 'woof'
# end
# thr.join #=> #<Thread:0x401b3f10 dead>
# thr.keys #=> [:dog, :cat]
#
def keys: () -> ::Array[Symbol]
# <!--
# rdoc-file=thread.c
# - thr.name -> string
# -->
# show the name of the thread.
#
def name: () -> String
# <!--
# rdoc-file=thread.c
# - thr.name=(name) -> string
# -->
# set given name to the ruby thread. On some platform, it may set the name to
# pthread and/or kernel.
#
def name=: (untyped name) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.native_thread_id -> integer
# -->
# Return the native thread ID which is used by the Ruby thread.
#
# The ID depends on the OS. (not POSIX thread ID returned by pthread_self(3))
# * On Linux it is TID returned by gettid(2).
# * On macOS it is the system-wide unique integral ID of thread returned by
# pthread_threadid_np(3).
# * On FreeBSD it is the unique integral ID of the thread returned by
# pthread_getthreadid_np(3).
# * On Windows it is the thread identifier returned by GetThreadId().
# * On other platforms, it raises NotImplementedError.
#
# NOTE: If the thread is not associated yet or already deassociated with a
# native thread, it returns *nil*. If the Ruby implementation uses M:N thread
# model, the ID may change depending on the timing.
#
def native_thread_id: () -> Integer
# <!--
# rdoc-file=thread.c
# - target_thread.pending_interrupt?(error = nil) -> true/false
# -->
# Returns whether or not the asynchronous queue is empty for the target thread.
#
# If `error` is given, then check only for `error` type deferred events.
#
# See ::pending_interrupt? for more information.
#
def pending_interrupt?: (*untyped args) -> bool
# <!--
# rdoc-file=thread.c
# - thr.priority -> integer
# -->
# Returns the priority of *thr*. Default is inherited from the current thread
# which creating the new thread, or zero for the initial main thread;
# higher-priority thread will run more frequently than lower-priority threads
# (but lower-priority threads can also run).
#
# This is just hint for Ruby thread scheduler. It may be ignored on some
# platform.
#
# Thread.current.priority #=> 0
#
def priority: () -> Integer
# <!--
# rdoc-file=thread.c
# - thr.priority= integer -> thr
# -->
# Sets the priority of *thr* to *integer*. Higher-priority threads will run more
# frequently than lower-priority threads (but lower-priority threads can also
# run).
#
# This is just hint for Ruby thread scheduler. It may be ignored on some
# platform.
#
# count1 = count2 = 0
# a = Thread.new do
# loop { count1 += 1 }
# end
# a.priority = -1
#
# b = Thread.new do
# loop { count2 += 1 }
# end
# b.priority = -2
# sleep 1 #=> 1
# count1 #=> 622504
# count2 #=> 5832
#
def priority=: (Integer priority) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.report_on_exception -> true or false
# -->
# Returns the status of the thread-local ``report on exception'' condition for
# this `thr`.
#
# The default value when creating a Thread is the value of the global flag
# Thread.report_on_exception.
#
# See also #report_on_exception=.
#
# There is also a class level method to set this for all new threads, see
# ::report_on_exception=.
#
def report_on_exception: () -> bool
# <!--
# rdoc-file=thread.c
# - thr.report_on_exception= boolean -> true or false
# -->
# When set to `true`, a message is printed on $stderr if an exception kills this
# `thr`. See ::report_on_exception for details.
#
# See also #report_on_exception.
#
# There is also a class level method to set this for all new threads, see
# ::report_on_exception=.
#
def report_on_exception=: (boolish report_on_exception) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.run -> thr
# -->
# Wakes up `thr`, making it eligible for scheduling.
#
# a = Thread.new { puts "a"; Thread.stop; puts "c" }
# sleep 0.1 while a.status!='sleep'
# puts "Got here"
# a.run
# a.join
#
# This will produce:
#
# a
# Got here
# c
#
# See also the instance method #wakeup.
#
def run: () -> Thread
# Returns the safe level.
#
# This method is obsolete because $SAFE is a process global state. Simply
# check $SAFE.
def safe_level: () -> Integer
# <!--
# rdoc-file=thread.c
# - thr.status -> string, false or nil
# -->
# Returns the status of `thr`.
#
# `"sleep"`
# : Returned if this thread is sleeping or waiting on I/O
#
# `"run"`
# : When this thread is executing
#
# `"aborting"`
# : If this thread is aborting
#
# `false`
# : When this thread is terminated normally
#
# `nil`
# : If terminated with an exception.
#
#
# a = Thread.new { raise("die now") }
# b = Thread.new { Thread.stop }
# c = Thread.new { Thread.exit }
# d = Thread.new { sleep }
# d.kill #=> #<Thread:0x401b3678 aborting>
# a.status #=> nil
# b.status #=> "sleep"
# c.status #=> false
# d.status #=> "aborting"
# Thread.current.status #=> "run"
#
# See also the instance methods #alive? and #stop?
#
def status: () -> (String | bool)?
# <!--
# rdoc-file=thread.c
# - thr.stop? -> true or false
# -->
# Returns `true` if `thr` is dead or sleeping.
#
# a = Thread.new { Thread.stop }
# b = Thread.current
# a.stop? #=> true
# b.stop? #=> false
#
# See also #alive? and #status.
#
def stop?: () -> bool
# <!-- rdoc-file=thread.c -->
# Terminates `thr` and schedules another thread to be run, returning the
# terminated Thread. If this is the main thread, or the last thread, exits the
# process.
#
def terminate: () -> Thread?
# <!--
# rdoc-file=thread.c
# - thr.thread_variable?(key) -> true or false
# -->
# Returns `true` if the given string (or symbol) exists as a thread-local
# variable.
#
# me = Thread.current
# me.thread_variable_set(:oliver, "a")
# me.thread_variable?(:oliver) #=> true
# me.thread_variable?(:stanley) #=> false
#
# Note that these are not fiber local variables. Please see Thread#[] and
# Thread#thread_variable_get for more details.
#
def thread_variable?: (interned key) -> bool
# <!--
# rdoc-file=thread.c
# - thr.thread_variable_get(key) -> obj or nil
# -->
# Returns the value of a thread local variable that has been set. Note that
# these are different than fiber local values. For fiber local values, please
# see Thread#[] and Thread#[]=.
#
# Thread local values are carried along with threads, and do not respect fibers.
# For example:
#
# Thread.new {
# Thread.current.thread_variable_set("foo", "bar") # set a thread local
# Thread.current["foo"] = "bar" # set a fiber local
#
# Fiber.new {
# Fiber.yield [
# Thread.current.thread_variable_get("foo"), # get the thread local
# Thread.current["foo"], # get the fiber local
# ]
# }.resume
# }.join.value # => ['bar', nil]
#
# The value "bar" is returned for the thread local, where nil is returned for
# the fiber local. The fiber is executed in the same thread, so the thread
# local values are available.
#
def thread_variable_get: (untyped key) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.thread_variable_set(key, value)
# -->
# Sets a thread local with `key` to `value`. Note that these are local to
# threads, and not to fibers. Please see Thread#thread_variable_get and
# Thread#[] for more information.
#
def thread_variable_set: (untyped key, untyped value) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.thread_variables -> array
# -->
# Returns an array of the names of the thread-local variables (as Symbols).
#
# thr = Thread.new do
# Thread.current.thread_variable_set(:cat, 'meow')
# Thread.current.thread_variable_set("dog", 'woof')
# end
# thr.join #=> #<Thread:0x401b3f10 dead>
# thr.thread_variables #=> [:dog, :cat]
#
# Note that these are not fiber local variables. Please see Thread#[] and
# Thread#thread_variable_get for more details.
#
def thread_variables: () -> ::Array[Symbol]
# <!--
# rdoc-file=thread.c
# - thr.value -> obj
# -->
# Waits for `thr` to complete, using #join, and returns its value or raises the
# exception which terminated the thread.
#
# a = Thread.new { 2 + 2 }
# a.value #=> 4
#
# b = Thread.new { raise 'something went wrong' }
# b.value #=> RuntimeError: something went wrong
#
def value: () -> untyped
# <!--
# rdoc-file=thread.c
# - thr.wakeup -> thr
# -->
# Marks a given thread as eligible for scheduling, however it may still remain
# blocked on I/O.
#
# **Note:** This does not invoke the scheduler, see #run for more information.
#
# c = Thread.new { Thread.stop; puts "hey!" }
# sleep 0.1 while c.status!='sleep'
# c.wakeup
# c.join
# #=> "hey!"
#
def wakeup: () -> Thread
# <!--
# rdoc-file=thread.c
# - Thread.abort_on_exception -> true or false
# -->
# Returns the status of the global ``abort on exception'' condition.
#
# The default is `false`.
#
# When set to `true`, if any thread is aborted by an exception, the raised
# exception will be re-raised in the main thread.
#
# Can also be specified by the global $DEBUG flag or command line option `-d`.
#
# See also ::abort_on_exception=.
#
# There is also an instance level method to set this for a specific thread, see
# #abort_on_exception.
#
def self.abort_on_exception: () -> untyped
# <!--
# rdoc-file=thread.c
# - Thread.abort_on_exception= boolean -> true or false
# -->
# When set to `true`, if any thread is aborted by an exception, the raised
# exception will be re-raised in the main thread. Returns the new state.
#
# Thread.abort_on_exception = true
# t1 = Thread.new do
# puts "In new thread"
# raise "Exception from thread"
# end
# sleep(1)
# puts "not reached"
#
# This will produce:
#
# In new thread
# prog.rb:4: Exception from thread (RuntimeError)
# from prog.rb:2:in `initialize'
# from prog.rb:2:in `new'
# from prog.rb:2
#
# See also ::abort_on_exception.
#
# There is also an instance level method to set this for a specific thread, see
# #abort_on_exception=.
#
def self.abort_on_exception=: (untyped abort_on_exception) -> untyped
# <!--
# rdoc-file=vm_backtrace.c
# - Thread.each_caller_location(...) { |loc| ... } -> nil
# -->
# Yields each frame of the current execution stack as a backtrace location
# object.
#
def self.each_caller_location: () { (Backtrace::Location) -> void } -> nil
# <!--
# rdoc-file=thread.c
# - Thread.exit -> thread
# -->
# Terminates the currently running thread and schedules another thread to be
# run.
#
# If this thread is already marked to be killed, ::exit returns the Thread.
#
# If this is the main thread, or the last thread, exit the process.
#
def self.exit: () -> untyped
# <!--
# rdoc-file=thread.c
# - Thread.start([args]*) {|args| block } -> thread
# - Thread.fork([args]*) {|args| block } -> thread
# -->
# Basically the same as ::new. However, if class Thread is subclassed, then
# calling `start` in that subclass will not invoke the subclass's `initialize`
# method.
#
def self.fork: (*untyped args) -> untyped
# <!--
# rdoc-file=thread.c
# - Thread.handle_interrupt(hash) { ... } -> result of the block
# -->
# Changes asynchronous interrupt timing.
#
# *interrupt* means asynchronous event and corresponding procedure by
# Thread#raise, Thread#kill, signal trap (not supported yet) and main thread
# termination (if main thread terminates, then all other thread will be killed).
#
# The given `hash` has pairs like `ExceptionClass => :TimingSymbol`. Where the
# ExceptionClass is the interrupt handled by the given block. The TimingSymbol
# can be one of the following symbols:
#
# `:immediate`
# : Invoke interrupts immediately.
#
# `:on_blocking`
# : Invoke interrupts while *BlockingOperation*.
#
# `:never`
# : Never invoke all interrupts.
#
#
# *BlockingOperation* means that the operation will block the calling thread,
# such as read and write. On CRuby implementation, *BlockingOperation* is any
# operation executed without GVL.
#
# Masked asynchronous interrupts are delayed until they are enabled. This method
# is similar to sigprocmask(3).
#
# ### NOTE
#
# Asynchronous interrupts are difficult to use.
#
# If you need to communicate between threads, please consider to use another way
# such as Queue.
#
# Or use them with deep understanding about this method.
#
# ### Usage
#
# In this example, we can guard from Thread#raise exceptions.
#
# Using the `:never` TimingSymbol the RuntimeError exception will always be
# ignored in the first block of the main thread. In the second
# ::handle_interrupt block we can purposefully handle RuntimeError exceptions.
#
# th = Thread.new do
# Thread.handle_interrupt(RuntimeError => :never) {
# begin
# # You can write resource allocation code safely.
# Thread.handle_interrupt(RuntimeError => :immediate) {
# # ...
# }
# ensure
# # You can write resource deallocation code safely.
# end
# }
# end
# Thread.pass
# # ...
# th.raise "stop"
#
# While we are ignoring the RuntimeError exception, it's safe to write our
# resource allocation code. Then, the ensure block is where we can safely
# deallocate your resources.
#
# #### Stack control settings
#
# It's possible to stack multiple levels of ::handle_interrupt blocks in order
# to control more than one ExceptionClass and TimingSymbol at a time.
#
# Thread.handle_interrupt(FooError => :never) {
# Thread.handle_interrupt(BarError => :never) {
# # FooError and BarError are prohibited.
# }
# }
#
# #### Inheritance with ExceptionClass
#
# All exceptions inherited from the ExceptionClass parameter will be considered.
#
# Thread.handle_interrupt(Exception => :never) {
# # all exceptions inherited from Exception are prohibited.
# }
#
# For handling all interrupts, use `Object` and not `Exception` as the
# ExceptionClass, as kill/terminate interrupts are not handled by `Exception`.
#
def self.handle_interrupt: (untyped hash) -> untyped
# <!--
# rdoc-file=thread.c
# - thr.raise
# - thr.raise(string)
# - thr.raise(exception [, string [, array]])
# -->
# Raises an exception from the given thread. The caller does not have to be
# `thr`. See Kernel#raise for more information.
#
# Thread.abort_on_exception = true
# a = Thread.new { sleep(200) }
# a.raise("Gotcha")
#
# This will produce:
#
# prog.rb:3: Gotcha (RuntimeError)
# from prog.rb:2:in `initialize'
# from prog.rb:2:in `new'
# from prog.rb:2
#
def raise: (?String message) -> nil
| (_Exception, ?_ToS message, ?Array[Thread::Backtrace::Location] | Array[String] | nil backtrace) -> nil
# <!--
# rdoc-file=thread.c
# - Thread.kill(thread) -> thread
# -->
# Causes the given `thread` to exit, see also Thread::exit.
#
# count = 0
# a = Thread.new { loop { count += 1 } }
# sleep(0.1) #=> 0
# Thread.kill(a) #=> #<Thread:0x401b3d30 dead>
# count #=> 93947
# a.alive? #=> false
#
def self.kill: (Thread thread) -> untyped
# <!--
# rdoc-file=thread.c
# - Thread.list -> array
# -->
# Returns an array of Thread objects for all threads that are either runnable or
# stopped.
#
# Thread.new { sleep(200) }
# Thread.new { 1000000.times {|i| i*i } }
# Thread.new { Thread.stop }
# Thread.list.each {|t| p t}
#
# This will produce:
#
# #<Thread:0x401b3e84 sleep>
# #<Thread:0x401b3f38 run>
# #<Thread:0x401b3fb0 sleep>
# #<Thread:0x401bdf4c run>
#