Add SDUtilsAddCleanUpHandlesHandler and SDUtilsRemoveCleanUpHandlesHandler

This commit is contained in:
Maschell 2022-04-28 20:09:25 +02:00
parent 82aa1e2073
commit b3879a806c
3 changed files with 53 additions and 2 deletions

View File

@ -6,6 +6,8 @@
#define MAX_HANDLERS 16
static SDAttachHandlerFn sHandlers[MAX_HANDLERS] = {nullptr};
static SDCleanUpHandlesHandlerFn sCleanupHandlesHandlers[MAX_HANDLERS] = {nullptr};
void callAttachCallbacks(SDUtilsAttachStatus status) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
@ -15,10 +17,23 @@ void callAttachCallbacks(SDUtilsAttachStatus status) {
}
}
void callCleanUpHandlesCallbacks() {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sCleanupHandlesHandlers[i]) {
sCleanupHandlesHandlers[i]();
}
}
}
void cleanUpAttachCallbacks() {
memset(sHandlers, 0, sizeof(sHandlers));
}
void cleanUpCleanUpHandlesCallbacks() {
memset(sCleanupHandlesHandlers, 0, sizeof(sCleanupHandlesHandlers));
}
bool SDUtilsAddAttachHandler(SDAttachHandlerFn fn) {
int i;
@ -48,6 +63,35 @@ bool SDUtilsRemoveAttachHandler(SDAttachHandlerFn fn) {
return false;
}
bool SDUtilsAddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sCleanupHandlesHandlers[i] == fn) {
return true;
}
if (!sCleanupHandlesHandlers[i]) {
sCleanupHandlesHandlers[i] = fn;
return true;
}
}
return false;
}
bool SDUtilsRemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sCleanupHandlesHandlers[i] == fn) {
sCleanupHandlesHandlers[i] = nullptr;
return true;
}
}
return false;
}
SDUtilsVersion SDUtilsGetVersion() {
return SDUTILS_MODULE_VERSION;
}
@ -55,4 +99,7 @@ SDUtilsVersion SDUtilsGetVersion() {
WUMS_EXPORT_FUNCTION(SDUtilsGetVersion);
WUMS_EXPORT_FUNCTION(SDUtilsAddAttachHandler);
WUMS_EXPORT_FUNCTION(SDUtilsRemoveAttachHandler);
WUMS_EXPORT_FUNCTION(SDUtilsRemoveAttachHandler);
WUMS_EXPORT_FUNCTION(SDUtilsAddCleanUpHandlesHandler);
WUMS_EXPORT_FUNCTION(SDUtilsRemoveCleanUpHandlesHandler);

View File

@ -2,5 +2,7 @@
#include <sdutils/sdutils.h>
void callAttachCallbacks(SDUtilsAttachStatus status);
void cleanUpAttachCallbacks();
void cleanUpAttachCallbacks();
void callCleanUpHandlesCallbacks();
void cleanUpCleanUpHandlesCallbacks();

View File

@ -40,6 +40,7 @@ void DeInitSDCheck() {
}
cleanUpAttachCallbacks();
cleanUpCleanUpHandlesCallbacks();
OSMemoryBarrier();
}
@ -85,6 +86,7 @@ int UnmountSDCard() {
return -1;
}
std::lock_guard<std::mutex> lk(*mutex);
callCleanUpHandlesCallbacks();
FSCmdBlock fsCmd;
FSInitCmdBlock(&fsCmd);