This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
thread.rb
347 lines (275 loc) · 6.58 KB
/
thread.rb
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
# frozen_string_literal: true
# Single-threaded implementation of Thread and Mutex.
#
# Threads are executed synchronously and support "spawning" new threads that
# get pushed onto the stack and executed synchronously.
#
# A special "root" thread that never terminates is created on require.
#
# Mutex immediately acquires locks and yields since there is no possibility of
# contention in a single-threaded environement.
class Thread
alias __raise__ raise
attr_accessor :abort_on_exception
attr_accessor :name
attr_accessor :report_on_exception
attr_accessor :__unwind_with_exception
def self.__mark_unwind(exc)
@thread_stack.each do |thread|
thread.__unwind_with_exception = exc
end
nil
end
def self.__gen_thread_name(id)
"thread-#{@thread_stack.length}-#{id}"
end
@abort_on_exception = false
class << self
attr_accessor :abort_on_exception
end
# To simulate concurrent execution, Thread maintains a stack of Threads.
@thread_stack = []
def self.__push_stack(thread)
@thread_stack.push(thread)
nil
end
def self.__pop_stack
@thread_stack.pop
end
def self.current
@thread_stack.last
end
def self.exclusive
yield
end
def self.exit
# TODO: not implemented
end
singleton_class.send(:alias_method, :kill, :exit)
def self.fork(*args, &blk)
# TODO: handle subclassing behavior correctly.
new(*args, blk)
end
singleton_class.send(:alias_method, :start, :fork)
def self.handle_interrupt(_hash)
# https://ruby-doc.org/core-2.6.3/Thread.html#method-c-handle_interrupt
# Implemented as an immediate yield because interrupts are not a thing in
# the mruby interpreter. `Thread::exit` is not implemented.
yield
end
def self.list
# make sure to clone the list
@thread_stack.map(&:itself)
end
def self.main
@thread_stack.first
end
def self.pass
# noop since there is no scheduler
nil
end
def self.pending_interrupt?(_error = nil)
# See `Thread::handle_interrupt`.
false
end
@report_on_exception = false
class << self
attr_accessor :report_on_exception
end
def self.stop
# noop since there is no scheduler
nil
end
def initialize(root = false)
__raise__ ThreadError, 'must be called with a block' unless block_given?
@priority = 0
@priority = self.class.current.priority unless self.class.current.nil?
self.class.__push_stack(self)
@fiber_locals = {}
@thread_locals = {}
@abort_on_exception = false
@name = self.class.__gen_thread_name(object_id)
@report_on_exception = false
@terminated_with_exception = nil
# mruby is not multi-threaded. Threads are executed synchronously.
@alive = true
@value = yield
rescue StandardError => e
if @__unwind_with_exception.nil?
@terminated_with_exception = true
@value = e
self.class.__mark_unwind(e) if self.class.abort_on_exception || abort_on_exception
end
ensure
@alive = false unless root
self.class.__pop_stack unless root
__raise__ @__unwind_with_exception if self.class.current == self.class.main && !@__unwind_with_exception.nil?
end
def [](sym)
@fiber_locals[sym]
end
alias thr []
def []=(sym, obj)
@fiber_locals[sym] = obj
end
alias thr= []=
def add_trace_func(func)
# TODO: not implemented
end
def alive?
@alive
end
def backtrace
# TODO: implement this in Rust using the C API
[]
end
def backtrace_locations(*_args)
# TODO: not implemented
nil
end
def exit
# Because mruby Thread instances are run synchronously on initialize, this
# method is a noop.
nil
end
alias kill exit
alias terminate exit
def fetch(*args)
__raise__ ArgumentError, 'Thread#fetch requires 1 or 2 arguments' unless [1, 2].include?(args.length)
key = args[0]
if @fiber_locals.key?(key)
@fiber_locals[key]
elsif block_given?
# block supersedes default argument
yield key
elsif args.length == 2
args[1]
else
__raise__ "key '#{key}' not found in Thread locals"
end
end
def group
nil
end
alias inspect to_s
def join(*_args)
# Because mruby Thread instances are run synchronously on initialize, this
# method is a noop.
self
end
def key?(sym)
@fiber_locals.key?(sym)
end
def keys
@fiber_locals.keys.map(&:to_sym)
end
def pending_interrupt?(_error = nil)
false
end
attr_reader :priority
def priority=(pri)
@priority = pri
self # rubocop:disable Lint/Void
end
def raise(*args)
exc, message, array = *args
case args.length
when 0 then __raise__
when 1 then __raise__(exc)
when 2 then __raise__(exc, message)
when 3 then __raise__(exc, message, array)
else __raise__ ArgumentError
end
end
def run(*_args)
# Because mruby Thread instances are run synchronously on initialize, this
# method is a noop.
self
end
def safe_level
# TODO: not implemented
0
end
def set_trace_fun(func) # rubocop:disable Naming/AccessorMethodName
# TODO: not implemented
end
def status
if alive?
'run'
elsif @terminated_with_exception
nil
else
false
end
end
def stop?
!alive?
end
def thread_variable?(key)
@thread_locals.key?(key)
end
def thread_variable_get(key)
@thread_locals[key]
end
def thread_variable_set(key, value)
@thread_locals[key] = value
nil
end
def thread_variables
@thread_locals.keys.map(&:to_sym)
end
def to_s
"#<Thread:#{name}:#{object_id} #{status}>"
end
def value
__raise__ @value if @terminated_with_exception
@value
end
def wakeup
# Because mruby Thread instances are run synchronously on initialize, this
# method is a noop.
self
end
end
class ThreadError < StandardError; end
class Mutex
def initialize
@owner = nil
end
def lock
raise ThreadError, 'locked by current Thread' if owned?
@owner = Thread.current
self
end
def locked?
!@owner.nil?
end
def owned?
@owner == Thread.current
end
# This method does not actually sleep
def sleep(timeout = nil)
unlock
lock
timeout
end
# mruby interpreters are single threaded and are not `Send`. A `Mutex` can
# never be contended, so synchronize just immediately yields.
def synchronize
lock
yield
ensure
unlock
end
def try_lock
lock
locked? && owned?
end
def unlock
raise ThreadError, 'not locked by current Thread' unless owned?
@owner = nil
self
end
end
# Spawn the special "root" thread that never terminates.
Thread.new(true) {}