home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2000 February
/
PCWorld_2000-02_cd.bin
/
Software
/
TemaCD
/
tcvpa
/
data1.cab
/
MyFileGroup
/
INCLUDE
/
Thread.hpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-06-03
|
9KB
|
281 lines
#ifndef _INC_THREAD_HPP
#define _INC_THREAD_HPP
class TC_CLock;
class TC_CEvent;
class TC_CGuard;
class TC_CEndGuard;
class TC_CCriticalGuard;
class TC_CCriticalSect;
class TC_CRWAccess;
class TC_CThread;
const long TC_WAIT_INDEFINITE = 0xFFFFFFFF;
const long TC_WAIT_NONE = 0;
// **********************************************************************
class TC_COREEX_EXPORT TC_CCriticalSect
{
public: CRITICAL_SECTION m_Sect ;
public: TC_CCriticalSect ()
{
InitializeCriticalSection(&m_Sect);
} // end of func
// **********************************************************************
public: ~TC_CCriticalSect () ;
public: void Enter () ;
public: void Leave () ;
}; // end of class TC_CCriticalSect
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CLock
{
private: HANDLE m_hMutex ;
public: TC_CLock (BOOL init_owned = FALSE)
{
m_hMutex = CreateMutex(NULL, init_owned, NULL);
} // end of func
// **********************************************************************
public: ~TC_CLock () ;
public: BOOL Aquire (long timeout = TC_WAIT_INDEFINITE) ;
public: BOOL TryAquire () ;
public: void Release () ;
}; // end of class TC_CLock
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CEvent
{
private: HANDLE m_hEvent ;
public: TC_CEvent (BOOL is_manual = TRUE, BOOL init_set = TRUE)
{
m_hEvent = CreateEvent(NULL, is_manual, init_set, NULL);
} // end of func
// **********************************************************************
public: ~TC_CEvent () ;
public: BOOL Wait (long timeout = TC_WAIT_INDEFINITE) ;
public: BOOL Try () ;
public: void Set () ;
public: void Reset () ;
public: void Pulse () ;
public: HANDLE HEvent () ;
}; // end of class TC_CEvent
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CGuard
{
private: TC_CLock * m_Lock ;
public: TC_CGuard (TC_CLock * lock)
{
m_Lock = lock;
m_Lock->Aquire(TC_WAIT_INDEFINITE);
} // end of func
// **********************************************************************
public: ~TC_CGuard ()
{
m_Lock->Release();
} // end of func
// **********************************************************************
}; // end of class TC_CGuard
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CEndGuard
{
private: TC_CLock * m_Lock ;
public: TC_CEndGuard (TC_CLock * lock)
{
m_Lock = lock;
} // end of func
// **********************************************************************
public: ~TC_CEndGuard ()
{
m_Lock->Release();
} // end of func
// **********************************************************************
}; // end of class TC_CEndGuard
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CCriticalGuard
{
private: static TC_CLock m_Lock ;
public: TC_CCriticalGuard ()
{
m_Lock.Aquire();
} // end of func
// **********************************************************************
public: ~TC_CCriticalGuard ()
{
m_Lock.Release();
} // end of func
// **********************************************************************
}; // end of class TC_CCriticalGuard
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CRWAccess
{
private: TCRWPrivilege m_Privilege ;
private: TC_CLock m_Lock ;
private: TC_CEvent m_Event ;
private: long m_RWorking ;
private: long m_WWorking ;
private: long m_RWaiting ;
private: long m_WWaiting ;
public: TC_CRWAccess (TCRWPrivilege privilege = R_Privilege) ;
public: ~TC_CRWAccess () ;
public: TCRWPrivilege GetPrivilege () ;
public: void SetPrivilege (TCRWPrivilege privilege) ;
public: BOOL Request (TCRWRequest request = R_Request, long timeout = TC_WAIT_INDEFINITE, long param=0) ;
public: void Release (long param=0) ;
}; // end of class TC_CRWAccess
// **********************************************************************
ULONG TC_COREEX_EXPORT tcPrimaryThreadID () ;
ULONG TC_COREEX_EXPORT tcCurrentThreadID () ;
long tcThreadEntry (TC_CThread * thr) ;
#define TC_CURRENT_THREAD TC_CThread::WorkingThread
// **********************************************************************
class TC_COREEX_EXPORT TC_CThread
{
private:
friend long tcThreadEntry(TC_CThread *thr);
private: TC_TNameNode<TC_CThread> m_Link ;
private: static TC_TNameList<TC_CThread> m_Chain ;
protected: static TC_CLock m_CreateLock ;
protected: static TC_CEvent m_SyncEvent ;
private: HANDLE m_Handle ;
protected: long m_Flags ;
private: long m_ExitCode ;
public: TC_CEvent * m_EvTerm ;
public: BOOL m_AutoDelete ;
public: TC_CThread (const char* name) ;
public: virtual ~TC_CThread () ;
protected: TC_CThread () ;
protected: void Create (const char *name, BOOL is_suspended = FALSE, int stacksize = 0) ;
public: long ExitCode ()
{
return m_ExitCode;
} // end of func
// **********************************************************************
public: ULONG GetID ()
{
return (ULONG)m_Link.GetKey();
} // end of func
// **********************************************************************
public: const char * GetName () ;
public: HANDLE GetHandle ()
{
return m_Handle;
} // end of func
// **********************************************************************
public: BOOL IsPrimary ()
{
return GetID() == tcPrimaryThreadID();
} // end of func
// **********************************************************************
public: static TC_CThread * FindByID (ULONG id) ;
public: static TC_CThread * FindByName (const char * name) ;
public: static TC_CThread * FindByHandle (HANDLE handle) ;
public: static TC_CThread * WorkingThread () ;
public: static void Sleep (ULONG msec) ;
public: BOOL SetPriority (int priority) ;
public: BOOL IsActive () ;
public: BOOL IsAlive () ;
public: BOOL Suspend () ;
public: BOOL Resume () ;
public: BOOL Wait (long timeout = TC_WAIT_INDEFINITE) ;
public: void Exit (long exit_code) ;
public: virtual long Start () =0 ;
}; // end of class TC_CThread
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CThreadAction
{
public: TC_CThreadAction () ;
public: virtual ~TC_CThreadAction () ;
public: virtual long Run () =0 ;
}; // end of class TC_CThreadAction
// **********************************************************************
// **********************************************************************
template <class T>
class TC_TThreadAction
: public TC_CThreadAction
{
typedef long (T::*THREAD_FUNC)();
private: T* m_Obj ;
private: THREAD_FUNC m_Func ;
public: TC_TThreadAction (T* obj, THREAD_FUNC func)
{
m_Obj = obj;
m_Func = func;
} // end of func
// **********************************************************************
public: ~TC_TThreadAction ()
{
} // end of func
// **********************************************************************
public: long Run ()
{
return (m_Obj->*m_Func)();
} // end of func
// **********************************************************************
}; // end of class TC_TThreadAction
// **********************************************************************
// **********************************************************************
class TC_COREEX_EXPORT TC_CInstThread
: public TC_CThread
{
protected: TC_CThreadAction* m_Action ;
public: TC_CInstThread (char * name, TC_CThreadAction * action, BOOL is_suspended, BOOL is_autodelete) ;
public: ~TC_CInstThread () ;
public: long Start () ;
}; // end of class TC_CInstThread
// **********************************************************************
#endif // _INC_THREAD_HPP