Skip to content

Commit

Permalink
Do not blanket-catch all exceptions from worker threads
Browse files Browse the repository at this point in the history
Unhandled exceptions should crash the process, not unexpectedly stop the
thread.
  • Loading branch information
darksylinc committed Mar 20, 2024
1 parent 9983d53 commit 07f9773
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions OgreMain/include/Threading/OgreThreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,31 @@ THE SOFTWARE.
# define OGRE_THREAD_CALL_CONVENTION
#endif

namespace Ogre
{
template <typename T>
struct DeleteOnDestructor
{
T *ptr;
DeleteOnDestructor( T *_ptr ) : ptr( _ptr ) {}
~DeleteOnDestructor() { delete ptr; }

// Prevent being able to copy this object
DeleteOnDestructor( const DeleteOnDestructor & ) = delete;
DeleteOnDestructor &operator=( const DeleteOnDestructor & ) = delete;
};
} // namespace Ogre

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT
/// See Threads::CreateThread for an example on how to use
# define THREAD_DECLARE( threadFunction ) \
unsigned long OGRE_THREAD_CALL_CONVENTION threadFunction##_internal( void *argName ) \
{ \
unsigned long retVal = 0; \
Ogre::ThreadHandle *threadHandle( reinterpret_cast<Ogre::ThreadHandle *>( argName ) ); \
try \
{ \
threadHandle->_setOsHandleToSelf(); \
retVal = threadFunction( threadHandle ); \
} \
catch( ... ) \
{ \
} \
delete threadHandle; \
Ogre::DeleteOnDestructor<Ogre::ThreadHandle> container( threadHandle ); \
threadHandle->_setOsHandleToSelf(); \
retVal = threadFunction( threadHandle ); \
return retVal; \
}
#else
Expand All @@ -66,16 +75,9 @@ THE SOFTWARE.
{ \
unsigned long retVal = 0; \
Ogre::ThreadHandle *threadHandle( reinterpret_cast<Ogre::ThreadHandle *>( argName ) ); \
try \
{ \
threadHandle->_setOsHandleToSelf(); \
retVal = threadFunction( threadHandle ); \
} \
catch( ... ) \
{ \
} \
delete threadHandle; \
\
Ogre::DeleteOnDestructor<Ogre::ThreadHandle> container( threadHandle ); \
threadHandle->_setOsHandleToSelf(); \
retVal = threadFunction( threadHandle ); \
return (void *)retVal; \
}
#endif
Expand Down

0 comments on commit 07f9773

Please sign in to comment.