I really like RCU (read-copy-update) in user space and trying to simulate one via tr1 :: shared_ptr, here is the code, while I'm really new to parallel programming, do some experts help me revise?
The basic idea is that the reader calls get_reading_copy () to get a pointer to the current protected data (let's say this is generation one or G1). the writer calls get_updating_copy () to get a copy of G1 (say, G2), and only one author is allowed to enter the critical section. After the update is completed, the author calls update () to perform the exchange, and do m_data_ptr pointing to the G2 data. Current readers and the writer now hold shared_ptr (s) G1, and either the reader or author will eventually release the G1 data.
Any new readers will receive a pointer to G2, and the new author will receive a copy of G2 (say, G3). Perhaps G1 has not yet been released, so several generations of data can coexist.
template <typename T> class rcu_protected { public: typedef T type; typedef const T const_type; typedef std::tr1::shared_ptr<type> rcu_pointer; typedef std::tr1::shared_ptr<const_type> rcu_const_pointer; rcu_protected() : m_is_writing(0), m_is_swapping(0), m_data_ptr (new type()) {} rcu_const_pointer get_reading_copy () { spin_until_eq (m_is_swapping, 0); return m_data_ptr; } rcu_pointer get_updating_copy () { spin_until_eq (m_is_swapping, 0); while (!CAS (m_is_writing, 0, 1)) {} rcu_pointer new_data_ptr(new type(*m_data_ptr));
source share