Index: trunk/ArchTOIPipe/Kernel/toisegment.cc
===================================================================
--- trunk/ArchTOIPipe/Kernel/toisegment.cc	(revision 1670)
+++ trunk/ArchTOIPipe/Kernel/toisegment.cc	(revision 1671)
@@ -1,1 +1,61 @@
 #include "toisegment.h"
+
+
+/******* BufferSegment *********/
+TOISegmented::BufferSegment::BufferSegment(int sz) {
+  status     = NEW;
+  bufferSize = sz;
+  sn0        = -1;
+
+  refcount   = 0;
+
+  data       = new double[sz];
+  flags     = new uint_8[sz];
+
+  pthread_mutex_init(&refcount_mutex, NULL);
+}
+
+TOISegmented::BufferSegment::~BufferSegment() {
+  if (refcount > 0) {
+    throw(ForbiddenError("TOISegment : delete Buffer with refcount>0"));
+  }
+  delete[] data;
+  delete[] flags;
+  pthread_mutex_destroy(&refcount_mutex);
+}
+
+
+void TOISegmented::BufferSegment::putData(int sn, double d, uint_8 f) {
+  if (status == NEW) {
+    status = WRITE;
+    sn0 = sn;
+  }
+  if (status == COMMITTED) {
+    throw(ForbiddenError("TOISegment : putData in committed buffer"));
+  }
+  checkInRange(sn);
+  data[sn-sn0] = d;
+  flags[sn-sn0] = f;
+}
+
+void TOISegmented::BufferSegment::incRefCount() {
+  pthread_mutex_lock(&refcount_mutex);
+  refcount++;
+  pthread_mutex_unlock(&refcount_mutex);
+}
+
+void TOISegmented::BufferSegment::decRefCount() {
+  pthread_mutex_lock(&refcount_mutex);
+  int nrc = --refcount;
+  pthread_mutex_unlock(&refcount_mutex);
+  if (nrc<0)
+    throw(ForbiddenError("TOISegment : buffer refcount < 0"));
+}
+
+int TOISegmented::BufferSegment::getRefCount() {
+  pthread_mutex_lock(&refcount_mutex);
+  int rc = refcount;
+  pthread_mutex_unlock(&refcount_mutex);
+  return rc;
+}
+
Index: trunk/ArchTOIPipe/Kernel/toisegment.h
===================================================================
--- trunk/ArchTOIPipe/Kernel/toisegment.h	(revision 1670)
+++ trunk/ArchTOIPipe/Kernel/toisegment.h	(revision 1671)
@@ -2,4 +2,7 @@
 #ifndef TOISEGMENT_H
 #define TOISEGMENT_H
+
+#include <vector>
+#include <set>
 
 #include "toi.h"
@@ -21,4 +24,9 @@
   
  protected:
+  class BufferSegment;
+  class BufferView;
+  class MasterView;
+
+
   class BufferSegment {
   public:
@@ -26,13 +34,19 @@
     ~BufferSegment();
     static const int NEW = 0;
-    static const int WRITE = 1;
-    static const int COMMITTED = 2;
+    static const int WRITE = 1;     // single-thread write access
+    static const int COMMITTED = 2; // multiple read without lock are ok
     
     int getStatus() {return status;}
-
-
+    void incRefCount();
+    void decRefCount();
+    int  getRefCount();
+    
     void putData(int sn, double data, uint_8 flag);
     inline double getData(int sn);
     inline uint_8 getFlag(int sn);
+
+    bool isPastEnd(int sn) {
+      return sn >= sn+bufferSize;
+    }
     
   private:
@@ -59,15 +73,38 @@
   };
   
-  // Master view, gere le lock...
+  // Master view, gere le lock, et l'ecriture
   class MasterView {
   public:
-    MasterView();
+    MasterView(int bufsz=256, int maxseg=20);
     ~MasterView();
 
+    void putData(int sn, double data, uint_8 flag);
+    void addToWaitList(BufferView* bv);
+
+    BufferView* getView(); // thread-specific
+
   protected:
-    int s0;
+    friend class BufferView;
+    void signalWaitingViews();
+    void nextSegment();
+    BufferView* createView();
+    void updateView(BufferView*); // called on reader thread of the view
+    
+    BufferSegment*   currentSegment;
+
+    int maxSegments;
+    int bufferSize;
+    int sn0;                // First sn in first buffer
+    vector<BufferSegment*> segments; // Committed
     
     pthread_mutex_t  mutex; // lock for master buffer list access
-    pthread_cond_t   condv; // waiting for new buffer
+    pthread_cond_t   condv; // waiting (read or write)
+
+    static const int NO_WAIT   = 0;
+    static const int WAIT_READ = 1;
+    static const int WAIT_WRITE= 2;
+    int  waitStatus;
+
+    set<BufferView*>  waitingBuffers;
   };
 
@@ -76,9 +113,19 @@
   class BufferView {
   public:
+    BufferView();
+    ~BufferView();
     
-    void sync();
+    void sync();  // recupere les nouveaux segments, resync avec master
+    void wait();  // Passe en attente d'un nouveau segment
   protected:
-    MasterView master;
+    friend class MasterView;
+    MasterView* master;
+    vector<BufferSegment*> segments; // Committed
+    pthread_mutex_t  mutex; // lock pour attente de segments
+    pthread_cond_t   condv; // attente de segments
   };  
+
+  
+  
 };
 
