public barrier functions



Hi,

Is there a specific reasons why the barrier functions implemented by
gatomic.c and gatomic.h are not exported APIs?

I'd like to avoid locking in some situations where these memory barrier
instructions would come handy.

One thread:
  ptr = NULL

Other thread:
  void *my_ptr = ptr;

  if (ptr)
    {
    }

Of course this would be a race condition if I was trying to use the
pointer, but if I add reference counting like this:

One thread:
  loc_ptr = ptr;
  ptr = NULL;
  g_data_unref(loc_ptr);

Other thread:
  void *my_ptr = g_data_ref(ptr);

  if (my_ptr)
    {
    }

Unless I miss something this should work, provided:
  1) provided g_data_ref handles NULL pointers
  2) the reference count of ptr itself is manipilated with atomic
operations.
  3) the CPU ensures proper read/write memory ordering

Now this is not true on some non-x86 CPUs in which case I'd need
something like:

One thread:
  loc_ptr = ptr;
  ptr = NULL;
  wmb();
  g_data_unref(loc_ptr);

Other thread:
  void *my_ptr = g_data_ref(ptr);

  if (my_ptr)
    {
    }

Now the question is why the memory barrier macros are hidden in the
gatomic module and not exported.

And while I am at it, would it be possible to change the atomic
operations to inline functions? I'd think it is much better inline
single-instruction functions as otherwise the call overhead is too
great.

Thanks in advance,

-- 
Bazsi




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