Skip to content

Commit

Permalink
Merge pull request #1035 from uyjulian/add_locks_various_gui
Browse files Browse the repository at this point in the history
Added various locking to prevent/fix race conditions
  • Loading branch information
rickgaiser committed Mar 16, 2024
2 parents 7542b3c + e1df538 commit 9ee3361
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
2 changes: 2 additions & 0 deletions include/bdmsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ void bdmLoadModules(void);
void bdmLaunchGame(int id, config_set_t *configSet);
void bdmSetPrefix(void);

void bdmInitSemaphore();

#endif
13 changes: 13 additions & 0 deletions src/bdmsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static char bdmDriver[5];

static int iLinkModLoaded = 0;
static int mx4sioModLoaded = 0;
static s32 bdmLoadModuleLock = -1;

// forward declaration
static item_list_t bdmGameList;
Expand Down Expand Up @@ -83,6 +84,8 @@ static void bdmEventHandler(void *packet, void *opt)

static void bdmLoadBlockDeviceModules(void)
{
WaitSema(bdmLoadModuleLock);

if (gEnableILK && !iLinkModLoaded) {
// Load iLink Block Device drivers
LOG("[ILINKMAN]:\n");
Expand All @@ -100,6 +103,8 @@ static void bdmLoadBlockDeviceModules(void)

mx4sioModLoaded = 1;
}

SignalSema(bdmLoadModuleLock);
}

void bdmLoadModules(void)
Expand Down Expand Up @@ -550,3 +555,11 @@ static item_list_t bdmGameList = {
BDM_MODE, 2, 0, 0, MENU_MIN_INACTIVE_FRAMES, BDM_MODE_UPDATE_DELAY, &bdmGetTextId, &bdmGetPrefix, &bdmInit, &bdmNeedsUpdate,
&bdmUpdateGameList, &bdmGetGameCount, &bdmGetGame, &bdmGetGameName, &bdmGetGameNameLength, &bdmGetGameStartup, &bdmDeleteGame, &bdmRenameGame,
&bdmLaunchGame, &bdmGetConfig, &bdmGetImage, &bdmCleanUp, &bdmShutdown, &bdmCheckVMC, &bdmGetIconId};

void bdmInitSemaphore()
{
// Create a semaphore so only one thread can load IOP modules at a time.
if (bdmLoadModuleLock < 0) {
bdmLoadModuleLock = sbCreateSemaphore();
}
}
31 changes: 20 additions & 11 deletions src/menusys.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static submenu_list_t *appMenu;
static submenu_list_t *appMenuCurrent;

static s32 menuSemaId;
static s32 menuListSemaId = -1;
static ee_sema_t menuSema;

static void menuRenameGame(submenu_list_t **submenu)
Expand Down Expand Up @@ -292,6 +293,9 @@ void menuInit()
menuSema.max_count = 1;
menuSema.option = 0;
menuSemaId = CreateSema(&menuSema);
if (menuListSemaId < 0) {
menuListSemaId = sbCreateSemaphore();
}
}

void menuEnd()
Expand Down Expand Up @@ -321,6 +325,8 @@ void menuEnd()
}

DeleteSema(menuSemaId);
DeleteSema(menuListSemaId);
menuListSemaId = -1;
}

static menu_list_t *AllocMenuItem(menu_item_t *item)
Expand All @@ -340,24 +346,27 @@ void menuAppendItem(menu_item_t *item)
{
assert(item);

WaitSema(menuListSemaId);

if (menu == NULL) {
menu = AllocMenuItem(item);
selected_item = menu;
return;
}
} else {
menu_list_t *cur = menu;

menu_list_t *cur = menu;
// traverse till the end
while (cur->next)
cur = cur->next;

// traverse till the end
while (cur->next)
cur = cur->next;
// create new item
menu_list_t *newitem = AllocMenuItem(item);

// create new item
menu_list_t *newitem = AllocMenuItem(item);
// link
cur->next = newitem;
newitem->prev = cur;
}

// link
cur->next = newitem;
newitem->prev = cur;
SignalSema(menuListSemaId);
}

void submenuRebuildCache(submenu_list_t *submenu)
Expand Down
2 changes: 2 additions & 0 deletions src/opl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,8 @@ static void init(void)

startPads();

bdmInitSemaphore();

// compatibility update handler
ioRegisterHandler(IO_COMPAT_UPDATE_DEFFERED, &compatDeferredUpdate);

Expand Down
24 changes: 19 additions & 5 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern unsigned int size_eesync_irx;

#define MAX_MODULES 64
static void *g_sysLoadedModBuffer[MAX_MODULES];
static s32 sysLoadModuleLock = -1;

#define ELF_MAGIC 0x464c457f
#define ELF_PT_LOAD 1
Expand Down Expand Up @@ -104,6 +105,8 @@ int sysLoadModuleBuffer(void *buffer, int size, int argc, char *argv)

int i, id, ret, index = 0;

WaitSema(sysLoadModuleLock);

// check we have not reached MAX_MODULES
for (i = 0; i < MAX_MODULES; i++) {
if (g_sysLoadedModBuffer[i] == NULL) {
Expand All @@ -113,27 +116,34 @@ int sysLoadModuleBuffer(void *buffer, int size, int argc, char *argv)
}
if (i == MAX_MODULES) {
LOG("WARNING: REACHED MODULES LIMIT (%d)\n", MAX_MODULES);
return -1;
ret = -1;
goto exit;
}

// check if the module was already loaded
for (i = 0; i < MAX_MODULES; i++) {
if (g_sysLoadedModBuffer[i] == buffer) {
LOG("MODULE ALREADY LOADED (%d)\n", i);
return 0;
ret = 0;
goto exit;
}
}

// load the module
id = SifExecModuleBuffer(buffer, size, argc, argv, &ret);
LOG("\t-- ID=%d, ret=%d\n", id, ret);
if ((id < 0) || (ret))
return -2;
if ((id < 0) || (ret)) {
ret = -2;
goto exit;
}

// add the module to the list
g_sysLoadedModBuffer[index] = buffer;

return 0;
ret = 0;
exit:
SignalSema(sysLoadModuleLock);
return ret;
}

#define OPL_SIF_CMD_BUFF_SIZE 1
Expand Down Expand Up @@ -210,6 +220,10 @@ void sysReset(int modload_mask)
sbv_patch_enable_lmb();
sbv_patch_disable_prefix_check();

if (sysLoadModuleLock < 0) {
sysLoadModuleLock = sbCreateSemaphore();
}

// clears modules list
memset((void *)&g_sysLoadedModBuffer[0], 0, MAX_MODULES * 4);

Expand Down

0 comments on commit 9ee3361

Please sign in to comment.