2
thread safe fifo queue for commands
3
Copyright (C) 2000 Martin Vogt
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.
9
For more information look at the file COPYRIGHT in this package
14
#include "commandPipe.h"
16
#define _COMMAND_ARRAY_SIZE 100
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);
31
commandArray=new Command*[_COMMAND_ARRAY_SIZE];
34
for(i=0;i<_COMMAND_ARRAY_SIZE;i++) {
35
commandArray[i]=new Command(_COMMAND_NONE,0);
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);
49
for(i=0;i<_COMMAND_ARRAY_SIZE;i++) {
50
delete commandArray[i];
55
void CommandPipe::sendCommand(Command& cmd) {
56
sendCommand(cmd,true);
59
void CommandPipe::sendCommandNoWait(Command& cmd) {
60
sendCommand(cmd,false);
64
void CommandPipe::sendCommand(Command& cmd,int lWait) {
66
if (entries==_COMMAND_ARRAY_SIZE) {
69
cmd.copyTo(commandArray[writePos]);
71
if (writePos==_COMMAND_ARRAY_SIZE) {
86
int CommandPipe::hasCommand(Command* dest) {
93
commandArray[readPos]->copyTo(dest);
95
if (readPos==_COMMAND_ARRAY_SIZE) {
105
// if we are on highwater, signal space
106
if (entries==_COMMAND_ARRAY_SIZE-1) {
114
void CommandPipe::waitForEmptyQueue() {
116
while (entries > 0) {
122
void CommandPipe::waitForCommand() {
124
while (entries == 0) {
131
void CommandPipe::lockCommandPipe() {
132
abs_thread_mutex_lock(&pipeMut);
136
void CommandPipe::unlockCommandPipe() {
137
abs_thread_mutex_unlock(&pipeMut);
140
void CommandPipe::waitForSpace() {
141
abs_thread_cond_wait(&spaceCond,&pipeMut);
144
void CommandPipe::waitForEmpty() {
145
abs_thread_cond_wait(&emptyCond,&pipeMut);
148
void CommandPipe::waitForData() {
149
abs_thread_cond_wait(&dataCond,&pipeMut);
153
void CommandPipe::signalSpace() {
154
abs_thread_cond_signal(&spaceCond);
157
void CommandPipe::signalEmpty() {
158
abs_thread_cond_signal(&emptyCond);
161
void CommandPipe::signalData() {
162
abs_thread_cond_signal(&dataCond);