*** NEW CODING POLICY ***
[ardour.git] / libs / pbd / pbd / rcu.h
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef __pbd_rcu_h__
21 #define __pbd_rcu_h__
22
23 #include "boost/shared_ptr.hpp"
24 #include "glibmm/thread.h"
25  
26 #include <list> 
27
28 template<class T>
29 class RCUManager
30 {
31   public:
32  
33         RCUManager (T* new_rcu_value) {
34                 x.m_rcu_value = new boost::shared_ptr<T> (new_rcu_value);
35         }
36  
37         virtual ~RCUManager() { delete x.m_rcu_value; }
38  
39         boost::shared_ptr<T> reader () const { return *((boost::shared_ptr<T> *) g_atomic_pointer_get (&x.gptr)); }
40  
41         virtual boost::shared_ptr<T> write_copy () = 0;
42         virtual bool update (boost::shared_ptr<T> new_value) = 0;
43
44   protected:
45         union {
46             boost::shared_ptr<T>* m_rcu_value;
47             mutable volatile gpointer gptr;
48         } x;
49 };
50  
51  
52 template<class T>
53 class SerializedRCUManager : public RCUManager<T>
54 {
55 public:
56  
57         SerializedRCUManager(T* new_rcu_value)
58                 : RCUManager<T>(new_rcu_value)
59         {
60  
61         }
62  
63         boost::shared_ptr<T> write_copy ()
64         {
65                 m_lock.lock();
66
67                 // clean out any dead wood
68
69                 typename std::list<boost::shared_ptr<T> >::iterator i;
70
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);
74                         } else {
75                                 ++i;
76                         }
77                 }
78
79                 // store the current 
80
81                 current_write_old = RCUManager<T>::x.m_rcu_value;
82                 
83                 boost::shared_ptr<T> new_copy (new T(**current_write_old));
84
85                 return new_copy;
86         }
87  
88         bool update (boost::shared_ptr<T> new_value)
89         {
90                 // we hold the lock at this point effectively blocking
91                 // other writers.
92
93                 boost::shared_ptr<T>* new_spp = new boost::shared_ptr<T> (new_value);
94
95                 // update, checking that nobody beat us to it
96
97                 bool ret = g_atomic_pointer_compare_and_exchange (&RCUManager<T>::x.gptr,
98                                                                   (gpointer) current_write_old,
99                                                                   (gpointer) new_spp);
100                 
101                 if (ret) {
102
103                         // successful update : put the old value into dead_wood,
104
105                         m_dead_wood.push_back (*current_write_old);
106
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
110
111                         delete current_write_old;
112                 }
113
114                 m_lock.unlock();
115
116                 return ret;
117         }
118
119         void flush () {
120                 Glib::Mutex::Lock lm (m_lock);
121                 m_dead_wood.clear ();
122         }
123  
124 private:
125         Glib::Mutex                      m_lock;
126         boost::shared_ptr<T>*            current_write_old;
127         std::list<boost::shared_ptr<T> > m_dead_wood;
128 };
129  
130 template<class T>
131 class RCUWriter
132 {
133 public:
134  
135         RCUWriter(RCUManager<T>& manager)
136                 : m_manager(manager)
137         {
138                 m_copy = m_manager.write_copy();        
139         }
140  
141         ~RCUWriter()
142         {
143                 // we can check here that the refcount of m_copy is 1
144  
145                 if(m_copy.use_count() == 1) {
146                         m_manager.update(m_copy);
147                 } else {
148  
149                         // critical error.
150                 }
151  
152         }
153  
154         // or operator boost::shared_ptr<T> ();
155         boost::shared_ptr<T> get_copy() { return m_copy; }
156  
157 private:
158  
159         RCUManager<T>& m_manager;
160  
161         // preferably this holds a pointer to T
162         boost::shared_ptr<T> m_copy;
163 };
164
165 #endif /* __pbd_rcu_h__ */