|
发表于 2020-9-2 19:00:01
|
显示全部楼层
#ifndef _M_THREAD_BASEOBJ_DEF_H_
#define _M_THREAD_BASEOBJ_DEF_H_
#include <pthread.h>
#include <semaphore.h>
#include <sys/select.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <Carbon/Carbon.h>
class MCritSec
{
private:
pthread_mutex_t m_mutex;
public:
MCritSec()
{
pthread_mutex_init (&m_mutex,NULL);
}
virtual ~MCritSec()
{
pthread_mutex_destroy(&m_mutex);
}
void Lock()
{
pthread_mutex_lock(&m_mutex);
}
void Unlock()
{
pthread_mutex_unlock(&m_mutex);
}
};
class MAutoLock
{
protected:
MCritSec * m_pLock;
public:
MAutoLock(MCritSec * plock)
{
m_pLock = plock;
m_pLock->Lock();
}
~MAutoLock()
{
m_pLock->Unlock();
}
};
class IDAllocator
{
typedef struct _IDNode
{
unsigned long ID;
_IDNode* Next;
}Node;
public:
IDAllocator();
virtual ~IDAllocator();
unsigned long GetID();
long ReleaseID(unsigned long nID);
private:
MCritSec m_Lock;
Node* m_pHead;
unsigned long m_nNextID;
int m_nLength;
unsigned long CalNextID(unsigned long nPreID);
};
#define MHANDLE_SUCCESS( T ) ((long)T >=0 )
enum
{
HANDLE_TYPE_BASE,
HANDLE_TYPE_MUTEX,
HANDLE_TYPE_THREAD,
HANDLE_TYPE_SEMAPHORE,
HANDLE_TYPE_EVENT
};
enum
{
HANDLE_STATE_THREADCREATE_SUSPEND=-1,
HANDLE_STATE_UNVALID=0,
HANDLE_STATE_VALID=1
};
enum
{
HANDLE_NOTIFYTYPE_SIGNAL,
HANDLE_NOTIFYTYPE_UNSIGNAL,
HANDLE_NOTIFYTYPE_UPDATA
};
class MHandleManager;
typedef struct _ManagerNode
{
MHandleManager* pMng;
_ManagerNode* Next;
}ManagerNode;
class MHBase
{
public:
MHBase();
virtual ~MHBase();
private:
static IDAllocator g_ObjIDAllocator;
protected:
int m_HandleType;
unsigned long m_HandleID;
unsigned long m_HandleState;
virtual long DelHeadManager();
virtual long DelManager(MHandleManager* pManager);
virtual long AddManager(MHandleManager* pManager);
virtual long NotifyManager(int NotifyType)=0;
ManagerNode* m_pMngListHead;
ManagerNode* m_pMngListTail;
MCritSec m_MngListLock;
MCritSec m_OPLock;
friend class MHandleManager;
};
typedef MHBase* MHANDLE;
class MEvent:public MHBase
{
public:
MEvent(bool InitState,bool IsManaul);
virtual ~MEvent();
void Set();
void Reset();
void Pulse();
private:
bool m_bManaulReset;
MCritSec m_Lock;
protected:
long NotifyManager(int NotifyType);
};
class MSemaphore:public MHBase
{
public:
MSemaphore(long nInitCounts,long nMaxCounts);
virtual ~MSemaphore();
long Signal(long nCounts=1);
private:
long m_nMaxSigCounts;
MCritSec m_Lock;
protected:
long NotifyManager(int NotifyType);
};
class MMutex:public MSemaphore
{
public:
MMutex(bool bInitState);
~MMutex();
};
typedef void*(*THREADFUNCPTR)(void*);
MHANDLE CreateThread(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);
class MThread:public MHBase
{
typedef enum {CREATE_BYNEW,CREATE_BYFUNC}CREATE_TYPE;
public:
MThread();
virtual ~MThread();
long InitUsrThread(const pthread_attr_t *pAttr,void *pArg,bool IsCreateSuspend);
virtual long Resume();
virtual long Suspend(){return 0;};
int GetState(){return m_HandleState;}
protected:
CREATE_TYPE m_nCreateType;
pthread_t m_Thread;
pthread_attr_t m_ThreadAttribute;
void* m_pArg;
THREADFUNCPTR m_pThrdFunc;
MHANDLE m_evntRun;
protected:
static void* ThreadFunction(void* pv);
long NotifyManager(int NotifyType);
virtual long WorkFunc(void* pUsrData);
private:
long Init(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);
friend MHANDLE CreateThread(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);
};
typedef struct _ID_INDEX_MAP
{
long objID;
bool bValid;
}IIMap;
class MHandleManager
{
public:
MHandleManager(MHANDLE* pHandles,int nCounts);
virtual ~MHandleManager();
long RecieveNotify(MHANDLE pHandle,int nNotifyType);
long WaitForHandles(bool bWaitForAll,long nWaitMillionSecond= 0x7FFFFFFF);
private:
MCritSec m_NotifyLock;
MCritSec m_ConstructLock;
//sem_t m_semComplete;
MPSemaphoreID m_semComplete;
MHANDLE* m_pHandle;
int m_nHandleCounts;
IIMap* m_pSignalInfoList;
int m_nNoSignalInfoCounts;
bool m_bTimeFlag;
bool m_bIsFirstSignal;
long m_lFirstSignalIndex;
//void WaitTimeOver();
friend class MHBase;
};
MHANDLE CreateEvent(bool bManaulReset,bool bInitState);
void SetEvent(MHANDLE event);
void ResetEvent(MHANDLE event);
void PulseEvent(MHANDLE event);
MHANDLE CreateSemaphore(long nInitSigs,long nMaxSigs);
void ReleaseSemaphore(MHANDLE semaphore,long nSigCounts,long* lRet=NULL);
MHANDLE CreateMutex(bool bInitState);
void ReleaseMutex(MHANDLE mutex);
MHANDLE CreateThread(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);
long ResumeThread(MHANDLE pthread);
long SuspendThread(MHANDLE pthread);
void CloseHandle(MHANDLE handle);
#define WAIT_FAILED -29296
long WaitForMultipleObjects(int nCounts,MHANDLE* pHandle,bool IsAll,long lMillionSeconds);
long WaitForSingleObject(MHANDLE Handle,long lMillionSeconds);
#endif //_M_THREAD_BASEOBJ_DEF_H_ |
|