Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Programming Threads > Re: std::msync
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 19 Topic 1373 of 4179
Post > Topic >>

Re: std::msync

by Alexander Terekhov <terekhov@[EMAIL PROTECTED] > Mar 30, 2005 at 07:24 PM

< Forward Inline http://gcc.gnu.org/ml/libstdc++/2005-03/msg00371.html>

-------- Original Message --------
Message-ID: <e52efbe10503300728333c87d9@[EMAIL PROTECTED]
>
Subject: Re: [patch] Make std::tr1::shared_ptr thread-safe.

"Peter Dimov" <pdimov@[EMAIL PROTECTED]
> on 03/30/2005 04:36:54 PM:
>
> Alexander Terekhov wrote:
> > [...  __release/acquire_memory_barrier() ...]
> >
> >> The only reliable implementation of these barriers that I see
> >> is an empty pthread_mutex_lock/pthread_mutex_unlock pair.
> >
> > Nope. That won't work. [...]
> 
> Update:
> 
> I think that we've reached the conclusion that the following
> implementation:
> 
>  void release() // nothrow
>  {
>    if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
>    {
>      pthread_mutex_lock( &_M_mutex );
>      dispose();
>      pthread_mutex_unlock( &_M_mutex );
>      weak_release();
>    }
>  }

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();
  }
}

would also work. The key here is...

> 
>  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();
>    }
>  }

to have the same lock on both sides. Some "thread-specific" locks to 
"emulate" bidirectional fences with minimum contention won't work.

> 
> will work *provided that __exchange_and_add imposes at least the
ordering
> that is required for reference-counted immutable objects to work*.

That's msync::slb (relaxed msync::rel not affecting sinking of preceding 
stores) when result > 1 and msync::cchsb (relaxed msync::acq [cc stands
control condition hint] not affecting hoisting of subsequent loads) when 
result == 0.

No _M_mutex above is needed with more constrained version of
__exchange_and_add() providing msync::rel when result > 1 and msync::ccacq

when result == 0. Such semantics will also sup****t basic thread-safety of 
shared_ptr<> for mutable managed objects without unpleasant requirement 
for client provided synchronizing deleters.

> 
> The last point is im****tant, because it affects the other uses of
> __exchange_and_add in libstdc++.
> 
> Alexander has identified the following problematic case:
> 
> // thread A
> 
> read *p1
> p1.drop_reference()
> 
> // thread B
> 
> p2.drop_reference(); // destroys *p1
> 
> If the __exchange_and_adds hidden in drop_reference do not establish
> ordering, it is possible for *p1 to be destroyed (and the storage
> invalidated, zeroed or reused) by thread B before the read in thread A.

Yep. 

regards,
alexander.
 




 19 Posts in Topic:
std::msync
Alexander Terekhov <te  2004-09-20 11:09:20 
Re: std::msync
"SenderX" <x  2004-09-20 18:39:58 
Re: std::msync
"SenderX" <x  2004-09-20 18:42:53 
Re: std::msync
Alexander Terekhov <te  2004-11-04 23:38:31 
Re: std::msync
Alexander Terekhov <te  2005-02-18 13:36:32 
Re: std::msync
Alexander Terekhov <te  2005-03-30 19:24:51 
Re: std::msync
Alexander Terekhov <te  2005-03-31 19:25:40 
Re: std::msync
Alexander Terekhov <te  2005-04-01 00:55:52 
Re: std::msync
Alexander Terekhov <te  2005-04-01 01:11:10 
Re: std::msync
Alexander Terekhov <te  2005-04-01 13:35:46 
Re: std::msync
"Peter Dimov" &  2005-04-02 14:23:01 
Re: std::msync
Alexander Terekhov <te  2005-04-02 17:29:47 
Re: std::msync
Alexander Terekhov <te  2005-04-06 20:58:38 
Re: std::msync
Alexander Terekhov <te  2005-04-06 20:59:48 
Re: std::msync
Alexander Terekhov <te  2005-04-11 13:07:47 
Re: std::msync
"Peter Dimov" &  2005-04-11 14:48:51 
Re: std::msync
Alexander Terekhov <te  2005-04-11 15:01:58 
Re: std::msync
"Peter Dimov" &  2005-04-02 17:43:36 
Re: std::msync
Alexander Terekhov <te  2005-04-02 17:07:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 23:01:54 CST 2008.