Fix potential race condition in ungetService().


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1684594 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
index 96fe8ba..a66f7e3 100644
--- a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
+++ b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
@@ -414,9 +414,10 @@
 
             // If usage count will go to zero, then unget the service
             // from the registration.
+            int count = usage.m_count.decrementAndGet();
             try
             {
-                if (usage.m_count.get() == 1)
+                if (count == 0)
                 {
                     // Remove reference from usages array.
                     ((ServiceRegistrationImpl.ServiceReferenceImpl) ref)
@@ -428,13 +429,9 @@
                 // Finally, decrement usage count and flush if it goes to zero or
                 // the registration became invalid.
 
-                // Decrement usage count, which spec says should happen after
-                // ungetting the service object.
-                int c = usage.m_count.decrementAndGet();
-
                 // If the registration is invalid or the usage count has reached
                 // zero, then flush it.
-                if ((c <= 0) || !reg.isValid())
+                if ((count <= 0) || !reg.isValid())
                 {
                     usage.m_svcHolderRef.set(null);
                     flushUsageCount(bundle, ref, usage);