JD2022-TU1/main/extern/dtest/dtcore/inc/dtthread.h

70 lines
1.9 KiB
C++

#pragma once
#include "dtcore/inc/dtdefinitions.h"
#include "dtcore/inc/dterror.h"
#include "dtcore/inc/dtchrono.h"
#pragma warning(push)
#include <functional>
#include <atomic>
#pragma warning(pop)
namespace dt
{
// This function is used to trap access from different thread on a unsafe resource.
#define DT_THREAD_SINGLE_STATIC_ACCESS_ASSERT() \
do { static std::atomic<dt::ThreadId> s_threadId(0); DT_ASSERT(dt::Thread::CheckThreadId(s_threadId)); } while (0)
// This function is used to trap access from different thread on a unsafe resource.
#define DT_THREAD_SINGLE_ACCESS_ASSERT(threadId) \
do { DT_ASSERT(dt::Thread::CheckThreadId((threadId))); } while (0)
// This macro is there to asses that a given function implementation is thread called from multiple threads and shall be thread safe
#define DT_THREAD_MULTIPLE_ACCESS_ASSERT()
class ThreadImpl;
typedef U64 ThreadId;
class Thread
{
public:
DT_CORE_DLL_ENTRY Thread();
DT_CORE_DLL_ENTRY ~Thread();
DT_CORE_DLL_ENTRY void Start(eal_id ealId, const std::function<void()>& function, bool silent = false);
DT_CORE_DLL_ENTRY void Stop();
DT_CORE_DLL_ENTRY bool IsRunning() const;
// only call after the thread is started, otherwise it will hang.
DT_CORE_DLL_ENTRY ThreadId GetThreadId() const;
DT_CORE_DLL_ENTRY static void Sleep(MilliSeconds millisecond);
enum class Priority
{
Idle,
Lowest,
BelowNormal,
Normal,
AboveNormal,
Highest,
TimeCritical
};
DT_CORE_DLL_ENTRY static void SetDefaultPriority(Priority value);
DT_CORE_DLL_ENTRY static ThreadId GetId();
DT_CORE_DLL_ENTRY static bool CheckThreadId(std::atomic<dt::ThreadId>& threadId);
private:
ThreadImpl* m_Impl;
};
}