[systemd-devel] question about sd_bus_slot_unref()

Lennart Poettering lennart at poettering.net
Tue Sep 1 02:45:09 PDT 2015


On Tue, 01.09.15 12:56, eshark (eshark77 at 163.com) wrote:

> 
> Hi, all
> 
> 
>     I wonder why  the sd_bus_slot_unref() always returns NULL?  I expect that I can check whether the slot->n_ref ==0 by checking its return value.
> That is
> 
> 
>     _public_ sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot) {
> 
>         if (!slot)
>                 return NULL;
> 
>         assert(slot->n_ref > 0);
> 
>         if (slot->n_ref > 1) {
>                 slot->n_ref --;
> 
>                 //return NULL;
>                retrun slot;       
>       }
> 
>         bus_slot_disconnect(slot);
>         free(slot->description);
>         free(slot);
> 
>         return NULL;
> 
> }
> 
> 
>  As the libsystem doesn't provide some API for the application to get the n_ref from the sd_bus_slot,  maybe we can provide the same feature this way?

As Jakub already pointed out: the idea is that after unreffing your
reference it is gone, and hence should be invalidated to NULL. Thus,
if you want to get rid of a reference you do this:
   
   foo = sd_bus_slot_unref(foo);

Or to say this differently: sd_bus_slot_ref() and sd_bus_slot_unref()
always return the the ref you passed in. In sd_bus_slot_ref() it is
valid afterwards, hence we return the same value as passed
in. However, in sd_bus_slot_unref() that specific ref is not valid
anymore, hence we return it as NULL.

What would be the usecase for querying the current ref counter?

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list