monitor and threads
This commit is contained in:
@@ -29,6 +29,8 @@
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include "Core/Monitor.h"
|
||||
#include "Core/Object.h"
|
||||
|
||||
@@ -66,6 +68,26 @@ public:
|
||||
/** @brief Static helper to yield the current thread. */
|
||||
static void Yield();
|
||||
|
||||
/** @brief Returns the native handle of the thread. */
|
||||
std::thread::native_handle_type GetNativeHandle() { return m_Thread.native_handle(); }
|
||||
|
||||
/** @brief Sets CPU affinity for the thread. (Linux only) */
|
||||
void SetAffinity(int cpu);
|
||||
|
||||
/** @brief Sets CPU affinity for the thread using a list of CPUs. (Linux only) */
|
||||
void SetAffinity(const std::vector<int>& cpus);
|
||||
|
||||
// OpenMP Support //
|
||||
|
||||
/** @brief Sets the number of threads for OpenMP parallel regions. */
|
||||
static void SetNumThreads(int n);
|
||||
|
||||
/** @brief Returns the number of threads for OpenMP parallel regions. */
|
||||
static int GetNumThreads();
|
||||
|
||||
/** @brief Returns the ID of the current thread in an OpenMP parallel region. */
|
||||
static int GetThreadNum();
|
||||
|
||||
protected:
|
||||
// Internal thread entry point
|
||||
void ThreadEntryPoint();
|
||||
@@ -75,6 +97,51 @@ protected:
|
||||
mutable Mutex m_ThreadMutex;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Task class wraps a function call to be executed by a Team.
|
||||
*/
|
||||
class Task : public Object {
|
||||
public:
|
||||
Task(std::function<void()> func) : m_Func(func) {}
|
||||
virtual ~Task() = default;
|
||||
|
||||
/** @brief Executes the task. */
|
||||
virtual void Execute() { if (m_Func) m_Func(); }
|
||||
|
||||
protected:
|
||||
std::function<void()> m_Func;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Team class manages a group of threads and can execute Tasks.
|
||||
* This is designed to be compatible with OpenMP tasks and teams.
|
||||
*/
|
||||
class Team : public Object {
|
||||
public:
|
||||
Team(int num_threads = -1);
|
||||
virtual ~Team();
|
||||
|
||||
/** @brief Runs a task within the team. Uses OpenMP task if available. */
|
||||
void Run(Task* task);
|
||||
|
||||
/** @brief Waits for all tasks in the team to finish. */
|
||||
void Wait();
|
||||
|
||||
/** @brief Sets the number of threads for this team. */
|
||||
void SetSize(int n);
|
||||
|
||||
/** @brief Returns the number of threads in the team. */
|
||||
int GetSize() const { return m_Size; }
|
||||
|
||||
/** @brief Sets CPU affinity for all threads in the team. */
|
||||
void SetAffinity(const std::vector<int>& cpus);
|
||||
|
||||
protected:
|
||||
int m_Size;
|
||||
bool m_UseOpenMP;
|
||||
std::vector<Thread*> m_Threads;
|
||||
};
|
||||
|
||||
} // namespace uLib
|
||||
|
||||
#endif // U_CORE_THREADS_H
|
||||
|
||||
Reference in New Issue
Block a user