Skip to content

Commit

Permalink
Less synchronization for AbstractBundle.registeredServices/usedServices
Browse files Browse the repository at this point in the history
  • Loading branch information
bosschaert committed Sep 21, 2010
1 parent 72b9883 commit 290a16f
Showing 1 changed file with 17 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public abstract class AbstractBundle implements Bundle
private AbstractBundleContext bundleContext;
private BundleWrapper bundleWrapper;
private long lastModified = System.currentTimeMillis();
private List<ServiceState> registeredServices;
private Map<ServiceState, AtomicInteger> usedServices;
private final CopyOnWriteArrayList<ServiceState> registeredServices =
new CopyOnWriteArrayList<ServiceState>();
private final ConcurrentHashMap<ServiceState, AtomicInteger> usedServices =
new ConcurrentHashMap<ServiceState, AtomicInteger>();

// Cache commonly used plugins
private FrameworkEventsPlugin eventsPlugin;
Expand Down Expand Up @@ -273,40 +275,31 @@ public void addRegisteredService(ServiceState serviceState)
{
log.debug("Add registered service [" + serviceState + "] to: " + this);

synchronized (this)
{
if (registeredServices == null)
registeredServices = new CopyOnWriteArrayList<ServiceState>();
}
registeredServices.add(serviceState);
}

public void removeRegisteredService(ServiceState serviceState)
{
log.debug("Remove registered service [" + serviceState + "] from: " + this);

if (registeredServices != null)
registeredServices.remove(serviceState);
registeredServices.remove(serviceState);
}

public List<ServiceState> getRegisteredServicesInternal()
{
if (registeredServices == null)
return Collections.emptyList();

return Collections.unmodifiableList(registeredServices);
}

@Override
public ServiceReference[] getRegisteredServices()
{
assertNotUninstalled();
List<ServiceState> registeredServices = getRegisteredServicesInternal();
if (registeredServices.isEmpty())
List<ServiceState> rs = getRegisteredServicesInternal();
if (rs.isEmpty())
return null;

List<ServiceReference> srefs = new ArrayList<ServiceReference>();
for (ServiceState serviceState : registeredServices)
for (ServiceState serviceState : rs)
srefs.add(serviceState.getReference());

return srefs.toArray(new ServiceReference[srefs.size()]);
Expand All @@ -316,44 +309,28 @@ public void addServiceInUse(ServiceState serviceState)
{
log.debug("Add service in use [" + serviceState + "] to: " + this);

AtomicInteger count;
synchronized (this)
{
if (usedServices == null)
usedServices = new ConcurrentHashMap<ServiceState, AtomicInteger>();

count = usedServices.get(serviceState);
if (count == null)
usedServices.put(serviceState, count = new AtomicInteger());
}
usedServices.putIfAbsent(serviceState, new AtomicInteger());
AtomicInteger count = usedServices.get(serviceState);
count.incrementAndGet();
}

public int removeServiceInUse(ServiceState serviceState)
{
log.debug("Remove service in use [" + serviceState + "] from: " + this);

AtomicInteger count;
synchronized (this)
{
if (usedServices == null)
return -1;
AtomicInteger count = usedServices.get(serviceState);
if (count == null)
return -1;

count = usedServices.get(serviceState);
if (count == null)
return -1;
int countVal = count.decrementAndGet();
if (countVal == 0)
usedServices.remove(serviceState);

if (count.decrementAndGet() == 0)
usedServices.remove(serviceState);
}
return count.get();
return countVal;
}

public Set<ServiceState> getServicesInUseInternal()
{
if (usedServices == null)
return Collections.emptySet();

return Collections.unmodifiableSet(usedServices.keySet());
}

Expand Down

0 comments on commit 290a16f

Please sign in to comment.