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