commit 5a1eaf5f25e0f559ab7b401945389721cb59b7b9
parent 34ae6453cfce684234ca64d7fc446c090175a651
Author: fundamental <mark.d.mccurry@gmail.com>
Date: Fri, 1 Jan 2010 22:58:50 -0500
OutMgr: Reworking to use semaphore over cond
Diffstat:
2 files changed, 20 insertions(+), 41 deletions(-)
diff --git a/src/Nio/OutMgr.cpp b/src/Nio/OutMgr.cpp
@@ -12,14 +12,13 @@ using namespace std;
OutMgr *sysOut;
OutMgr::OutMgr(Master *nmaster)
- :running(false),numRequests(2)
+ :running(false)
{
master = nmaster;
//initialize mutex
- pthread_mutex_init(&mutex, NULL);
- pthread_mutex_init(&processing, NULL);
- pthread_cond_init(&needsProcess, NULL);
+ pthread_mutex_init(&mutex, NULL);
+ sem_init(&requested, PTHREAD_PROCESS_PRIVATE, 0);
//init samples
outr = new REALTYPE[SOUND_BUFFER_SIZE];
@@ -33,15 +32,12 @@ OutMgr::~OutMgr()
itr->second->Stop();
}
running = false;
- pthread_mutex_lock(&processing);
- pthread_cond_signal(&needsProcess);
- pthread_mutex_unlock(&processing);
+ sem_post(&requested);
pthread_join(outThread, NULL);
pthread_mutex_destroy(&mutex);
- pthread_mutex_destroy(&processing);
- pthread_cond_destroy(&needsProcess);
+ sem_destroy(&requested);
}
void *_outputThread(void *arg)
@@ -68,7 +64,6 @@ void *OutMgr::outputThread()
//setup
running = true;
init = true;
- bool doWait = false;
while(running()) {
if(false) {
@@ -78,9 +73,7 @@ void *OutMgr::outputThread()
cout << unmanagedOuts.size();
pthread_mutex_unlock(&mutex);
cout << " outs, ";
- cout << doWait;
- cout << " waits, ";
- cout << numRequests();
+ cout << getRunning();
cout << " requests" << endl;
}
@@ -92,6 +85,7 @@ void *OutMgr::outputThread()
smps = Stereo<Sample>(Sample(SOUND_BUFFER_SIZE, outl),
Sample(SOUND_BUFFER_SIZE, outr));
+ //this mutex might be redundant
pthread_mutex_lock(&mutex);
for(list<Engine*>::iterator itr = sysEngine->engines.begin();
@@ -104,24 +98,12 @@ void *OutMgr::outputThread()
for(list<AudioOut*>::iterator itr = unmanagedOuts.begin();
itr != unmanagedOuts.end(); ++itr) {
(*itr)->out(smps);
- if(false)
- cout << *itr << " ";
}
pthread_mutex_unlock(&mutex);
//wait for next run
- --numRequests;
- doWait = (numRequests()<1);
- if(doWait) {
- pthread_mutex_lock(&processing);
- pthread_cond_wait(&needsProcess, &processing);
- pthread_mutex_unlock(&processing);
- }
- else
- if(false)
- cout << "Run Forest Run!" << endl;
-
+ sem_wait(&requested);
}
pthread_exit(NULL);
return NULL;
@@ -163,16 +145,16 @@ void OutMgr::remove(AudioOut *out)
int OutMgr::getRunning()
{
- return numRequests();
+ int tmp;
+ sem_getvalue(&requested, &tmp);
+ if(tmp < 0)
+ tmp = 0;
+ return tmp;
}
-int OutMgr::requestSamples(unsigned int n)
+void OutMgr::requestSamples(unsigned int n)
{
- numRequests = numRequests() + n;
-
- pthread_mutex_lock(&processing);
- pthread_cond_signal(&needsProcess);
- pthread_mutex_unlock(&processing);
- return 0;
+ for(unsigned int i = 0; i < n; ++i)
+ sem_post(&requested);
}
diff --git a/src/Nio/OutMgr.h b/src/Nio/OutMgr.h
@@ -9,6 +9,7 @@
#include <map>
#include <string>
#include <pthread.h>
+#include <semaphore.h>
class AudioOut;
@@ -29,7 +30,7 @@ class OutMgr
/**Request a new set of samples
* @param n number of requested samples (defaults to 1)
* @return -1 for locking issues 0 for valid request*/
- int requestSamples(unsigned int n=1);
+ void requestSamples(unsigned int n=1);
/**Return the number of building samples*/
int getRunning();
@@ -56,13 +57,9 @@ class OutMgr
std::list<AudioOut *> unmanagedOuts;
mutable pthread_mutex_t mutex;
- pthread_mutex_t processing;
-
pthread_t outThread;
- pthread_cond_t needsProcess;
- Atomic<int> numRequests;
- /**for closing*/
- pthread_cond_t close_cond;
+ sem_t requested;
+
/**Buffer*/
Stereo<Sample> smps;
REALTYPE *outl;