Member
|
12-28-2011
, 10:35
amxmodx not support dual Thread???
|
#1
|
my write one modules, createthread running amxx code
but use the my dual thread modules ,amxx running error
the code:
Code:
#include "amxxmodule.h"
#include <windows.h>
typedef struct ThreadParam
{
int fwd;
LPVOID data_addr;
int DataLen;
}ThreadParam,*lpThreadParam;
typedef HANDLE (__stdcall * fnOpenThread)
(
DWORD dwDesiredAccess, // access right
BOOL bInheritHandle, // handle inheritance option
DWORD dwThreadId // thread identifier
);
fnOpenThread OpenThread;
HANDLE g_MainThreadHandle;
//------------------------Params---------------------------------
lpThreadParam AllocParam()
{
return (lpThreadParam)VirtualAlloc(0,sizeof(ThreadParam),MEM_COMMIT,PAGE_EXECUTE_READWRITE);
}
void FreeParam(lpThreadParam lpAddress)
{
VirtualFree(lpAddress,sizeof(ThreadParam),MEM_DECOMMIT);
}
void* AllocData(int amxLen)
{
return VirtualAlloc(0,(amxLen+1)*4,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
}
void FreeData(void* lpAddress,int amxLen)
{
VirtualFree(lpAddress,(amxLen+1)*4,MEM_DECOMMIT);
}
int ReisgerFwd(AMX* amx,const char* handler)
{
return MF_RegisterSPForwardByName(amx, handler, FP_ARRAY, FP_CELL,FP_DONE);
}
cell MakeData(void* DataAddr,int DataLen)
{
return MF_PrepareCellArray((cell*)DataAddr,DataLen);
}
//-----------------------------EndParams---------------------------------
//-----------------------------NParams-----------------------------------
int ReisgerNFwd(AMX* amx,const char* handler)
{
return MF_RegisterSPForwardByName(amx, handler,FP_DONE);
}
//-----------------------------EndNparams---------------------------------
VOID ThreadNParamCall(int fwd)
{
SuspendThread(g_MainThreadHandle);
MF_ExecuteForward(fwd);
MF_UnregisterSPForward(fwd);
ResumeThread(g_MainThreadHandle);
}
static cell AMX_NATIVE_CALL ThreadExec(AMX *amx, cell *params)
{
int m_dwHandlerLen ;
char* m_handler = MF_GetAmxString(amx,params[1],0,&m_dwHandlerLen);
int fwd = ReisgerNFwd(amx,m_handler);
if(fwd)
{
HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadNParamCall,(void*)fwd,0,NULL);
SetThreadPriority(hThread,REALTIME_PRIORITY_CLASS);
return (cell)hThread;
}else
{
MF_UnregisterSPForward(fwd);
}
return -1;
}
//Use Param Call
VOID ThreadParamCall(lpThreadParam c_lpParam)
{
cell data_addr = MakeData(c_lpParam->data_addr,c_lpParam->DataLen);
MF_ExecuteForward(c_lpParam->fwd,data_addr,c_lpParam->DataLen);
MF_UnregisterSPForward(c_lpParam->fwd);
FreeData(c_lpParam->data_addr,c_lpParam->DataLen);
FreeParam(c_lpParam);
}
static cell AMX_NATIVE_CALL ParamThread(AMX *amx, cell *params)
{
int m_dwHandlerLen ;
char* m_handler = MF_GetAmxString(amx,params[1],0,&m_dwHandlerLen);
lpThreadParam m_lpParam = AllocParam();
m_lpParam->data_addr = AllocData(params[3]);
m_lpParam->fwd = ReisgerFwd(amx,m_handler);
if(m_lpParam->fwd)
{
memcpy(m_lpParam->data_addr,MF_GetAmxAddr(amx,params[2]),params[3]*4);
m_lpParam->DataLen = params[3];
HANDLE hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadParamCall,m_lpParam,CREATE_SUSPENDED,NULL);
SetThreadPriority(hThread,NORMAL_PRIORITY_CLASS);
return (cell)hThread;
}else
{
MF_UnregisterSPForward(m_lpParam->fwd);
FreeData(m_lpParam->data_addr,params[3]);
FreeParam(m_lpParam);
}
return -1;
}
static cell AMX_NATIVE_CALL API_Sleep(AMX *amx, cell *params)
{
Sleep(params[1]);
return 1;
}
static cell AMX_NATIVE_CALL nv_TerminateThread(AMX *amx, cell *params)
{
return TerminateThread((HANDLE)params[1],0);
}
static cell AMX_NATIVE_CALL nv_ResumeThread(AMX *amx,cell *params)
{
return ResumeThread((HANDLE)params[1]);
}
AMX_NATIVE_INFO thread_ext[] = {
{"ThreadExec",ThreadExec},
{"ParamThread",ParamThread},
{"TerminateThread",nv_TerminateThread},
{"ResumeThread",nv_ResumeThread},
{NULL,NULL}
};
void OnAmxxAttach()
{
MF_AddNatives(thread_ext);
OpenThread = (fnOpenThread)GetProcAddress(GetModuleHandle("kernel32.dll"),"OpenThread");
g_MainThreadHandle = OpenThread(THREAD_ALL_ACCESS,FALSE,GetCurrentThreadId());
}
VOID OnAmxxDetach()
{
CloseHandle(g_MainThreadHandle);
}
|
|