summary refs log blame commit diff stats
path: root/code/scheduler.rb
blob: e35f28d654777caa9d310200f63e22d3cfaf52f8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                         










                                                                                   





                                   
                              







                                    
                                              
                                         


                                           


                   




                                            
                   
                            


                        
                                                          
           
   
 
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
		end
		force_update
	end

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