Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52238 - in trunk: boost/flyweight libs/flyweight/doc
From: joaquin_at_[hidden]
Date: 2009-04-07 15:36:14


Author: joaquin
Date: 2009-04-07 15:36:13 EDT (Tue, 07 Apr 2009)
New Revision: 52238
URL: http://svn.boost.org/trac/boost/changeset/52238

Log:
fixed a thread safety bug in refcounted
Text files modified:
   trunk/boost/flyweight/refcounted.hpp | 55 +++++++++++++++++++++++++++++++--------
   trunk/libs/flyweight/doc/acknowledgements.html | 11 ++++++-
   trunk/libs/flyweight/doc/release_notes.html | 16 ++++++++++-
   3 files changed, 66 insertions(+), 16 deletions(-)

Modified: trunk/boost/flyweight/refcounted.hpp
==============================================================================
--- trunk/boost/flyweight/refcounted.hpp (original)
+++ trunk/boost/flyweight/refcounted.hpp 2009-04-07 15:36:13 EDT (Tue, 07 Apr 2009)
@@ -1,4 +1,4 @@
-/* Copyright 2006-2008 Joaquin M Lopez Munoz.
+/* Copyright 2006-2009 Joaquin M Lopez Munoz.
  * Distributed under the Boost Software License, Version 1.0.
  * (See accompanying file LICENSE_1_0.txt or copy at
  * http://www.boost.org/LICENSE_1_0.txt)
@@ -22,9 +22,23 @@
 #include <boost/flyweight/tracking_tag.hpp>
 #include <boost/utility/swap.hpp>
 
-/* Refcounting tracking policy: values have an embedded ref count,
- * when this goes down to zero the element is erased from the
- * factory.
+/* Refcounting tracking policy.
+ * The implementation deserves some explanation; values are equipped with a
+ * reference count with the following semantics:
+ * - 0: newly created value.
+ * - n: (n-1) active references to the value.
+ * When the number of references reaches zero, the value can be erased. The
+ * exact protocol, however, is a little more complicated to avoid data races
+ * like the following:
+ * - Thread A detaches the last reference to x and is preempted.
+ * - Thread B looks for x, finds it and attaches a reference to it.
+ * - Thread A resumes and proceeds with erasing x, leaving a dangling
+ * reference in thread B.
+ * To cope with this, values are equipped with an additional count of threads
+ * preempted during erasure. Such a preemption are detected by the preempting
+ * thread by checking whether the reference count of the object is 1 (hence
+ * the uncommon refcounting semantics distinguishing between a newly created
+ * value and a value with no active references.
  */
 
 namespace boost{
@@ -38,22 +52,22 @@
 {
 public:
   explicit refcounted_value(const Value& x_):
- x(x_),ref(0)
+ x(x_),ref(0),del_ref(0)
   {}
   
   refcounted_value(const refcounted_value& r):
- x(r.x),ref(0)
+ x(r.x),ref(0),del_ref(0)
   {}
 
   ~refcounted_value()
   {
- /* count()!=0 most likely indicates that the flyweight factory
+ /* count()>1 most likely indicates that the flyweight factory
      * has been destructed before some of the flyweight objects using
      * it. Check for static initialization order problems with this
      * flyweight type.
      */
 
- BOOST_ASSERT(count()==0);
+ BOOST_ASSERT(count()<=1);
   }
 
   refcounted_value& operator=(const refcounted_value& r)
@@ -71,12 +85,17 @@
 #endif
 
   long count()const{return ref;}
- void add_ref()const{++ref;}
- bool release()const{return (--ref==0);}
+ long add_ref()const{return ++ref;}
+ bool release()const{return (--ref==1);}
+
+ long count_deleters()const{return del_ref;}
+ void add_deleter()const{++del_ref;}
+ void release_deleter()const{--del_ref;}
 
 private:
   Value x;
   mutable boost::detail::atomic_count ref;
+ mutable long del_ref;
 };
 
 template<typename Handle,typename TrackingHelper>
@@ -85,7 +104,15 @@
 public:
   explicit refcounted_handle(const Handle& h_):h(h_)
   {
- TrackingHelper::entry(*this).add_ref();
+ switch(TrackingHelper::entry(*this).add_ref()){
+ case 1: /* newly created object, make count()==2 (1 active reference) */
+ TrackingHelper::entry(*this).add_ref();
+ break;
+ case 2: /* object was about to be erased, increment the deleter count */
+ TrackingHelper::entry(*this).add_deleter();
+ break;
+ default:break;
+ }
   }
   
   refcounted_handle(const refcounted_handle& x):h(x.h)
@@ -116,7 +143,11 @@
 private:
   static bool check_erase(const refcounted_handle& x)
   {
- return TrackingHelper::entry(x).count()==0;
+ if(TrackingHelper::entry(x).count_deleters()){
+ TrackingHelper::entry(x).release_deleter();
+ return false;
+ }
+ return true;
   }
 
   Handle h;

Modified: trunk/libs/flyweight/doc/acknowledgements.html
==============================================================================
--- trunk/libs/flyweight/doc/acknowledgements.html (original)
+++ trunk/libs/flyweight/doc/acknowledgements.html 2009-04-07 15:36:13 EDT (Tue, 07 Apr 2009)
@@ -62,6 +62,13 @@
 dire straits gentler oceans will lie.
 </p>
 
+<h2><a name="boost_1_39">Boost 1.39 release</a></h2>
+
+<p>
+Many thanks to Tim Blechmann for helping identify and solve a serious
+tread safety problem.
+</p>
+
 <hr>
 
 <div class="prev_link"><a href="release_notes.html"><img src="prev.gif" alt="release notes" border="0"><br>
@@ -75,9 +82,9 @@
 
 <br>
 
-<p>Revised December 10th 2008</p>
+<p>Revised April 7th 2009</p>
 
-<p>&copy; Copyright 2006-2008 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
+<p>&copy; Copyright 2006-2009 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
 Distributed under the Boost Software
 License, Version 1.0. (See accompanying file <a href="../../../LICENSE_1_0.txt">
 LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">

Modified: trunk/libs/flyweight/doc/release_notes.html
==============================================================================
--- trunk/libs/flyweight/doc/release_notes.html (original)
+++ trunk/libs/flyweight/doc/release_notes.html 2009-04-07 15:36:13 EDT (Tue, 07 Apr 2009)
@@ -31,9 +31,21 @@
 <h2>Contents</h2>
 
 <ul>
+ <li>Boost 1.39 release</li>
   <li>Boost 1.38 release</li>
 </ul>
 
+<h2><a name="boost_1_39">Boost 1.39 release</a></h2>
+
+<p>
+<ul>
+ <li><a name="refcounted_bug">The refcounted
+ component was not thread-safe due to an incorrect implementation and could deadlock
+ under heavy usage conditions. This problem has been corrected.</a>
+ </li>
+</ul>
+</p>
+
 <h2><a name="boost_1_38">Boost 1.38 release</a></h2>
 
 <p>
@@ -57,9 +69,9 @@
 
 <br>
 
-<p>Revised August 27th 2008</p>
+<p>Revised April 7th 2009</p>
 
-<p>&copy; Copyright 2006-2008 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
+<p>&copy; Copyright 2006-2009 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
 Distributed under the Boost Software
 License, Version 1.0. (See accompanying file <a href="../../../LICENSE_1_0.txt">
 LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk