inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
thread_manager.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "workers.hpp"
9#include <atomic>
10#include <future>
11#include <memory>
12#include <mutex>
13#include <thread>
14
20{
21public:
27 {
28 std::mutex play_mutex;
29 std::mutex queue_mutex;
30 std::mutex audioDevicesMutex;
31
32 std::atomic<bool> is_processing{false};
33 std::atomic<bool> is_playing{false};
34
35 std::future<void> play_future;
36 std::unique_ptr<std::thread> mpris_dbus_thread;
37 std::thread playNextSongThread;
38 };
39
43 ThreadManager() : worker_pool(std::make_shared<WorkerThreadPool>()) {}
44
48 /*~ThreadManager() { cleanupAllThreads(); }*/
49
54 auto getThreadState() -> ThreadState& { return thread_state; }
55
60 auto getWorkerThreadPool() -> WorkerThreadPool& { return *worker_pool; }
61
66 void lockPlayMutex(ThreadState& thread_state)
67 {
68 std::lock_guard<std::mutex> lock(thread_state.play_mutex);
69 }
70
75 void lockQueueMutex(ThreadState& thread_state)
76 {
77 std::lock_guard<std::mutex> lock(thread_state.queue_mutex);
78 }
79
80private:
84 void cleanupAllThreads()
85 {
86 try
87 {
88 // Clean up playNextSongThread if joinable.
89 if (thread_state.playNextSongThread.joinable())
90 {
91 thread_state.playNextSongThread.join();
92 }
93 }
94 catch (const std::exception& e)
95 {
96 // Log exceptions during cleanup (adjust logging mechanism as needed).
97 std::cerr << "Exception during thread cleanup: " << e.what() << std::endl;
98 }
99 catch (...)
100 {
101 std::cerr << "Unknown exception during thread cleanup." << std::endl;
102 }
103 }
104
108 ThreadState thread_state;
109
113 std::shared_ptr<WorkerThreadPool> worker_pool;
114};
void lockQueueMutex(ThreadState &thread_state)
Locks the queue mutex for the provided thread state.
Definition thread_manager.hpp:75
auto getThreadState() -> ThreadState &
Destructor to clean up resources and threads.
Definition thread_manager.hpp:54
ThreadManager()
Construct a ThreadManager with a worker thread pool.
Definition thread_manager.hpp:43
void lockPlayMutex(ThreadState &thread_state)
Locks the play mutex for the provided thread state.
Definition thread_manager.hpp:66
auto getWorkerThreadPool() -> WorkerThreadPool &
Get the worker thread pool.
Definition thread_manager.hpp:60
A thread pool that manages a collection of worker threads.
Definition workers.hpp:23
Represents the state of a thread managed by ThreadManager.
Definition thread_manager.hpp:27
std::thread playNextSongThread
Thread for handling "play next song" operations.
Definition thread_manager.hpp:37
std::future< void > play_future
Future object for async play operations.
Definition thread_manager.hpp:35
std::mutex play_mutex
Mutex for synchronizing play state.
Definition thread_manager.hpp:28
std::mutex audioDevicesMutex
Mutex for synchronizing audio device access.
Definition thread_manager.hpp:30
std::mutex queue_mutex
Mutex for synchronizing queue operations.
Definition thread_manager.hpp:29
std::unique_ptr< std::thread > mpris_dbus_thread
Unique pointer for DBus thread.
Definition thread_manager.hpp:36
std::atomic< bool > is_processing
Indicates whether the thread is processing.
Definition thread_manager.hpp:32
std::atomic< bool > is_playing
Indicates whether the thread is playing.
Definition thread_manager.hpp:33