Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r51163 - sandbox/synchro/boost/synchro/conc
From: vicente.botet_at_[hidden]
Date: 2009-02-09 17:55:10


Author: viboes
Date: 2009-02-09 17:55:09 EST (Mon, 09 Feb 2009)
New Revision: 51163
URL: http://svn.boost.org/trac/boost/changeset/51163

Log:
Boost.Synchro V0.0.0
Added:
   sandbox/synchro/boost/synchro/conc/conc_tuple.hpp (contents, props changed)
   sandbox/synchro/boost/synchro/conc/concurrent_component.hpp (contents, props changed)

Added: sandbox/synchro/boost/synchro/conc/conc_tuple.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/synchro/conc/conc_tuple.hpp 2009-02-09 17:55:09 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,190 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008. 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/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_SYNCHRO_CONC_TUPLE__HPP
+#define BOOST_SYNCHRO_CONC_TUPLE__HPP
+
+#include "boost/thread.hpp"
+#include "boost/ref.hpp"
+#include "boost/bind.hpp"
+
+namespace boost { namespace synchro {
+struct none{};
+#if BOOST_HAS_VARIADIC_TMPl
+template <
+ typename S...
+>
+struct conc_tuple;
+
+template <
+ typename Head, Tail...
+>
+struct conc_tuple;
+{
+ Head& head_;
+ boost::thread thr_head_;
+ conc_tuple<Tail> tail_;
+public:
+ conc_tuple(Head& head, Tail... tail) : head_(head), tail_(...tail) {};
+ void operator()() {
+ head_();
+ tail_();
+ }
+ void join() {
+ thr_head_.join();
+ tail_.join();
+ }
+ void interrupt() {
+ thr_head_.interrupt();
+ tail_.interrupt();
+ }
+ int size() const {return 1;}
+};
+#endif
+
+//[conc_tuple
+template <
+ typename S1
+>
+struct conc_tuple_1 {
+// BOOST_CONCEPT_ASSERT((ConcurrentComponentConcept<S1>));
+ S1& s1_;
+ boost::thread thr1_;
+public:
+ conc_tuple_1(S1& s1) : s1_(s1) {};
+ void operator()() {
+ thr1_= boost::thread(boost::ref(s1_));
+ }
+ void join() {
+ thr1_.join();
+ }
+ void interrupt() {
+ thr1_.interrupt();
+ }
+ int size() const {return 1;}
+};
+
+template <
+ typename S1,
+ typename S2
+>
+struct conc_tuple_2 {
+// BOOST_CONCEPT_ASSERT((ConcurrentComponentConcept<S1>));
+ conc_tuple_1<S1> s1_;
+ conc_tuple_1<S2> s2_;
+// S1& s1_; S2& s2_;
+// boost::thread thr1_;
+// boost::thread thr2_;
+public:
+ conc_tuple_2(S1& s1, S2& s2) : s1_(s1), s2_(s2) {};
+ void operator()() {
+ s1_();
+ s2_();
+ }
+ void join() {
+ s1_.join();
+ s2_.join();
+ }
+ void interrupt() {
+ s1_.interrupt();
+ s2_.interrupt();
+ }
+ int size() const {return 2;}
+};
+template <
+ typename S1,
+ typename S2,
+ typename S3
+>
+struct conc_tuple_3 {
+ conc_tuple_1<S1> s1_;
+ conc_tuple_2<S2, S3> s23_;
+public:
+ conc_tuple_3(S1& s1, S2& s2, S3& s3) : s1_(s1), s23_(s2, s3) {};
+ void operator()() {
+ s1_();
+ s23_();
+ }
+ void join() {
+ s1_.join();
+ s23_.join();
+ }
+ void interrupt() {
+ s1_.interrupt();
+ s23_.interrupt();
+ }
+ int size() const {return 3;}
+};
+template <
+ typename S1,
+ typename S2,
+ typename S3,
+ typename S4
+>
+struct conc_tuple_4 {
+ conc_tuple_2<S1, S2> s12_;
+ conc_tuple_2<S3, S4> s34_;
+public:
+ conc_tuple_4(S1& s1, S2& s2, S3& s3, S4& s4) : s12_(s1, s2), s34_(s3, s4) {};
+ void operator()() {
+ s12_();
+ s34_();
+ }
+ void join() {
+ s12_.join();
+ s34_.join();
+ }
+ void interrupt() {
+ s12_.interrupt();
+ s34_.interrupt();
+ }
+ int size() const {return 4;}
+};
+//]
+
+template <
+ typename S1,
+ typename S2,
+ typename S3=none,
+ typename S4=none,
+ typename S5=none
+>
+struct conc_tuple;
+
+template <
+ typename S1,
+ typename S2,
+ typename S3,
+ typename S4
+>
+struct conc_tuple<S1, S2, S3, S4, none> : conc_tuple_4<S1, S2, S3, S4> {
+ conc_tuple(S1& s1, S2& s2, S3& s3, S4& s4) : conc_tuple_4<S1, S2, S3, S4>(s1, s2, s3, s4) {};
+};
+
+template <
+ typename S1,
+ typename S2,
+ typename S3
+>
+struct conc_tuple<S1, S2, S3, none, none> : conc_tuple_3<S1, S2, S3>{
+ conc_tuple(S1& s1, S2& s2, S3& s3) : conc_tuple_3<S1, S2, S3>(s1, s2, s3) {};
+};
+
+template <
+ typename S1,
+ typename S2
+>
+struct conc_tuple<S1, S2, none, none, none> : conc_tuple_2<S1, S2>{
+ conc_tuple(S1& s1, S2& s2) : conc_tuple_2<S1, S2>(s1, s2) {};
+};
+
+}
+}
+#endif

