1
0
Fork 0

msgqueue now uses std::concurrency

This commit is contained in:
Nukem 2015-03-31 20:58:51 -04:00
parent 187a20c48a
commit 29cd3e42f1
4 changed files with 49 additions and 92 deletions

View File

@ -47,22 +47,22 @@ bool FunctionGet(uint Address, uint* Start, uint* End)
if(!DbgIsDebugging())
return false;
const uint modbase = ModBaseFromAddr(Address);
const uint moduleBase = ModBaseFromAddr(Address);
// Lookup by module hash, then function range
SHARED_ACQUIRE(LockFunctions);
auto found = functions.find(ModuleRange(ModHashFromAddr(modbase), Range(Address - modbase, Address - modbase)));
auto found = functions.find(ModuleRange(ModHashFromAddr(moduleBase), Range(Address - moduleBase, Address - moduleBase)));
// Was this range found?
if(found == functions.end())
return false;
if(Start)
*Start = found->second.start + modbase;
*Start = found->second.start + moduleBase;
if(End)
*End = found->second.end + modbase;
*End = found->second.end + moduleBase;
return true;
}

View File

@ -1,89 +1,49 @@
#include "msgqueue.h"
#include <stdio.h>
//allocate a message (internal)
static MESSAGE* msgalloc()
// Allocate a message stack
MESSAGE_STACK* MsgAllocStack()
{
return (MESSAGE*)emalloc(sizeof(MESSAGE), "msgalloc:msg");
// Use placement new to ensure all constructors are called correctly
PVOID memoryBuffer = emalloc(sizeof(MESSAGE_STACK), "MsgAllocStack:memoryBuffer");
MESSAGE_STACK* messageStack = new(memoryBuffer) MESSAGE_STACK;
if(!messageStack)
return nullptr;
return messageStack;
}
//free a message (internal)
static void msgfree(MESSAGE* msg)
// Free a message stack and all messages in the queue
void MsgFreeStack(MESSAGE_STACK* Stack)
{
efree(msg, "msgfree:msg");
// Destructor must be called manually due to placement new
Stack->FIFOStack.~unbounded_buffer();
// Free memory
efree(Stack, "MsgFreeStack:Stack");
}
//allocate a message stack
MESSAGE_STACK* msgallocstack()
// Add a message to the stack
bool MsgSend(MESSAGE_STACK* msgstack, int msg, uint param1, uint param2)
{
MESSAGE_STACK* msgstack = (MESSAGE_STACK*)emalloc(sizeof(MESSAGE_STACK), "msgallocstack:msgstack");
if(!msgstack)
return 0;
memset(msgstack, 0, sizeof(MESSAGE_STACK));
InitializeCriticalSection(&msgstack->cr);
return msgstack;
}
MESSAGE messageInfo;
messageInfo.msg = msg;
messageInfo.param1 = param1;
messageInfo.param2 = param2;
//free a message stack
void msgfreestack(MESSAGE_STACK* msgstack)
{
DeleteCriticalSection(&msgstack->cr);
int stackpos = msgstack->stackpos;
for(int i = 0; i < stackpos; i++) //free all messages left in stack
msgfree(msgstack->msg[i]);
efree(msgstack, "msgfreestack:msgstack");
}
//add a message to the stack
bool msgsend(MESSAGE_STACK* msgstack, int msg, uint param1, uint param2)
{
CRITICAL_SECTION* cr = &msgstack->cr;
EnterCriticalSection(cr);
int stackpos = msgstack->stackpos;
if(stackpos >= MAX_MESSAGES)
{
LeaveCriticalSection(cr);
return false;
}
MESSAGE* newmsg = msgalloc();
if(!newmsg)
{
LeaveCriticalSection(cr);
return false;
}
newmsg->msg = msg;
newmsg->param1 = param1;
newmsg->param2 = param2;
msgstack->msg[stackpos] = newmsg;
msgstack->stackpos++; //increase stack pointer
LeaveCriticalSection(cr);
// Asynchronous send. Return value doesn't matter.
concurrency::asend(msgstack->FIFOStack, messageInfo);
return true;
}
//get a message from the stack (will return false when there are no messages)
bool msgget(MESSAGE_STACK* msgstack, MESSAGE* msg)
// Get a message from the stack (will return false when there are no messages)
bool MsgGet(MESSAGE_STACK* Stack, MESSAGE* Message)
{
CRITICAL_SECTION* cr = &msgstack->cr;
EnterCriticalSection(cr);
int stackpos = msgstack->stackpos;
if(!msgstack->stackpos) //no messages to process
{
LeaveCriticalSection(cr);
return false;
}
msgstack->stackpos--; //current message is at stackpos-1
stackpos--;
MESSAGE* stackmsg = msgstack->msg[stackpos];
memcpy(msg, stackmsg, sizeof(MESSAGE));
msgfree(stackmsg);
msgstack->msg[stackpos] = 0;
LeaveCriticalSection(cr);
return true;
return concurrency::try_receive(Stack->FIFOStack, *Message);
}
//wait for a message on the specified stack
void msgwait(MESSAGE_STACK* msgstack, MESSAGE* msg)
// Wait for a message on the specified stack
void MsgWait(MESSAGE_STACK* Stack, MESSAGE* Message)
{
while(!msgget(msgstack, msg))
Sleep(1);
*Message = concurrency::receive(Stack->FIFOStack);
}

