001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.util;
016:
017: import org.apache.tapestry.ioc.internal.util.ConcurrentBarrier;
018:
019: import java.util.concurrent.TimeUnit;
020:
021: public class ConcurrentTarget {
022: private final ConcurrentBarrier _barrier = new ConcurrentBarrier();
023:
024: private int _counter;
025:
026: // Used to check if read locks accumulate when a read lock method calls another read lock method
027: public int readCounter() {
028: return _barrier.withRead(new Invokable<Integer>() {
029: public Integer invoke() {
030: return getCounter();
031: }
032: });
033: }
034:
035: public int getCounter() {
036: return _barrier.withRead(new Invokable<Integer>() {
037: public Integer invoke() {
038: return _counter;
039: }
040: });
041: }
042:
043: public void incrementCounter() {
044: _barrier.withWrite(new Runnable() {
045: public void run() {
046: _counter++;
047: }
048: });
049: }
050:
051: public void setCounter(final int counter) {
052: _barrier.withWrite(new Runnable() {
053: public void run() {
054: _counter = counter;
055: }
056: });
057: }
058:
059: public void incrementIfNonNegative() {
060: _barrier.withRead(new Runnable() {
061: public void run() {
062: if (_counter >= 0)
063: incrementCounter();
064: }
065: });
066: }
067:
068: public void incrementViaRunnable() {
069: _barrier.withRead(new Runnable() {
070: public void run() {
071: Runnable r = new Runnable() {
072: public void run() {
073: incrementCounter();
074: }
075: };
076:
077: r.run();
078: }
079: });
080: }
081:
082: public void incrementCounterHard() {
083: _barrier.withWrite(new Runnable() {
084: public void run() {
085: _counter = getCounter() + 1;
086: }
087: });
088: }
089:
090: public void tryIncrementCounter() {
091: _barrier.tryWithWrite(new Runnable() {
092: public void run() {
093: _counter++;
094: }
095: }, 20, TimeUnit.MILLISECONDS);
096: }
097:
098: public void tryIncrementCounterHard() {
099: _barrier.tryWithWrite(new Runnable() {
100: public void run() {
101: _counter = getCounter() + 1;
102: }
103: }, 20, TimeUnit.MILLISECONDS);
104: }
105:
106: public void tryIncrementIfNonNegative() {
107: _barrier.withRead(new Runnable() {
108: public void run() {
109: if (_counter >= 0)
110: tryIncrementCounter();
111: }
112: });
113: }
114:
115: public void withRead(Runnable runnable) {
116: _barrier.withRead(runnable);
117: }
118: }
|