Heiko previously enquired on this list whether bind/unbind can be
called concurrently with activate/deactivate for a DS service
reference with the dynamic policy. Subsequently bug #35 was opened in
order for this point to be clarified in the spec.

I would like to further clarify whether bind/unbind can be called
concurrently with other invocations of bind/unbind.

I am trying to avoid synchronized blocks in my bind/unbind methods and
in my service methods. It seems that I can use a volatile field to
store the service reference provided by the bind method to ensure
visibility when the service method is called, so long as I copy the
field value into a local variable before null checking it and using it
(to avoid check-then-act problems). However, Section 112.5.10 says
that during service replacement, the bind of the new service will
happen before the unbind of the old service. Therefore in my unbind I
must write:

protected void unbindFoo(FooService foo) {
    if(this.foo == foo) this.foo = null;
}

However this is a check-then-act. It would fail if SCR was able to
call bindFoo between the check (this.foo == foo) and the act (this.foo
= null). However it would be safe if SCR was not allowed to call
bind/unbind concurrently with each other.

If these methods can be called concurrently then it seems the only
safe way to use a dynamic reference is either to synchronize around
it, or store it in an AtomicReference. In the latter case I can
perform the check-then-act atomically with compareAndSet.

Regards,
Neil
_______________________________________________
OSGi Developer Mail List
[email protected]
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to