2 Copyright (C) 2000-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "boost/shared_ptr.hpp"
24 #include "glibmm/thread.h"
33 RCUManager (T* new_rcu_value) {
34 x.m_rcu_value = new boost::shared_ptr<T> (new_rcu_value);
37 virtual ~RCUManager() { delete x.m_rcu_value; }
39 boost::shared_ptr<T> reader () const { return *((boost::shared_ptr<T> *) g_atomic_pointer_get (&x.gptr)); }
41 virtual boost::shared_ptr<T> write_copy () = 0;
42 virtual bool update (boost::shared_ptr<T> new_value) = 0;
46 boost::shared_ptr<T>* m_rcu_value;
47 mutable volatile gpointer gptr;
53 class SerializedRCUManager : public RCUManager<T>
57 SerializedRCUManager(T* new_rcu_value)
58 : RCUManager<T>(new_rcu_value)
63 boost::shared_ptr<T> write_copy ()
67 // clean out any dead wood
69 typename std::list<boost::shared_ptr<T> >::iterator i;
71 for (i = m_dead_wood.begin(); i != m_dead_wood.end(); ) {
72 if ((*i).use_count() == 1) {
73 i = m_dead_wood.erase (i);
81 current_write_old = RCUManager<T>::x.m_rcu_value;
83 boost::shared_ptr<T> new_copy (new T(**current_write_old));
88 bool update (boost::shared_ptr<T> new_value)
90 // we hold the lock at this point effectively blocking
93 boost::shared_ptr<T>* new_spp = new boost::shared_ptr<T> (new_value);
95 // update, checking that nobody beat us to it
97 bool ret = g_atomic_pointer_compare_and_exchange (&RCUManager<T>::x.gptr,
98 (gpointer) current_write_old,
103 // successful update : put the old value into dead_wood,
105 m_dead_wood.push_back (*current_write_old);
107 // now delete it - this gets rid of the shared_ptr<T> but
108 // because dead_wood contains another shared_ptr<T> that
109 // references the same T, the underlying object lives on
111 delete current_write_old;
120 Glib::Mutex::Lock lm (m_lock);
121 m_dead_wood.clear ();
126 boost::shared_ptr<T>* current_write_old;
127 std::list<boost::shared_ptr<T> > m_dead_wood;
135 RCUWriter(RCUManager<T>& manager)
138 m_copy = m_manager.write_copy();
143 // we can check here that the refcount of m_copy is 1
145 if(m_copy.use_count() == 1) {
146 m_manager.update(m_copy);
154 // or operator boost::shared_ptr<T> ();
155 boost::shared_ptr<T> get_copy() { return m_copy; }
159 RCUManager<T>& m_manager;
161 // preferably this holds a pointer to T
162 boost::shared_ptr<T> m_copy;
165 #endif /* __pbd_rcu_h__ */