diff --git a/src/utils/src/Threadpool.c b/src/utils/src/Threadpool.c index 985e10b0..f5589316 100644 --- a/src/utils/src/Threadpool.c +++ b/src/utils/src/Threadpool.c @@ -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 @@ -311,6 +312,7 @@ STATUS threadpoolFree(PThreadpool pThreadpool) while (!finished) { // lock list mutex MUTEX_LOCK(pThreadpool->listMutex); + listMutedLocked = TRUE; do { // iterate on list @@ -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); @@ -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); @@ -363,8 +379,6 @@ STATUS threadpoolFree(PThreadpool pThreadpool) safeBlockingQueueFree(pThreadpool->taskQueue); SAFE_MEMFREE(pThreadpool); -CleanUp: - return retStatus; }