Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Common/Cpp/Concurrency/Backends/ThreadPool_Default.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ class ThreadPool_Default final : public ThreadPool{
}
virtual WallDuration cpu_time() const override;

virtual size_t queued_tasks() const override{
std::lock_guard<Mutex> lg(m_lock);
return m_queue.size();
}
virtual size_t busy_threads() const override{
std::lock_guard<Mutex> lg(m_lock);
return m_busy_count;
}


public:
[[nodiscard]] virtual AsyncTask dispatch(std::function<void()>&& func) override;
Expand Down
3 changes: 3 additions & 0 deletions Common/Cpp/Concurrency/Backends/Thread_Qt.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Common/Cpp/Containers/Pimpl.tpp"
#include "Common/Cpp/Concurrency/Qt6.9ThreadBugWorkaround.h"
#include "Common/Cpp/Concurrency/Thread.h"
#include "Common/Cpp/TestRunners/HangWatchdog.h"

//using std::cout;
//using std::endl;
Expand Down Expand Up @@ -64,6 +65,8 @@ struct Thread::Data : public QThread{

virtual void run() override{
// cout << "starting: " << this << endl;
// Register this thread for the hang watchdog's stack dump.
HangWatchdog::register_thread();
m_function();
// m_finished.store(true, std::memory_order_acquire);
// cout << "exiting: " << this << endl;
Expand Down
7 changes: 6 additions & 1 deletion Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <thread>
#include "Common/Cpp/Containers/Pimpl.tpp"
#include "Common/Cpp/Concurrency/Thread.h"
#include "Common/Cpp/TestRunners/HangWatchdog.h"

namespace PokemonAutomation{

Expand Down Expand Up @@ -46,7 +47,11 @@ Thread::Thread() = default;
// - Uses CONSTRUCT_TOKEN to construct Pimpl<Data> with Data constructor
// - Thread starts running immediately (same as std::thread behavior)
Thread::Thread(std::function<void()>&& function)
: m_data(CONSTRUCT_TOKEN, std::move(function))
: m_data(CONSTRUCT_TOKEN, [function = std::move(function)](){
// Register the new thread for the hang watchdog's stack dump.
HangWatchdog::register_thread();
function();
})
{}

void Thread::join(){
Expand Down
9 changes: 9 additions & 0 deletions Common/Cpp/Concurrency/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class ThreadPool{
virtual size_t max_threads() const = 0;
virtual WallDuration cpu_time() const = 0;

// Diagnostics: how many tasks are queued up waiting for a thread, and
// how many threads are currently executing a task.
virtual size_t queued_tasks() const{
return 0;
}
virtual size_t busy_threads() const{
return 0;
}

public:
// As of this writing, tasks dispatched earlier are not allowed to block
// on tasks that are dispatched later as it may cause a deadlock.
Expand Down
19 changes: 19 additions & 0 deletions Common/Cpp/Logging/FileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*
*/

#include <chrono>
#include <iostream>
#include <thread>
#include "Common/Cpp/PrettyPrint.h"
#include "Common/Cpp/Filesystem/FilePath.h"
#include "Common/Cpp/Filesystem/Filesystem.h"
Expand Down Expand Up @@ -37,6 +39,23 @@ FileLogger::FileLogger(ThreadPool& thread_pool, FileLoggerConfig config)
FileLogger::~FileLogger(){
stop();
}
size_t FileLogger::pending_count() const{
// std::mutex has no try_lock_for, so poll try_lock() for up to ~500ms.
// The flusher only ever holds the lock briefly (file I/O happens outside
// of it), so this essentially never times out; the timeout is there so the
// diagnostic itself can never deadlock behind a stuck flusher.
std::unique_lock<Mutex> lg(m_lock, std::defer_lock);
for (int i = 0; i < 50; i++){
if (lg.try_lock()){
return m_queue.size();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return (size_t)-1;
}



void FileLogger::stop() noexcept{
if (!m_thread){
return;
Expand Down
7 changes: 7 additions & 0 deletions Common/Cpp/Logging/FileLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class FileLogger : public Logger{
~FileLogger();
void stop() noexcept;

// Diagnostics: # of log entries currently queued for writing, or
// (size_t)-1 if the lock could not be taken in time.
size_t pending_count() const;
size_t max_queue_size() const{
return m_config.max_queue_size;
}

// Logger interface implementation
virtual void log(const std::string& msg, Color color = Color()) override;
virtual void log(std::string&& msg, Color color = Color()) override;
Expand Down
Loading
Loading