View Javadoc

1   package delight.concurrency.gwt;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.HashSet;
6   import java.util.LinkedList;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Queue;
10  import java.util.Set;
11  
12  import com.google.gwt.core.client.Scheduler;
13  import com.google.gwt.core.client.Scheduler.ScheduledCommand;
14  import com.google.gwt.user.client.Command;
15  import com.google.gwt.user.client.Timer;
16  
17  import delight.async.callbacks.SimpleCallback;
18  import delight.concurrency.Concurrency;
19  import delight.concurrency.factories.CollectionFactory;
20  import delight.concurrency.factories.ExecutorFactory;
21  import delight.concurrency.factories.TimerFactory;
22  import delight.concurrency.wrappers.SimpleAtomicBoolean;
23  import delight.concurrency.wrappers.SimpleAtomicInteger;
24  import delight.concurrency.wrappers.SimpleAtomicLong;
25  import delight.concurrency.wrappers.SimpleExecutor;
26  import delight.concurrency.wrappers.SimpleLock;
27  import delight.concurrency.wrappers.SimpleReadWriteLock;
28  import delight.concurrency.wrappers.SimpleTimer;
29  
30  /**
31   * GWT implementation of basic concurrency classes.
32   * 
33   * @author <a href="http://www.mxro.de">Max Rohde</a>
34   *
35   */
36  public final class GwtConcurrencyImpl implements Concurrency {
37  
38      @Override
39      public TimerFactory newTimer() {
40  
41          return new TimerFactory() {
42  
43              @Override
44              public SimpleTimer scheduleOnce(final int when, final Runnable runnable) {
45  
46                  if (when == 0) {
47                      Scheduler.get().scheduleDeferred(new ScheduledCommand() {
48  
49                          @Override
50                          public void execute() {
51                              runnable.run();
52                          }
53                      });
54                      return new SimpleTimer() {
55  
56                          @Override
57                          public void stop() {
58  
59                          }
60                      };
61  
62                  }
63  
64                  final Timer timer = new Timer() {
65  
66                      @Override
67                      public void run() {
68                          runnable.run();
69                      }
70  
71                  };
72  
73                  timer.schedule(when);
74                  return new SimpleTimer() {
75  
76                      @Override
77                      public void stop() {
78                          timer.cancel();
79                      }
80                  };
81              }
82  
83              @Override
84              public SimpleTimer scheduleRepeating(final int offsetInMs, final int intervallInMs,
85                      final Runnable runnable) {
86  
87                  final Timer timer = new Timer() {
88  
89                      @Override
90                      public void run() {
91                          runnable.run();
92                      }
93  
94                  };
95                  timer.scheduleRepeating(intervallInMs);
96                  return new SimpleTimer() {
97  
98                      @Override
99                      public void stop() {
100                         timer.cancel();
101                     }
102                 };
103 
104             }
105 
106         };
107     }
108 
109     @Override
110     public ExecutorFactory newExecutor() {
111 
112         return new ExecutorFactory() {
113 
114             @Override
115             public SimpleExecutor newAsyncExecutor(final Object owner) {
116 
117                 return createExecutor(true);
118             }
119 
120             @Override
121             public SimpleExecutor newSingleThreadExecutor(final Object owner) {
122 
123                 return createExecutor(false);
124             }
125 
126             @Override
127             public SimpleExecutor newParallelExecutor(final int maxParallelThreads, final Object owner) {
128 
129                 return createExecutor(false);
130             }
131 
132             @Override
133             public SimpleExecutor newImmideateExecutor() {
134 
135                 return createExecutor(false);
136             }
137 
138             @Override
139             public SimpleExecutor newParallelExecutor(final int minThreads, final int maxParallelThreads,
140                     final Object owner) {
141                 return createExecutor(false);
142             }
143 
144             private SimpleExecutor createExecutor(final boolean async) {
145                 return new SimpleExecutor() {
146 
147                     @Override
148                     public void shutdown(final SimpleCallback callback) {
149                         callback.onSuccess();
150                     }
151 
152                     @Override
153                     public void execute(final Runnable runnable) {
154                         run(runnable);
155 
156                     }
157 
158                     private void run(final Runnable runnable) {
159 
160                         if (!async) {
161                             runnable.run();
162                         } else {
163 
164                             Scheduler.get().scheduleDeferred(new Command() {
165 
166                                 @Override
167                                 public void execute() {
168                                     runnable.run();
169                                 }
170                             });
171                         }
172                     }
173 
174                     @Override
175                     public void execute(final Runnable runnable, final int timeout, final Runnable onTimeout) {
176                         run(runnable);
177 
178                     }
179 
180                     @Override
181                     public int pendingTasks() {
182                         return 0;
183                     }
184 
185                 };
186             }
187 
188         };
189     }
190 
191     @Override
192     public SimpleLock newLock() {
193 
194         return new SimpleLock() {
195 
196             @Override
197             public void lock() {
198 
199             }
200 
201             @Override
202             public void unlock() {
203 
204             }
205 
206             @Override
207             public boolean isHeldByCurrentThread() {
208                 return true;
209             }
210 
211         };
212     }
213 
214     
215     
216     @Override
217 	public SimpleReadWriteLock newReadWriteLock() {
218 		
219     	final SimpleLock lock = newLock();
220     	
221 		return new SimpleReadWriteLock() {
222 			
223 			@Override
224 			public SimpleLock writeLock() {
225 				
226 				return lock;
227 			}
228 			
229 			@Override
230 			public SimpleLock readLock() {
231 				
232 				return lock;
233 			}
234 		};
235 	}
236 
237 	@Override
238     public CollectionFactory newCollection() {
239 
240         return new CollectionFactory() {
241 
242             @Override
243             public <GPType> Queue<GPType> newThreadSafeQueue(final Class<GPType> itemType) {
244                 return new LinkedList<GPType>();
245             }
246 
247             @Override
248             public <KeyType, ValueType> Map<KeyType, ValueType> newThreadSafeMap(final Class<KeyType> keyType,
249                     final Class<ValueType> valueType) {
250                 return new HashMap<KeyType, ValueType>();
251             }
252 
253             @Override
254             public <ItemType> List<ItemType> newThreadSafeList(final Class<ItemType> itemType) {
255                 return new ArrayList<ItemType>();
256             }
257 
258             @Override
259             public <ItemType> Set<ItemType> newThreadSafeSet(final Class<ItemType> itemType) {
260                 return new HashSet<ItemType>();
261             }
262 
263 			@Override
264 			public <KeyType, ValueType> Map<KeyType, ValueType> newWeakHashMap(Class<KeyType> keyType,
265 					Class<ValueType> valueType) {
266 				
267 				return new HashMap<KeyType, ValueType>();
268 			}
269 
270         };
271     }
272 
273     @Override
274     public SimpleAtomicBoolean newAtomicBoolean(final boolean value) {
275 
276         return new SimpleAtomicBoolean() {
277 
278             boolean wrapped = value;
279 
280             @Override
281             public void set(final boolean newValue) {
282                 wrapped = newValue;
283             }
284 
285             @Override
286             public boolean getAndSet(final boolean newValue) {
287                 final boolean oldValue = wrapped;
288                 wrapped = newValue;
289                 return oldValue;
290             }
291 
292             @Override
293             public boolean get() {
294                 return wrapped;
295             }
296 
297             @Override
298             public boolean compareAndSet(final boolean expect, final boolean update) {
299 
300                 if (wrapped == expect) {
301                     wrapped = update;
302                     return true;
303                 }
304 
305                 return false;
306             }
307         };
308     }
309 
310     @Override
311     public SimpleAtomicInteger newAtomicInteger(final int value) {
312 
313         return new SimpleAtomicInteger() {
314 
315             int wrapped = value;
316 
317             @Override
318             public void set(final int newValue) {
319                 wrapped = newValue;
320             }
321 
322             @Override
323             public int incrementAndGet() {
324                 wrapped += 1;
325                 return wrapped;
326             }
327 
328             @Override
329             public int getAndSet(final int newValue) {
330                 final int oldwrapped = wrapped;
331                 wrapped = newValue;
332                 return oldwrapped;
333             }
334 
335             @Override
336             public int get() {
337                 return wrapped;
338             }
339 
340             @Override
341             public int decrementAndGet() {
342                 wrapped -= 1;
343                 return wrapped;
344             }
345         };
346     }
347 
348     @Override
349     public SimpleAtomicLong newAtomicLong(final long value) {
350 
351         return new SimpleAtomicLong() {
352 
353             long wrapped = value;
354 
355             @Override
356             public void set(final long newValue) {
357                 wrapped = newValue;
358             }
359 
360             @Override
361             public long incrementAndGet() {
362                 wrapped += 1;
363                 return wrapped;
364             }
365 
366             @Override
367             public long getAndSet(final long newValue) {
368                 final long oldwrapped = wrapped;
369                 wrapped = newValue;
370                 return oldwrapped;
371             }
372 
373             @Override
374             public long get() {
375                 return wrapped;
376             }
377 
378             @Override
379             public long decrementAndGet() {
380                 wrapped -= 1;
381                 return wrapped;
382             }
383         };
384     }
385 
386 }