Blame view

circus/src/jcircus/classesdescompiladas/CSPParallel.java 12.7 KB
8d0dc533f   Madiel de Souza Conserva Filho   first
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  // Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/10/2010 00:21:23
  // Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
  // Decompiler options: packimports(3) 
  
  //package org.jcsp.lang; DJ
  package jcircus.classesdescompiladas;
  
  import java.io.PrintStream;
  import java.util.*;
  
  import org.jcsp.lang.*;
  
  // Referenced classes of package org.jcsp.lang:
  //            Barrier, CSProcess, ParThread, ProcessInterruptedException
  
  public class CSPParallel
      implements CSProcess
  {
      private final Object sync;
      private CSProcess processes[];
      private int nProcesses;
      private ParThread parThreads[];
      private int nThreads;
      private Barrier barrier;
      private boolean priority;
      private boolean processesChanged;
      private static final Set allParThreads = Collections.synchronizedSet(new HashSet());
      private static boolean destroyCalled = false;
      private static boolean displayExceptions = true;
      private static boolean displayErrors = true;
  
      static void addToAllParThreads(Thread thread)
          throws InterruptedException
      {
          synchronized(allParThreads)
          {
              if(destroyCalled)
                  throw new InterruptedException("Parallel.destroy() has been called");
              allParThreads.add(thread);
          }
      }
  
      static void removeFromAllParThreads(Thread thread)
      {
          synchronized(allParThreads)
          {
              allParThreads.remove(thread);
          }
      }
  
      public static void destroy()
      {
          synchronized(allParThreads)
          {
              if(!destroyCalled)
              {
                  System.out.println("*** org.jcsp.lang.Parallel: stopping " + allParThreads.size() + " threads");
                  for(Iterator iterator = allParThreads.iterator(); iterator.hasNext();)
                  {
                      Thread thread = (Thread)iterator.next();
                      try
                      {
                          thread.interrupt();
                      }
                      catch(SecurityException securityexception)
                      {
                          System.out.println("*** org.jcsp.lang.Parallel: couldn't stop thread " + thread + " - security exception");
                      }
                  }
  
                  destroyCalled = true;
              }
          }
      }
  
      public static void resetDestroy()
      {
          synchronized(allParThreads)
          {
              destroyCalled = false;
          }
      }
  
      public CSPParallel()
      {
          this(null, false);
      }
  
      CSPParallel(boolean flag)
      {
          this(null, flag);
      }
  
      public CSPParallel(CSProcess acsprocess[])
      {
          this(acsprocess, false);
      }
  
      public CSPParallel(CSProcess acsprocess[][])
      {
          sync = new Object();
          nProcesses = 0;
          nThreads = 0;
          barrier = new Barrier();
          if(acsprocess != null)
          {
              int i = 0;
              for(int j = 0; j < acsprocess.length; j++)
                  if(acsprocess[j] != null)
                      i += acsprocess[j].length;
  
              nProcesses = i;
              processes = new CSProcess[nProcesses];
              i = 0;
              for(int k = 0; k < acsprocess.length; k++)
                  if(acsprocess[k] != null)
                  {
                      int l = acsprocess[k].length;
                      System.arraycopy(acsprocess[k], 0, processes, i, l);
                      i += l;
                  }
  
              parThreads = new ParThread[nProcesses];
          } else
          {
              nProcesses = 0;
              processes = new CSProcess[0];
              parThreads = new ParThread[0];
          }
          processesChanged = true;
          priority = false;
      }
  
      CSPParallel(CSProcess acsprocess[], boolean flag)
      {
          sync = new Object();
          nProcesses = 0;
          nThreads = 0;
          barrier = new Barrier();
          if(acsprocess != null)
          {
              nProcesses = acsprocess.length;
              processes = new CSProcess[nProcesses];
              System.arraycopy(acsprocess, 0, processes, 0, nProcesses);
              parThreads = new ParThread[nProcesses];
          } else
          {
              nProcesses = 0;
              processes = new CSProcess[0];
              parThreads = new ParThread[0];
          }
          processesChanged = true;
          priority = flag;
      }
  
      public void addProcess(CSProcess csprocess)
      {
          synchronized(sync)
          {
              if(csprocess != null)
              {
                  int i = nProcesses + 1;
                  if(i > processes.length)
                  {
                      CSProcess acsprocess[] = processes;
                      processes = new CSProcess[2 * i];
                      System.arraycopy(acsprocess, 0, processes, 0, nProcesses);
                  }
                  processes[nProcesses] = csprocess;
                  nProcesses = i;
                  processesChanged = true;
              }
          }
      }
  
      public void addProcess(CSProcess acsprocess[])
      {
          synchronized(sync)
          {
              if(processes != null)
              {
                  int i = acsprocess.length;
                  int j = nProcesses + i;
                  if(j > processes.length)
                  {
                      CSProcess acsprocess1[] = processes;
                      processes = new CSProcess[2 * j];
                      System.arraycopy(acsprocess1, 0, processes, 0, nProcesses);
                  }
                  System.arraycopy(acsprocess, 0, processes, nProcesses, i);
                  nProcesses = j;
                  processesChanged = true;
              }
          }
      }
  
      public void insertProcessAt(CSProcess csprocess, int i)
      {
          synchronized(sync)
          {
              if(i >= nProcesses + 1)
                  throw new ArrayIndexOutOfBoundsException(i + " > " + (nProcesses + 1));
              if(csprocess != null)
              {
                  int j = nProcesses + 1;
                  if(j > processes.length)
                  {
                      CSProcess acsprocess[] = processes;
                      processes = new CSProcess[2 * j];
                      System.arraycopy(acsprocess, 0, processes, 0, i);
                      System.arraycopy(acsprocess, i, processes, i + 1, nProcesses - i);
                  } else
                  if(i < nProcesses)
                      System.arraycopy(processes, i, processes, i + 1, nProcesses - i);
                  processes[i] = csprocess;
                  nProcesses = j;
                  processesChanged = true;
              }
          }
      }
      
      public void removeProcess (CSProcess csprocess) {
      	
      }
  /*    public void removeProcess(CSProcess csprocess)
      {
          Object obj = sync;
          //JVM INSTR monitorenter ; //DJ
          int i = 0;
  _L2:
          if(i >= nProcesses)
              break MISSING_BLOCK_LABEL_94;
          if(processes[i] != csprocess)
              break MISSING_BLOCK_LABEL_88;
          if(i < nProcesses - 1)
              System.arraycopy(processes, i + 1, processes, i, nProcesses - (i + 1));
          nProcesses--;
          processes[nProcesses] = null;
          processesChanged = true;
          return;
          i++;
          //if(true) goto _L2; //comentado
          //else goto _L1; //comentado
  _L1:
          break MISSING_BLOCK_LABEL_106;
          Exception exception 
          	= new Exception(); //<--Sam
          //exception; //DJ
          throw exception;
      }
  */
      public void removeAllProcesses()
      {
          synchronized(sync)
          {
              for(int i = 0; i < nProcesses; i++)
                  processes[i] = null;
  
              nProcesses = 0;
              processesChanged = true;
          }
      }
  
      protected void finalize()
          throws Throwable
      {
          releaseAllThreads();
      }
  
      public void releaseAllThreads()
      {
          synchronized(sync)
          {
              for(int i = 0; i < nThreads; i++)
              {
                  parThreads[i].terminate();
                  parThreads[i] = null;
              }
  
              nThreads = 0;
              processesChanged = true;
          }
      }
  
      public int getNumberProcesses()
      {
          int i;
          synchronized(sync)
          {
              i = nProcesses;
          }
          return i;
      }
  
      public void run()
      {
          boolean flag = true;
          CSProcess csprocess = null;
          synchronized(sync)
          {
              if(nProcesses > 0)
              {
                  flag = false;
                  int i = 0;
                  int j = 0;
                  if(priority)
                  {
                      Thread thread = Thread.currentThread();
                      i = thread.getPriority();
                      j = Math.min((i + nProcesses) - 1, Math.min(10, thread.getThreadGroup().getMaxPriority()));
                  }
                  barrier.reset(nProcesses);
                  csprocess = processes[nProcesses - 1];
                  if(processesChanged)
                  {
                      if(nThreads < nProcesses - 1)
                      {
                          if(parThreads.length < nProcesses - 1)
                          {
                              ParThread aparthread[] = parThreads;
                              parThreads = new ParThread[processes.length];
                              System.arraycopy(aparthread, 0, parThreads, 0, nThreads);
                          }
                          for(int l = 0; l < nThreads; l++)
                          {
                              parThreads[l].reset(processes[l], barrier);
                              if(priority)
                                  parThreads[l].setPriority(Math.max(i, j - l));
                              parThreads[l].release();
                          }
  
                          for(int i1 = nThreads; i1 < nProcesses - 1; i1++)
                          {
                              parThreads[i1] = new ParThread(processes[i1], barrier);
                              if(priority)
                                  parThreads[i1].setPriority(Math.max(i, j - i1));
                              parThreads[i1].start();
                          }
  
                          nThreads = nProcesses - 1;
                      } else
                      {
                          for(int j1 = 0; j1 < nProcesses - 1; j1++)
                          {
                              parThreads[j1].reset(processes[j1], barrier);
                              if(priority)
                                  parThreads[j1].setPriority(Math.max(i, j - j1));
                              parThreads[j1].release();
                          }
  
                      }
                      processesChanged = false;
                  } else
                  {
                      for(int k1 = 0; k1 < nProcesses - 1; k1++)
                      {
                          if(priority)
                              parThreads[k1].setPriority(Math.max(i, j - k1));
                          parThreads[k1].release();
                      }
  
                  }
              }
          }
          if(!flag)
          {
              try
              {
                  csprocess.run();
              }
              catch(ProcessInterruptedException processinterruptedexception)
              {
                  synchronized(sync)
                  {
                      for(int k = 0; k < nThreads; k++)
                          try
                          {
                              parThreads[k].interrupt();
                          }
                          catch(Throwable throwable1)
                          {
                              System.out.println("*** org.jcsp.lang.Parallel: couldn't stop thread " + throwable1 + " - security exception");
                          }
  
                  }
              }
              catch(Throwable throwable)
              {
                  uncaughtException("org.jcsp.lang.Parallel", throwable);
              }
              barrier.sync();
          }
      }
  
      public static void setUncaughtExceptionDisplay(boolean flag)
      {
          displayExceptions = flag;
      }
  
      public static void setUncaughtErrorDisplay(boolean flag)
      {
          displayErrors = flag;
      }
  
      static void uncaughtException(String s, Throwable throwable)
      {
          if((throwable instanceof Error) && displayErrors || (throwable instanceof Exception) && displayExceptions)
              synchronized(System.err)
              {
                  System.err.println("
  *** " + s + ": A process threw the following exception :");
                  throwable.printStackTrace();
                  System.err.println();
              }
      }
  
  }