|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r51160 - sandbox/synchro/boost/synchro/queues
From: vicente.botet_at_[hidden]
Date: 2009-02-09 17:44:40
Author: viboes
Date: 2009-02-09 17:44:40 EST (Mon, 09 Feb 2009)
New Revision: 51160
URL: http://svn.boost.org/trac/boost/changeset/51160
Log:
Boost.Synchro V0.0.0
Added:
sandbox/synchro/boost/synchro/queues/
sandbox/synchro/boost/synchro/queues/synchro_buffer_family.hpp (contents, props changed)
sandbox/synchro/boost/synchro/queues/synchro_buffer_monitor.hpp (contents, props changed)
Added: sandbox/synchro/boost/synchro/queues/synchro_buffer_family.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/synchro/queues/synchro_buffer_family.hpp 2009-02-09 17:44:40 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,114 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_QUEUES_SYNCHRO_BUFFER__HPP
+#define BOOST_SYNCHRO_QUEUES_SYNCHRO_BUFFER__HPP
+
+#include <boost/thread/locks.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/condition_variable.hpp>
+
+namespace boost { namespace synchro {
+#if 0
+//[sync_buffer_boost_thread_style
+template <typename T, unsigned size>
+class sync_buffer
+{
+ typedef boost::mutex mutex_type;
+ typedef boost::condition_variable condition_type;
+ typedef boost::unique_lock<mutex_type> unique_lock_type;
+ mutex_type mtx_;
+ condition_type not_full_;
+ condition_type not_empty_;
+
+ T data_[size+1];
+ unsigned in_, out_;
+
+public:
+ sync_buffer():in_(0), out_(0) {}
+
+ bool full() { return out_==(in_+1)%(size+1); }
+ bool empty() { return out_==in_; }
+
+ unsigned get_in() {return in_;}
+ unsigned get_out() {return out_;}
+ void push(T v) {
+ unique_lock_type guard(mtx_); /*< ensure the mutex is locked!!! >*/
+ while (full()) { /*< loop until not full!!! >*/
+ not_full_.wait(guard);
+ }
+ data_[in_]=v;
+ in_ = (in_+1)% (size+1);
+ not_empty_.notify_one(); /*< notifies a not_empty condition >*/
+ }
+
+ T pull() {
+ unique_lock_type guard(mtx_); /*< ensure the mutex is locked!!! >*/
+ while (empty()) { /*< loop until not full!!! >*/
+ not_empty_.wait(guard);
+ }
+ unsigned idx = out_;
+ out_ = (out_+1)% (size+1);
+ not_full_.notify_one(); /*< notifies a not_full condition >*/
+ return data_[idx];
+ }
+};
+
+//]
+#endif
+//[sync_buffer_family_style
+template <typename T, unsigned size, typename Sync>
+class sync_buffer
+{
+ typedef typename Sync::mutex_type mutex_type;
+ typedef typename Sync::condition_type condition_type;
+ typedef typename boost::unique_lock<mutex_type> unique_lock_type;
+ mutex_type mtx_;
+ condition_type not_full_;
+ condition_type not_empty_;
+
+ T data_[size+1];
+ unsigned in_, out_;
+
+public:
+ sync_buffer():in_(0), out_(0) {}
+
+ bool full() { return out_==(in_+1)%(size+1); }
+ bool empty() { return out_==in_; }
+
+ unsigned get_in() {return in_;}
+ unsigned get_out() {return out_;}
+ void push(T v) {
+ unique_lock_type guard(mtx_); /*< ensure the mutex is locked!!! >*/
+ while (full()) { /*< loop until not full!!! >*/
+ not_full_.wait(guard);
+ }
+ data_[in_]=v;
+ in_ = (in_+1)% (size+1);
+ not_empty_.notify_one(); /*< notifies a not_empty condition >*/
+ }
+
+ T pull() {
+ unique_lock_type guard(mtx_); /*< ensure the mutex is locked!!! >*/
+ while (empty()) { /*< loop until not full!!! >*/
+ not_empty_.wait(guard);
+ }
+ unsigned idx = out_;
+ out_ = (out_+1)% (size+1);
+ not_full_.notify_one(); /*< notifies a not_full condition >*/
+ return data_[idx];
+ }
+};
+
+//]
+
+}
+}
+#endif
Added: sandbox/synchro/boost/synchro/queues/synchro_buffer_monitor.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/synchro/queues/synchro_buffer_monitor.hpp 2009-02-09 17:44:40 EST (Mon, 09 Feb 2009)
@@ -0,0 +1,95 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_BUFFER__HPP
+#define BOOST_SYNCHRO_CONC_BUFFER__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/lockable_traits.hpp"
+//#include "boost/static_assert.hpp"
+//#include "boost/type_traits/is_same.hpp"
+//#include "boost/synchro/lockable_concepts.hpp"
+#include "boost/synchro/monitor.hpp"
+
+namespace boost { namespace synchro {
+
+#if 0
+//[sync_buffer_schema
+class sync_buffer {
+ boost::mutex mtx_; /*< explicit mutex declaration >*/
+public:
+ ...
+ bool full() { return in_==out_; }
+ bool empty() { return in_==(out_%size)+1; }
+ void push(T& v) {
+ // wait if buffer is full
+ data_[in_]=v;
+ in_ = (in_% size)+1;
+ }
+ T pull() {
+ // wait if buffer is empty
+ out_ = (out_% size)+1;
+ return data_[out_];
+ }
+};
+//]
+#endif
+
+//[sync_buffer_monitor
+template <typename T, unsigned size>
+class sync_buffer : protected exclusive_monitor<>
+{
+ condition not_full_;
+ condition not_empty_;
+
+ T data_[size+1];
+ unsigned in_, out_;
+
+ struct not_full {
+ explicit not_full(sync_buffer &b):that_(b){};
+ bool operator()() const { return !that_.full(); }
+ sync_buffer &that_;
+ };
+ struct not_empty {
+ explicit not_empty(sync_buffer &b):that_(b){};
+ bool operator()() const { return !that_.empty(); }
+ sync_buffer &that_;
+ };
+public:
+ sync_buffer():in_(0), out_(0) {}
+
+ bool full() { return out_==(in_+1)%(size+1); }
+ bool empty() { return out_==in_; }
+
+ unsigned get_in() {return in_;}
+ unsigned get_out() {return out_;}
+
+ void push(T v) {
+ synchronizer _(*this->mutex(), not_full_, not_full(*this)); /*< waits until the condition not_full is satisfyed >*/
+ data_[in_]=v;
+ in_ = (in_+1)% (size+1);
+ not_empty_.notify_one(); /*< notifies a not_empty condition >*/
+ }
+
+ T pull() {
+ synchronizer _(*this->mutex(), not_empty_, not_empty(*this)); /*< waits until the condition not_empty is satisfyed >*/
+ unsigned idx = out_;
+ out_ = (out_+1)% (size+1);
+ not_full_.notify_one(); /*< notifies a not_full condition >*/
+ return data_[idx];
+ }
+};
+//]
+
+}
+}
+#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