#pragma once
#include "platform_config.h"
#ifdef PLATFORM_THREAD_OVERRIDE
#include "platform_thread.h"
#else
#include "core/typedefs.h"
#ifdef THREADS_ENABLED
#include "core/templates/safe_refcount.h"
#include <new>
#ifdef MINGW_ENABLED
#define MINGW_STDTHREAD_REDUNDANCY_WARNING
#include "thirdparty/mingw-std-threads/mingw.thread.h"
#define THREADING_NAMESPACE mingw_stdthread
#else
#include <thread>
#define THREADING_NAMESPACE std
#endif
class String;
class Thread {
public:
typedef void (*Callback)(void *p_userdata);
typedef uint64_t ID;
enum : ID {
UNASSIGNED_ID = 0,
MAIN_ID = 1
};
enum Priority {
PRIORITY_LOW,
PRIORITY_NORMAL,
PRIORITY_HIGH
};
struct Settings {
Priority priority;
Settings() { priority = PRIORITY_NORMAL; }
};
struct PlatformFunctions {
Error (*set_name)(const String &) = nullptr;
void (*set_priority)(Thread::Priority) = nullptr;
void (*init)() = nullptr;
void (*wrapper)(Thread::Callback, void *) = nullptr;
void (*term)() = nullptr;
};
#if defined(__cpp_lib_hardware_interference_size) && !defined(ANDROID_ENABLED)
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Winterference-size")
static constexpr size_t CACHE_LINE_BYTES = std::hardware_destructive_interference_size;
GODOT_GCC_WARNING_POP
#else
static constexpr size_t CACHE_LINE_BYTES = 128;
#endif
private:
friend class Main;
static PlatformFunctions platform_functions;
ID id = UNASSIGNED_ID;
static SafeNumeric<uint64_t> id_counter;
static thread_local ID caller_id;
THREADING_NAMESPACE::thread thread;
static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata);
static void make_main_thread() { caller_id = MAIN_ID; }
static void release_main_thread() { caller_id = id_counter.increment(); }
public:
static void _set_platform_functions(const PlatformFunctions &p_functions);
_FORCE_INLINE_ static void yield() { std::this_thread::yield(); }
_FORCE_INLINE_ ID get_id() const { return id; }
_FORCE_INLINE_ static ID get_caller_id() {
return caller_id;
}
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
_FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; }
static Error set_name(const String &p_name);
ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
bool is_started() const;
void wait_to_finish();
Thread();
~Thread();
};
#else
class String;
class Thread {
public:
typedef void (*Callback)(void *p_userdata);
typedef uint64_t ID;
static constexpr size_t CACHE_LINE_BYTES = sizeof(void *);
enum : ID {
UNASSIGNED_ID = 0,
MAIN_ID = 1
};
enum Priority {
PRIORITY_LOW,
PRIORITY_NORMAL,
PRIORITY_HIGH
};
struct Settings {
Priority priority;
Settings() { priority = PRIORITY_NORMAL; }
};
struct PlatformFunctions {
Error (*set_name)(const String &) = nullptr;
void (*set_priority)(Thread::Priority) = nullptr;
void (*init)() = nullptr;
void (*wrapper)(Thread::Callback, void *) = nullptr;
void (*term)() = nullptr;
};
private:
friend class Main;
static PlatformFunctions platform_functions;
static void make_main_thread() {}
static void release_main_thread() {}
public:
static void _set_platform_functions(const PlatformFunctions &p_functions);
_FORCE_INLINE_ ID get_id() const { return 0; }
_FORCE_INLINE_ static ID get_caller_id() { return MAIN_ID; }
_FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; }
_FORCE_INLINE_ static bool is_main_thread() { return true; }
static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; }
void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {}
bool is_started() const { return false; }
void wait_to_finish() {}
};
#endif
#endif