root / trunk / web / dojo / dojox / timing / ThreadPool.js
History | View | Annotate | Download (2.57 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.timing.ThreadPool"]){ |
||
9 | dojo._hasResource["dojox.timing.ThreadPool"]=true; |
||
10 | dojo.provide("dojox.timing.ThreadPool");
|
||
11 | dojo.require("dojox.timing");
|
||
12 | dojo.experimental("dojox.timing.ThreadPool");
|
||
13 | (function(){
|
||
14 | var t=dojox.timing;
|
||
15 | t.threadStates={UNSTARTED:"unstarted",STOPPED:"stopped",PENDING:"pending",RUNNING:"running",SUSPENDED:"suspended",WAITING:"waiting",COMPLETE:"complete",ERROR:"error"}; |
||
16 | t.threadPriorities={LOWEST:1,BELOWNORMAL:2,NORMAL:3,ABOVENORMAL:4,HIGHEST:5}; |
||
17 | t.Thread=function(fn,_1){ |
||
18 | var _2=this; |
||
19 | this.state=t.threadStates.UNSTARTED;
|
||
20 | this.priority=_1||t.threadPriorities.NORMAL;
|
||
21 | this.lastError=null; |
||
22 | this.func=fn;
|
||
23 | this.invoke=function(){ |
||
24 | _2.state=t.threadStates.RUNNING; |
||
25 | try{
|
||
26 | fn(this);
|
||
27 | _2.state=t.threadStates.COMPLETE; |
||
28 | } |
||
29 | catch(e){
|
||
30 | _2.lastError=e; |
||
31 | _2.state=t.threadStates.ERROR; |
||
32 | } |
||
33 | }; |
||
34 | }; |
||
35 | t.ThreadPool=new (function(_3,_4){ |
||
36 | var _5=this; |
||
37 | var _6=_3;
|
||
38 | var _7=_6;
|
||
39 | var _8=_4;
|
||
40 | var _9=Math.floor((_8/2)/_6); |
||
41 | var _a=[];
|
||
42 | var _b=new Array(_6+1); |
||
43 | var _c=new dojox.timing.Timer(); |
||
44 | var _d=function(){ |
||
45 | var _e=_b[0]={}; |
||
46 | for(var i=0;i<_b.length;i++){ |
||
47 | window.clearTimeout(_b[i]); |
||
48 | var _f=_a.shift();
|
||
49 | if(typeof (_f)=="undefined"){ |
||
50 | break;
|
||
51 | } |
||
52 | _e["thread-"+i]=_f;
|
||
53 | _b[i]=window.setTimeout(_f.invoke,(_9*i)); |
||
54 | } |
||
55 | _7=_6-(i-1);
|
||
56 | }; |
||
57 | this.getMaxThreads=function(){ |
||
58 | return _6;
|
||
59 | }; |
||
60 | this.getAvailableThreads=function(){ |
||
61 | return _7;
|
||
62 | }; |
||
63 | this.getTickInterval=function(){ |
||
64 | return _8;
|
||
65 | }; |
||
66 | this.queueUserWorkItem=function(fn){ |
||
67 | var _10=fn;
|
||
68 | if(_10 instanceof Function){ |
||
69 | _10=new t.Thread(_10);
|
||
70 | } |
||
71 | var idx=_a.length;
|
||
72 | for(var i=0;i<_a.length;i++){ |
||
73 | if(_a[i].priority<_10.priority){
|
||
74 | idx=i; |
||
75 | break;
|
||
76 | } |
||
77 | } |
||
78 | if(idx<_a.length){
|
||
79 | _a.splice(idx,0,_10);
|
||
80 | }else{
|
||
81 | _a.push(_10); |
||
82 | } |
||
83 | return true; |
||
84 | }; |
||
85 | this.removeQueuedUserWorkItem=function(_11){ |
||
86 | if(_11 instanceof Function){ |
||
87 | var idx=-1; |
||
88 | for(var i=0;i<_a.length;i++){ |
||
89 | if(_a[i].func==_11){
|
||
90 | idx=i; |
||
91 | break;
|
||
92 | } |
||
93 | } |
||
94 | if(idx>-1){ |
||
95 | _a.splice(idx,1);
|
||
96 | return true; |
||
97 | } |
||
98 | return false; |
||
99 | } |
||
100 | var idx=-1; |
||
101 | for(var i=0;i<_a.length;i++){ |
||
102 | if(_a[i]==_11){
|
||
103 | idx=i; |
||
104 | break;
|
||
105 | } |
||
106 | } |
||
107 | if(idx>-1){ |
||
108 | _a.splice(idx,1);
|
||
109 | return true; |
||
110 | } |
||
111 | return false; |
||
112 | }; |
||
113 | this.start=function(){ |
||
114 | _c.start(); |
||
115 | }; |
||
116 | this.stop=function(){ |
||
117 | _c.stop(); |
||
118 | }; |
||
119 | this.abort=function(){ |
||
120 | this.stop();
|
||
121 | for(var i=1;i<_b.length;i++){ |
||
122 | if(_b[i]){
|
||
123 | window.clearTimeout(_b[i]); |
||
124 | } |
||
125 | } |
||
126 | for(var _12 in _b[0]){ |
||
127 | this.queueUserWorkItem(_12);
|
||
128 | } |
||
129 | _b[0]={};
|
||
130 | }; |
||
131 | this.reset=function(){ |
||
132 | this.abort();
|
||
133 | _a=[]; |
||
134 | }; |
||
135 | this.sleep=function(_13){ |
||
136 | _c.stop(); |
||
137 | window.setTimeout(_c.start,_13); |
||
138 | }; |
||
139 | _c.onTick=_5.invoke; |
||
140 | })(16,5000); |
||
141 | })(); |
||
142 | } |