summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/input/simpleRingBuffer.cpp
blob: 71c92329e37b5b25f9c811585ce8ad1cc94b6b74 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
  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

 */




#include "simpleRingBuffer.h"
#include <string.h>

#include <iostream>

using namespace std;

static int instanceCnt=0;

SimpleRingBuffer::SimpleRingBuffer(int size,int minLinSize) {
  abs_thread_mutex_init(&mut);
  abs_thread_cond_init(&dataCond);
  abs_thread_cond_init(&spaceCond);

  instance=instanceCnt;
  instanceCnt++;
  this->size=size;
  startPos=(char*)malloc(size);
  readPos=startPos;
  writePos=startPos;
  lockPos=startPos;

  lockgrade=0;
  fillgrade=0;
  linAvail=size;
  lastPos=(startPos+size-1);
  eofPos=lastPos+1;

  canWrite=size;
  canRead=0;

  minLinBuf=new char[minLinSize];
  this->minLinBufSize=minLinSize;
  waitMinData=0;
  waitMinSpace=0;
  lWaitForData=false;
  lWaitForSpace=false;

  readBytes=0;
  writeBytes=0;

  lCanWaitForData=true;
  lCanWaitForSpace=true;
}


SimpleRingBuffer::~SimpleRingBuffer() {
  // The user of this class must take care that the threads
  // have exited!
  free(startPos);
  delete [] minLinBuf;
  abs_thread_mutex_destroy(&mut);
  abs_thread_cond_destroy(&dataCond);
  abs_thread_cond_destroy(&spaceCond);
}
  

int SimpleRingBuffer::getSize() {
  return size;
}


int SimpleRingBuffer::getWriteArea(char* &ptr,int &size) {

  // When we are in this area the following
  // can happen by the other thread
  // fillgrade is decreased (because reader fowards)
  // readPos is changed.

  ptr=writePos;
  size=canWrite;

  return size;
}


void SimpleRingBuffer::updateCanWrite() {
  if (lockPos < writePos) {
    canWrite=eofPos-writePos;
    //printf("1 c:%d l:%p w:%p",canWrite,lockPos,writePos);
  } else if (lockPos > writePos) {
    canWrite=lockPos-writePos;
    //printf("2 c:%d l:%p w:%p",canWrite,lockPos,writePos);
  } else {
    if (fillgrade > 0) {
      canWrite=0;
    } else {
      canWrite=eofPos-writePos;
    }
    //printf("2 c:%d ",canWrite);
    
  }
  if (canWrite < 0) {
    printf("error canWrite:%d fill:%d lock:%p start:%p eof:%p write:%p\n",
           canWrite,fillgrade,lockPos,startPos,eofPos,writePos);
  }

} 


void SimpleRingBuffer::updateCanRead() {
  canRead=fillgrade-lockgrade;
  int currentSpace=size-fillgrade;
  if (currentSpace >= waitMinSpace) {
    abs_thread_cond_signal(&spaceCond);
  } 
  if (canRead < 0) {
    printf("error canRead:%d fillgrade:%d lockgrade:%d \n",
           canRead,fillgrade,lockgrade);
  }
 
} 


void SimpleRingBuffer::forwardLockPtr(int nBytes) {
  abs_thread_mutex_lock(&mut);

  if (fillgrade < lockgrade) {
    printf("1:fillgrade:%d < lockgrade:%d\n",fillgrade,lockgrade);
  }
  fillgrade-=nBytes;
  lockgrade-=nBytes;
  if (fillgrade < lockgrade) {
    printf("2:fillgrade:%d < lockgrade:%d nBytes:%d\n",
	   fillgrade,lockgrade,nBytes);
  }
  lockPos=lockPos+nBytes;
  if (lockPos > lastPos) {   // we expects that we had a linAvail part
    // if user forwards more than buffer boundary
    nBytes=lockPos-lastPos;
    lockPos=startPos+nBytes-1;
  }
  updateCanWrite();
  updateCanRead();

  abs_thread_mutex_unlock(&mut);
  return;
}


