Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.timing.ThreadPool"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.timing.ThreadPool"] = true;
3
dojo.provide("dojox.timing.ThreadPool");
4
dojo.require("dojox.timing");
5
 
6
dojo.experimental("dojox.timing.ThreadPool");
7
 
8
//	dojox.timing.Timer is included as part of _base
9
/********************************************************************
10
	This is a port of the original System.Threading.ThreadPool from
11
	the f(m) class library.
12
 
13
	Donated to the Dojo toolkit by the author :)
14
*********************************************************************/
15
(function(){
16
	var t=dojox.timing;
17
	t.threadStates={
18
		UNSTARTED:"unstarted",
19
		STOPPED:"stopped",
20
		PENDING:"pending",
21
		RUNNING:"running",
22
		SUSPENDED:"suspended",
23
		WAITING:"waiting",
24
		COMPLETE:"complete",
25
		ERROR:"error"
26
	};
27
 
28
	//	Before rar says a word, we actually *use* these numbers for a purpose :)
29
	t.threadPriorities={
30
		LOWEST:1,
31
		BELOWNORMAL:2,
32
		NORMAL:3,
33
		ABOVENORMAL:4,
34
		HIGHEST:5
35
	};
36
 
37
	t.Thread=function(/* Function */fn, /* dojox.timing.threadPriorities? */priority){
38
		var self=this;
39
		this.state=t.threadStates.UNSTARTED;
40
		this.priority=priority||t.threadPriorities.NORMAL;
41
		this.lastError=null;
42
		this.func=fn;	//	for lookup purposes.
43
		this.invoke=function(){
44
			self.state=t.threadStates.RUNNING;
45
			try{
46
				fn(this);
47
				self.state=t.threadStates.COMPLETE;
48
			}catch(e){
49
				self.lastError=e;
50
				self.state=t.threadStates.ERROR;
51
			}
52
		};
53
	};
54
 
55
	//	TODO: allow for changing of maxThreads and tick interval
56
	t.ThreadPool=new (function(/* Number */mxthrs, /* Number */intvl){
57
		var self=this;
58
		var maxThreads=mxthrs;
59
		var availableThreads=maxThreads;
60
		var interval=intvl;
61
		var fireInterval=Math.floor((interval/2)/maxThreads);
62
		var queue=[];
63
		var timers=new Array(maxThreads+1);
64
		var timer=new dojox.timing.Timer();
65
		var invoke=function(){
66
			var tracker=timers[0]={};
67
			for(var i=0; i<timers.length; i++){
68
				window.clearTimeout(timers[i]);
69
				var thread=queue.shift();
70
				if(typeof(thread)=="undefined"){ break; }
71
				tracker["thread-"+i]=thread;
72
				timers[i]=window.setTimeout(thread.invoke,(fireInterval*i));
73
			}
74
			availableThreads=maxThreads-(i-1);
75
		};
76
 
77
		//	public methods
78
		this.getMaxThreads=function(){ return maxThreads; };
79
		this.getAvailableThreads=function(){ return availableThreads; };
80
		this.getTickInterval=function(){ return interval; };
81
		this.queueUserWorkItem=function(/* Function || dojox.timing.Thread */fn){
82
			var item=fn;
83
			if(item instanceof Function){
84
				item=new t.Thread(item);
85
			}
86
			var idx=queue.length;
87
			for(var i=0; i<queue.length; i++){
88
				if(queue[i].priority<item.priority){
89
					idx=i;
90
					break;
91
				}
92
			}
93
			if(idx<queue.length){
94
				queue.splice(idx, 0, item);
95
			} else {
96
				queue.push(item);
97
			}
98
			return true;
99
		};
100
		this.removeQueuedUserWorkItem=function(/* Function || dojox.timing.Thread */item){
101
			if(item instanceof Function){
102
				var idx=-1;
103
				for(var i=0; i<queue.length; i++){
104
					if(queue[i].func==item){
105
						idx=i;
106
						break;
107
					}
108
				}
109
				if(idx>-1){
110
					queue.splice(idx,1);
111
					return true;
112
				}
113
				return false;
114
			}
115
 
116
			var idx=-1;
117
			for(var i=0; i<queue.length; i++){
118
				if(queue[i]==item){
119
					idx=i;
120
					break;
121
				}
122
			}
123
			if(idx>-1){
124
				queue.splice(idx,1);
125
				return true;
126
			}
127
			return false;
128
		};
129
		this.start=function(){ timer.start(); };
130
		this.stop=function(){ timer.stop(); };
131
		this.abort=function(){
132
			this.stop();
133
			for(var i=1; i<timers.length; i++){
134
				if(timers[i]){
135
					window.clearTimeout(timers[i]);
136
				}
137
			}
138
			for(var thread in timers[0]){
139
				this.queueUserWorkItem(thread);
140
			}
141
			timers[0]={};
142
		};
143
		this.reset=function(){
144
			this.abort();
145
			queue=[];
146
		};
147
		this.sleep=function(/* Number */nSleep){
148
			timer.stop();
149
			window.setTimeout(timer.start, nSleep);
150
		};
151
 
152
		//	dedicate the timer to us.
153
		timer.onTick=self.invoke;
154
	})(16, 5000);
155
})();
156
 
157
}