Skip to content

Commit

Permalink
In debug mode, check work queue is empty, and the job counter matches
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed Feb 5, 2024
1 parent 31e8de1 commit 8250d82
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/ammonite/core/threadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <thread>

#include "../types.hpp"
#include "../utils/debug.hpp"

#define MAX_EXTRA_THREADS 512

Expand Down Expand Up @@ -171,6 +172,31 @@ namespace ammonite {
}
}

#ifdef DEBUG
bool debugCheckRemainingWork(bool verbose) {
bool issuesFound = false;

workQueueMutex.lock();
if (workQueue.size() != 0) {
issuesFound = true;
if (verbose) {
ammoniteInternalDebug << "WARNING: Work queue not empty" << std::endl;
}
}

if (workQueue.size() != (unsigned)jobCount) {
issuesFound = true;
if (verbose) {
ammoniteInternalDebug << "WARNING: Work queue size and job count don't match" \
<< std::endl;
}
}
workQueueMutex.unlock();

return issuesFound;
}
#endif

//Finish work already in the queue and kill the threads
void destroyThreadPool() {
//Finish existing work and block new work from starting
Expand All @@ -187,6 +213,14 @@ namespace ammonite {
threadPool[i].thread.join();
}

/* In debug mode, check that the queue is empty, and matches the job counter
- If it doesn't, it's not technically a bug, but in practice it will be, as work
that was expected to be finished wasn't
*/
#ifdef DEBUG
debugCheckRemainingWork(true);
#endif

//Reset remaining data
delete[] threadPool;
extraThreadCount = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/ammonite/core/threadManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ namespace ammonite {
}
}

#ifdef DEBUG
namespace ammonite {
namespace thread {
namespace internal {
bool debugCheckRemainingWork(bool verbose);
}
}
}
#endif

#endif
12 changes: 11 additions & 1 deletion src/threadDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ static void shortTask(void*) {
}

int main() {
bool passed = true;
ammonite::utils::Timer runTimer;

if (ammonite::thread::internal::createThreadPool(0) == -1) {
ammonite::utils::error << "Failed to create thread pool, exiting" << std::endl;
return EXIT_FAILURE;
Expand All @@ -39,5 +41,13 @@ int main() {

//Clean up and exit
ammonite::thread::internal::destroyThreadPool();
return EXIT_SUCCESS;

//Check work queue is empty, and job counter is also 0 (debug mode)
#ifdef DEBUG
if (ammonite::thread::internal::debugCheckRemainingWork(false)) {
passed = false;
}
#endif

return passed ? EXIT_SUCCESS : EXIT_FAILURE;
}

0 comments on commit 8250d82

Please sign in to comment.