36 #ifndef _U_Thread_Win32_ 37 #define _U_Thread_Win32_ 50 # ifdef _USE_BEGINTHREAD 51 # define THREAD_CALL __cdecl 52 # define THREAD_HANDLE uintptr_t 53 # define THREAD_RET_T void 54 # define CREATE_THREAD_FAILED (-1L) 55 # define CREATE_THREAD_ERROR (errno) 56 # define CREATE_THREAD(_S,_F,_P) ((Handle)_beginthread((void (__cdecl *)(void *))_F,_S,(void *)_P)) 57 # define EXIT_THREAD _endthread() 58 # define CLOSE_HANDLE(x) 1 59 # define THREAD_RETURN(x) return 61 # define THREAD_CALL WINAPI 62 # define THREAD_HANDLE HANDLE 63 # define THREAD_RET_T UINT 64 # define CREATE_THREAD_FAILED (0L) 65 # define CREATE_THREAD_ERROR (errno) 66 # define CREATE_THREAD(_S,_F,_P) ((Handle)_beginthreadex(0,_S,(UINT (WINAPI *)(void *))_F,(void *)_P,0,0)) 67 # define EXIT_THREAD _endthreadex(0) 68 # define CLOSE_HANDLE(x) CloseHandle(x) 69 # define THREAD_RETURN(x) return(x) 72 # define THREAD_CALL WINAPI 73 # define THREAD_HANDLE HANDLE 74 # define THREAD_RET_T DWORD 75 # define CREATE_THREAD_FAILED (0L) 76 # define CREATE_THREAD_ERROR GetLastError() 77 # define CREATE_THREAD(_S,_F,_P) ((Handle)CreateThread(0,_S,(DWORD (WINAPI *)(void *))_F,(void *)_P,0,0)) 78 # define CREATE_THREAD2(_S,_F,_P,_ID) ((Handle)CreateThread(0,_S,(DWORD (WINAPI *)(void *))_F,(void *)_P,0,_ID)) 79 # define EXIT_THREAD ExitThread(0) 80 # define CLOSE_HANDLE(x) CloseHandle(x) 81 # define THREAD_RETURN(x) return(x) 84 #define InvalidHandle 0 100 typedef void (* Handler)( Thread_R );
105 virtual void ThreadMain( Thread_R ) = 0;
116 DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),GetCurrentProcess(),(LPHANDLE)&Hnd,
NULL,0,
NULL);
126 const Handler & Function,
128 Handle *
const & H = 0,
129 const bool & CreateDetached =
false,
130 const unsigned int & StackSize = 0,
131 const bool & CancelEnable =
false,
132 const bool & CancelAsync =
false 159 Handle *
const & H = 0,
160 const bool & CreateDetached =
false,
161 const unsigned int & StackSize = 0,
162 const bool & CancelEnable =
false,
163 const bool & CancelAsync =
false 168 Instance I(Param,const_cast<UThreadC *>(
this));
188 static int Join(
const Handle &H )
190 DWORD R = WaitForSingleObject((HANDLE)H,INFINITE);
192 if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) )
198 if ( R == WAIT_TIMEOUT )
return EAGAIN;
202 static int Kill(
const Handle &H )
203 {
return TerminateThread((HANDLE)H,0) ? 0 : EINVAL; }
216 Thread_T Data(I.
Data);
220 I.
Owner->ThreadMain(Data);
231 : pFN(pH), Data(P), Owner(O) {}
251 typedef void ( *Handler)();
258 virtual void ThreadMain() = 0;
268 return (
int)GetCurrentThreadId();
280 const Handler & Function,
281 Handle *
const & H = 0,
282 const bool & CreateDetached =
false,
283 const unsigned int & StackSize = 0,
284 const bool & CancelEnable =
false,
285 const bool & CancelAsync =
false 288 Handle Hnd(
CREATE_THREAD(StackSize,ThreadMainHandler_S,Function));
302 Handle *
const & H = 0,
303 const bool & CreateDetached =
false,
304 const unsigned int & StackSize = 0,
305 const bool & CancelEnable =
false,
306 const bool & CancelAsync =
false 324 unsigned long & ThreadId,
325 Handle *
const & H = 0,
326 const bool & CreateDetached =
false,
327 const unsigned int & StackSize = 0,
328 const bool & CancelEnable =
false,
329 const bool & CancelAsync =
false 334 *H =
CREATE_THREAD2(StackSize,ThreadMainHandler,
this, (LPDWORD)&
id);
335 ThreadId = (
unsigned long)
id;
347 static int Join(
const Handle &H )
349 DWORD R = WaitForSingleObject((HANDLE)H,INFINITE);
351 if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) )
357 if ( R == WAIT_TIMEOUT )
return EAGAIN;
361 static int Kill(
const Handle &H )
362 {
return TerminateThread((HANDLE)H,0) ? 0 : EINVAL; }
384 #endif // !_U_Thread_Win32_
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
#define CREATE_THREAD_ERROR
static THREAD_RET_T THREAD_CALL ThreadMainHandler(Instance *Param)
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)
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)
#define CREATE_THREAD(_S, _F, _P)
UThreadC< Thread_T >::Thread_C_R Data
static THREAD_RET_T THREAD_CALL ThreadMainHandler(UThreadC< void > *Param)
Instance(Thread_C_R P, UThreadC< Thread_T > *const &O, const typename UThreadC< Thread_T >::Handler &pH=0)
static int Join(const Handle &H)
static int Kill(const Handle &H)
const Thread_T & Thread_C_R
void uSleep(unsigned int ms)
static int Detach(const Handle &H)
static THREAD_RET_T THREAD_CALL ThreadMainHandler_S(Handler Param)
virtual void ThreadMain()=0
UThreadC< Thread_T > * Owner
UThreadC< Thread_T >::Handler pFN
int Create(Handle *const &H=0, const bool &CreateDetached=false, const unsigned int &StackSize=0, const bool &CancelEnable=false, const bool &CancelAsync=false) const
static int Join(const Handle &H)
static const UMutex & M_Create()
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
#define CREATE_THREAD_FAILED
#define CREATE_THREAD2(_S, _F, _P, _ID)
static int Detach(const Handle &H)
static const USemaphore & S_Create()
static int Kill(const Handle &H)