001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
009: import com.tc.exception.TCRuntimeException;
010: import com.tc.object.config.ConfigLockLevel;
011: import com.tc.object.config.ConfigVisitor;
012: import com.tc.object.config.DSOClientConfigHelper;
013: import com.tc.object.config.TransparencyClassSpec;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tc.util.Assert;
017: import com.tctest.runner.AbstractTransparentApp;
018:
019: public class ConcurrentReadLockTestApp extends AbstractTransparentApp {
020:
021: private final CyclicBarrier barrier;
022: private final CyclicBarrier barrier2;
023: private final DataRoot dataRoot = new DataRoot();
024: private final SharedObject sharedObjRoot = new SharedObject();
025:
026: public ConcurrentReadLockTestApp(String appId,
027: ApplicationConfig cfg, ListenerProvider listenerProvider) {
028: super (appId, cfg, listenerProvider);
029: barrier = new CyclicBarrier(getParticipantCount());
030: barrier2 = new CyclicBarrier(getParticipantCount() * 2
031: + getParticipantCount());
032: }
033:
034: public void run() {
035: try {
036: int index = barrier.barrier();
037:
038: testConcurrentReadMethodWithinSingleJVM(index);
039: testConcurrentReadMethod(index);
040: testConcurrentReadBlockWithinSingleJVM(index);
041: testConcurrentReadBlock(index);
042: testConcurrentReadBlockWithinSingleJVMOnSharedObject(index);
043: testConcurrentReadBlockWithinSingleJVMOnSharedStr(index);
044: testConcurrentReadBlockWithinSingleJVMOnUnsharedAndSharedObject(index);
045:
046: } catch (Throwable t) {
047: notifyError(t);
048: }
049: }
050:
051: private void testConcurrentReadMethod(int index) throws Exception {
052: Runnable runnable = new Runnable() {
053: public void run() {
054: try {
055: int rv = dataRoot
056: .testConcurrentReadMethod(barrier2);
057: Assert.assertEquals(1, rv);
058: } catch (Exception e) {
059: throw new TCRuntimeException(e);
060: }
061: }
062: };
063:
064: Thread t1 = new Thread(runnable);
065: Thread t2 = new Thread(runnable);
066:
067: t1.start();
068: t2.start();
069: dataRoot.testConcurrentReadMethod(barrier2);
070:
071: barrier.barrier();
072: }
073:
074: private void testConcurrentReadMethodWithinSingleJVM(int index)
075: throws Exception {
076: if (index == 0) {
077: final CyclicBarrier localBarrier = new CyclicBarrier(3);
078: Runnable runnable = new Runnable() {
079: public void run() {
080: try {
081: int rv = dataRoot
082: .testConcurrentReadMethod(localBarrier);
083: Assert.assertEquals(1, rv);
084: } catch (Exception e) {
085: throw new TCRuntimeException(e);
086: }
087: }
088: };
089:
090: Thread t1 = new Thread(runnable);
091: Thread t2 = new Thread(runnable);
092:
093: t1.start();
094: t2.start();
095: dataRoot.testConcurrentReadMethod(localBarrier);
096: }
097:
098: barrier.barrier();
099: }
100:
101: private void testConcurrentReadBlock(int index) throws Exception {
102: Runnable runnable = new Runnable() {
103: public void run() {
104: try {
105: int rv = dataRoot.testConcurrentReadBlock(barrier2);
106: Assert.assertEquals(2, rv);
107: } catch (Exception e) {
108: throw new TCRuntimeException(e);
109: }
110: }
111: };
112:
113: Thread t1 = new Thread(runnable);
114: Thread t2 = new Thread(runnable);
115:
116: t1.start();
117: t2.start();
118: dataRoot.testConcurrentReadMethod(barrier2);
119:
120: barrier.barrier();
121: }
122:
123: private void testConcurrentReadBlockWithinSingleJVM(int index)
124: throws Exception {
125: if (index == 0) {
126: final CyclicBarrier localBarrier = new CyclicBarrier(3);
127: Runnable runnable = new Runnable() {
128: public void run() {
129: try {
130: int rv = dataRoot
131: .testConcurrentReadBlock(localBarrier);
132: Assert.assertEquals(2, rv);
133: } catch (Exception e) {
134: throw new TCRuntimeException(e);
135: }
136: }
137: };
138:
139: Thread t1 = new Thread(runnable);
140: Thread t2 = new Thread(runnable);
141:
142: t1.start();
143: t2.start();
144: dataRoot.testConcurrentReadMethod(localBarrier);
145: }
146:
147: barrier.barrier();
148: }
149:
150: private void testConcurrentReadBlockWithinSingleJVMOnSharedObject(
151: int index) throws Exception {
152: if (index == 0) {
153: final CyclicBarrier localBarrier = new CyclicBarrier(3);
154: final SharedObject sharedObj = new SharedObject();
155: sharedObjRoot.makeShared(sharedObj);
156:
157: Runnable runnable = new Runnable() {
158: public void run() {
159: try {
160: int rv = dataRoot
161: .testConcurrentReadBlockOnSharedObj(
162: sharedObj, localBarrier);
163: Assert.assertEquals(3, rv);
164: } catch (Exception e) {
165: throw new TCRuntimeException(e);
166: }
167: }
168: };
169:
170: Thread t1 = new Thread(runnable);
171: Thread t2 = new Thread(runnable);
172:
173: t1.start();
174: t2.start();
175: dataRoot.testConcurrentReadBlockOnSharedObj(sharedObj,
176: localBarrier);
177: }
178:
179: barrier.barrier();
180: }
181:
182: private void testConcurrentReadBlockWithinSingleJVMOnSharedStr(
183: int index) throws Exception {
184: if (index == 0) {
185: final CyclicBarrier localBarrier = new CyclicBarrier(3);
186: final String str = "1234";
187: sharedObjRoot.makeShared(str);
188:
189: Runnable runnable = new Runnable() {
190: public void run() {
191: try {
192: int rv = dataRoot
193: .testConcurrentReadBlockOnSharedObj(
194: str, localBarrier);
195: Assert.assertEquals(3, rv);
196: } catch (Exception e) {
197: throw new TCRuntimeException(e);
198: }
199: }
200: };
201:
202: Thread t1 = new Thread(runnable);
203: Thread t2 = new Thread(runnable);
204:
205: t1.start();
206: t2.start();
207: dataRoot.testConcurrentReadBlockOnSharedObj(str,
208: localBarrier);
209: }
210:
211: barrier.barrier();
212: }
213:
214: private void testConcurrentReadBlockWithinSingleJVMOnUnsharedAndSharedObject(
215: int index) throws Exception {
216: if (index == 0) {
217: final CyclicBarrier localBarrier = new CyclicBarrier(3);
218: final SharedObject sharedObj = new SharedObject();
219: sharedObjRoot.makeShared(sharedObj);
220:
221: Runnable runnable = new Runnable() {
222: public void run() {
223: try {
224: int rv = dataRoot
225: .testConcurrentReadBlockOnUnsharedAndSharedObj(
226: new SharedObject(), sharedObj,
227: localBarrier);
228: Assert.assertEquals(4, rv);
229: } catch (Exception e) {
230: throw new TCRuntimeException(e);
231: }
232: }
233: };
234:
235: Thread t1 = new Thread(runnable);
236: Thread t2 = new Thread(runnable);
237:
238: t1.start();
239: t2.start();
240: dataRoot.testConcurrentReadBlockOnUnsharedAndSharedObj(
241: new SharedObject(), sharedObj, localBarrier);
242: }
243:
244: barrier.barrier();
245: }
246:
247: public static void visitL1DSOConfig(ConfigVisitor visitor,
248: DSOClientConfigHelper config) {
249: TransparencyClassSpec spec = config
250: .getOrCreateSpec(CyclicBarrier.class.getName());
251: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
252: + "*.*(..)");
253:
254: String testClass = ConcurrentReadLockTestApp.class.getName();
255: spec = config.getOrCreateSpec(testClass);
256: config.addIncludePattern(testClass + "$*");
257:
258: String methodExpression = "* " + testClass
259: + "$DataRoot.testConcurrentReadMethod(..)";
260: config.addAutolock(methodExpression, ConfigLockLevel.READ);
261:
262: methodExpression = "* " + testClass
263: + "$DataRoot.testConcurrentReadBlock(..)";
264: config.addAutolock(methodExpression, ConfigLockLevel.READ);
265:
266: methodExpression = "* " + testClass
267: + "$DataRoot.testConcurrentReadBlockOnSharedObj(..)";
268: config.addAutolock(methodExpression, ConfigLockLevel.READ);
269:
270: methodExpression = "* "
271: + testClass
272: + "$DataRoot.testConcurrentReadBlockOnUnsharedAndSharedObj(..)";
273: config.addAutolock(methodExpression, ConfigLockLevel.READ);
274:
275: methodExpression = "* " + testClass
276: + "$SharedObject.makeShared(..)";
277: config.addAutolock(methodExpression, ConfigLockLevel.WRITE);
278:
279: spec.addRoot("barrier", "barrier");
280: spec.addRoot("barrier2", "barrier2");
281: spec.addRoot("dataRoot", "dataRoot");
282: spec.addRoot("sharedObjRoot", "sharedObjRoot");
283: }
284:
285: private static class SharedObject {
286: private SharedObject obj;
287: private String str;
288:
289: public SharedObject() {
290: super ();
291: }
292:
293: public synchronized void makeShared(SharedObject obj) {
294: this .obj = obj;
295: }
296:
297: public synchronized void makeShared(String obj) {
298: this .str = obj;
299: }
300: }
301:
302: private static class DataRoot {
303: public DataRoot() {
304: super ();
305: }
306:
307: public synchronized int testConcurrentReadMethod(
308: CyclicBarrier barrier) throws Exception {
309: barrier.barrier();
310:
311: return 1;
312: }
313:
314: public int testConcurrentReadBlock(CyclicBarrier barrier)
315: throws Exception {
316: synchronized (this ) {
317: barrier.barrier();
318:
319: return 2;
320: }
321: }
322:
323: public int testConcurrentReadBlockOnSharedObj(Object sharedObj,
324: CyclicBarrier barrier) throws Exception {
325: synchronized (sharedObj) {
326: barrier.barrier();
327:
328: return 3;
329: }
330: }
331:
332: public int testConcurrentReadBlockOnUnsharedAndSharedObj(
333: Object unsharedObj, Object sharedObj,
334: CyclicBarrier barrier) throws Exception {
335: synchronized (unsharedObj) {
336: synchronized (sharedObj) {
337: barrier.barrier();
338:
339: return 4;
340: }
341: }
342: }
343:
344: public int testConcurrentReadBlockOnSharedAndUnsharedObj(
345: Object unsharedObj, Object sharedObj,
346: CyclicBarrier barrier) throws Exception {
347: synchronized (sharedObj) {
348: barrier.barrier();
349:
350: synchronized (unsharedObj) {
351: return 5;
352: }
353: }
354: }
355:
356: }
357:
358: }
|