Boost logo

Boost :

From: Pavel Vasiliev (pavel_at_[hidden])
Date: 2003-02-11 11:12:12


Alexander Terekhov wrote:

[...]

> void release_strong() {
> int status = pthread_refcount_decrement(&strong_count);
> if (PTHREAD_REFCOUNT_DROPPED_TO_ZERO == status) {
> destruct_object();
> release_weak();
> }
> }

> bool acquire_strong_from_weak() {
> int status = pthread_refcount_enroll_one(&strong_count); // _many( &refcount, 1);
> if (PTHREAD_REFCOUNT_DROPPED_TO_ZERO == status) // Ouch, did not work [too late].
> return false;
> return true;
> }

Interestingly. pthread_refcount_enroll_one() in your implementation
really eliminates need in any explicit locking.

Your code showed also that I overlooked the more simple way to deal
with weak_count. To satisfy aesthetic feelings I place here the
corrected version of my previous code.

Pavel

-----
// If atomic_decrement() sets read memory barrier when returns 0,
// there is no more need to care about cache coherency
// (from http://www.boost.org/boost/detail/atomic_count.hpp ).

class weak_ref_control_block
{
  ...
weak_ref_control_block() : strong_count(1), weak_count(1) {}

void strong_refs_lost()
{
  {
  scope_lock lock(mutex_destruct);
  if(atomic_query(&strong_count) != 0)
    return;

  // removed acquire_weak()
  atomic_set(&strong_count, atomic_int_type_MIN);
  }

  destruct_allocated();
  release_weak();
}

void release_weak()
{
  if(atomic_decrement(&weak_count) == 0)
    destruct_self();
}

}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk