Changeset 1671 in Sophya


Ignore:
Timestamp:
Oct 6, 2001, 1:46:35 AM (24 years ago)
Author:
aubourg
Message:

* empty log message *

Location:
trunk/ArchTOIPipe/Kernel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/ArchTOIPipe/Kernel/toisegment.cc

    r1670 r1671  
    11#include "toisegment.h"
     2
     3
     4/******* BufferSegment *********/
     5TOISegmented::BufferSegment::BufferSegment(int sz) {
     6  status     = NEW;
     7  bufferSize = sz;
     8  sn0        = -1;
     9
     10  refcount   = 0;
     11
     12  data       = new double[sz];
     13  flags     = new uint_8[sz];
     14
     15  pthread_mutex_init(&refcount_mutex, NULL);
     16}
     17
     18TOISegmented::BufferSegment::~BufferSegment() {
     19  if (refcount > 0) {
     20    throw(ForbiddenError("TOISegment : delete Buffer with refcount>0"));
     21  }
     22  delete[] data;
     23  delete[] flags;
     24  pthread_mutex_destroy(&refcount_mutex);
     25}
     26
     27
     28void TOISegmented::BufferSegment::putData(int sn, double d, uint_8 f) {
     29  if (status == NEW) {
     30    status = WRITE;
     31    sn0 = sn;
     32  }
     33  if (status == COMMITTED) {
     34    throw(ForbiddenError("TOISegment : putData in committed buffer"));
     35  }
     36  checkInRange(sn);
     37  data[sn-sn0] = d;
     38  flags[sn-sn0] = f;
     39}
     40
     41void TOISegmented::BufferSegment::incRefCount() {
     42  pthread_mutex_lock(&refcount_mutex);
     43  refcount++;
     44  pthread_mutex_unlock(&refcount_mutex);
     45}
     46
     47void TOISegmented::BufferSegment::decRefCount() {
     48  pthread_mutex_lock(&refcount_mutex);
     49  int nrc = --refcount;
     50  pthread_mutex_unlock(&refcount_mutex);
     51  if (nrc<0)
     52    throw(ForbiddenError("TOISegment : buffer refcount < 0"));
     53}
     54
     55int TOISegmented::BufferSegment::getRefCount() {
     56  pthread_mutex_lock(&refcount_mutex);
     57  int rc = refcount;
     58  pthread_mutex_unlock(&refcount_mutex);
     59  return rc;
     60}
     61
  • trunk/ArchTOIPipe/Kernel/toisegment.h

    r1670 r1671  
    22#ifndef TOISEGMENT_H
    33#define TOISEGMENT_H
     4
     5#include <vector>
     6#include <set>
    47
    58#include "toi.h"
     
    2124 
    2225 protected:
     26  class BufferSegment;
     27  class BufferView;
     28  class MasterView;
     29
     30
    2331  class BufferSegment {
    2432  public:
     
    2634    ~BufferSegment();
    2735    static const int NEW = 0;
    28     static const int WRITE = 1;
    29     static const int COMMITTED = 2;
     36    static const int WRITE = 1;     // single-thread write access
     37    static const int COMMITTED = 2; // multiple read without lock are ok
    3038   
    3139    int getStatus() {return status;}
    32 
    33 
     40    void incRefCount();
     41    void decRefCount();
     42    int  getRefCount();
     43   
    3444    void putData(int sn, double data, uint_8 flag);
    3545    inline double getData(int sn);
    3646    inline uint_8 getFlag(int sn);
     47
     48    bool isPastEnd(int sn) {
     49      return sn >= sn+bufferSize;
     50    }
    3751   
    3852  private:
     
    5973  };
    6074 
    61   // Master view, gere le lock...
     75  // Master view, gere le lock, et l'ecriture
    6276  class MasterView {
    6377  public:
    64     MasterView();
     78    MasterView(int bufsz=256, int maxseg=20);
    6579    ~MasterView();
    6680
     81    void putData(int sn, double data, uint_8 flag);
     82    void addToWaitList(BufferView* bv);
     83
     84    BufferView* getView(); // thread-specific
     85
    6786  protected:
    68     int s0;
     87    friend class BufferView;
     88    void signalWaitingViews();
     89    void nextSegment();
     90    BufferView* createView();
     91    void updateView(BufferView*); // called on reader thread of the view
     92   
     93    BufferSegment*   currentSegment;
     94
     95    int maxSegments;
     96    int bufferSize;
     97    int sn0;                // First sn in first buffer
     98    vector<BufferSegment*> segments; // Committed
    6999   
    70100    pthread_mutex_t  mutex; // lock for master buffer list access
    71     pthread_cond_t   condv; // waiting for new buffer
     101    pthread_cond_t   condv; // waiting (read or write)
     102
     103    static const int NO_WAIT   = 0;
     104    static const int WAIT_READ = 1;
     105    static const int WAIT_WRITE= 2;
     106    int  waitStatus;
     107
     108    set<BufferView*>  waitingBuffers;
    72109  };
    73110
     
    76113  class BufferView {
    77114  public:
     115    BufferView();
     116    ~BufferView();
    78117   
    79     void sync();
     118    void sync();  // recupere les nouveaux segments, resync avec master
     119    void wait();  // Passe en attente d'un nouveau segment
    80120  protected:
    81     MasterView master;
     121    friend class MasterView;
     122    MasterView* master;
     123    vector<BufferSegment*> segments; // Committed
     124    pthread_mutex_t  mutex; // lock pour attente de segments
     125    pthread_cond_t   condv; // attente de segments
    82126  }; 
     127
     128 
     129 
    83130};
    84131
Note: See TracChangeset for help on using the changeset viewer.