about summary refs log tree commit diff stats
path: root/code/scheduler.rb
blob: 2a0f0a217f465971a2c37557c986861d64814682 (plain) (blame)
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
require 'thread'

# This thread inspects directories
module Scheduler
	extend self

	UPDATE_SIGNAL = 31
	PRIORITY = -1

	def reset()
		@scheduled = []
		@active = false

		@thread ||= Thread.new do
			if EVIL
				Thread.current.priority = PRIORITY
				while true
					Thread.stop
					manage unless @scheduled.empty? or !@active
				end

			else
				while true
					sleep 0.1
					manage unless @scheduled.empty? or !@active
				end
			end
		end
	end

	def run
		@active = true
	end

	def stop
		@active = false
	end

	def <<(dir)
		dir.scheduled = true
		unless @scheduled.include? dir
			@scheduled << dir
			if EVIL
				@thread.run
			end
		end
	end

	private
	def manage
		while dir = @scheduled.shift
			dir.refresh(true)
			dir.resize
			force_update
		end
	end

	def force_update
		Process.kill( UPDATE_SIGNAL, PID )
	end
end