Skip to content

Commit

Permalink
Assert that active objects exist in deleters
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Feb 18, 2024
1 parent 59f7f62 commit 8af4562
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct thread_context {
static int activeThreads = 0;
static int activeMutexes = 0;
static int activeEvents = 0;
static int activeCondVars = 0;

#if defined(LC_WINDOWS)

Expand Down Expand Up @@ -151,6 +152,7 @@ int PltCreateMutex(PLT_MUTEX* mutex) {
}

void PltDeleteMutex(PLT_MUTEX* mutex) {
LC_ASSERT(activeMutexes > 0);
activeMutexes--;
#if defined(LC_WINDOWS)
// No-op to destroy a SRWLOCK
Expand Down Expand Up @@ -192,6 +194,7 @@ void PltUnlockMutex(PLT_MUTEX* mutex) {
}

void PltJoinThread(PLT_THREAD* thread) {
LC_ASSERT(activeThreads > 0);
activeThreads--;

#if defined(LC_WINDOWS)
Expand All @@ -212,7 +215,7 @@ void PltJoinThread(PLT_THREAD* thread) {
}

void PltDetachThread(PLT_THREAD* thread) {
// Assume detached threads are no longer active
LC_ASSERT(activeThreads > 0);
activeThreads--;

#if defined(LC_WINDOWS)
Expand Down Expand Up @@ -357,6 +360,7 @@ int PltCreateEvent(PLT_EVENT* event) {
}

void PltCloseEvent(PLT_EVENT* event) {
LC_ASSERT(activeEvents > 0);
activeEvents--;
#if defined(LC_WINDOWS)
CloseHandle(*event);
Expand Down Expand Up @@ -412,10 +416,13 @@ int PltCreateConditionVariable(PLT_COND* cond, PLT_MUTEX* mutex) {
#else
pthread_cond_init(cond, NULL);
#endif
activeCondVars++;
return 0;
}

void PltDeleteConditionVariable(PLT_COND* cond) {
LC_ASSERT(activeCondVars > 0);
activeCondVars--;
#if defined(LC_WINDOWS)
// No-op to delete a CONDITION_VARIABLE
#elif defined(__vita__)
Expand Down Expand Up @@ -537,4 +544,5 @@ void cleanupPlatform(void) {
LC_ASSERT(activeThreads == 0);
LC_ASSERT(activeMutexes == 0);
LC_ASSERT(activeEvents == 0);
LC_ASSERT(activeCondVars == 0);
}

0 comments on commit 8af4562

Please sign in to comment.