1
0
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:
kgv 2017-04-15 18:57:30 +03:00 committed by bugmaster
parent 6fd4ffd9eb
commit b2ec0b4da5
2 changed files with 103 additions and 125 deletions

View File

@ -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>
//============================================= //=============================================
@ -47,54 +46,35 @@ OSD_Thread::OSD_Thread (const OSD_Thread &other)
void OSD_Thread::Assign (const OSD_Thread &other) void OSD_Thread::Assign (const OSD_Thread &other)
{ {
// copy function pointer // copy function pointer
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;
} }
@ -141,7 +121,7 @@ static DWORD WINAPI WNTthread_func (LPVOID data)
} }
#endif #endif
Standard_Boolean OSD_Thread::Run (const Standard_Address data, Standard_Boolean OSD_Thread::Run (const Standard_Address data,
#ifdef _WIN32 #ifdef _WIN32
const Standard_Integer WNTStackSize const Standard_Integer WNTStackSize
#else #else
@ -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) );
@ -167,8 +143,8 @@ Standard_Boolean OSD_Thread::Run (const Standard_Address data,
adata->data = data; adata->data = data;
adata->func = myFunc; adata->func = myFunc;
// then try to create a new thread // then try to create a new thread
myThread = CreateThread ( NULL, WNTStackSize, WNTthread_func, myThread = CreateThread ( NULL, WNTStackSize, WNTthread_func,
adata, 0, &myThreadId ); adata, 0, &myThreadId );
if ( myThread ) if ( myThread )
@ -180,12 +156,15 @@ 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
#endif {
myThreadId = myThread;
}
#endif
return myThread != 0; return myThread != 0;
} }
@ -197,17 +176,17 @@ void OSD_Thread::Detach ()
{ {
#ifdef _WIN32 #ifdef _WIN32
// On Windows, close current handle // On Windows, close current handle
if ( myThread ) if ( myThread )
CloseHandle ( myThread ); CloseHandle ( myThread );
#else #else
// On Unix/Linux, detach a thread // On Unix/Linux, detach a thread
if ( myThread ) if ( myThread )
pthread_detach ( myThread ); pthread_detach ( myThread );
#endif #endif
myThread = 0; myThread = 0;
myThreadId = 0; myThreadId = 0;
@ -217,63 +196,74 @@ 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;
}
#endif myThread = 0;
myThreadId = 0;
return Standard_True;
#endif
} }
//============================================= //=============================================
// 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
} }
@ -325,12 +323,11 @@ Standard_ThreadId OSD_Thread::GetId () const
// OSD_Thread::Current // OSD_Thread::Current
//============================================= //=============================================
Standard_ThreadId OSD_Thread::Current () Standard_ThreadId OSD_Thread::Current ()
{ {
#ifdef _WIN32 #ifdef _WIN32
return GetCurrentThreadId(); return GetCurrentThreadId();
#else #else
return pthread_self(); return pthread_self();
#endif #endif
} }

View File

@ -30,49 +30,43 @@
//! A simple platform-intependent interface to execute //! A simple platform-intependent interface to execute
//! and control threads. //! and control threads.
class OSD_Thread class OSD_Thread
{ {
public: public:
DEFINE_STANDARD_ALLOC DEFINE_STANDARD_ALLOC
//! Empty constructor //! Empty constructor
Standard_EXPORT OSD_Thread(); Standard_EXPORT OSD_Thread();
//! Initialize the tool by the thread function //! Initialize the tool by the thread function
//! //!
//! Note: On Windows, you might have to take an address of the thread //! Note: On Windows, you might have to take an address of the thread
//! function explicitly to pass it to this constructor without compiler error //! function explicitly to pass it to this constructor without compiler error
Standard_EXPORT OSD_Thread(const OSD_ThreadFunction& func); Standard_EXPORT OSD_Thread(const OSD_ThreadFunction& func);
//! Copy constructor //! Copy constructor
Standard_EXPORT OSD_Thread(const OSD_Thread& other); Standard_EXPORT OSD_Thread(const OSD_Thread& other);
//! Copy thread handle from other OSD_Thread object. //! Copy thread handle from other OSD_Thread object.
Standard_EXPORT void Assign (const OSD_Thread& other); Standard_EXPORT void Assign (const OSD_Thread& other);
void operator = (const OSD_Thread& other) 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);
//! Initialize the tool by the thread function. //! Initialize the tool by the thread function.
//! If the current thread handle is not null, nullifies it. //! If the current thread handle is not null, nullifies it.
//! //!
//! Note: On Windows, you might have to take an address of the thread //! Note: On Windows, you might have to take an address of the thread
//! function explicitly to pass it to this method without compiler error //! function explicitly to pass it to this method without compiler error
Standard_EXPORT void SetFunction (const OSD_ThreadFunction& func); Standard_EXPORT void SetFunction (const OSD_ThreadFunction& func);
//! Starts a thread with thread function given in constructor, //! Starts a thread with thread function given in constructor,
//! passing the specified input data (as void *) to it. //! passing the specified input data (as void *) to it.
//! The parameter \a WNTStackSize (on Windows only) //! The parameter \a WNTStackSize (on Windows only)
@ -80,7 +74,7 @@ void operator = (const OSD_Thread& other)
//! (by default - the same as for the current executable). //! (by default - the same as for the current executable).
//! Returns True if thread started successfully //! Returns True if thread started successfully
Standard_EXPORT Standard_Boolean Run (const Standard_Address data = 0, const Standard_Integer WNTStackSize = 0); Standard_EXPORT Standard_Boolean Run (const Standard_Address data = 0, const Standard_Integer WNTStackSize = 0);
//! Detaches the execution thread from this Thread object, //! Detaches the execution thread from this Thread object,
//! so that it cannot be waited. //! so that it cannot be waited.
//! Note that mechanics of this operation is different on //! Note that mechanics of this operation is different on
@ -89,9 +83,14 @@ void operator = (const OSD_Thread& other)
//! However, the purpose is the same: to instruct the system to //! However, the purpose is the same: to instruct the system to
//! 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,45 +100,27 @@ 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
Standard_EXPORT Standard_ThreadId GetId() const; Standard_EXPORT Standard_ThreadId GetId() const;
//! 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