Skip to content

Commit

Permalink
re-doing crit sections
Browse files Browse the repository at this point in the history
added sleep in game thread to help cpu usage
increased gc rate
  • Loading branch information
bobode committed Jun 10, 2013
1 parent cde52d8 commit 5d4564d
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 115 deletions.
27 changes: 27 additions & 0 deletions CriticalSections.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ class CriticalRoom
}
};

class AutoCriticalRoom
{
private:
bool bEnteredCriticalSection;
void EnterSection() {
InterlockedIncrement(&Vars.SectionCount);
bEnteredCriticalSection = true;
EnterCriticalSection(&Vars.cGameLoopSection);

}

void LeaveSection() {
if(bEnteredCriticalSection) {
bEnteredCriticalSection = false;
LeaveCriticalSection(&Vars.cGameLoopSection);
InterlockedDecrement(&Vars.SectionCount);
}
}

public:
AutoCriticalRoom() : bEnteredCriticalSection(false) {EnterSection();}
~AutoCriticalRoom() {LeaveSection(); }




};
6 changes: 3 additions & 3 deletions D2BS.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define XP_WIN

#define D2BS_VERSION "1.5.1777" //uptodate with d branch 1765 ff 20b
#define D2BS_VERSION "1.5.1778" //uptodate with d branch 1765 ff 20b

#include <windows.h>
#include <vector>
Expand Down Expand Up @@ -44,7 +44,7 @@ struct Variables
BOOL bShutdownFromDllMain;
BOOL bChangedAct;
BOOL bGameLoopEntered;

DWORD dwMaxGameTime;
DWORD dwGameTimeout;
BOOL bQuitOnError;
Expand Down Expand Up @@ -82,7 +82,7 @@ struct Variables

std::map<unsigned __int32, CellFile*> mCachedCellFiles;
std::vector<std::pair<DWORD, DWORD> > vUnitList;
std::list<Event*> EventList;
//std::list<Event*> EventList;
CRITICAL_SECTION cEventSection;
CRITICAL_SECTION cRoomSection;
CRITICAL_SECTION cMiscSection;
Expand Down
2 changes: 1 addition & 1 deletion D2Handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)

while(Vars.SectionCount)
Sleep(0);

Sleep(10);
EnterCriticalSection(&Vars.cGameLoopSection);
}

Expand Down
12 changes: 4 additions & 8 deletions JSArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ JSAPI_PROP(area_getProperty)
Level* pLevel = GetLevel(pArea->AreaId);
if(!pLevel)
return JS_FALSE;

AutoCriticalRoom* cRoom = new AutoCriticalRoom;
jsval ID;
JS_IdToValue(cx,id,&ID);
switch(JSVAL_TO_INT(ID))
Expand All @@ -40,9 +40,6 @@ JSAPI_PROP(area_getProperty)
pArea->ExitArray = JS_NewArrayObject(cx, 0, NULL);
JS_AddRoot(cx, &pArea->ExitArray);

CriticalRoom cRoom;
cRoom.EnterSection();

ActMap* map = ActMap::GetMap(pLevel);

ExitArray exits;
Expand All @@ -62,16 +59,15 @@ JSAPI_PROP(area_getProperty)
JSObject* pExit = BuildObject(cx, &exit_class, NULL, exit_props, exit);
if(!pExit)
{
cRoom.LeaveSection();
delete cRoom;
delete exit;
JS_EndRequest(cx);
THROW_ERROR(cx, "Failed to create exit object!");
}

jsval a = OBJECT_TO_JSVAL(pExit);
JS_SetElement(cx, pArea->ExitArray, i, &a);
}
cRoom.LeaveSection();

}
vp.set(OBJECT_TO_JSVAL(pArea->ExitArray));
if(pArea->ExitArray)
Expand Down Expand Up @@ -102,7 +98,7 @@ JSAPI_PROP(area_getProperty)
vp.setInt32(pLevel->dwLevelNo);
break;
}

delete cRoom;
return JS_TRUE;
}

Expand Down
2 changes: 1 addition & 1 deletion JSCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ JSAPI_FUNC(my_delay)
LeaveCriticalSection(&Vars.cEventSection);
ExecScriptEvent(evt,false);
}
if (GetTickCount() - script->LastGC > 5000)
if (GetTickCount() - script->LastGC > 2000)
{
script->LastGC = start;
JS_GC(JS_GetRuntime(cx));
Expand Down
Loading

0 comments on commit 5d4564d

Please sign in to comment.