commit 6351098ec1a244e6f628e0bbdb80ed619c14fb2a
parent 56e3b07cbd47e2fc2c70ac7969b4cd4b1e1007f4
Author: fundamental <[email protected]>
Date: Sat, 19 Dec 2009 19:24:22 -0500
Nio: Started using pthread_join
Diffstat:
6 files changed, 16 insertions(+), 25 deletions(-)
diff --git a/src/Nio/AlsaEngine.cpp b/src/Nio/AlsaEngine.cpp
@@ -42,15 +42,11 @@ AlsaEngine::AlsaEngine(OutMgr *out)
// midi.alsaId = -1;
// midi.pThread = 0;
- pthread_mutex_init(&close_m, NULL);
}
AlsaEngine::~AlsaEngine()
{
Stop();
- pthread_mutex_lock(&close_m);
- pthread_mutex_unlock(&close_m);
- pthread_mutex_destroy(&close_m);
}
@@ -111,7 +107,6 @@ void *AlsaEngine::_AudioThread(void *arg)
void *AlsaEngine::AudioThread()
{
RunStuff();
- return NULL;
}
@@ -210,7 +205,7 @@ bool AlsaEngine::Start()
enabled = true;
pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&audio.pThread, &attr, _AudioThread, this);
// if (NULL != midi.handle)
@@ -233,6 +228,7 @@ void AlsaEngine::Stop()
if(!enabled())
return;
enabled = false;
+ pthread_join(audio.pThread, NULL);
if (NULL != audio.handle && audio.pThread)
if (pthread_cancel(audio.pThread))
@@ -417,7 +413,6 @@ bool AlsaEngine::OpenStuff()
void AlsaEngine::RunStuff()
{
- pthread_mutex_lock(&close_m);
while (enabled()) {
buffer = interleave(getNext());
rc = snd_pcm_writei(handle, buffer, SOUND_BUFFER_SIZE);
@@ -432,5 +427,5 @@ void AlsaEngine::RunStuff()
//else if (rc != (int)frames)
// cerr << "short write, write " << rc << "frames" << endl;
}
- pthread_mutex_unlock(&close_m);
+ pthread_exit(NULL);
}
diff --git a/src/Nio/NulEngine.cpp b/src/Nio/NulEngine.cpp
@@ -49,7 +49,7 @@ void *NulEngine::AudioThread()
const Stereo<Sample> smps = getNext();
dummyOut();
}
- return NULL;
+ pthread_exit(NULL);
}
void NulEngine::dummyOut()
@@ -75,7 +75,6 @@ void NulEngine::dummyOut()
playing_until.tv_usec -= remaining;
playing_until.tv_sec += playing_until.tv_usec / 1000000;
playing_until.tv_usec %= 1000000;
- return;
}
@@ -89,7 +88,7 @@ bool NulEngine::Start()
pthread_attr_t attr;
enabled = true;
pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&pThread, &attr, _AudioThread, this);
return true;
@@ -98,5 +97,6 @@ bool NulEngine::Start()
void NulEngine::Stop()
{
enabled = false;
+ pthread_join(pThread, NULL);
}
diff --git a/src/Nio/OssEngine.cpp b/src/Nio/OssEngine.cpp
@@ -81,7 +81,7 @@ bool OssEngine::Start()
return false;
pthread_attr_t attr;
pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&pThread, &attr, _AudioThread, this);
cout << "Starting Oss";
enabled = true;
@@ -92,6 +92,7 @@ bool OssEngine::Start()
void OssEngine::Stop()
{
enabled = false;
+ pthread_join(pThread, NULL);
close(snd_handle);
}
@@ -113,7 +114,7 @@ void *OssEngine::AudioThread()
const Stereo<Sample> smps = getNext();
OSSout(smps.l().c_buf(),smps.r().c_buf());
}
- return NULL;
+ pthread_exit(NULL);
}
/*
diff --git a/src/Nio/OutMgr.cpp b/src/Nio/OutMgr.cpp
@@ -24,7 +24,6 @@ OutMgr::OutMgr(Master *nmaster)
master = nmaster;
//initialize mutex
- pthread_mutex_init(&close_m, NULL);
pthread_mutex_init(&mutex, NULL);
pthread_mutex_init(&processing, NULL);
pthread_cond_init(&needsProcess, NULL);
@@ -72,13 +71,11 @@ OutMgr::~OutMgr()
pthread_cond_signal(&needsProcess);
pthread_mutex_unlock(&processing);
- pthread_mutex_lock(&close_m);
- pthread_mutex_unlock(&close_m);
+ pthread_join(outThread, NULL);
for(map<string,AudioOut*>::iterator itr = managedOuts.begin();
itr != managedOuts.end(); ++itr) {
delete itr->second;
}
- pthread_mutex_destroy(&close_m);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&processing);
pthread_cond_destroy(&needsProcess);
@@ -92,7 +89,6 @@ void *_outputThread(void *arg)
void *OutMgr::outputThread()
{
- pthread_mutex_lock(&close_m);
//open up the default output
if(!defaultOut->Start())//there should be a better failsafe
cerr << "ERROR: The default Audio Output Failed to Open!" << endl;
@@ -157,15 +153,14 @@ void *OutMgr::outputThread()
cout << "Run Forest Run!" << endl;
}
- pthread_mutex_unlock(&close_m);
- return NULL;
+ pthread_exit(NULL);
}
void OutMgr::run()
{
pthread_attr_t attr;
pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&outThread, &attr, _outputThread, this);
}
diff --git a/src/Nio/OutMgr.h b/src/Nio/OutMgr.h
@@ -62,7 +62,6 @@ class OutMgr
pthread_cond_t needsProcess;
Atomic<int> numRequests;
/**for closing*/
- pthread_mutex_t close_m;
pthread_cond_t close_cond;
/**Buffer*/
Stereo<Sample> smps;
diff --git a/src/Nio/WavEngine.cpp b/src/Nio/WavEngine.cpp
@@ -56,7 +56,7 @@ bool WavEngine::Start()
pthread_attr_t attr;
enabled = true;
pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&pThread, &attr, _AudioThread, this);
return true;
@@ -64,7 +64,8 @@ bool WavEngine::Start()
void WavEngine::Stop()
{
- enabled = false;
+ enabled = false;
+ pthread_join(pThread, NULL);
}
void WavEngine::Close()
@@ -154,7 +155,7 @@ void *WavEngine::AudioThread()
}
write_stereo_samples(size, recordbuf_16bit);
}
- return NULL;
+ pthread_exit(NULL);
}
void WavEngine::write_stereo_samples(int nsmps, short int *smps)