Commit 308d46a4 authored by sletz's avatar sletz
Browse files

Correct thread code

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@1232 0c269be4-1314-0410-8aa9-9f06e86f4224
parent 00a8d52a
......@@ -19,7 +19,7 @@
#define PRINTDEBUG
#define VERSION "0.58"
#define VERSION "0.59"
#define FORK_SERVER 1
......
......@@ -43,10 +43,12 @@ void* JackPosixThread::ThreadHandler(void* arg)
JackLog("ThreadHandler: start\n");
// If Init succeed, start the thread loop
while ((obj->fRunning = runnable->Execute())) {
//pthread_testcancel();
}
// If Init succeed, start the thread loop
bool res = true;
while (obj->fRunning && res) {
res = runnable->Execute();
//pthread_testcancel();
}
JackLog("ThreadHandler: exit\n");
return 0;
......@@ -132,8 +134,9 @@ int JackPosixThread::Kill()
JackLog("JackPosixThread::Kill\n");
void* status;
pthread_cancel(fThread);
pthread_join(fThread, &status);
pthread_join(fThread, &status);
fRunning = false;
fThread = (pthread_t)NULL;
return 0;
} else {
return -1;
......@@ -147,6 +150,7 @@ int JackPosixThread::Stop()
void* status;
fRunning = false; // Request for the thread to stop
pthread_join(fThread, &status);
fThread = (pthread_t)NULL;
return 0;
} else {
return -1;
......
......@@ -89,12 +89,7 @@ class JackThread
virtual void SetParams(UInt64 period, UInt64 computation, UInt64 constraint) // Empty implementation, will only make sense on OSX...
{}
virtual pthread_t GetThreadID() = 0;
bool IsRunning()
{
return fRunning;
}
virtual pthread_t GetThreadID() = 0;
};
} // end of namespace
......
......@@ -259,23 +259,6 @@
4BFA99AA0AAAF40C009E916C /* jdelay.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4BFA99A90AAAF40C009E916C /* jdelay.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXBuildStyle section */
4BEC9CDB0AAEA43A0088025A /* Development */ = {
isa = PBXBuildStyle;
buildSettings = {
COPY_PHASE_STRIP = NO;
};
name = Development;
};
4BEC9CDC0AAEA43A0088025A /* Deployment */ = {
isa = PBXBuildStyle;
buildSettings = {
COPY_PHASE_STRIP = YES;
};
name = Deployment;
};
/* End PBXBuildStyle section */
/* Begin PBXContainerItemProxy section */
4B699DB3097D421700A18468 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
......@@ -1742,12 +1725,6 @@
08FB7793FE84155DC02AAC07 /* Project object */ = {
isa = PBXProject;
buildConfigurationList = 4B699DD5097D427F00A18468 /* Build configuration list for PBXProject "Jackdmp" */;
buildSettings = {
};
buildStyles = (
4BEC9CDB0AAEA43A0088025A /* Development */,
4BEC9CDC0AAEA43A0088025A /* Deployment */,
);
hasScannedForEncodings = 1;
mainGroup = 08FB7794FE84155DC02AAC07 /* JackServer */;
projectDirPath = "";
......
......@@ -53,7 +53,7 @@ int JackClientPipeThread::Open(JackServer* server) // Open the Server/Client con
fServer = server;
// Start listening
if (fThread->Start() != 0){
if (fThread->Start() != 0) {
jack_error("Cannot start Jack server listener\n");
return -1;
} else {
......@@ -63,30 +63,32 @@ int JackClientPipeThread::Open(JackServer* server) // Open the Server/Client con
void JackClientPipeThread::Close() // Close the Server/Client connection
{
fThread->Kill();
fThread->Kill();
fPipe->Close();
fRefNum = -1;
}
bool JackClientPipeThread::Execute()
{
JackLog("JackClientPipeThread::Execute\n");
return (HandleRequest() == 0);
bool JackClientPipeThread::Execute()
{
JackLog("JackClientPipeThread::Execute\n");
return (HandleRequest() == 0);
}
int JackClientPipeThread::HandleRequest()
{
// Read header
JackRequest header;
int res = header.Read(fPipe);
int ret = 0;
JackRequest header;
int res = header.Read(fPipe);
int ret = 0;
// Lock the global mutex
if (WaitForSingleObject(fMutex, INFINITE) == WAIT_FAILED)
jack_error("JackClientPipeThread::HandleRequest: mutex wait error");
if (res < 0) {
jack_error("HandleRequest: cannot read header");
KillClient();
ret = -1;
jack_error("JackClientPipeThread::HandleRequest: mutex wait error");
if (res < 0) {
jack_error("HandleRequest: cannot read header");
KillClient();
ret = -1;
} else {
// Read data
......@@ -97,7 +99,7 @@ int JackClientPipeThread::HandleRequest()
JackClientNewRequest req;
JackClientNewResult res;
if (req.Read(fPipe) == 0)
AddClient(req.fName, &res.fSharedEngine, &res.fSharedClient, &res.fSharedPorts, &res.fResult);
AddClient(req.fName, &res.fSharedEngine, &res.fSharedClient, &res.fSharedPorts, &res.fResult);
res.Write(fPipe);
break;
}
......@@ -105,20 +107,20 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kClientClose: {
JackLog("JackRequest::ClientClose\n");
JackClientCloseRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->ClientClose(req.fRefNum);
res.fResult = fServer->GetEngine()->ClientClose(req.fRefNum);
res.Write(fPipe);
RemoveClient(req.fRefNum);
RemoveClient();
break;
}
case JackRequest::kActivateClient: {
JackActivateRequest req;
JackResult res;
JackLog("JackRequest::ActivateClient\n");
JackLog("JackRequest::ActivateClient\n");
if (req.Read(fPipe) == 0)
res.fResult = fServer->Activate(req.fRefNum);
res.fResult = fServer->Activate(req.fRefNum);
res.Write(fPipe);
break;
}
......@@ -126,9 +128,9 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kDeactivateClient: {
JackLog("JackRequest::DeactivateClient\n");
JackDeactivateRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->Deactivate(req.fRefNum);
res.fResult = fServer->Deactivate(req.fRefNum);
res.Write(fPipe);
break;
}
......@@ -136,9 +138,9 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kRegisterPort: {
JackLog("JackRequest::RegisterPort\n");
JackPortRegisterRequest req;
JackPortRegisterResult res;
JackPortRegisterResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->PortRegister(req.fRefNum, req.fName, req.fFlags, req.fBufferSize, &res.fPortIndex);
res.fResult = fServer->GetEngine()->PortRegister(req.fRefNum, req.fName, req.fFlags, req.fBufferSize, &res.fPortIndex);
res.Write(fPipe);
break;
}
......@@ -146,10 +148,10 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kUnRegisterPort: {
JackLog("JackRequest::UnRegisterPort\n");
JackPortUnRegisterRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->PortUnRegister(req.fRefNum, req.fPortIndex);
res.fResult = fServer->GetEngine()->PortUnRegister(req.fRefNum, req.fPortIndex);
res.Write(fPipe);
break;
}
......@@ -157,9 +159,9 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kConnectNamePorts: {
JackLog("JackRequest::ConnectPorts\n");
JackPortConnectNameRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
res.Write(fPipe);
break;
}
......@@ -167,7 +169,7 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kDisconnectNamePorts: {
JackLog("JackRequest::DisconnectPorts\n");
JackPortDisconnectNameRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
res.Write(fPipe);
......@@ -177,7 +179,7 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kConnectPorts: {
JackLog("JackRequest::ConnectPorts\n");
JackPortConnectRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->PortConnect(req.fRefNum, req.fSrc, req.fDst);
res.Write(fPipe);
......@@ -187,7 +189,7 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kDisconnectPorts: {
JackLog("JackRequest::DisconnectPorts\n");
JackPortDisconnectRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->PortDisconnect(req.fRefNum, req.fSrc, req.fDst);
res.Write(fPipe);
......@@ -197,7 +199,7 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kSetBufferSize: {
JackLog("JackRequest::SetBufferSize\n");
JackSetBufferSizeRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->SetBufferSize(req.fBufferSize);
res.Write(fPipe);
......@@ -207,7 +209,7 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kSetFreeWheel: {
JackLog("JackRequest::SetFreeWheel\n");
JackSetFreeWheelRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->SetFreewheel(req.fOnOff);
res.Write(fPipe);
......@@ -217,7 +219,7 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kReleaseTimebase: {
JackLog("JackRequest::kReleaseTimebase\n");
JackReleaseTimebaseRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->ReleaseTimebase(req.fRefNum);
res.Write(fPipe);
......@@ -227,7 +229,7 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kSetTimebaseCallback: {
JackLog("JackRequest::kSetTimebaseCallback\n");
JackSetTimebaseCallbackRequest req;
JackResult res;
JackResult res;
if (req.Read(fPipe) == 0)
res.fResult = fServer->GetEngine()->SetTimebaseCallback(req.fRefNum, req.fConditionnal);
res.Write(fPipe);
......@@ -236,16 +238,16 @@ int JackClientPipeThread::HandleRequest()
case JackRequest::kNotification: {
JackLog("JackRequest::Notification\n");
JackClientNotificationRequest req;
JackClientNotificationRequest req;
if (req.Read(fPipe) == 0)
fServer->Notify(req.fRefNum, req.fNotify, req.fValue);
fServer->Notify(req.fRefNum, req.fNotify, req.fValue);
break;
}
default:
JackLog("Unknown request %ld\n", header.fType);
break;
}
}
}
// Unlock the global mutex
......@@ -260,11 +262,10 @@ void JackClientPipeThread::AddClient(char* name, int* shared_engine, int* shared
*result = fServer->GetEngine()->ClientNew(name, &fRefNum, shared_engine, shared_client, shared_ports);
}
void JackClientPipeThread::RemoveClient(int refnum)
void JackClientPipeThread::RemoveClient()
{
JackLog("JackWinNamedPipeServerChannel::RemoveClient ref = %d\n", refnum);
fRefNum = -1;
fPipe->Close();
JackLog("JackWinNamedPipeServerChannel::RemoveClient ref = %d\n", fRefNum);
Close();
}
void JackClientPipeThread::KillClient()
......@@ -280,8 +281,7 @@ void JackClientPipeThread::KillClient()
fServer->Notify(fRefNum, JackNotifyChannelInterface::kDeadClient, 0);
}
fRefNum = -1;
fPipe->Close();
Close();
}
JackWinNamedPipeServerChannel::JackWinNamedPipeServerChannel()
......@@ -365,17 +365,17 @@ void JackWinNamedPipeServerChannel::AddClient(JackWinNamedPipeClient* pipe)
JackClientPipeThread* client;
while (it != fClientList.end()) {
client = *it;
if (client->IsRunning()) {
if (client->IsRunning()) {
it++;
} else {
JackLog("Remove client from list\n");
it = fClientList.erase(it);
JackLog("Remove client from list\n");
it = fClientList.erase(it);
delete(client);
}
}
client = new JackClientPipeThread(pipe);
client->Open(fServer);
client->Open(fServer);
// Here we are sure that the client is running (because it's thread is in "running" state).
fClientList.push_back(client);
}
......
......@@ -40,7 +40,7 @@ class JackClientPipeThread : public JackRunnableInterface
int fRefNum;
void AddClient(char* name, int* shared_engine, int* shared_client, int* shared_ports, int* result);
void RemoveClient(int refnum);
void RemoveClient();
void KillClient();
static HANDLE fMutex;
......@@ -57,11 +57,11 @@ class JackClientPipeThread : public JackRunnableInterface
// JackRunnableInterface interface
bool Execute();
// To be used for find out if the object can be deleted
bool IsRunning()
{
return fThread->IsRunning();
return (fRefNum >= 0);
}
};
......
......@@ -20,6 +20,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "JackWinThread.h"
#include "JackError.h"
#include <assert.h>
namespace Jack
{
......@@ -44,67 +45,40 @@ DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
JackLog("ThreadHandler: start\n");
// If Init succeed, start the thread loop
while ((obj->fRunning = runnable->Execute())) {}
bool res = true;
while (obj->fRunning && res) {
res = runnable->Execute();
}
SetEvent(obj->fEvent);
JackLog("ThreadHandler: exit\n");
return 0;
}
int JackWinThread::Start()
JackWinThread::JackWinThread(JackRunnableInterface* runnable)
: JackThread(runnable, 0, false, 0)
{
DWORD id;
fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (fEvent == NULL) {
jack_error("Cannot create event error = %d", GetLastError());
return -1;
}
fRunning = true;
if (fRealTime) {
JackLog("Create RT thread\n");
fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
if (fThread == NULL) {
jack_error("Cannot create thread error = %d", GetLastError());
return -1;
}
if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
jack_error("Cannot set priority class = %d", GetLastError());
return -1;
}
return 0;
} else {
JackLog("Create non RT thread\n");
fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
fThread = NULL;
assert(fEvent);
}
if (fThread == NULL) {
jack_error("Cannot create thread error = %d", GetLastError());
return -1;
}
JackWinThread::~JackWinThread()
{
CloseHandle(fEvent);
}
return 0;
}
int JackWinThread::Start()
{
fRunning = true;
StartSync():
}
int JackWinThread::StartSync()
{
DWORD id;
fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (fEvent == NULL) {
jack_error("Cannot create event error = %d", GetLastError());
return -1;
}
if (fRealTime) {
if (fRealTime) {
JackLog("Create RT thread\n");
fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
......@@ -153,7 +127,7 @@ int JackWinThread::Kill()
JackLog("JackWinThread::Kill\n");
TerminateThread(fThread, 0); /// TO CHECK : dangerous
CloseHandle(fThread);
CloseHandle(fEvent);
fThread = NULL;
fRunning = false;
return 0;
} else {
......@@ -168,10 +142,9 @@ int JackWinThread::Stop()
fRunning = false; // Request for the thread to stop
WaitForSingleObject(fEvent, INFINITE);
CloseHandle(fThread);
CloseHandle(fEvent);
fThread = NULL;
return 0;
} else {
CloseHandle(fEvent);
return -1;
}
}
......@@ -188,7 +161,6 @@ int JackWinThread::AcquireRealTime()
JackLog("JackWinThread::AcquireRealTime OK\n");
return 0;
} else {
CloseHandle(fEvent);
return -1;
}
}
......
......@@ -44,13 +44,9 @@ class JackWinThread : public JackThread
public:
JackWinThread(JackRunnableInterface* runnable)
: JackThread(runnable, 0, false, 0)
{}
virtual ~JackWinThread()
{}
JackWinThread(JackRunnableInterface* runnable);
virtual ~JackWinThread();
int Start();
int StartSync();
int Kill();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment