-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.rb
81 lines (65 loc) · 1.99 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
require 'miner_mover/run'
require 'thread'
include MinerMover
run = Run.new.cfg_banner!(duration: 1).start!
run.timestamp!
run.log "Starting #{__FILE__}"
stop_mining = false
Signal.trap("INT") {
run.timestamp!
run.log " *** SIGINT *** Stop Mining"
stop_mining = true
}
run.log "MOVE Moving operation started"
queue = Thread::Queue.new
run.log "WAIT Waiting for ore ..."
# store mover threads in an array
movers = Array.new(run.num_movers) { |i|
Thread.new {
m = run.new_mover
run.log "MOVE Mover #{i} started"
# movers pull from the queue, load the ore, and move it
loop {
ore = queue.pop
break if ore == :quit
m.load_ore ore # moving of ore possibly happens here (on a full batch)
}
# move any remaining ore and quit
m.move_batch while m.batch > 0
m.log "QUIT #{m.status}"
m
}
}
run.log "MINE Mining operation started [ctrl-c] to stop"
# store the miner threads in an array
miners = Array.new(run.num_miners) { |i|
Thread.new {
m = run.new_miner
m.log "MINE Miner #{i} started"
ore_mined = 0
# miners wait for the SIGINT signal to quit
while !stop_mining
ore = m.mine_ore
ore_mined += ore
queue.push ore if ore > 0 # send any ore mined to the movers
# stop mining after a while
if run.time_limit? or run.ore_limit?(ore_mined)
run.timestamp!
m.log format("Mining limit reached: %s", Ore.display(ore_mined))
stop_mining = true
end
end
m.log format("MINE Miner %i finished after mining %s",
i, Ore.display(ore_mined))
ore_mined
}
}
# wait on all mining threads to stop
ore_mined = miners.map { |thr| thr.value }.sum
run.log format("MINE %s mined (%i)", Ore.display(ore_mined), ore_mined)
# tell all the movers to quit; gather their results
run.num_movers.times { queue.push :quit }
# wait for results
ore_moved = movers.map { |thr| thr.value.ore_moved }.sum
run.log format("MOVE %s moved (%i)", Ore.display(ore_moved), ore_moved)
run.timestamp!