~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/decoder/commandPipe.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  thread safe fifo queue for commands
 
3
  Copyright (C) 2000  Martin Vogt
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU Library General Public License as published by
 
7
  the Free Software Foundation.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
#include "commandPipe.h"
 
15
 
 
16
#define _COMMAND_ARRAY_SIZE   100
 
17
 
 
18
CommandPipe::CommandPipe() {
 
19
  abs_thread_cond_init(&spaceCond);
 
20
  abs_thread_cond_init(&emptyCond);
 
21
  abs_thread_cond_init(&dataCond);
 
22
  abs_thread_mutex_init(&pipeMut);
 
23
 
 
24
  entries=0;
 
25
  readPos=0;
 
26
  writePos=0;
 
27
 
 
28
  
 
29
  int i;
 
30
  
 
31
  commandArray=new Command*[_COMMAND_ARRAY_SIZE];
 
32
 
 
33
 
 
34
  for(i=0;i<_COMMAND_ARRAY_SIZE;i++) {
 
35
    commandArray[i]=new Command(_COMMAND_NONE,0);
 
36
  }
 
37
}
 
38
 
 
39
 
 
40
CommandPipe::~CommandPipe() {
 
41
  abs_thread_cond_destroy(&spaceCond);
 
42
  abs_thread_cond_destroy(&emptyCond);
 
43
  abs_thread_cond_destroy(&dataCond);
 
44
  abs_thread_mutex_destroy(&pipeMut);
 
45
 
 
46
 
 
47
  int i;
 
48
 
 
49
  for(i=0;i<_COMMAND_ARRAY_SIZE;i++) {
 
50
    delete commandArray[i];
 
51
  }
 
52
  delete commandArray;
 
53
}
 
54
 
 
55
void CommandPipe::sendCommand(Command& cmd) {
 
56
  sendCommand(cmd,true);
 
57
}
 
58
 
 
59
void CommandPipe::sendCommandNoWait(Command& cmd) {
 
60
  sendCommand(cmd,false);
 
61
}
 
62
 
 
63
 
 
64
void CommandPipe::sendCommand(Command& cmd,int lWait) {
 
65
  lockCommandPipe();
 
66
  if (entries==_COMMAND_ARRAY_SIZE) {
 
67
    waitForSpace();
 
68
  }
 
69
  cmd.copyTo(commandArray[writePos]);
 
70
  writePos++;
 
71
  if (writePos==_COMMAND_ARRAY_SIZE) {
 
72
    writePos=0;
 
73
  }
 
74
  entries++;
 
75
  // low water signal
 
76
  if (entries==1) {
 
77
    signalData();
 
78
  }
 
79
  unlockCommandPipe(); 
 
80
  if (lWait) {
 
81
    waitForEmptyQueue();
 
82
  }
 
83
}
 
84
 
 
85
 
 
86
int CommandPipe::hasCommand(Command* dest) {
 
87
  lockCommandPipe();
 
88
  if (entries==0) {
 
89
    unlockCommandPipe();
 
90
    return false;
 
91
  }
 
92
 
 
93
  commandArray[readPos]->copyTo(dest);
 
94
  readPos++;
 
95
  if (readPos==_COMMAND_ARRAY_SIZE) {
 
96
    readPos=0;
 
97
  }  
 
98
  entries--;
 
99
  // low water
 
100
  if (entries==0) {
 
101
    signalEmpty();
 
102
    unlockCommandPipe();
 
103
    return true;
 
104
  }
 
105
  // if we are on highwater, signal space
 
106
  if (entries==_COMMAND_ARRAY_SIZE-1) {
 
107
    signalSpace();
 
108
  }
 
109
  unlockCommandPipe();
 
110
  return true;
 
111
}
 
112
 
 
113
 
 
114
void CommandPipe::waitForEmptyQueue() {
 
115
  lockCommandPipe();
 
116
  while (entries > 0) {
 
117
    waitForEmpty();
 
118
  }
 
119
  unlockCommandPipe();
 
120
}
 
121
 
 
122
void CommandPipe::waitForCommand() {
 
123
  lockCommandPipe();
 
124
  while (entries == 0) {
 
125
    waitForData();
 
126
  }
 
127
  unlockCommandPipe();
 
128
}
 
129
 
 
130
 
 
131
void CommandPipe::lockCommandPipe() {
 
132
  abs_thread_mutex_lock(&pipeMut);
 
133
}
 
134
 
 
135
 
 
136
void CommandPipe::unlockCommandPipe() {
 
137
  abs_thread_mutex_unlock(&pipeMut);
 
138
}
 
139
 
 
140
void CommandPipe::waitForSpace() {
 
141
  abs_thread_cond_wait(&spaceCond,&pipeMut);
 
142
}
 
143
 
 
144
void CommandPipe::waitForEmpty() {
 
145
  abs_thread_cond_wait(&emptyCond,&pipeMut);
 
146
}
 
147
 
 
148
void CommandPipe::waitForData() {
 
149
  abs_thread_cond_wait(&dataCond,&pipeMut);
 
150
}
 
151
 
 
152
 
 
153
void CommandPipe::signalSpace() {
 
154
  abs_thread_cond_signal(&spaceCond);
 
155
}
 
156
 
 
157
void CommandPipe::signalEmpty() {
 
158
  abs_thread_cond_signal(&emptyCond);
 
159
}
 
160
 
 
161
void CommandPipe::signalData() {
 
162
  abs_thread_cond_signal(&dataCond);
 
163
}