mirror of
https://github.com/introlab/rtabmap.git
synced 2026-10-04 09:07:47 +08:00
Included UtiLite library directly in Rtabmap source (to simplify the installation)
git-svn-id: http://rtabmap.googlecode.com/svn/trunk/rtabmap@857 f169173b-cf89-36c8-b27e-44dbe73f0c83
This commit is contained in:
@@ -0,0 +1,400 @@
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Written by Phillip Sitbon
|
||||
// Copyright 2003
|
||||
//
|
||||
// Modified by Mathieu Labbe
|
||||
//
|
||||
// Posix/Thread.h
|
||||
// - Posix thread
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _U_Thread_Posix_
|
||||
#define _U_Thread_Posix_
|
||||
|
||||
#include "rtabmap/utilite/USemaphore.h"
|
||||
#include "rtabmap/utilite/UMutex.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
/**
|
||||
* Calling thread sleeps for some milliseconds.
|
||||
*/
|
||||
inline void uSleep(unsigned int ms)
|
||||
{
|
||||
struct timespec req;
|
||||
struct timespec rem;
|
||||
req.tv_sec = ms / 1000;
|
||||
req.tv_nsec = (ms - req.tv_sec * 1000) * 1000 * 1000;
|
||||
nanosleep (&req, &rem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling thread sleeps for some microseconds.
|
||||
*/
|
||||
inline void uSleepMicro(unsigned int us)
|
||||
{
|
||||
struct timespec req;
|
||||
struct timespec rem;
|
||||
req.tv_sec = us / 1000000;
|
||||
req.tv_nsec = (us - req.tv_sec * 1000000) * 1000;
|
||||
nanosleep (&req, &rem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling thread sleeps for some nanoseconds.
|
||||
*/
|
||||
inline void uSleepNano(unsigned int ns)
|
||||
{
|
||||
struct timespec req;
|
||||
struct timespec rem;
|
||||
req.tv_sec = ns / 1000000000;
|
||||
req.tv_nsec = (ns - req.tv_sec * 1000000000);
|
||||
nanosleep (&req, &rem);
|
||||
}
|
||||
|
||||
|
||||
#define InvalidHandle 0
|
||||
#define THREAD_HANDLE pthread_t
|
||||
|
||||
typedef void *( * pthread_fn )( void * );
|
||||
|
||||
template
|
||||
<
|
||||
typename Thread_T
|
||||
>
|
||||
class UThreadC
|
||||
{
|
||||
private:
|
||||
struct Instance;
|
||||
|
||||
public:
|
||||
typedef Thread_T & Thread_R;
|
||||
typedef const Thread_T & Thread_C_R;
|
||||
|
||||
typedef THREAD_HANDLE Handle;
|
||||
typedef void ( *Handler)( Thread_R );
|
||||
|
||||
virtual ~UThreadC() {}
|
||||
|
||||
protected:
|
||||
UThreadC() {}
|
||||
|
||||
virtual void ThreadMain( Thread_R ) = 0;
|
||||
|
||||
static void Exit()
|
||||
{ pthread_exit(0); }
|
||||
|
||||
static void TestCancel()
|
||||
{ pthread_testcancel(); }
|
||||
|
||||
static Handle Self()
|
||||
{ return (Handle)pthread_self(); }
|
||||
|
||||
public:
|
||||
|
||||
static int Create(
|
||||
const Handler & Function,
|
||||
Thread_C_R Param,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false,
|
||||
const bool & CancelAsync = false
|
||||
)
|
||||
{
|
||||
M_Create().lock();
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
if ( CreateDetached )
|
||||
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
|
||||
|
||||
if ( StackSize )
|
||||
pthread_attr_setstacksize(&attr,StackSize);
|
||||
|
||||
Instance I(Param,0,Function,CancelEnable,CancelAsync);
|
||||
|
||||
Handle h=InvalidHandle;
|
||||
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
if(H) *H = h;
|
||||
if ( !R ) S_Create().acquire();
|
||||
|
||||
M_Create().unlock();
|
||||
return R;
|
||||
}
|
||||
|
||||
int Create(
|
||||
Thread_C_R Param,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false,
|
||||
const bool & CancelAsync = false
|
||||
) const
|
||||
{
|
||||
M_Create().lock();
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
if ( CreateDetached )
|
||||
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
|
||||
|
||||
if ( StackSize )
|
||||
pthread_attr_setstacksize(&attr,StackSize);
|
||||
|
||||
Instance I(Param,const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
|
||||
|
||||
Handle h=InvalidHandle;
|
||||
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
if(H) *H = h;
|
||||
if ( !R ) S_Create().acquire();
|
||||
|
||||
M_Create().unlock();
|
||||
return R;
|
||||
}
|
||||
|
||||
static int Join( Handle H )
|
||||
{ return pthread_join(H,0); }
|
||||
|
||||
static int Kill( Handle H )
|
||||
{ return pthread_cancel(H); }
|
||||
|
||||
static int Detach( Handle H )
|
||||
{ return pthread_detach(H); }
|
||||
|
||||
private:
|
||||
|
||||
static const UMutex &M_Create() { static UMutex M; return M; }
|
||||
static USemaphore &S_Create() { static USemaphore S; return S; }
|
||||
|
||||
static void *ThreadMainHandler( Instance *Param )
|
||||
{
|
||||
Instance I(*Param);
|
||||
Thread_T Data(I.Data);
|
||||
S_Create().release();
|
||||
|
||||
if ( I.Flags & 1 /*CancelEnable*/ )
|
||||
{
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
|
||||
|
||||
if ( I.Flags & 2 /*CancelAsync*/ )
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
|
||||
else
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
|
||||
}
|
||||
|
||||
if ( I.Owner )
|
||||
I.Owner->ThreadMain(Data);
|
||||
else
|
||||
I.pFN(Data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct Instance
|
||||
{
|
||||
Instance( Thread_C_R P, UThreadC<Thread_T> *const &O, const UThreadC<Thread_T>::Handler &pH = 0, const bool &CE=false, const bool &CA=false )
|
||||
: Data(P), Owner(O), pFN(pH), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; }
|
||||
|
||||
Thread_C_R Data;
|
||||
UThreadC<Thread_T> * Owner;
|
||||
Handler pFN;
|
||||
unsigned char Flags;
|
||||
};
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Explicit specialization, no thread parameters
|
||||
//
|
||||
template<>
|
||||
class UThreadC<void>
|
||||
{
|
||||
private:
|
||||
struct Instance;
|
||||
|
||||
public:
|
||||
typedef THREAD_HANDLE Handle;
|
||||
typedef void ( *Handler)();
|
||||
|
||||
virtual ~UThreadC<void>() {}
|
||||
|
||||
protected:
|
||||
UThreadC<void>() {}
|
||||
|
||||
virtual void ThreadMain() = 0;
|
||||
|
||||
static void Exit()
|
||||
{ pthread_exit(0); }
|
||||
|
||||
static void TestCancel()
|
||||
{ pthread_testcancel(); }
|
||||
|
||||
static Handle Self()
|
||||
{ return (Handle)pthread_self(); }
|
||||
|
||||
public:
|
||||
|
||||
static int Create(
|
||||
const Handler & Function,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false,
|
||||
const bool & CancelAsync = false
|
||||
)
|
||||
{
|
||||
M_Create().lock();
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
if ( CreateDetached )
|
||||
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
|
||||
|
||||
if ( StackSize )
|
||||
pthread_attr_setstacksize(&attr,StackSize);
|
||||
|
||||
Instance I(0,Function,CancelEnable,CancelAsync);
|
||||
|
||||
Handle h=InvalidHandle;
|
||||
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
if(H) *H = h;
|
||||
if ( !R ) S_Create().acquire();
|
||||
|
||||
M_Create().unlock();
|
||||
return R;
|
||||
}
|
||||
|
||||
int Create(
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false,
|
||||
const bool & CancelAsync = false
|
||||
) const
|
||||
{
|
||||
M_Create().lock();
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
if ( CreateDetached )
|
||||
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
|
||||
|
||||
if ( StackSize )
|
||||
pthread_attr_setstacksize(&attr,StackSize);
|
||||
|
||||
Instance I(const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
|
||||
|
||||
Handle h=InvalidHandle;
|
||||
int R = pthread_create((pthread_t *)&h,&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
if(H) *H = h;
|
||||
if ( !R ) S_Create().acquire();
|
||||
|
||||
M_Create().unlock();
|
||||
return R;
|
||||
}
|
||||
|
||||
int Create(
|
||||
unsigned long & ThreadId,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false,
|
||||
const bool & CancelAsync = false
|
||||
) const
|
||||
{
|
||||
M_Create().lock();
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
if ( CreateDetached )
|
||||
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
|
||||
|
||||
if ( StackSize )
|
||||
pthread_attr_setstacksize(&attr,StackSize);
|
||||
|
||||
Instance I(const_cast<UThreadC *>(this),0,CancelEnable,CancelAsync);
|
||||
|
||||
*H = InvalidHandle;
|
||||
int R = pthread_create((pthread_t *)&(*H),&attr,(pthread_fn)ThreadMainHandler,(void *)&I);
|
||||
|
||||
ThreadId = (unsigned long)*H;
|
||||
|
||||
pthread_attr_destroy(&attr);
|
||||
|
||||
if ( !R ) S_Create().acquire();
|
||||
|
||||
M_Create().unlock();
|
||||
return R;
|
||||
}
|
||||
|
||||
static int Join( Handle H )
|
||||
{ return pthread_join(H,0); }
|
||||
|
||||
static int Kill( Handle H )
|
||||
{ return pthread_cancel(H); }
|
||||
|
||||
static int Detach( Handle H )
|
||||
{ return pthread_detach(H); }
|
||||
|
||||
private:
|
||||
|
||||
static const UMutex &M_Create() { static UMutex M; return M; }
|
||||
static USemaphore &S_Create() { static USemaphore S; return S; }
|
||||
|
||||
static void *ThreadMainHandler( Instance *Param )
|
||||
{
|
||||
Instance I(*Param);
|
||||
S_Create().release();
|
||||
|
||||
if ( I.Flags & 1 /*CancelEnable*/ )
|
||||
{
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
|
||||
|
||||
if ( I.Flags & 2 /*CancelAsync*/ )
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
|
||||
else
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
|
||||
}
|
||||
|
||||
if ( I.Owner )
|
||||
I.Owner->ThreadMain();
|
||||
else
|
||||
I.pFN();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct Instance
|
||||
{
|
||||
Instance( UThreadC<void> *const &O, const UThreadC<void>::Handler &pH = 0, const bool &CE=false, const bool &CA=false )
|
||||
: pFN(pH), Owner(O), Flags(0) { if ( CE ) Flags|=1; if ( CA ) Flags|=2; }
|
||||
|
||||
UThreadC<void>::Handler pFN;
|
||||
UThreadC<void> * Owner;
|
||||
unsigned char Flags;
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
#endif // !_U_Thread_Posix_
|
||||
@@ -0,0 +1,382 @@
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Written by Phillip Sitbon
|
||||
// Copyright 2003
|
||||
//
|
||||
// Modified by Mathieu Labbe
|
||||
//
|
||||
// Win32/Thread.h
|
||||
// - Windows thread
|
||||
//
|
||||
// - From CreateThread Platform SDK Documentation:
|
||||
//
|
||||
// "A thread that uses functions from the static C run-time
|
||||
// libraries should use the beginthread and endthread C run-time
|
||||
// functions for thread management rather than CreateThread and
|
||||
// ExitThread. Failure to do so results in small memory leaks
|
||||
// when ExitThread is called. Note that this is not a problem
|
||||
// with the C run-time in a DLL."
|
||||
//
|
||||
// With regards to this, I have decided to use the CreateThread
|
||||
// API, unless you define _CRT_ in which case there are two
|
||||
// possibilities:
|
||||
//
|
||||
// 1. Define _USE_BEGINTHREAD: Uses _beginthread/_endthread
|
||||
// (said to be *unreliable* in the SDK docs)
|
||||
//
|
||||
// 2. Don't - Uses _beginthreaded/_endthreadex
|
||||
//
|
||||
// A note about _endthread:
|
||||
//
|
||||
// It will call CloseHandle() on exit, and if it was already
|
||||
// closed then you will get an exception. To prevent this, I
|
||||
// removed the CloseHandle() functionality - this means that
|
||||
// a Join() WILL wait on a Detach()'ed thread.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
#ifndef _U_Thread_Win32_
|
||||
#define _U_Thread_Win32_
|
||||
|
||||
#include "rtabmap/utilite/Win32/UWin32.h"
|
||||
#include "rtabmap/utilite/USemaphore.h"
|
||||
#include "rtabmap/utilite/UMutex.h"
|
||||
|
||||
inline void uSleep(unsigned int ms)
|
||||
{
|
||||
Sleep(ms);
|
||||
}
|
||||
|
||||
#ifdef _CRT_
|
||||
# include <process.h>
|
||||
# ifdef _USE_BEGINTHREAD
|
||||
# define THREAD_CALL __cdecl
|
||||
# define THREAD_HANDLE uintptr_t
|
||||
# define THREAD_RET_T void
|
||||
# define CREATE_THREAD_FAILED (-1L)
|
||||
# define CREATE_THREAD_ERROR (errno)
|
||||
# define CREATE_THREAD(_S,_F,_P) ((Handle)_beginthread((void (__cdecl *)(void *))_F,_S,(void *)_P))
|
||||
# define EXIT_THREAD _endthread()
|
||||
# define CLOSE_HANDLE(x) 1
|
||||
# define THREAD_RETURN(x) return
|
||||
# else
|
||||
# define THREAD_CALL WINAPI
|
||||
# define THREAD_HANDLE HANDLE
|
||||
# define THREAD_RET_T UINT
|
||||
# define CREATE_THREAD_FAILED (0L)
|
||||
# define CREATE_THREAD_ERROR (errno)
|
||||
# define CREATE_THREAD(_S,_F,_P) ((Handle)_beginthreadex(0,_S,(UINT (WINAPI *)(void *))_F,(void *)_P,0,0))
|
||||
# define EXIT_THREAD _endthreadex(0)
|
||||
# define CLOSE_HANDLE(x) CloseHandle(x)
|
||||
# define THREAD_RETURN(x) return(x)
|
||||
# endif
|
||||
#else
|
||||
# define THREAD_CALL WINAPI
|
||||
# define THREAD_HANDLE HANDLE
|
||||
# define THREAD_RET_T DWORD
|
||||
# define CREATE_THREAD_FAILED (0L)
|
||||
# define CREATE_THREAD_ERROR GetLastError()
|
||||
# define CREATE_THREAD(_S,_F,_P) ((Handle)CreateThread(0,_S,(DWORD (WINAPI *)(void *))_F,(void *)_P,0,0))
|
||||
# define CREATE_THREAD2(_S,_F,_P,_ID) ((Handle)CreateThread(0,_S,(DWORD (WINAPI *)(void *))_F,(void *)_P,0,_ID))
|
||||
# define EXIT_THREAD ExitThread(0)
|
||||
# define CLOSE_HANDLE(x) CloseHandle(x)
|
||||
# define THREAD_RETURN(x) return(x)
|
||||
#endif
|
||||
|
||||
#define InvalidHandle 0
|
||||
|
||||
template
|
||||
<
|
||||
typename Thread_T
|
||||
>
|
||||
class UTILITE_EXP UThreadC
|
||||
{
|
||||
private:
|
||||
struct Instance;
|
||||
|
||||
public:
|
||||
typedef Thread_T & Thread_R;
|
||||
typedef const Thread_T & Thread_C_R;
|
||||
|
||||
typedef THREAD_HANDLE Handle;
|
||||
typedef void (* Handler)( Thread_R );
|
||||
|
||||
protected:
|
||||
UThreadC() {}
|
||||
|
||||
virtual void ThreadMain( Thread_R ) = 0;
|
||||
|
||||
static void Exit()
|
||||
{ EXIT_THREAD; }
|
||||
|
||||
static void TestCancel()
|
||||
{ Sleep(0); }
|
||||
|
||||
static int Self()
|
||||
{
|
||||
Handle Hnd = InvalidHandle;
|
||||
DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),GetCurrentProcess(),(LPHANDLE)&Hnd,NULL,0,NULL);
|
||||
return Hnd;
|
||||
|
||||
// only a pseudo-handle!
|
||||
//return (Handle)GetCurrentThread();
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
static int Create(
|
||||
const Handler & Function,
|
||||
Thread_C_R Param,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false, // UNUSED
|
||||
const bool & CancelAsync = false // UNUSED
|
||||
)
|
||||
{
|
||||
M_Create().lock();
|
||||
|
||||
Instance I(Param,0,Function);
|
||||
|
||||
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler,&I));
|
||||
|
||||
if ( Hnd == CREATE_THREAD_FAILED )
|
||||
{
|
||||
if ( H ) *H = InvalidHandle;
|
||||
M_Create().unlock();
|
||||
return CREATE_THREAD_ERROR;
|
||||
}
|
||||
|
||||
if ( H ) *H = Hnd;
|
||||
|
||||
S_Create().Wait();
|
||||
M_Create().unlock();
|
||||
|
||||
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Create(
|
||||
Thread_C_R Param,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false, // UNUSED
|
||||
const bool & CancelAsync = false // UNUSED
|
||||
) const
|
||||
{
|
||||
M_Create().lock();
|
||||
|
||||
Instance I(Param,const_cast<UThreadC *>(this));
|
||||
|
||||
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler,&I));
|
||||
|
||||
if ( Hnd == CREATE_THREAD_FAILED )
|
||||
{
|
||||
if ( H ) *H = InvalidHandle;
|
||||
M_Create().unlock();
|
||||
return CREATE_THREAD_ERROR;
|
||||
}
|
||||
|
||||
if ( H ) *H = Hnd;
|
||||
|
||||
S_Create().Wait();
|
||||
M_Create().unlock();
|
||||
|
||||
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Join( const Handle &H )
|
||||
{
|
||||
DWORD R = WaitForSingleObject((HANDLE)H,INFINITE);
|
||||
|
||||
if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) )
|
||||
{
|
||||
CLOSE_HANDLE(H);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( R == WAIT_TIMEOUT ) return EAGAIN;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
static int Kill( const Handle &H )
|
||||
{ return TerminateThread((HANDLE)H,0) ? 0 : EINVAL; }
|
||||
|
||||
static int Detach( const Handle &H )
|
||||
{ return (CLOSE_HANDLE(H)?0:EINVAL); }
|
||||
|
||||
private:
|
||||
|
||||
static const UMutex &M_Create() { static UMutex M; return M; }
|
||||
static const USemaphore &S_Create() { static USemaphore S; return S; }
|
||||
|
||||
static THREAD_RET_T THREAD_CALL ThreadMainHandler( Instance *Param )
|
||||
{
|
||||
Instance I(*Param);
|
||||
Thread_T Data(I.Data);
|
||||
S_Create().Post();
|
||||
|
||||
if ( I.Owner )
|
||||
I.Owner->ThreadMain(Data);
|
||||
else
|
||||
I.pFN(Data);
|
||||
|
||||
Exit();
|
||||
THREAD_RETURN(0);
|
||||
}
|
||||
|
||||
struct Instance
|
||||
{
|
||||
Instance( Thread_C_R P, UThreadC<Thread_T> *const &O, const typename UThreadC<Thread_T>::Handler &pH = 0 )
|
||||
: pFN(pH), Data(P), Owner(O) {}
|
||||
|
||||
typename UThreadC<Thread_T>::Handler pFN;
|
||||
typename UThreadC<Thread_T>::Thread_C_R Data;
|
||||
UThreadC<Thread_T> * Owner;
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Explicit Specialization of void
|
||||
//
|
||||
template<>
|
||||
class UTILITE_EXP UThreadC<void>
|
||||
{
|
||||
private:
|
||||
struct Instance;
|
||||
|
||||
public:
|
||||
typedef THREAD_HANDLE Handle;
|
||||
typedef void ( *Handler)();
|
||||
|
||||
virtual ~UThreadC<void>() {}
|
||||
|
||||
protected:
|
||||
UThreadC<void>() {}
|
||||
|
||||
virtual void ThreadMain() = 0;
|
||||
|
||||
static void Exit()
|
||||
{ EXIT_THREAD; }
|
||||
|
||||
static void TestCancel()
|
||||
{ Sleep(0); }
|
||||
|
||||
static int Self()
|
||||
{
|
||||
return (int)GetCurrentThreadId();
|
||||
//Handle Hnd = InvalidHandle;
|
||||
//DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),GetCurrentProcess(),(LPHANDLE)&Hnd,NULL,0,NULL);
|
||||
//return Hnd;
|
||||
|
||||
// only a pseudo-handle!
|
||||
//return (Handle)GetCurrentThread();
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
static int Create(
|
||||
const Handler & Function,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false, // UNUSED
|
||||
const bool & CancelAsync = false // UNUSED
|
||||
)
|
||||
{
|
||||
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler_S,Function));
|
||||
|
||||
if ( Hnd == CREATE_THREAD_FAILED )
|
||||
{
|
||||
if ( H ) *H = InvalidHandle;
|
||||
return (int)CREATE_THREAD_ERROR;
|
||||
}
|
||||
|
||||
if ( H ) *H = Hnd;
|
||||
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Create(
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false, // UNUSED
|
||||
const bool & CancelAsync = false // UNUSED
|
||||
) const
|
||||
{
|
||||
Handle Hnd(CREATE_THREAD(StackSize,ThreadMainHandler,this));
|
||||
|
||||
if ( Hnd == CREATE_THREAD_FAILED )
|
||||
{
|
||||
if ( H ) *H = InvalidHandle;
|
||||
return (int)CREATE_THREAD_ERROR;
|
||||
}
|
||||
|
||||
if ( H ) *H = Hnd;
|
||||
if ( CreateDetached ) CLOSE_HANDLE(Hnd);
|
||||
Self();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Create(
|
||||
int & ThreadId,
|
||||
Handle * const & H = 0,
|
||||
const bool & CreateDetached = false,
|
||||
const unsigned int & StackSize = 0,
|
||||
const bool & CancelEnable = false, // UNUSED
|
||||
const bool & CancelAsync = false // UNUSED
|
||||
) const
|
||||
{
|
||||
*H = InvalidHandle;
|
||||
*H = CREATE_THREAD2(StackSize,ThreadMainHandler,this, (LPDWORD)&ThreadId);
|
||||
|
||||
if ( *H == CREATE_THREAD_FAILED )
|
||||
{
|
||||
*H = InvalidHandle;
|
||||
return (int)CREATE_THREAD_ERROR;
|
||||
}
|
||||
|
||||
if ( CreateDetached ) CLOSE_HANDLE(*H);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Join( const Handle &H )
|
||||
{
|
||||
DWORD R = WaitForSingleObject((HANDLE)H,INFINITE);
|
||||
|
||||
if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) )
|
||||
{
|
||||
CLOSE_HANDLE(H);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( R == WAIT_TIMEOUT ) return EAGAIN;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
static int Kill( const Handle &H )
|
||||
{ return TerminateThread((HANDLE)H,0) ? 0 : EINVAL; }
|
||||
|
||||
static int Detach( const Handle &H )
|
||||
{ return (CLOSE_HANDLE(H)?0:EINVAL); }
|
||||
|
||||
private:
|
||||
|
||||
static THREAD_RET_T THREAD_CALL ThreadMainHandler( UThreadC<void> *Param )
|
||||
{
|
||||
Param->ThreadMain();
|
||||
Exit();
|
||||
THREAD_RETURN(0);
|
||||
}
|
||||
|
||||
static THREAD_RET_T THREAD_CALL ThreadMainHandler_S( Handler Param )
|
||||
{
|
||||
Param();
|
||||
Exit();
|
||||
THREAD_RETURN(0);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !_U_Thread_Win32_
|
||||
@@ -0,0 +1,64 @@
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
// Written by Phillip Sitbon
|
||||
// Copyright 2003
|
||||
//
|
||||
// Win32.h
|
||||
// - Windows includes
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
#ifndef _U_Win32_
|
||||
#define _U_Win32_
|
||||
|
||||
#include "rtabmap/utilite/UtiLiteExp.h"
|
||||
|
||||
#if !defined(_WINDOWS_)
|
||||
// WIN32 Excludes
|
||||
#ifdef WIN32_LEAN_AND_MEAN
|
||||
# define VC_EXTRALEAN
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# define _PRSHT_H_
|
||||
# define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
|
||||
# define NOVIRTUALKEYCODES // VK_*
|
||||
# define NOWINMESSAGES // WM_*, EM_*, LB_*, CB_*
|
||||
# define NOWINSTYLES // WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
|
||||
# define NOSYSMETRICS // SM_*
|
||||
# define NOMENUS // MF_*
|
||||
# define NOICONS // IDI_*
|
||||
# define NOKEYSTATES // MK_*
|
||||
# define NOSYSCOMMANDS // SC_*
|
||||
# define NORASTEROPS // Binary and Tertiary raster ops
|
||||
# define NOSHOWWINDOW // SW_*
|
||||
# define OEMRESOURCE // OEM Resource values
|
||||
# define NOATOM // Atom Manager routines
|
||||
# define NOCLIPBOARD // Clipboard routines
|
||||
# define NOCOLOR // Screen colors
|
||||
# define NOCTLMGR // Control and Dialog routines
|
||||
# define NODRAWTEXT // DrawText() and DT_*
|
||||
# define NOGDI // All GDI defines and routines
|
||||
# define NOKERNEL // All KERNEL defines and routines
|
||||
# define NOUSER // All USER defines and routines
|
||||
# define NONLS // All NLS defines and routines
|
||||
# define NOMB // MB_* and MessageBox()
|
||||
# define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
|
||||
# define NOMETAFILE // typedef METAFILEPICT
|
||||
# define NOMINMAX // Macros min(a,b) and max(a,b)
|
||||
# define NOMSG // typedef MSG and associated routines
|
||||
# define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
|
||||
# define NOSCROLL // SB_* and scrolling routines
|
||||
# define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
|
||||
# define NOSOUND // Sound driver routines
|
||||
# define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
|
||||
# define NOWH // SetWindowsHook and WH_*
|
||||
# define NOWINOFFSETS // GWL_*, GCL_*, associated routines
|
||||
# define NOCOMM // COMM driver routines
|
||||
# define NOKANJI // Kanji support stuff.
|
||||
# define NOHELP // Help engine interface.
|
||||
# define NOPROFILER // Profiler interface.
|
||||
# define NODEFERWINDOWPOS // DeferWindowPos routines
|
||||
# define NOMCX // Modem Configuration Extensions
|
||||
#endif // WIN32_LEAN_AND_MEAN
|
||||
//
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#endif // !_U_Win32_
|
||||
Reference in New Issue
Block a user