mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-04-10 18:51:21 +03:00
0028657: Foundation Classes - OSD_Thread does not release thread resources on non-Windows platforms
~OSD_Thread(), OSD_Thread::SetFunction() and OSD_Thread::Run() now detach old thread also on platforms using pthreads. OSD_Thread::Wait() now closes thread handle after joining.
This commit is contained in:
parent
6fd4ffd9eb
commit
b2ec0b4da5
@ -13,7 +13,6 @@
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
|
||||
#include <OSD_Thread.hxx>
|
||||
|
||||
//=============================================
|
||||
@ -51,50 +50,31 @@ void OSD_Thread::Assign (const OSD_Thread &other)
|
||||
myFunc = other.myFunc;
|
||||
myPriority = other.myPriority;
|
||||
|
||||
// detach current thread
|
||||
Detach();
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// On Windows, close current handle
|
||||
if ( myThread )
|
||||
CloseHandle ( myThread );
|
||||
myThread = 0;
|
||||
|
||||
// and replace it by duplicate of the source handle
|
||||
// duplicate the source handle
|
||||
if ( other.myThread ) {
|
||||
HANDLE hProc = GetCurrentProcess(); // we are always within the same process
|
||||
DuplicateHandle ( hProc, other.myThread, hProc, &myThread,
|
||||
0, TRUE, DUPLICATE_SAME_ACCESS );
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// On Unix/Linux, just copy the thread id
|
||||
myThread = other.myThread;
|
||||
|
||||
#endif
|
||||
|
||||
myThreadId = other.myThreadId;
|
||||
}
|
||||
|
||||
//=============================================
|
||||
// OSD_Thread::Destroy
|
||||
// OSD_Thread::~OSD_Thread
|
||||
//=============================================
|
||||
|
||||
void OSD_Thread::Destroy ()
|
||||
OSD_Thread::~OSD_Thread()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
|
||||
// On Windows, close current handle
|
||||
if ( myThread )
|
||||
CloseHandle ( myThread );
|
||||
|
||||
#else
|
||||
|
||||
// On Unix/Linux, do nothing
|
||||
|
||||
#endif
|
||||
|
||||
myThread = 0;
|
||||
myThreadId = 0;
|
||||
Detach();
|
||||
}
|
||||
|
||||
//=============================================
|
||||
@ -118,7 +98,7 @@ void OSD_Thread::SetPriority (const Standard_Integer thePriority)
|
||||
void OSD_Thread::SetFunction (const OSD_ThreadFunction &func)
|
||||
{
|
||||
// close current handle if any
|
||||
Destroy();
|
||||
Detach();
|
||||
myFunc = func;
|
||||
}
|
||||
|
||||
@ -151,15 +131,11 @@ Standard_Boolean OSD_Thread::Run (const Standard_Address data,
|
||||
{
|
||||
if ( ! myFunc ) return Standard_False;
|
||||
|
||||
myThreadId = 0;
|
||||
// detach current thread, if open
|
||||
Detach();
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// On Windows, close current handle if open
|
||||
if ( myThread )
|
||||
CloseHandle ( myThread );
|
||||
myThread = 0;
|
||||
|
||||
// allocate intermediate data structure to pass both data parameter and address
|
||||
// of the real thread function to Windows thread wrapper function
|
||||
WNTthread_data *adata = (WNTthread_data*)malloc ( sizeof(WNTthread_data) );
|
||||
@ -180,11 +156,14 @@ Standard_Boolean OSD_Thread::Run (const Standard_Address data,
|
||||
|
||||
#else
|
||||
|
||||
// On Unix/Linux, create a new thread
|
||||
if ( pthread_create ( &myThread, 0, myFunc, data ) )
|
||||
if (pthread_create (&myThread, 0, myFunc, data) != 0)
|
||||
{
|
||||
myThread = 0;
|
||||
else myThreadId = myThread;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
myThreadId = myThread;
|
||||
}
|
||||
#endif
|
||||
return myThread != 0;
|
||||
}
|
||||
@ -217,40 +196,43 @@ void OSD_Thread::Detach ()
|
||||
// OSD_Thread::Wait
|
||||
//=============================================
|
||||
|
||||
Standard_Boolean OSD_Thread::Wait () const
|
||||
{
|
||||
Standard_Address aRes = 0;
|
||||
return Wait ( aRes );
|
||||
}
|
||||
|
||||
//=============================================
|
||||
// OSD_Thread::Wait
|
||||
//=============================================
|
||||
|
||||
Standard_Boolean OSD_Thread::Wait (Standard_Address &result) const
|
||||
Standard_Boolean OSD_Thread::Wait (Standard_Address& theResult)
|
||||
{
|
||||
// check that thread handle is not null
|
||||
result = 0;
|
||||
theResult = 0;
|
||||
if (!myThread)
|
||||
{
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// On Windows, wait for the thread handle to be signaled
|
||||
if (WaitForSingleObject (myThread, INFINITE) != WAIT_OBJECT_0)
|
||||
{
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
// and convert result of the thread execution to Standard_Address
|
||||
DWORD anExitCode;
|
||||
if (GetExitCodeThread (myThread, &anExitCode))
|
||||
result = ULongToPtr (anExitCode);
|
||||
{
|
||||
theResult = ULongToPtr (anExitCode);
|
||||
}
|
||||
|
||||
CloseHandle (myThread);
|
||||
myThread = 0;
|
||||
myThreadId = 0;
|
||||
return Standard_True;
|
||||
|
||||
#else
|
||||
|
||||
// On Unix/Linux, join the thread
|
||||
return ! pthread_join ( myThread, &result );
|
||||
if (pthread_join (myThread, &theResult) != 0)
|
||||
{
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
myThread = 0;
|
||||
myThreadId = 0;
|
||||
return Standard_True;
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -258,22 +240,30 @@ Standard_Boolean OSD_Thread::Wait (Standard_Address &result) const
|
||||
// OSD_Thread::Wait
|
||||
//=============================================
|
||||
|
||||
Standard_Boolean OSD_Thread::Wait (const Standard_Integer theTimeMs, Standard_Address &result) const
|
||||
Standard_Boolean OSD_Thread::Wait (const Standard_Integer theTimeMs,
|
||||
Standard_Address& theResult)
|
||||
{
|
||||
// check that thread handle is not null
|
||||
result = 0;
|
||||
theResult = 0;
|
||||
if (!myThread)
|
||||
{
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// On Windows, wait for the thread handle to be signaled
|
||||
DWORD ret = WaitForSingleObject (myThread, theTimeMs);
|
||||
if (ret == WAIT_OBJECT_0)
|
||||
{
|
||||
DWORD anExitCode;
|
||||
if (GetExitCodeThread (myThread, &anExitCode))
|
||||
result = ULongToPtr (anExitCode);
|
||||
{
|
||||
theResult = ULongToPtr (anExitCode);
|
||||
}
|
||||
|
||||
CloseHandle (myThread);
|
||||
myThread = 0;
|
||||
myThreadId = 0;
|
||||
return Standard_True;
|
||||
}
|
||||
else if (ret == WAIT_TIMEOUT)
|
||||
@ -282,7 +272,6 @@ Standard_Boolean OSD_Thread::Wait (const Standard_Integer theTimeMs, Standard_Ad
|
||||
}
|
||||
|
||||
return Standard_False;
|
||||
|
||||
#else
|
||||
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
|
||||
#if __GLIBC_PREREQ(2,4)
|
||||
@ -302,13 +291,22 @@ Standard_Boolean OSD_Thread::Wait (const Standard_Integer theTimeMs, Standard_Ad
|
||||
aTimeout.tv_sec += aSeconds;
|
||||
aTimeout.tv_nsec += aMicroseconds * 1000;
|
||||
|
||||
return pthread_timedjoin_np (myThread, &result, &aTimeout) == 0;
|
||||
if (pthread_timedjoin_np (myThread, &theResult, &aTimeout) != 0)
|
||||
{
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
#else
|
||||
// join the thread without timeout
|
||||
(void )theTimeMs;
|
||||
return pthread_join (myThread, &result) == 0;
|
||||
if (pthread_join (myThread, &theResult) != 0)
|
||||
{
|
||||
return Standard_False;
|
||||
}
|
||||
#endif
|
||||
|
||||
myThread = 0;
|
||||
myThreadId = 0;
|
||||
return Standard_True;
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -333,4 +331,3 @@ Standard_ThreadId OSD_Thread::Current ()
|
||||
return pthread_self();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,6 @@ public:
|
||||
|
||||
DEFINE_STANDARD_ALLOC
|
||||
|
||||
|
||||
//! Empty constructor
|
||||
Standard_EXPORT OSD_Thread();
|
||||
|
||||
@ -56,13 +55,8 @@ void operator = (const OSD_Thread& other)
|
||||
Assign(other);
|
||||
}
|
||||
|
||||
//! Destructor. On Windows, closes handle to the thread.
|
||||
//! On UNIX/Linux, does nothing.
|
||||
Standard_EXPORT void Destroy();
|
||||
~OSD_Thread()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
//! Destructor. Detaches the thread if it wasn't done already.
|
||||
Standard_EXPORT ~OSD_Thread();
|
||||
|
||||
Standard_EXPORT void SetPriority (const Standard_Integer thePriority);
|
||||
|
||||
@ -90,7 +84,12 @@ void operator = (const OSD_Thread& other)
|
||||
//! release all thread data upon its completion.
|
||||
Standard_EXPORT void Detach();
|
||||
|
||||
Standard_EXPORT Standard_Boolean Wait() const;
|
||||
//! Waits till the thread finishes execution.
|
||||
Standard_Boolean Wait()
|
||||
{
|
||||
Standard_Address aRes = 0;
|
||||
return Wait (aRes);
|
||||
}
|
||||
|
||||
//! Wait till the thread finishes execution.
|
||||
//! Returns True if wait was successful, False in case of error.
|
||||
@ -101,12 +100,12 @@ void operator = (const OSD_Thread& other)
|
||||
//! Note however that it is advisable not to rely upon returned result
|
||||
//! value, as it is not always the value actually returned by the thread
|
||||
//! function. In addition, on Windows it is converted via DWORD.
|
||||
Standard_EXPORT Standard_Boolean Wait (Standard_Address& result) const;
|
||||
Standard_EXPORT Standard_Boolean Wait (Standard_Address& theResult);
|
||||
|
||||
//! Waits for some time and if the thread is finished,
|
||||
//! it returns the result.
|
||||
//! The function returns false if the thread is not finished yet.
|
||||
Standard_EXPORT Standard_Boolean Wait (const Standard_Integer time, Standard_Address& result) const;
|
||||
Standard_EXPORT Standard_Boolean Wait (const Standard_Integer time, Standard_Address& theResult);
|
||||
|
||||
//! Returns ID of the currently controlled thread ID,
|
||||
//! or 0 if no thread is run
|
||||
@ -115,31 +114,13 @@ void operator = (const OSD_Thread& other)
|
||||
//! Auxiliary: returns ID of the current thread
|
||||
Standard_EXPORT static Standard_ThreadId Current();
|
||||
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
|
||||
OSD_ThreadFunction myFunc;
|
||||
OSD_PThread myThread;
|
||||
Standard_ThreadId myThreadId;
|
||||
Standard_Integer myPriority;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // _OSD_Thread_HeaderFile
|
||||
|
Loading…
x
Reference in New Issue
Block a user