This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Fw: [patch] Make std::tr1::shared_ptr thread-safe.


On Apr 1, 2005 2:05 AM, Alexander Terekhov <alexander.terekhov@gmail.com> wrote:
[...]
> > Hmm. Now comes Whitehead (mutex inside lock())..
> >
> > Without "real" fence in lock(), I don't see how to make it safe with
> > relaxed msync protocol for self_count_.decrement() in weak_release().

Empty lock/unlock in weak_release() will take care of 

  thread A:

    weak_p1.add_ref_lock(); // Whitehead
    weak_p1.drop_reference();

  thread B:

    weak_p2.drop_reference();

  (when both weak_p1 and weak_p2 point to the same "expired" object) 

and...

> 
> Mutex dtor is another issue. 

...it will also take care of that issue.

So here is Peter's stage one version ("no more broken than the rest of 
the library"... "will work *provided that __exchange_and_add imposes at 
least the ordering that is required for reference-counted immutable 
objects to work*"):

void add_ref_lock() 
{
  pthread_mutex_lock( &_M_mutex );
  // ...  Whitehead  ...
  pthread_mutex_unlock( &_M_mutex );
}

void release() // nothrow
{
  if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
  {
    dispose();
    pthread_mutex_lock( &_M_mutex );
    pthread_mutex_unlock( &_M_mutex );
    weak_release();
  }
}

void weak_release() // nothrow
{
  if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
  {
    pthread_mutex_lock( &_M_mutex );
    pthread_mutex_unlock( &_M_mutex );
    destroy();
  }
}

regards,
alexander.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]