Return-Path: Delivered-To: apmail-incubator-ode-commits-archive@locus.apache.org Received: (qmail 36550 invoked from network); 20 Jun 2007 17:41:47 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 20 Jun 2007 17:41:47 -0000 Received: (qmail 52048 invoked by uid 500); 20 Jun 2007 17:41:50 -0000 Delivered-To: apmail-incubator-ode-commits-archive@incubator.apache.org Received: (qmail 52018 invoked by uid 500); 20 Jun 2007 17:41:50 -0000 Mailing-List: contact ode-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ode-dev@incubator.apache.org Delivered-To: mailing list ode-commits@incubator.apache.org Received: (qmail 52009 invoked by uid 99); 20 Jun 2007 17:41:50 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Jun 2007 10:41:50 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Jun 2007 10:41:46 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 508021A981D; Wed, 20 Jun 2007 10:41:26 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r549168 - /incubator/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/engine/NStateLatch.java Date: Wed, 20 Jun 2007 17:41:26 -0000 To: ode-commits@incubator.apache.org From: mszefler@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070620174126.508021A981D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mszefler Date: Wed Jun 20 10:41:25 2007 New Revision: 549168 URL: http://svn.apache.org/viewvc?view=rev&rev=549168 Log: Added NStateLatch for hydrationlatch. Added: incubator/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/engine/NStateLatch.java (with props) Added: incubator/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/engine/NStateLatch.java URL: http://svn.apache.org/viewvc/incubator/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/engine/NStateLatch.java?view=auto&rev=549168 ============================================================================== --- incubator/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/engine/NStateLatch.java (added) +++ incubator/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/engine/NStateLatch.java Wed Jun 20 10:41:25 2007 @@ -0,0 +1,110 @@ +package org.apache.ode.bpel.engine; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * An N state synchronization latch useful for implementing hydration/dehydration. The + * latch functions as follows. At any time, the latch is in one of N states and has a + * count. Clients can "latch" and "release" the latch, which increments/decrements the + * count; however, when latching, a state must be specified. If the state does not match + * the current state, the latch blocks until the count is zero. Essentially, the latch + * can change state only when the count is zero. Every time the latch changes state an + * optional {@link Runnable} corresponding to the new state is executed. + * + * + * @author Maciej Szefler ( m s z e f l e r @ g m a i l . c o m ) + * + */ +public class NStateLatch { + + /** Current state. */ + private int _state = -1; + + /** Current depth (i.e. number of enter() calls) */ + private int _depth = 0; + + /** Action for state transition ?-->i */ + protected Runnable _transitions[]; + + /** Synchronization lock .*/ + private Lock _lock; + + /** _depth == 0 condition. */ + private Condition _depth0; + + private boolean _transitioning = false; + + /** + * Constructor, the array of {@link Runnable}s defines the number of states and the transition + * actions. + * @param transitions action to perform when entering state x. + */ + public NStateLatch(Runnable [] transitions) { + _transitions = transitions; + _lock = new ReentrantLock(); + _depth0 = _lock.newCondition(); + } + + public void latch(int state) { + if (state >= _transitions.length || state < 0) + throw new IllegalArgumentException("Invalid state."); + + _lock.lock(); + try { + + if (_transitioning ) + throw new IllegalStateException("Manipulating latch from transition. "); + + if (_state != state) { + // wait for the depth to become 0 + while (_depth != 0) + _depth0.awaitUninterruptibly(); + + if (_state != state) { + if (_transitions[state] != null) + try { + _transitioning = true; + _transitions[state].run(); + } catch (Throwable t) { + t.printStackTrace(); + } finally { + _transitioning = false; + } + + + _state = state; + + } + } + + _depth ++; + + + } finally { + _lock.unlock(); + } + } + + public void release(int state) { + _lock.lock(); + try { + + if (_transitioning ) + throw new IllegalStateException("Manipulating latch from transition. "); + + if (_state != state) + throw new IllegalStateException("Wrong state."); + if (_depth <= 0) + throw new IllegalStateException("Too many release() calls."); + + _depth --; + + if (_depth == 0) + _depth0.signal(); + } finally { + _lock.unlock(); + } + } +} Propchange: incubator/ode/trunk/bpel-runtime/src/main/java/org/apache/ode/bpel/engine/NStateLatch.java ------------------------------------------------------------------------------ svn:eol-style = native