1 | #! /usr/bin/env python |
---|
2 | # encoding: utf-8 |
---|
3 | # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file |
---|
4 | |
---|
5 | import random,atexit |
---|
6 | try: |
---|
7 | from queue import Queue |
---|
8 | except ImportError: |
---|
9 | from Queue import Queue |
---|
10 | from waflib import Utils,Task,Errors,Logs |
---|
11 | GAP=10 |
---|
12 | class TaskConsumer(Utils.threading.Thread): |
---|
13 | def __init__(self): |
---|
14 | Utils.threading.Thread.__init__(self) |
---|
15 | self.ready=Queue() |
---|
16 | self.setDaemon(1) |
---|
17 | self.start() |
---|
18 | def run(self): |
---|
19 | try: |
---|
20 | self.loop() |
---|
21 | except Exception: |
---|
22 | pass |
---|
23 | def loop(self): |
---|
24 | while 1: |
---|
25 | tsk=self.ready.get() |
---|
26 | if not isinstance(tsk,Task.TaskBase): |
---|
27 | tsk(self) |
---|
28 | else: |
---|
29 | tsk.process() |
---|
30 | pool=Queue() |
---|
31 | def get_pool(): |
---|
32 | try: |
---|
33 | return pool.get(False) |
---|
34 | except Exception: |
---|
35 | return TaskConsumer() |
---|
36 | def put_pool(x): |
---|
37 | pool.put(x) |
---|
38 | def _free_resources(): |
---|
39 | global pool |
---|
40 | lst=[] |
---|
41 | while pool.qsize(): |
---|
42 | lst.append(pool.get()) |
---|
43 | for x in lst: |
---|
44 | x.ready.put(None) |
---|
45 | for x in lst: |
---|
46 | x.join() |
---|
47 | pool=None |
---|
48 | atexit.register(_free_resources) |
---|
49 | class Parallel(object): |
---|
50 | def __init__(self,bld,j=2): |
---|
51 | self.numjobs=j |
---|
52 | self.bld=bld |
---|
53 | self.outstanding=[] |
---|
54 | self.frozen=[] |
---|
55 | self.out=Queue(0) |
---|
56 | self.count=0 |
---|
57 | self.processed=1 |
---|
58 | self.stop=False |
---|
59 | self.error=[] |
---|
60 | self.biter=None |
---|
61 | self.dirty=False |
---|
62 | def get_next_task(self): |
---|
63 | if not self.outstanding: |
---|
64 | return None |
---|
65 | return self.outstanding.pop(0) |
---|
66 | def postpone(self,tsk): |
---|
67 | if random.randint(0,1): |
---|
68 | self.frozen.insert(0,tsk) |
---|
69 | else: |
---|
70 | self.frozen.append(tsk) |
---|
71 | def refill_task_list(self): |
---|
72 | while self.count>self.numjobs*GAP: |
---|
73 | self.get_out() |
---|
74 | while not self.outstanding: |
---|
75 | if self.count: |
---|
76 | self.get_out() |
---|
77 | elif self.frozen: |
---|
78 | try: |
---|
79 | cond=self.deadlock==self.processed |
---|
80 | except AttributeError: |
---|
81 | pass |
---|
82 | else: |
---|
83 | if cond: |
---|
84 | msg='check the build order for the tasks' |
---|
85 | for tsk in self.frozen: |
---|
86 | if not tsk.run_after: |
---|
87 | msg='check the methods runnable_status' |
---|
88 | break |
---|
89 | lst=[] |
---|
90 | for tsk in self.frozen: |
---|
91 | lst.append('%s\t-> %r'%(repr(tsk),[id(x)for x in tsk.run_after])) |
---|
92 | raise Errors.WafError('Deadlock detected: %s%s'%(msg,''.join(lst))) |
---|
93 | self.deadlock=self.processed |
---|
94 | if self.frozen: |
---|
95 | self.outstanding+=self.frozen |
---|
96 | self.frozen=[] |
---|
97 | elif not self.count: |
---|
98 | self.outstanding.extend(self.biter.next()) |
---|
99 | self.total=self.bld.total() |
---|
100 | break |
---|
101 | def add_more_tasks(self,tsk): |
---|
102 | if getattr(tsk,'more_tasks',None): |
---|
103 | self.outstanding+=tsk.more_tasks |
---|
104 | self.total+=len(tsk.more_tasks) |
---|
105 | def get_out(self): |
---|
106 | tsk=self.out.get() |
---|
107 | if not self.stop: |
---|
108 | self.add_more_tasks(tsk) |
---|
109 | self.count-=1 |
---|
110 | self.dirty=True |
---|
111 | return tsk |
---|
112 | def add_task(self,tsk): |
---|
113 | try: |
---|
114 | self.pool |
---|
115 | except AttributeError: |
---|
116 | self.init_task_pool() |
---|
117 | self.ready.put(tsk) |
---|
118 | def init_task_pool(self): |
---|
119 | pool=self.pool=[get_pool()for i in range(self.numjobs)] |
---|
120 | self.ready=Queue(0) |
---|
121 | def setq(consumer): |
---|
122 | consumer.ready=self.ready |
---|
123 | for x in pool: |
---|
124 | x.ready.put(setq) |
---|
125 | return pool |
---|
126 | def free_task_pool(self): |
---|
127 | def setq(consumer): |
---|
128 | consumer.ready=Queue(0) |
---|
129 | self.out.put(self) |
---|
130 | try: |
---|
131 | pool=self.pool |
---|
132 | except AttributeError: |
---|
133 | pass |
---|
134 | else: |
---|
135 | for x in pool: |
---|
136 | self.ready.put(setq) |
---|
137 | for x in pool: |
---|
138 | self.get_out() |
---|
139 | for x in pool: |
---|
140 | put_pool(x) |
---|
141 | self.pool=[] |
---|
142 | def skip(self,tsk): |
---|
143 | tsk.hasrun=Task.SKIPPED |
---|
144 | def error_handler(self,tsk): |
---|
145 | if not self.bld.keep: |
---|
146 | self.stop=True |
---|
147 | self.error.append(tsk) |
---|
148 | def task_status(self,tsk): |
---|
149 | try: |
---|
150 | return tsk.runnable_status() |
---|
151 | except Exception: |
---|
152 | self.processed+=1 |
---|
153 | tsk.err_msg=Utils.ex_stack() |
---|
154 | if not self.stop and self.bld.keep: |
---|
155 | self.skip(tsk) |
---|
156 | if self.bld.keep==1: |
---|
157 | if Logs.verbose>1 or not self.error: |
---|
158 | self.error.append(tsk) |
---|
159 | self.stop=True |
---|
160 | else: |
---|
161 | if Logs.verbose>1: |
---|
162 | self.error.append(tsk) |
---|
163 | return Task.EXCEPTION |
---|
164 | tsk.hasrun=Task.EXCEPTION |
---|
165 | self.error_handler(tsk) |
---|
166 | return Task.EXCEPTION |
---|
167 | def start(self): |
---|
168 | self.total=self.bld.total() |
---|
169 | while not self.stop: |
---|
170 | self.refill_task_list() |
---|
171 | tsk=self.get_next_task() |
---|
172 | if not tsk: |
---|
173 | if self.count: |
---|
174 | continue |
---|
175 | else: |
---|
176 | break |
---|
177 | if tsk.hasrun: |
---|
178 | self.processed+=1 |
---|
179 | continue |
---|
180 | if self.stop: |
---|
181 | break |
---|
182 | st=self.task_status(tsk) |
---|
183 | if st==Task.RUN_ME: |
---|
184 | tsk.position=(self.processed,self.total) |
---|
185 | self.count+=1 |
---|
186 | tsk.master=self |
---|
187 | self.processed+=1 |
---|
188 | if self.numjobs==1: |
---|
189 | tsk.process() |
---|
190 | else: |
---|
191 | self.add_task(tsk) |
---|
192 | if st==Task.ASK_LATER: |
---|
193 | self.postpone(tsk) |
---|
194 | elif st==Task.SKIP_ME: |
---|
195 | self.processed+=1 |
---|
196 | self.skip(tsk) |
---|
197 | self.add_more_tasks(tsk) |
---|
198 | while self.error and self.count: |
---|
199 | self.get_out() |
---|
200 | assert(self.count==0 or self.stop) |
---|
201 | self.free_task_pool() |
---|