View File

@ -3,10 +3,9 @@
#include "_global.h"
#include <windows.h>
#include <agents.h>
#define MAX_MESSAGES 256
//message structure
// Message structure
struct MESSAGE
{
int msg;
@ -14,19 +13,17 @@ struct MESSAGE
uint param2;
};
//message stack structure
// Message stack structure.
// Supports an unlimited number of messages.
struct MESSAGE_STACK
{
CRITICAL_SECTION cr;
int stackpos;
MESSAGE* msg[MAX_MESSAGES];
concurrency::unbounded_buffer<MESSAGE> FIFOStack;
};
//function definitions
MESSAGE_STACK* msgallocstack();
void msgfreestack(MESSAGE_STACK* msgstack);
bool msgsend(MESSAGE_STACK* msgstack, int msg, uint param1, uint param2);
bool msgget(MESSAGE_STACK* msgstack, MESSAGE* msg);
void msgwait(MESSAGE_STACK* msgstack, MESSAGE* msg);
MESSAGE_STACK* MsgAllocStack();
void MsgFreeStack(MESSAGE_STACK* Stack);
bool MsgSend(MESSAGE_STACK* msgstack, int msg, uint param1, uint param2);
bool MsgGet(MESSAGE_STACK* Stack, MESSAGE* Message);
void MsgWait(MESSAGE_STACK* Stack, MESSAGE* Message);
#endif // _MSGQUEUE_H

View File

@ -194,7 +194,7 @@ static void registercommands()
static bool cbCommandProvider(char* cmd, int maxlen)
{
MESSAGE msg;
msgwait(gMsgStack, &msg);
MsgWait(gMsgStack, &msg);
char* newcmd = (char*)msg.param1;
if(strlen(newcmd) >= deflen)
{
@ -211,7 +211,7 @@ extern "C" DLL_EXPORT bool _dbg_dbgcmdexec(const char* cmd)
int len = (int)strlen(cmd);
char* newcmd = (char*)emalloc((len + 1) * sizeof(char), "_dbg_dbgcmdexec:newcmd");
strcpy_s(newcmd, len + 1, cmd);
return msgsend(gMsgStack, 0, (uint)newcmd, 0);
return MsgSend(gMsgStack, 0, (uint)newcmd, 0);
}
static DWORD WINAPI DbgCommandLoopThread(void* a)
@ -259,7 +259,7 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
strcpy_s(szSymbolCachePath, dir);
PathAppendA(szSymbolCachePath, "symbols");
SetCurrentDirectoryW(StringUtils::Utf8ToUtf16(dir).c_str());;
gMsgStack = msgallocstack();
gMsgStack = MsgAllocStack();
if(!gMsgStack)
return "Could not allocate message stack!";
varinit();
@ -306,7 +306,7 @@ extern "C" DLL_EXPORT void _dbg_dbgexitsignal()
CloseHandle(hCommandLoopThread);
cmdfree(command_list);
varfree();
msgfreestack(gMsgStack);
MsgFreeStack(gMsgStack);
if(memleaks())
{
char msg[256] = "";