summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/decoder/commandPipe.cpp
blob: 05991ca52661cbbaa62bb9ec7f213a8c0ed6d4ea (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
/*
  thread safe fifo queue for commands
  Copyright (C) 2000  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 "commandPipe.h"

#define _COMMAND_ARRAY_SIZE   100

CommandPipe::CommandPipe() {
  abs_thread_cond_init(&spaceCond);
  abs_thread_cond_init(&emptyCond);
  abs_thread_cond_init(&dataCond);
  abs_thread_mutex_init(&pipeMut);

  entries=0;
  readPos=0;
  writePos=0;

  
  int i;
  
  commandArray=new Command*[_COMMAND_ARRAY_SIZE];


  for(i=0;i<_COMMAND_ARRAY_SIZE;i++) {
    commandArray[i]=new Command(_COMMAND_NONE,0);
  }
}


CommandPipe::~CommandPipe() {
  abs_thread_cond_destroy(&spaceCond);
  abs_thread_cond_destroy(&emptyCond);
  abs_thread_cond_destroy(&dataCond);
  abs_thread_mutex_destroy(&pipeMut);


  int i;

  for(i=0;i<_COMMAND_ARRAY_SIZE;i++) {
    delete commandArray[i];
  }
  delete [] commandArray;
}

void CommandPipe::sendCommand(Command& cmd) {
  sendCommand(cmd,true);
}

void CommandPipe::sendCommandNoWait(Command& cmd) {
  sendCommand(cmd,false);
}


void CommandPipe::sendCommand(Command& cmd,int lWait) {
  lockCommandPipe();
  if (entries==_COMMAND_ARRAY_SIZE) {
    waitForSpace();
  }
  cmd.copyTo(commandArray[writePos]);
  writePos++;
  if (writePos==_COMMAND_ARRAY_SIZE) {
    writePos=0;
  }
  entries++;
  // low water signal
  if (entries==1) {
    signalData();
  }
  unlockCommandPipe(); 
  if (lWait) {
    waitForEmptyQueue();
  }
}


int CommandPipe::hasCommand(Command* dest) {
  lockCommandPipe();
  if (entries==0) {
    unlockCommandPipe();
    return false;
  }

  commandArray[readPos]->copyTo(dest);
  readPos++;
  if (readPos==_COMMAND_ARRAY_SIZE) {
    readPos=0;
  }  
  entries--;
  // low water
  if (entries==0) {
    signalEmpty();
    unlockCommandPipe();
    return true;
  }
  // if we are on highwater, signal space
  if (entries==_COMMAND_ARRAY_SIZE-1) {
    signalSpace();
  }
  unlockCommandPipe();
  return true;
}


void CommandPipe::waitForEmptyQueue() {
  lockCommandPipe();
  while (entries > 0) {
    waitForEmpty();
  }
  unlockCommandPipe();
}

void CommandPipe::waitForCommand() {
  lockCommandPipe();
  while (entries == 0) {
    waitForData();
  }
  unlockCommandPipe();
}


void CommandPipe::lockCommandPipe() {
  abs_thread_mutex_lock(&pipeMut);
}


void CommandPipe::unlockCommandPipe() {
  abs_thread_mutex_unlock(&pipeMut);
}

void CommandPipe::waitForSpace() {
  abs_thread_cond_wait(&spaceCond,&pipeMut);
}

void CommandPipe::waitForEmpty() {
  abs_thread_cond_wait(&emptyCond,&pipeMut);
}
 
void CommandPipe::waitForData() {
  abs_thread_cond_wait(&dataCond,&pipeMut);
}


void CommandPipe::signalSpace() {
  abs_thread_cond_signal(&spaceCond);
}

void CommandPipe::signalEmpty() {
  abs_thread_cond_signal(&emptyCond);
}

void CommandPipe::signalData() {
  abs_thread_cond_signal(&dataCond);
}