summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/input/simpleRingBuffer.h
blob: 101725bfcb6d515a0591078f26d334046603e74b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
  a thread safe ring buffer without dependencies
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */




#ifndef _SIMPLERINGBUFFER_H
#define _SIMPLERINGBUFFER_H

#include "../util/abstract/abs_thread.h"

extern "C" {
  #include <stdio.h>
}

/** 
   Note to parameter minLinBufSize in Constructor:
   <p>
   If the fillgrade is sufficient we can delivier at least
   this amount of bytes with one "fill"
   (If the fillgrade is not suffficient we can only deliever fillgrade)
   This values adresses the problem that a ring buffer cannot
   deliever linear memory the whole time(eg. if you read near the
   upper end)
   <p>
   If the requested Buffersize by the device is smaller than
   this number you can be sure that you get exactly
   your preferred buffersize. not more not less.(but
   only if the fillgrade allows this)
*/




class SimpleRingBuffer {


 public:

  SimpleRingBuffer(int ringBufferSize, int minLinBufferSize);
  virtual ~SimpleRingBuffer();

  // Writer thread can call these:

  int getWriteArea(char* &ptr,int &size);
  void forwardWritePtr(int bytes);
  int waitForSpace(int minSpace);
  void exitWaitForSpace();
  void setCanWaitForSpace(int lCanWaitForSpace);
  
  
  // Reader thread these:
 
  void forwardReadPtr(int bytes);
  int getReadArea(char* &ptr,int &size);
  int waitForData(int minData);
  void exitWaitForData();
  void setCanWaitForData(int lCanWaitForData);
  int getCanWaitForData();


  // and the lockPos
  void forwardLockPtr(int bytes);


  // both:

  int getFillgrade();       // return how much buffer between reader/writer
  void emptyBuffer();    // frees the space between them
  int getFreeRead();
  int getFreeWrite();

  int getSize();
  int getReadBytes();
  int getWriteBytes();

  // make sure that no one calls getReadArea/getWriteArea
  void resizeBuffer(int changeSize);
 private:
  void updateCanWrite();
  void updateCanRead();

  int size;
  
  int lockgrade;
  int fillgrade;
  
  char* readPos;
  char* writePos;
  char* lockPos;

  char* startPos;

  char* lastPos;
  char* eofPos;
  int canWrite;
  int canRead;

  int waitMinData;
  int waitMinSpace;

  abs_thread_mutex_t mut;
  abs_thread_cond_t dataCond;
  abs_thread_cond_t spaceCond;
  
  int insertBlock;
  int readBlock;
  int linAvail;

  char* minLinBuf;
  int minLinBufSize;
  int lWaitForData;
  int lWaitForSpace;

  // statistic purpose:
  int readBytes;
  int writeBytes;

  int lCanWaitForSpace;
  int lCanWaitForData;
  int instance;
};

#endif