-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_thread.rb
More file actions
88 lines (77 loc) · 1.68 KB
/
b_thread.rb
File metadata and controls
88 lines (77 loc) · 1.68 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
require 'continuation'
require_relative 'base_events'
class BThread
include BaseEvents
attr_accessor :request #, []
attr_accessor :wait #, []
attr_accessor :block #, []
attr_accessor :cont #, nil
#set-able in constructor
attr_accessor :name
attr_accessor :program
attr_accessor :bodyfunc, :alive
def body_wrap(le)
body le
@alive = false
end
def body(le)
bodyfunc.call le
end
def initialize(args = {})
@request = [:startevent]
@wait = [:startevent]
@block = none
@alive = true
# puts args
# if args.is_a? Hash
args.each do |k, v|
# p "key is #{k}, value is #{v}"
instance_variable_set("@#{k}", v) unless v.nil?
# p "set #{instance_variable_get("@#{k}")}"
end
# end
end
def bsync(args = {})
@request = args[:request] || none
@wait = args[:wait] || none
@block = args[:block] || none
e = callcc do |cc|
@cont=cc
# p self.inspect + " bsyncing %s, %s, %s" % [@request.inspect,
# @wait.inspect,
# @block.inspect]
# call the program's cont
@program.bsync
end
loop_copy_return_otherwise e
# program calls cont with triggered event
end
def loop_copy_return_otherwise(e)
if e.is_a? CopyContEvent
e = callcc do |cc|
e.call cc
end
return loop_copy_return_otherwise e
end
e
end
def resume(le)
if @cont.nil?
body_wrap le
else
@cont.call le
end
end
def alive?
@alive
end
def state
end
def inspect
if name
name
else
self.class.name + self.hash.inspect
end
end
end