void SimpleRingBuffer::forwardWritePtr(int nBytes) {
  abs_thread_mutex_lock(&mut);
  
  fillgrade=fillgrade+nBytes;
  if (fillgrade < lockgrade) {
    printf("3:fillgrade:%d < lockgrade:%d nBytes:%d\n",
	   fillgrade,lockgrade,nBytes);
  }
  writeBytes+=nBytes;
  writePos=writePos+nBytes;
  if(writePos >= eofPos) {
    if (writePos == eofPos) {
      writePos=startPos;
    } else {
      cout << "writePos > eofPos ! forward error:"<<(eofPos-writePos)
	   <<" bytes"<<endl;
    }
  }

  updateCanWrite();
  updateCanRead();
  if (fillgrade >= waitMinData) {
    abs_thread_cond_signal(&dataCond);
  }
  abs_thread_mutex_unlock(&mut);
}


int SimpleRingBuffer::waitForSpace(int bytes){
  abs_thread_mutex_lock(&mut);
  int back=0;
  waitMinSpace=bytes;
  if (waitMinSpace > size) {
    waitMinSpace=size;
  }
  if (waitMinSpace < 0) {
    cout << "negative waitForSpace"<<endl;
    waitMinSpace=0;
  }
  int currentSpace=size-fillgrade;
  if (lCanWaitForSpace) {
    if (currentSpace < waitMinSpace) {
      lWaitForSpace=true;
      // it is not possible to wait for data/space simultanously
      if (lWaitForData == true) {
	abs_thread_cond_signal(&dataCond);
      }
      abs_thread_cond_wait(&spaceCond,&mut);
      lWaitForSpace=false;
    }
  }
  if (size-fillgrade >= waitMinSpace) {
    back=1;
  }
  abs_thread_mutex_unlock(&mut);
  return back;
}


void SimpleRingBuffer::exitWaitForSpace(){
  abs_thread_mutex_lock(&mut);
  abs_thread_cond_signal(&spaceCond);
  abs_thread_mutex_unlock(&mut);
}

void SimpleRingBuffer::setCanWaitForSpace(int lCanWaitForSpace) {
  abs_thread_mutex_lock(&mut);
  this->lCanWaitForSpace=lCanWaitForSpace;
  abs_thread_cond_signal(&spaceCond);
  abs_thread_mutex_unlock(&mut);

}
  
  

void SimpleRingBuffer::forwardReadPtr(int nBytes) {
  abs_thread_mutex_lock(&mut);
  readBytes+=nBytes;
  readPos+=nBytes;
  linAvail=linAvail-nBytes;
  lockgrade+=nBytes;
  if (readPos > lastPos) {   // we expects that we had a linAvail part
    // if user forwards more than buffer boundary
    nBytes=readPos-lastPos;
    readPos=startPos+nBytes-1;
    linAvail=lastPos+1-readPos;
  }
  if (fillgrade < lockgrade) {
    printf("5:fillgrade:%d < lockgrade:%d nBytes:%d\n",
	   fillgrade,lockgrade,nBytes);
  }
  updateCanRead();
  abs_thread_mutex_unlock(&mut);
}



int SimpleRingBuffer::getReadArea(char* &ptr,int &readSize) {
  int pSize=readSize;
  ptr=readPos;

  if (canRead == 0) {
    readSize=0;
    return 0;
  }
  if (pSize < 0) { 
    cout << "Generic Memory Info invalid"<<endl;
    pSize=size/2;
  }
  //
  // Now the part the we deliver a minimum buffer if it is
  // possible
  //

  if ( (pSize > linAvail) &&
       (minLinBufSize >linAvail) &&
       (canRead > linAvail) ) {
    int copySize;
    copySize=canRead;  // we cannot copy more than this
    if (copySize > pSize) { // if it is too much reduche it
      copySize=pSize;
    }
    if (copySize > minLinBufSize) { // if it does not fit in buffer->reduce
      copySize=minLinBufSize;
    }
    memcpy(minLinBuf,readPos,linAvail);
    memcpy(minLinBuf+linAvail,startPos,copySize-linAvail);
    readSize=copySize;
    ptr=minLinBuf;
    return copySize;
  }

  // linAvail part end

  int copyBytes=linAvail;
  if (canRead < copyBytes) {
    copyBytes=canRead;
  }
  if (copyBytes >= pSize) {
    readSize=pSize;
  } else {
    readSize=copyBytes;
  }
  return readSize;
}



void SimpleRingBuffer::exitWaitForData(){
  abs_thread_mutex_lock(&mut);
  abs_thread_cond_signal(&dataCond);
  abs_thread_mutex_unlock(&mut);
}



int SimpleRingBuffer::waitForData(int bytes){
  abs_thread_mutex_lock(&mut);
  int back=0;
  waitMinData=bytes;
  if (waitMinData > size) {
    waitMinData=size;
  }
  if (waitMinData < 0) {
    cout << "negative waitForData"<<endl;
    waitMinData=0;
  }
  if (lCanWaitForData) {
    if (fillgrade < waitMinData) {
      lWaitForData=true;
      // it is not possible to wait for data space simultanously
      if (lWaitForSpace == true) {
	abs_thread_cond_signal(&spaceCond);
      }
      abs_thread_cond_wait(&dataCond,&mut);
      lWaitForData=false;
    }
  }
  if (fillgrade >= waitMinData) {
    back=1;
  }
  abs_thread_mutex_unlock(&mut);
  return back;
}

int SimpleRingBuffer::getCanWaitForData() {
  return lCanWaitForData;
}
  

void SimpleRingBuffer::setCanWaitForData(int lCanWaitForData) {
  abs_thread_mutex_lock(&mut);
  this->lCanWaitForData=lCanWaitForData;
  abs_thread_cond_signal(&dataCond);
  abs_thread_mutex_unlock(&mut);

}

void SimpleRingBuffer::emptyBuffer() {
  abs_thread_mutex_lock(&mut);
  writePos=readPos;
  if (fillgrade < lockgrade) {
    printf("4:fillgrade:%d < lockgrade:%d\n",fillgrade,lockgrade);
  }
  linAvail=lastPos+1-writePos;
  fillgrade=lockgrade;
  updateCanRead();
  updateCanWrite();
  readBytes=0;
  writeBytes=0;
  if (size-fillgrade >= waitMinSpace) {
    abs_thread_cond_signal(&spaceCond);
  }
  if (fillgrade >= waitMinData) {
    abs_thread_cond_signal(&dataCond);
  }
  abs_thread_mutex_unlock(&mut);
}


int SimpleRingBuffer::getFillgrade() {
  return fillgrade;
}


int SimpleRingBuffer::getReadBytes() {
  return readBytes;
}


int SimpleRingBuffer::getWriteBytes() {
  return writeBytes;
}


int SimpleRingBuffer::getFreeRead() {
  return fillgrade;
}

int SimpleRingBuffer::getFreeWrite() {
  return size-fillgrade;
}

void SimpleRingBuffer::resizeBuffer(int changeSize) {
  abs_thread_mutex_lock(&mut);
  int lPos=lockPos-startPos;
  int wPos=writePos-startPos;
  int rPos=readPos-startPos;
  startPos=(char *)realloc(startPos,changeSize);
  size=changeSize;
  readPos=startPos+lPos;
  writePos=startPos+wPos;
  lockPos=startPos+rPos;

  lastPos=(startPos+size-1);
  eofPos=lastPos+1;

  linAvail=lastPos+1-readPos;

  updateCanWrite();
  updateCanRead();
  abs_thread_mutex_unlock(&mut);


}