2 * Copyright (C) 2000-2013 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2009 David Robillard <d@drobilla.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "boost/shared_ptr.hpp"
24 #include "glibmm/threads.h"
28 #include "pbd/libpbd_visibility.h"
31 * Define a set of classes to implement Read-Copy-Update. We do not attempt to define RCU here - use google.
33 * The design consists of two parts: an RCUManager and an RCUWriter.
36 /** An RCUManager is an object which takes over management of a pointer to another object.
38 * It provides three key methods:
40 * - reader() : obtains a shared pointer to the managed object that may be used for reading, without synchronization
41 * - write_copy() : obtains a shared pointer to the object that may be used for writing/modification
42 * - update() : accepts a shared pointer to a (presumed) modified instance of the object and causes all
43 * future reader() and write_copy() calls to use that instance.
45 * Any existing users of the value returned by reader() can continue to use their copy even as a write_copy()/update() takes place.
46 * The RCU manager will manage the various instances of "the managed object" in a way that is transparent to users of the manager
50 class /*LIBPBD_API*/ RCUManager
54 RCUManager (T* new_rcu_value) {
55 x.m_rcu_value = new boost::shared_ptr<T> (new_rcu_value);
58 virtual ~RCUManager() { delete x.m_rcu_value; }
60 boost::shared_ptr<T> reader () const { return *((boost::shared_ptr<T> *) g_atomic_pointer_get (&x.gptr)); }
62 /* this is an abstract base class - how these are implemented depends on the assumptions
63 that one can make about the users of the RCUManager. See SerializedRCUManager below
64 for one implementation.
67 virtual boost::shared_ptr<T> write_copy () = 0;
68 virtual bool update (boost::shared_ptr<T> new_value) = 0;
71 /* ordinarily this would simply be a declaration of a ptr to a shared_ptr<T>. however, the atomic
72 operations that we are using (from glib) have sufficiently strict typing that it proved hard
73 to get them to accept even a cast value of the ptr-to-shared-ptr() as the argument to get()
74 and comp_and_exchange(). Consequently, we play a litle trick here that relies on the fact
75 that sizeof(A*) == sizeof(B*) no matter what the types of A and B are. for most purposes
76 we will use x.m_rcu_value, but when we need to use an atomic op, we use x.gptr. Both expressions
77 evaluate to the same address.
81 boost::shared_ptr<T>* m_rcu_value;
82 mutable volatile gpointer gptr;
87 /** Serialized RCUManager implements the RCUManager interface. It is based on the
88 following key assumption: among its users we have readers that are bound by
89 RT time constraints, and writers who are not. Therefore, we do not care how
90 slow the write_copy()/update() operations are, or what synchronization
93 Because of this design assumption, this class will serialize all
94 writers. That is, objects calling write_copy()/update() will be serialized by
95 a mutex. Only a single writer may be in the middle of write_copy()/update();
96 all other writers will block until the first has finished. The order of
97 execution of multiple writers if more than one is blocked in this way is
100 The class maintains a lock-protected "dead wood" list of old value of
101 *m_rcu_value (i.e. shared_ptr<T>). The list is cleaned up every time we call
102 write_copy(). If the list is the last instance of a shared_ptr<T> that
103 references the object (determined by shared_ptr::unique()) then we
104 erase it from the list, thus deleting the object it points to. This is lazy
105 destruction - the SerializedRCUManager assumes that there will sufficient
106 calls to write_copy() to ensure that we do not inadvertently leave objects
107 around for excessive periods of time.
109 For extremely well defined circumstances (i.e. it is known that there are no
110 other writer objects in existence), SerializedRCUManager also provides a
111 flush() method that will unconditionally clear out the "dead wood" list. It
112 must be used with significant caution, although the use of shared_ptr<T>
113 means that no actual objects will be deleted incorrectly if this is misused.
116 class /*LIBPBD_API*/ SerializedRCUManager : public RCUManager<T>
120 SerializedRCUManager(T* new_rcu_value)
121 : RCUManager<T>(new_rcu_value)
125 boost::shared_ptr<T> write_copy ()
129 // clean out any dead wood
131 typename std::list<boost::shared_ptr<T> >::iterator i;
133 for (i = m_dead_wood.begin(); i != m_dead_wood.end(); ) {
135 i = m_dead_wood.erase (i);
141 /* store the current so that we can do compare and exchange
142 when someone calls update(). Notice that we hold
143 a lock, so this store of m_rcu_value is atomic.
146 current_write_old = RCUManager<T>::x.m_rcu_value;
148 boost::shared_ptr<T> new_copy (new T(**current_write_old));
152 /* notice that the write lock is still held: update() MUST
153 be called or we will cause another writer to stall.
157 bool update (boost::shared_ptr<T> new_value)
159 /* we still hold the write lock - other writers are locked out */
161 boost::shared_ptr<T>* new_spp = new boost::shared_ptr<T> (new_value);
163 /* update, by atomic compare&swap. Only succeeds if the old
164 value has not been changed.
166 XXX but how could it? we hold the freakin' lock!
169 bool ret = g_atomic_pointer_compare_and_exchange (&RCUManager<T>::x.gptr,
170 (gpointer) current_write_old,
175 // successful update : put the old value into dead_wood,
177 m_dead_wood.push_back (*current_write_old);
179 // now delete it - this gets rid of the shared_ptr<T> but
180 // because dead_wood contains another shared_ptr<T> that
181 // references the same T, the underlying object lives on
183 delete current_write_old;
186 /* unlock, allowing other writers to proceed */
194 Glib::Threads::Mutex::Lock lm (m_lock);
195 m_dead_wood.clear ();
199 Glib::Threads::Mutex m_lock;
200 boost::shared_ptr<T>* current_write_old;
201 std::list<boost::shared_ptr<T> > m_dead_wood;
204 /** RCUWriter is a convenience object that implements write_copy/update via
205 lifetime management. Creating the object obtains a writable copy, which can
206 be obtained via the get_copy() method; deleting the object will update
207 the manager's copy. Code doing a write/update thus looks like:
211 RCUWriter writer (object_manager);
212 boost::shared_ptr<T> copy = writer.get_copy();
215 } <= writer goes out of scope, update invoked
219 class /*LIBPBD_API*/ RCUWriter
223 RCUWriter(RCUManager<T>& manager)
224 : m_manager(manager) {
225 m_copy = m_manager.write_copy();
229 if (m_copy.unique()) {
230 /* As intended, our copy is the only reference
231 to the object pointed to by m_copy. Update
232 the manager with the (presumed) modified
235 m_manager.update(m_copy);
237 /* This means that some other object is using our copy
238 of the object. This can only happen if the scope in
239 which this RCUWriter exists passed it to a function
240 that created a persistent reference to it, since the
241 copy was private to this particular RCUWriter. Doing
242 so will not actually break anything but it violates
243 the design intention here and so we do not bother to
244 update the manager's copy.
246 XXX should we print a warning about this?
252 boost::shared_ptr<T> get_copy() const { return m_copy; }
255 RCUManager<T>& m_manager;
256 boost::shared_ptr<T> m_copy;
259 #endif /* __pbd_rcu_h__ */