Skip to content

Commit

Permalink
Avoid potential endless wait in dtor by calling TerminateThread
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeu committed Mar 4, 2015
1 parent d7738e7 commit 289829a
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions Modelica_DeviceDrivers/Resources/Include/MDDSerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ DWORD WINAPI MDD_serialPortReceivingThread(LPVOID p_serial) {
commSync.hEvent = commEvnt;
ClearCommError(serial->hComm, &commErr, &commStat);

while (serial->hComm != INVALID_HANDLE_VALUE) {
while (serial->hComm != INVALID_HANDLE_VALUE && serial->receiving == 1) {
BOOL waitState = WaitCommEvent(serial->hComm, &fdwEventMask, &commSync);
if (!waitState) {
if (GetLastError() == ERROR_IO_PENDING) {
Expand All @@ -61,10 +61,6 @@ DWORD WINAPI MDD_serialPortReceivingThread(LPVOID p_serial) {
}
}

if (serial->receiving != 1) {
ExitThread(0);
}

if (fdwEventMask & (EV_RXCHAR | EV_BREAK | EV_ERR | EV_TXEMPTY)) {
COMSTAT curStatus;
EnterCriticalSection(&serial->receiveLock);
Expand Down Expand Up @@ -126,7 +122,6 @@ DllExport void * MDD_serialPortConstructor(const char * deviceName, int bufferSi
free(serial);
serial = NULL;
ModelicaFormatError("MDDSerialPort.h: CreateFileA(..) of serial port %s failed with error %d\n", deviceName, GetLastError());
return serial;
}
ModelicaFormatMessage("Created serial port for device %s\n", deviceName);

Expand All @@ -137,7 +132,6 @@ DllExport void * MDD_serialPortConstructor(const char * deviceName, int bufferSi
free(serial);
serial = NULL;
ModelicaFormatError("MDDSerialPort.h: GetCommState(..) of serial port %s failed with error %d\n", deviceName, GetLastError());
return serial;
}

switch (baud) {
Expand Down Expand Up @@ -203,7 +197,6 @@ DllExport void * MDD_serialPortConstructor(const char * deviceName, int bufferSi
free(serial);
serial = NULL;
ModelicaFormatError("MDDSerialPort.h: SetCommState(..) of serial port %s failed with error %d\n", deviceName, GetLastError());
return serial;
}

if (!GetCommMask(serial->hComm, &fdwEventMask)) {
Expand Down Expand Up @@ -282,8 +275,9 @@ DllExport void MDD_serialPortDestructor(void * p_serial) {
MDD_serialPortSend(p_serial, &c, 1);
if (serial->hThread) {
DWORD dwEc = 1;
while (GetExitCodeThread(serial->hThread, &dwEc) && dwEc == STILL_ACTIVE) {
;
WaitForSingleObject(serial->hThread, 1000);
if (GetExitCodeThread(serial->hThread, &dwEc) && dwEc == STILL_ACTIVE) {
TerminateThread(serial->hThread, 1);
}
CloseHandle(serial->hThread);
DeleteCriticalSection(&serial->receiveLock);
Expand Down

0 comments on commit 289829a

Please sign in to comment.