Re: questions about g_object_unref.



On Sun, 1 Jun 2008, Yu Feng wrote:

Hi all, not sure if it is appropriate here, but I don't quite understand
the code in g_object_unref:

glib-2.16.1/gobject/gobject.c: line:1763
 /* here we want to atomically do: if (ref_count>1) { ref_count--;
return; } */
retry_atomic_decrement1:
 old_ref = g_atomic_int_get (&object->ref_count);
 if (old_ref > 1)
   {
     if (!g_atomic_int_compare_and_exchange (&object->ref_count,
old_ref, old_ref - 1))
   goto retry_atomic_decrement1;


How does the code achieve the goal stated in the comments? It seems to
me that the code will loop at retry_atomic_decrement1 untill old_ref ==
1?

no, it loops until object->ref_count == old_ref-1, which implements
the functionality outlined in the comment. you possibly want to read
up on g_atomic_int_compare_and_exchange() and maybe CAS (compare-and-swap)
algorithms in general.

Yu

---
ciaoTJ


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]