Skip to content

Commit

Permalink
Fix 32 bit crash issue (#230)
Browse files Browse the repository at this point in the history
* fix 32-bit crash in threadpool and possible seg fault if get item was null

* clang format

* remove null item and keep iterating
  • Loading branch information
hassanctech authored Dec 11, 2023
1 parent e1b4f92 commit 1c4f6ec
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/utils/src/Threadpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ STATUS threadpoolFree(PThreadpool pThreadpool)
STATUS retStatus = STATUS_SUCCESS;
StackQueueIterator iterator;
PThreadData item = NULL;
BOOL finished = FALSE, taskQueueEmpty = FALSE;
UINT64 data;
BOOL finished = FALSE, taskQueueEmpty = FALSE, listMutedLocked = FALSE;
CHK(pThreadpool != NULL, STATUS_NULL_ARG);

// Threads are not forced to finish their tasks. If the user has assigned
Expand All @@ -311,6 +312,7 @@ STATUS threadpoolFree(PThreadpool pThreadpool)
while (!finished) {
// lock list mutex
MUTEX_LOCK(pThreadpool->listMutex);
listMutedLocked = TRUE;

do {
// iterate on list
Expand All @@ -319,15 +321,22 @@ STATUS threadpoolFree(PThreadpool pThreadpool)
finished = TRUE;
break;
}
retStatus = stackQueueIteratorGetItem(iterator, &item);

// attempt to lock mutex of item
if (MUTEX_TRYLOCK(item->dataMutex)) {
CHK_STATUS(stackQueueIteratorGetItem(iterator, &data));
item = (PThreadData) data;

if (item == NULL) {
DLOGW("NULL thread data present on threadpool.");
if (stackQueueRemoveItem(pThreadpool->threadList, data) != STATUS_SUCCESS) {
DLOGE("Failed to remove NULL thread data from threadpool");
}
// attempt to lock mutex of item
} else if (MUTEX_TRYLOCK(item->dataMutex)) {
// set terminate flag of item
ATOMIC_STORE_BOOL(&item->terminate, TRUE);

// when we acquire the lock, remove the item from the list. Its thread will free it.
if (stackQueueRemoveItem(pThreadpool->threadList, (UINT64) item) != STATUS_SUCCESS) {
if (stackQueueRemoveItem(pThreadpool->threadList, data) != STATUS_SUCCESS) {
DLOGE("Failed to remove thread data from threadpool");
}
MUTEX_UNLOCK(item->dataMutex);
Expand All @@ -349,12 +358,19 @@ STATUS threadpoolFree(PThreadpool pThreadpool)
} while (1);

MUTEX_UNLOCK(pThreadpool->listMutex);
listMutedLocked = FALSE;
if (!finished) {
// the aforementioned sleep
THREAD_SLEEP(10 * HUNDREDS_OF_NANOS_IN_A_MILLISECOND);
}
}

CleanUp:

if (listMutedLocked) {
MUTEX_UNLOCK(pThreadpool->listMutex);
}

// now free all the memory
MUTEX_FREE(pThreadpool->listMutex);
stackQueueFree(pThreadpool->threadList);
Expand All @@ -363,8 +379,6 @@ STATUS threadpoolFree(PThreadpool pThreadpool)
safeBlockingQueueFree(pThreadpool->taskQueue);
SAFE_MEMFREE(pThreadpool);

CleanUp:

return retStatus;
}

Expand Down

0 comments on commit 1c4f6ec

Please sign in to comment.