Return-Path: X-Original-To: apmail-felix-commits-archive@www.apache.org Delivered-To: apmail-felix-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 20E2A76ED for ; Mon, 12 Sep 2011 21:40:43 +0000 (UTC) Received: (qmail 37186 invoked by uid 500); 12 Sep 2011 21:40:43 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 37141 invoked by uid 500); 12 Sep 2011 21:40:42 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 37134 invoked by uid 99); 12 Sep 2011 21:40:42 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Sep 2011 21:40:42 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Sep 2011 21:40:39 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B3B9123888FD for ; Mon, 12 Sep 2011 21:40:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1169945 - in /felix/trunk/framework/src/main/java/org/apache/felix/framework: BundleContextImpl.java Felix.java util/EventDispatcher.java Date: Mon, 12 Sep 2011 21:40:17 -0000 To: commits@felix.apache.org From: rickhall@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110912214017.B3B9123888FD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: rickhall Date: Mon Sep 12 21:40:17 2011 New Revision: 1169945 URL: http://svn.apache.org/viewvc?rev=1169945&view=rev Log: Avoid holding lock while adding listeners via the bundle context by double checking in the event dispatcher if the bundle context is still valid. (FELIX-3096) Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java felix/trunk/framework/src/main/java/org/apache/felix/framework/util/EventDispatcher.java Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java?rev=1169945&r1=1169944&r2=1169945&view=diff ============================================================================== --- felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java (original) +++ felix/trunk/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java Mon Sep 12 21:40:17 2011 @@ -208,9 +208,11 @@ class BundleContextImpl implements Felix { checkValidity(); - // CONCURRENCY NOTE: This is a NOT a check-then-act situation, - // because internally the framework acquires the bundle state - // lock to ensure state consistency. + // CONCURRENCY NOTE: This is a check-then-act situation, but + // internally the event dispatcher double checks whether or not + // the bundle context is valid before adding the service listener + // while holding the event queue lock, so it will either succeed + // or fail. Object sm = System.getSecurityManager(); @@ -266,9 +268,11 @@ class BundleContextImpl implements Felix { checkValidity(); - // CONCURRENCY NOTE: This is a NOT a check-then-act situation, - // because internally the framework acquires the bundle state - // lock to ensure state consistency. + // CONCURRENCY NOTE: This is a check-then-act situation, but + // internally the event dispatcher double checks whether or not + // the bundle context is valid before adding the service listener + // while holding the event queue lock, so it will either succeed + // or fail. m_felix.addServiceListener(m_bundle, l, s); } @@ -289,9 +293,11 @@ class BundleContextImpl implements Felix { checkValidity(); - // CONCURRENCY NOTE: This is a NOT a check-then-act situation, - // because internally the framework acquires the bundle state - // lock to ensure state consistency. + // CONCURRENCY NOTE: This is a check-then-act situation, but + // internally the event dispatcher double checks whether or not + // the bundle context is valid before adding the service listener + // while holding the event queue lock, so it will either succeed + // or fail. m_felix.addFrameworkListener(m_bundle, l); } Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java?rev=1169945&r1=1169944&r2=1169945&view=diff ============================================================================== --- felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java (original) +++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Felix.java Mon Sep 12 21:40:17 2011 @@ -70,7 +70,6 @@ import org.osgi.framework.wiring.BundleR import org.osgi.framework.wiring.BundleWiring; import org.osgi.framework.wiring.FrameworkWiring; import org.osgi.service.packageadmin.ExportedPackage; -import org.osgi.service.startlevel.StartLevel; public class Felix extends BundleImpl implements Framework { @@ -840,29 +839,7 @@ public class Felix extends BundleImpl im } } - // Set the start level using the start level service; - // this ensures that all start level requests are - // serialized. - StartLevel sl = null; - try - { - sl = (StartLevel) getService( - getBundle(0), getServiceReferences((BundleImpl) getBundle(0), - StartLevel.class.getName(), null, true)[0]); - } - catch (InvalidSyntaxException ex) - { - // Should never happen. - } - - if (sl instanceof StartLevelImpl) - { - m_fwkStartLevel.setStartLevelAndWait(startLevel); - } - else - { - sl.setStartLevel(startLevel); - } + m_fwkStartLevel.setStartLevelAndWait(startLevel); // The framework is now running. setBundleStateAndNotify(this, Bundle.ACTIVE); @@ -3077,26 +3054,8 @@ public class Felix extends BundleImpl im void addBundleListener(BundleImpl bundle, BundleListener l) { - // Acquire bundle lock. - try - { - acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING); - } - catch (IllegalStateException ex) - { - throw new IllegalStateException( - "Can only add listeners while bundle is active or activating."); - } - - try - { - m_dispatcher.addListener( - bundle._getBundleContext(), BundleListener.class, l, null); - } - finally - { - releaseBundleLock(bundle); - } + m_dispatcher.addListener( + bundle._getBundleContext(), BundleListener.class, l, null); } void removeBundleListener(BundleImpl bundle, BundleListener l) @@ -3117,29 +3076,11 @@ public class Felix extends BundleImpl im void addServiceListener(BundleImpl bundle, ServiceListener l, String f) throws InvalidSyntaxException { - // Acquire bundle lock. - try - { - acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING); - } - catch (IllegalStateException ex) - { - throw new IllegalStateException( - "Can only add listeners while bundle is active or activating."); - } - Filter oldFilter; Filter newFilter = (f == null) ? null : FrameworkUtil.createFilter(f); - try - { - oldFilter = m_dispatcher.addListener( - bundle._getBundleContext(), ServiceListener.class, l, newFilter); - } - finally - { - releaseBundleLock(bundle); - } + oldFilter = m_dispatcher.addListener( + bundle._getBundleContext(), ServiceListener.class, l, newFilter); // Invoke ListenerHook.removed() if filter updated. Set> listenerHooks = @@ -3241,26 +3182,8 @@ public class Felix extends BundleImpl im void addFrameworkListener(BundleImpl bundle, FrameworkListener l) { - // Acquire bundle lock. - try - { - acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING); - } - catch (IllegalStateException ex) - { - throw new IllegalStateException( - "Can only add listeners while bundle is active or activating."); - } - - try - { - m_dispatcher.addListener( - bundle._getBundleContext(), FrameworkListener.class, l, null); - } - finally - { - releaseBundleLock(bundle); - } + m_dispatcher.addListener( + bundle._getBundleContext(), FrameworkListener.class, l, null); } void removeFrameworkListener(BundleImpl bundle, FrameworkListener l) Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/util/EventDispatcher.java URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/util/EventDispatcher.java?rev=1169945&r1=1169944&r2=1169945&view=diff ============================================================================== --- felix/trunk/framework/src/main/java/org/apache/felix/framework/util/EventDispatcher.java (original) +++ felix/trunk/framework/src/main/java/org/apache/felix/framework/util/EventDispatcher.java Mon Sep 12 21:40:17 2011 @@ -188,6 +188,16 @@ public class EventDispatcher // Lock the object to add the listener. synchronized (this) { + // Verify that the bundle context is still valid. + try + { + bc.getBundle(); + } + catch (IllegalStateException ex) + { + // Bundle context is no longer valid, so just return. + } + Map> listeners = null; Object acc = null; @@ -384,6 +394,16 @@ public class EventDispatcher { synchronized (this) { + // Verify that the bundle context is still valid. + try + { + bc.getBundle(); + } + catch (IllegalStateException ex) + { + // Bundle context is no longer valid, so just return. + } + // See if the service listener is already registered; if so then // update its filter per the spec. List infos = m_svcListeners.get(bc);