Added: sandbox/synchro/boost/synchro/conc/concurrent_component.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/synchro/conc/concurrent_component.hpp 2009-02-09 17:55:09 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,287 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2008. 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/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_SYNCHRO_CONC_SYSTEM__HPP
+#define BOOST_SYNCHRO_CONC_SYSTEM__HPP
+
+//#include "boost/synchro/lockers/is_strict_locker.hpp"
+//#include "boost/synchro/lockers/strict_locker.hpp"
+//#include "boost/synchro/syntactic_lock_traits.hpp"
+//#include "boost/synchro/lock_traits.hpp"
+//#include "boost/static_assert.hpp"
+//#include "boost/type_traits/is_same.hpp"
+//#include "boost/synchro/lock_concepts.hpp"
+//#include <boost/synchro/thread/mutex.hpp>
+#include <boost/synchro/semaphore.hpp>
+#include <typeinfo>
+
+namespace boost { namespace synchro {
+
+struct concurrent_component_base {};
+struct bad_sender {};
+struct bad_type {};
+
+
+namespace detail {
+template <typename T>
+class concurrent_component_synchro
+{
+public:
+ concurrent_component_synchro()
+ : signaled_(false)
+ {
+ }
+
+ T wait()
+ {
+ boost::mutex::scoped_lock lock(mutex_);
+ cond_.wait(lock);
+ return val_;
+ }
+
+ bool post(T val)
+ {
+ boost::mutex::scoped_lock lock(mutex_);
+ if (!signaled_) { /*< first post assigns the current thread id >*/
+ signaled_=true;
+ val_ = val;
+ cond_.notify_one();
+ return true;
+ } else { /*< the other post do nothing >*/
+ return false;
+ }
+ }
+ void clear()
+ {
+ boost::mutex::scoped_lock lock(mutex_);
+ signaled_=false;
+ }
+
+private:
+ mutex mutex_;
+ condition_variable cond_;
+ bool signaled_;
+ T val_;
+};
+}
+
+//template <typename Lockable=boost::mutex>
+class concurrent_component : public concurrent_component_base {
+#if 0
+ In file included from ..\example\Histogram.cpp:11:
+ ../../../boost/synchro/conc/concurrent_component.hpp:32:
+ error: '*' cannot appear in a constant-expression
+
+ BOOST_CONCEPT_ASSERT((LockableConcept<Lockable>));
+#endif
+public:
+
+protected:
+//[port
+ struct port {
+ friend class concurrent_component;
+ port() : accept_call_(0), until_end_(0) {
+ }
+ ~port() {
+ }
+ struct synchronizer {
+ port& that_;
+ synchronizer(port& that) : that_(that) {
+#if 0
+ for (;;) {
+ that.accept_call_.wait();
+ if (that_.common_.post(1, that.accept_call_)) break;
+ that.accept_call_.post();
+ }
+#else
+ that.accept_call_.wait();
+#endif
+ }
+ ~synchronizer() {
+ that_.until_end_.post();
+ }
+ };
+ private:
+ void accept() {
+ accept_call_.post();
+ until_end_.wait();
+ }
+#if 0
+ void accept_one(port& common) {
+ common_ = &common;
+ }
+#endif
+ void accept_begin() {
+ accept_call_.post();
+ }
+ void accept_end() {
+ until_end_.wait();
+ }
+
+#if 0
+ bool accept(const boost::posix_time::ptime &abs_time) {
+ accept_call_.post();
+ this_thread::sleep(abs_time);
+ if (accept_call_.try_wait()) return false;
+ until_end_.wait();
+ return true;
+ }
+
+ static void accept(port& p, const boost::posix_time::ptime &abs_time) {
+ p.accept(abs_time);
+ }
+#endif
+ semaphore accept_call_;
+ semaphore until_end_;
+ };
+ static void accept(port& p) {
+// accept_call_.common_.post(1);
+ p.accept();
+ }
+ static void accept(port& p1, port& p2) {
+ p1.accept_begin();
+ p2.accept_begin();
+// p1.accept_call_.common_.wait();
+ p1.accept_end();
+ p2.accept_end();
+ }
+#if 0
+
+ static void accept_one(port& p1, port& p2) {
+ }
+#endif
+//]
+//[object_port
+ struct object_port {
+ friend class concurrent_component;
+ object_port() : accept_call_(0), until_end_(0) {
+ }
+ ~object_port() {
+ }
+ struct synchronizer {
+ object_port& that_;
+ synchronizer(object_port& that, const concurrent_component_base* snd) : that_(that) {
+ if (snd!= that_.sender_) {
+// std::cout<<"bad_sender "<<snd << "!="<< that_.sender_<< std::endl;
+ throw bad_sender();
+ }
+ that.accept_call_.wait();
+ }
+ ~synchronizer() {
+ that_.until_end_.post();
+ }
+ };
+ private:
+ void accept(const concurrent_component_base* snd) {
+ sender_=snd;
+ accept_call_.post();
+ until_end_.wait();
+ }
+ semaphore accept_call_;
+ semaphore until_end_;
+ const concurrent_component_base* sender_;
+ };
+ static void accept(object_port& that, const concurrent_component_base* snd) {
+ that.accept(snd);
+ }
+ static void accept_from(object_port& that, const concurrent_component_base* snd) {
+ that.accept(snd);
+ }
+//]
+//[qualified_port
+ template <typename TYPE>
+ struct qualified_port {
+ friend class concurrent_component;
+ qualified_port() : accept_call_(0), until_end_(0){
+ }
+ ~qualified_port() {
+ }
+ struct synchronizer {
+ qualified_port& that_;
+ synchronizer(qualified_port& that, const TYPE* snd) : that_(that) {
+ that.accept_call_.wait();
+ }
+ ~synchronizer() {
+ that_.until_end_.post();
+ }
+ };
+ private:
+ void accept() {
+// type_=&typeid(*snd);
+ accept_call_.post();
+ until_end_.wait();
+ }
+// static void accept(object_port& that, const concurrent_component_base* snd) {
+// that.accept(snd);
+// }
+ semaphore accept_call_;
+ semaphore until_end_;
+// const std::type_info* type_;
+ };
+ template <typename TYPE>
+ static void accept(qualified_port<TYPE>& that) {
+ that.accept();
+ }
+//]
+};
+#if EXAMPLES
+//[synchronized_communication_between_components_schema
+class R;
+class S : concurrent_component<> {
+public:
+ S(R& r);
+ void operator()()
+ {
+ // ...
+ e2 = r_.m(e1);
+ }
+};
+
+class R : concurrent_component<> {
+public:
+ void m() {
+ // ...
+ }
+ void operator()()
+ {
+ // ...
+ }
+};
+//]
+//[synchronized_communication_between_components
+class R;
+class S : concurrent_component<> {
+public:
+ S(R& r);
+ void operator()()
+ {
+ // ...
+ e2 = r_.m(e1);
+ }
+};
+
+class R : concurrent_component<> {
+ port p;
+public:
+ void m() {
+ port::synchronizer _(p);
+ // ...
+ }
+ void operator()()
+ {
+ // ...
+ port::accept(p);
+ }
+};
+//]
+#endif
+}
+}
+#endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk