$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r58875 - in sandbox/stm/branches/vbe/libs/stm: example/tx test
From: vicente.botet_at_[hidden]
Date: 2010-01-10 11:46:37
Author: viboes
Date: 2010-01-10 11:46:36 EST (Sun, 10 Jan 2010)
New Revision: 58875
URL: http://svn.boost.org/trac/boost/changeset/58875
Log:
TBoost.STM vbe:
* add example of coarse grained list using smart pointers 
* make list.cpp example more readable
Added:
   sandbox/stm/branches/vbe/libs/stm/example/tx/list_sp.cpp   (contents, props changed)
Text files modified: 
   sandbox/stm/branches/vbe/libs/stm/example/tx/list.cpp |    32 +++++++++++++++-----------------        
   sandbox/stm/branches/vbe/libs/stm/test/Jamfile.v2     |    40 ++++++++++++++++++++++++++++++----------
   2 files changed, 45 insertions(+), 27 deletions(-)
Modified: sandbox/stm/branches/vbe/libs/stm/example/tx/list.cpp
==============================================================================
--- sandbox/stm/branches/vbe/libs/stm/example/tx/list.cpp	(original)
+++ sandbox/stm/branches/vbe/libs/stm/example/tx/list.cpp	2010-01-10 11:46:36 EST (Sun, 10 Jan 2010)
@@ -83,7 +83,7 @@
 
     std::size_t size() const {
         BOOST_STM_ATOMIC(_) {
-            BOOST_STM_RETURN(size_);
+            BOOST_STM_TX_RETURN(_, size_);
         }  BOOST_STM_END_ATOMIC
         return 0;
     }
@@ -92,25 +92,23 @@
     // find the location to insert the node. if the value already exists, fail
     //--------------------------------------------------------------------------
     void insert(const T& val) {
-        //cerr << __LINE__ << " * insert" << endl;
         BOOST_STM_ATOMIC(_) {
-            list_node<T> const * prev = head_;
-            list_node<T> const * curr = prev->next_;
+            list_node<T> * prev = head_;
+            list_node<T> * curr = prev->next_;
             while (curr!=0) {
-                if (curr->value_ == val) return;
+                if (curr->value_ == val) BOOST_STM_TX_RETURN_NOTHING(_);
                 else if (curr->value_ > val) break;
                 prev = curr;
-                curr = curr->next_.value();
+                curr = curr->next_;
             }
             if (curr==0 || (curr->value_ > val)) {
-                const_cast<list_node<T> *>(prev)->next_=BOOST_STM_TX_NEW_PTR(_,list_node<T>(val, const_cast<list_node<T> *>(curr)));
+                prev->next_=BOOST_STM_TX_NEW_PTR(_,list_node<T>(val, curr));
                 ++size_;
             }
         } BOOST_STM_END_ATOMIC
         catch (...) {
-        cerr << __LINE__ << " * insert" << endl;
+            cerr << __LINE__ << " * insert" << endl;
         }
-        //cerr << __LINE__ << " * insert" << endl;
    }
 
     // search function
@@ -119,7 +117,7 @@
             list_node<T> const * curr=head_->next_;
             while (curr) {
                 if (curr->value_ >= val) break;
-                curr = curr->next_.value();
+                curr = curr->next_;
             }
             BOOST_STM_RETURN((curr) && (curr->value_ == val));
         }  BOOST_STM_END_ATOMIC
@@ -131,12 +129,12 @@
     {
         BOOST_STM_ATOMIC(_) {
             // find the node whose val matches the request
-            list_node<T> const * prev=head_;
-            list_node<T> const * curr=prev->next_;
+            list_node<T> * prev=head_;
+            list_node<T> * curr=prev->next_;
             while (curr) {
                 // if we find the node, disconnect it and end the search
                 if (curr->value_ == val) {
-                    const_cast<list_node<T> *>(prev)->next_=curr->next_;
+                    prev->next_=curr->next_;
                     // delete curr...
                     BOOST_STM_TX_DELETE_PTR(_,curr);
                     --size_;
@@ -240,7 +238,7 @@
         //cerr << __LINE__ << " * n3" << endl;
     BOOST_STM_ATOMIC(_) {
         test::list_node<int>* prev =&n;
-        test::list_node<int>* curr =prev->next_.value();
+        test::list_node<int>* curr =prev->next_;
         int val = 10;
         prev->next_=BOOST_STM_TX_NEW_PTR(_,test::list_node<int>(val, curr));
     } BOOST_STM_END_ATOMIC
@@ -253,10 +251,10 @@
 int test_all() {
     //create();
     bool fails=false;
-    //fails= fails || !n1();
-    //fails= fails || !n2();
+    fails= fails || !n1();
+    fails= fails || !n2();
     fails= fails || !n3();
-    //fails= fails || !check_size(0);
+    fails= fails || !check_size(0);
     //fails= fails || !insert1();
     thread  th1(insert1_th);
     thread  th2(insert2_th);
Added: sandbox/stm/branches/vbe/libs/stm/example/tx/list_sp.cpp
==============================================================================
--- (empty file)
+++ sandbox/stm/branches/vbe/libs/stm/example/tx/list_sp.cpp	2010-01-10 11:46:36 EST (Sun, 10 Jan 2010)
@@ -0,0 +1,278 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Justin E. Gottchlich 2009.
+// (C) Copyright Vicente J. Botet Escriba 2009.
+// 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)
+//
+// See http://www.boost.org/libs/stm for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include <boost/stm.hpp>
+#include <boost/thread.hpp>
+#include <stdlib.h>
+
+using namespace std;
+using namespace boost;
+using namespace boost::stm;
+
+//--------------------------------------------------------------------------
+
+namespace test {
+template <typename T>
+class list_node : public transaction_object<list_node<T> >
+{
+public:
+
+    list_node() : value_(), next_()
+    {
+    }
+    explicit list_node(T const &rhs)
+        : value_(rhs), next_(0) {}
+    list_node(T const &rhs, list_node<T>* next)
+        : value_(rhs), next_(next) {}
+
+    // zero initialization for native types
+    void clear() { value_ = T(); next_ = 0; }
+
+    T value_;
+    list_node<T> * next_;
+
+};
+
+template <typename OSTREAM, typename T>
+OSTREAM& operator<<(OSTREAM& os, list_node<T>& v) {
+    os << v.value_;
+    return os;
+}
+
+//--------------------------------------------------------------------------
+template <typename T>
+class list : public transaction_object<list<T> >
+{
+    list_node<T> head_storage;
+    list_node<T>* head_;
+    std::size_t size_;
+
+public:
+    list()
+    : head_(BOOST_STM_NEW_PTR(list_node<T>()))
+    , size_(0)
+    {}
+
+    ~list() {}
+
+    std::size_t size() const {
+        BOOST_STM_ATOMIC(_) {
+            BOOST_STM_TX_RETURN(_, size_);
+        }  BOOST_STM_END_ATOMIC
+        return 0;
+    }
+
+    //--------------------------------------------------------------------------
+    // find the location to insert the node. if the value already exists, fail
+    //--------------------------------------------------------------------------
+    void insert(const T& val) {
+        BOOST_STM_ATOMIC(_) {
+            read_ptr<list_node<T> > prev(_, head_);
+            read_ptr<list_node<T> > curr(_, prev->next_);
+            while (curr!=0) {
+                if (curr->value_ == val) BOOST_STM_TX_RETURN_NOTHING(_);
+                else if (curr->value_ > val) break;
+                prev = curr;
+                curr = curr->next_;
+            }
+            if (curr==0 || (curr->value_ > val)) {
+                make_write_ptr(prev)->next_=BOOST_STM_TX_NEW_PTR(_,list_node<T>(val, curr.get()));
+                ++(make_write_ptr(_, this)->size_);
+            }
+        } BOOST_STM_END_ATOMIC
+        catch (...) {
+        cout << __LINE__ << " * insert" << endl;
+        }
+    }
+
+    // search function
+    bool lookup(const T& val) const {
+        BOOST_STM_ATOMIC(_) {
+            read_ptr<list_node<T> > curr(_, head_->next_);
+            while (curr) {
+                if (curr->value_ >= val) break;
+                curr = curr->next_;
+            }
+            BOOST_STM_RETURN((curr) && (curr->value_ == val));
+        }  BOOST_STM_END_ATOMIC
+        return false;
+    }
+
+    // remove a node if its value == val
+    void remove(const T& val)
+    {
+        BOOST_STM_ATOMIC(_) {
+            // find the node whose val matches the request
+            read_ptr<list_node<T> > prev(_, head_);
+            read_ptr<list_node<T> > curr(_, prev->next_);
+            while (curr) {
+                // if we find the node, disconnect it and end the search
+                if (curr->value_ == val) {
+                    make_write_ptr(prev)->next_=curr->next_;
+                    // delete curr...
+                    BOOST_STM_TX_DELETE_PTR(_,curr);
+                    //--size_;
+                    --(make_write_ptr(_, this)->size_);
+                    //write_ptr<list<T> > that(_, this);
+                    //++(that->size_);
+                    break;
+                } else if (curr->value_ > val) {
+                    cout << __LINE__ << " * remove not found" << endl;
+                    break;
+                }
+                prev = curr;
+                curr = prev->next_;
+            }
+        }  BOOST_STM_END_ATOMIC
+    }
+
+};
+}
+//--------------------------------------------------------------------------
+
+test::list<int> l;
+test::list_node<int> n;
+
+void create() {
+    BOOST_STM_ATOMIC(_) {
+        cout << __LINE__ << " create" << endl;
+        cout << " create size " << l.size() << endl;
+    } BOOST_STM_END_ATOMIC
+    catch (...) {
+        cout << "aborted" << endl;
+    }
+}
+bool check_size(std::size_t val) {
+    //BOOST_STM_ATOMIC(_) {
+        BOOST_STM_RETURN(l.size()==val);
+    //} BOOST_STM_END_ATOMIC
+    return false;
+}
+bool check_lookup(int val) {
+    //BOOST_STM_ATOMIC(_) {
+        BOOST_STM_RETURN(l.lookup(val));
+    //} BOOST_STM_END_ATOMIC
+    return false;
+}
+
+bool insert1() {
+    //thread_initializer thi;
+    BOOST_STM_ATOMIC(_) {
+        int val = 10;
+        l.insert(val);
+    }  BOOST_STM_END_ATOMIC
+    return check_size(1);
+}
+void insert1_th() {
+    thread_initializer thi;
+    BOOST_STM_ATOMIC(_) {
+        l.insert(1);
+    }  BOOST_STM_END_ATOMIC
+}
+void insert2_th() {
+    thread_initializer thi;
+    BOOST_STM_ATOMIC(_) {
+        l.insert(2);
+    } BOOST_STM_END_ATOMIC
+}
+
+void remove2() {
+    //thread_initializer thi;
+    BOOST_STM_ATOMIC(_) {
+        l.remove(2);
+    } BOOST_STM_END_ATOMIC
+}
+
+void insert3_th() {
+    thread_initializer thi;
+    BOOST_STM_ATOMIC(_) {
+        l.insert(3);
+    } BOOST_STM_END_ATOMIC
+}
+bool n1() {
+    BOOST_STM_ATOMIC(_) {
+        int val = 10;
+        n.next_=BOOST_STM_TX_NEW_PTR(_,test::list_node<int>(val, 0));
+    } BOOST_STM_END_ATOMIC
+    BOOST_STM_ATOMIC(_) {
+        BOOST_STM_RETURN(n.next_->value_==10);
+    } BOOST_STM_END_ATOMIC
+    return false;
+}
+
+bool n2() {
+    BOOST_STM_ATOMIC(_) {
+        n.next_->value_=12;
+    } BOOST_STM_END_ATOMIC
+    BOOST_STM_ATOMIC(_) {
+        BOOST_STM_RETURN(n.next_->value_==12);
+    } BOOST_STM_END_ATOMIC
+    return false;
+}
+
+bool n3() {
+        //cout << __LINE__ << " * n3" << endl;
+    BOOST_STM_ATOMIC(_) {
+        test::list_node<int>* prev =&n;
+        test::list_node<int>* curr =prev->next_;
+        int val = 10;
+        prev->next_=BOOST_STM_TX_NEW_PTR(_,test::list_node<int>(val, curr));
+    } BOOST_STM_END_ATOMIC
+    BOOST_STM_ATOMIC(_) {
+        BOOST_STM_RETURN(n.next_->value_==10);
+    } BOOST_STM_END_ATOMIC
+    return false;
+}
+
+int test_all() {
+    //create();
+    bool fails=false;
+    fails= fails || !n1();
+    fails= fails || !n2();
+    fails= fails || !n3();
+    fails= fails || !check_size(0);
+    //fails= fails || !insert1();
+#if 1
+    thread  th1(insert1_th);
+    thread  th2(insert2_th);
+    thread  th3(insert2_th);
+    thread  th4(insert3_th);
+        cout << __LINE__ << " * test_all" << endl;
+
+    th1.join();
+    th2.join();
+    th3.join();
+    th4.join();
+        cout << __LINE__ << " * test_all" << endl;
+    fails= fails || !check_lookup(1);
+    fails= fails || !check_lookup(2);
+    fails= fails || !check_lookup(3);
+    fails= fails || !check_size(3);
+    remove2();
+    fails= fails || !check_lookup(1);
+    fails= fails || check_lookup(2);
+    fails= fails || !check_lookup(3);
+    fails= fails || !check_size(2);
+#endif
+    return fails;
+}
+
+int main() {
+    transaction::enable_dynamic_priority_assignment();
+    transaction::do_deferred_updating();
+    transaction::initialize();
+    thread_initializer thi;
+
+    return test_all();
+
+}
Modified: sandbox/stm/branches/vbe/libs/stm/test/Jamfile.v2
==============================================================================
--- sandbox/stm/branches/vbe/libs/stm/test/Jamfile.v2	(original)
+++ sandbox/stm/branches/vbe/libs/stm/test/Jamfile.v2	2010-01-10 11:46:36 EST (Sun, 10 Jan 2010)
@@ -186,29 +186,49 @@
             [ run stm : -bench list_hash_w_locks -dir -threads 4 -inserts 100 -latm tx : : : list_hash_w_locks_dir_tx_t4_i100 ]
     ;
 
-    alias examples
+    alias examples_tx
         :
-            [ run ../example/bank.cpp ]
+            [ compile-fail ../example/deep_singleton.cpp ]
+            [ run ../example/singleton.cpp ]
+            [ run ../example/dyn_poly.cpp ]
+            
             [ run ../example/tx/bank.cpp : : : : tx_bank ]
+            [ run ../example/tx/numeric.cpp ]
+            [ run ../example/tx/array.cpp ]
+            [ run ../example/tx/pointer.cpp ]
+            [ run ../example/tx/list.cpp : : : : tx_list ]
+            [ run ../example/tx/list_sp.cpp : : : : tx_list_sp ]
+    ;
+
+    alias examples_txw
+        :
+            [ link ../example/bank.cpp ]
 
             ########### fails
             # /bin/sh: line 4:  2656 Segmentation fault      (core dumped) "bin/list.test/gcc-3.4.4/debug/threading-multi/list.exe" > "bin/list.test/gcc-3.4.4/debug/threading-multi/list.output" 2>&1
-            #[ link ../example/list.cpp ]
-            #[ run ../example/counter.cpp ]
+            [ link ../example/list.cpp ]
+            [ link ../example/counter.cpp ]
             # fails sometimes
             # assertion "res==0" failed: file "../../../boost/synchro/pthread/mutex.hpp", line 52
-            [ run ../example/tx/numeric.cpp ]
-            [ run ../example/tx/array.cpp ]
-            [ run ../example/tx/pointer.cpp ]
-            [ run ../example/tx/list.cpp ]
             [ run ../example/counter_ptr.cpp ]
             # fails sometimes
             # assertion "res==0" failed: file "../../../boost/synchro/pthread/mutex.hpp", line 52
             #[ run ../example/non_tx_counter.cpp ]
-            [ compile-fail ../example/deep_singleton.cpp ]
-            [ run ../example/singleton.cpp ]
     ;
 
+    alias examples_non_tx
+        :
+            # fails sometimes
+            # assertion "res==0" failed: file "../../../boost/synchro/pthread/mutex.hpp", line 52
+            [ run ../example/non_tx_counter.cpp ]
+    ;
+
+    alias examples 
+        : examples_tx 
+          examples_txw
+        # examples_non_tx
+        ;
+
     alias def
         : def_invalid
     ;