Skip to content

Commit

Permalink
Lua should crash 99% less often
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 10, 2011
1 parent c6aa0b7 commit c502819
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions mast/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int frameCount;
#define HEADER_SIZE 0xF4
#define SAVE_STATE_SIZE 41472

extern CRITICAL_SECTION m_cs; // ../master/main.cpp
int DEGA_LuaRerecordCountSkip(); // ../master/luaengine.cpp

static int MastAcbVideoRead(struct MastArea *area) {
Expand Down Expand Up @@ -403,7 +404,9 @@ void MvidPostLoadState(int readonly) {
fclose(videoFile);
videoFile = fopen(videoFilename, "r+b");

EnterCriticalSection(&m_cs);
if(!DEGA_LuaRerecordCountSkip()) currentMovie.rerecordCount++;
LeaveCriticalSection(&m_cs);

if (embInfo.isOldType != 0) {
currentMovie.vidFrameCount = frameCount;
Expand Down
2 changes: 2 additions & 0 deletions master/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,6 @@ int ZipRead(unsigned char **pMem,int *pLen);
#include "ramsearch.h"
#include "ramwatch.h"

extern CRITICAL_SECTION m_cs;

#endif
2 changes: 2 additions & 0 deletions master/disp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ static int MemToSurf(IDirectDrawSurface *DestSurf)
memcpy(pd,ps,DispMemPitch);
}

EnterCriticalSection(&m_cs);
DEGA_LuaGui(Surf,ScrnWidth,ScrnHeight,DispBpp,Pitch);
LeaveCriticalSection(&m_cs);

Ret=DestSurf->Unlock(NULL);

Expand Down
2 changes: 2 additions & 0 deletions master/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ static LRESULT CALLBACK WindowProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lPara
char*& fname = recent_lua[wParam - LUA_FIRST_RECENT_FILE];
if(fname)
{
EnterCriticalSection(&m_cs);
DEGA_LoadLuaCode(fname);
LeaveCriticalSection(&m_cs);
}
}
if (Item==ID_LUA_OPEN) {
Expand Down
4 changes: 4 additions & 0 deletions master/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ static int MediaInit(int Level)
if (MastEx&MX_GG) { ScrnWidth=160; ScrnHeight=144; } // Game gear
else { ScrnWidth=256; ScrnHeight=192; } // Master System

EnterCriticalSection(&m_cs);
CallRegisteredLuaFunctions(LUACALL_ONSTART);
LeaveCriticalSection(&m_cs);
}

if (Level<=30) // DirectDraw
Expand Down Expand Up @@ -123,7 +125,9 @@ static int MediaInit(int Level)
if (Level<=58) // Load state
{
Update_RAM_Search();
EnterCriticalSection(&m_cs);
DEGA_LuaFrameBoundary();
LeaveCriticalSection(&m_cs);
DispDraw();
}

Expand Down
8 changes: 8 additions & 0 deletions master/luaconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,16 @@ INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM l
{
char filename[MAX_PATH];
GetDlgItemText(hDlg, IDC_EDIT_LUAPATH, filename, MAX_PATH);
EnterCriticalSection(&m_cs);
DEGA_LoadLuaCode(filename);
LeaveCriticalSection(&m_cs);
} break;

case IDC_BUTTON_LUASTOP:
{
EnterCriticalSection(&m_cs);
DEGA_LuaStop();
LeaveCriticalSection(&m_cs);
} break;

case IDC_BUTTON_LUAEDIT:
Expand Down Expand Up @@ -312,7 +316,9 @@ INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM l
break;

case WM_CLOSE: {
EnterCriticalSection(&m_cs);
DEGA_LuaStop();
LeaveCriticalSection(&m_cs);
DragAcceptFiles(hDlg, FALSE);
if (hFont) {
DeleteObject(hFont);
Expand All @@ -333,7 +339,9 @@ INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM l
DragQueryFile(hDrop, 0, filename, MAX_PATH);
}
DragFinish(hDrop);
EnterCriticalSection(&m_cs);
DEGA_LoadLuaCode(filename);
LeaveCriticalSection(&m_cs);
return true;
} break;

Expand Down
5 changes: 4 additions & 1 deletion master/luaengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,11 @@ void CallRegisteredLuaMemHook(unsigned int address, int size, unsigned int value
{
//if((hookType <= LUAMEMHOOK_EXEC) && (address >= 0xE00000))
// address |= 0xFF0000; // account for mirroring of RAM
if(hookedRegions[hookType].Contains(address, size))
if(hookedRegions[hookType].Contains(address, size)) {
EnterCriticalSection(&m_cs);
CallRegisteredLuaMemHook_LuaMatch(address, size, value, hookType); // something has hooked this specific address
LeaveCriticalSection(&m_cs);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions master/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ int Fullscreen=0;
HACCEL hAccel=NULL; // Key accelerators
int StartInFullscreen=0;

CRITICAL_SECTION m_cs;

static FILE *DebugFile=NULL;
// Debug printf to a file
extern "C" int dprintf (char *Format,...)
Expand Down Expand Up @@ -62,6 +64,8 @@ int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR pCmdLine,INT)

if (StartInFullscreen) Fullscreen=1;

InitializeCriticalSection(&m_cs);

// Main loop
LoopDo();

Expand Down
12 changes: 10 additions & 2 deletions master/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ void RunPostChangeStatus()
int RunFrame(int Draw,short *pSound)
{
if (!NoInput) InputGet();
if(DEGA_LuaUsingJoypad(0)) MastInput[0] = DEGA_LuaReadJoypad(0);
if(DEGA_LuaUsingJoypad(1)) MastInput[1] = DEGA_LuaReadJoypad(1);

EnterCriticalSection(&m_cs);
if(DEGA_LuaUsingJoypad(0)) MastInput[0] = DEGA_LuaReadJoypad(0);
if(DEGA_LuaUsingJoypad(1)) MastInput[1] = DEGA_LuaReadJoypad(1);
LeaveCriticalSection(&m_cs);

pMsndOut=pSound;

EnterCriticalSection(&m_cs);
CallRegisteredLuaFunctions(LUACALL_BEFOREEMULATION); //TODO: find proper place
LeaveCriticalSection(&m_cs);

// Run frame
EmuFrame();
Expand All @@ -58,8 +63,11 @@ int RunFrame(int Draw,short *pSound)
RunPostChangeStatus();

Update_RAM_Search();

EnterCriticalSection(&m_cs);
DEGA_LuaFrameBoundary();
CallRegisteredLuaFunctions(LUACALL_AFTEREMULATION); //TODO: find proper place
LeaveCriticalSection(&m_cs);

if (Draw) DispDraw();

Expand Down

0 comments on commit c502819

Please sign in to comment.