EHS
Thread.h
Go to the documentation of this file.
1#pragma once
2
3#include "ehs/EHS.h"
4#include "ehs/Serializer.h"
5#include "ehs/Array.h"
6#include "ehs/Log.h"
7#include "ehs/Str.h"
8
9#ifdef EHS_OS_WINDOWS
10 #include <processthreadsapi.h>
11#endif
12
13namespace ehs
14{
15 #if defined(EHS_OS_WINDOWS)
16 #define EHS_INVALID_THREAD nullptr
17 typedef void* THandle;
18 #elif defined(EHS_OS_LINUX)
19 #define EHS_INVALID_THREAD 0
20 typedef UInt_64 THandle;
21 #endif
22
23 class EHS_LIB_IO Thread
24 {
25 private:
26 static UInt_32 mainId;
27
28 #ifdef EHS_OS_WINDOWS
29 static Handle mainTaskHdl;
30 static UInt_32 mainTaskIndex;
31 #endif
32
33 UInt_64 stackSize;
34 THandle hdl;
35 UInt_32 id;
36
37 #ifdef EHS_OS_WINDOWS
38 Handle taskHdl;
39 UInt_32 taskIndex;
40 #endif
41
42 public:
44 ~Thread();
45
47 Thread(const UInt_64 stackSize = 0);
48
51 Thread(const Thread& thread);
52
56 Thread& operator=(const Thread& thread);
57
61 void Start(UInt_32 (*cb)(void*), void* args);
62
66 bool Join(const unsigned int timeout = EHS_INFINITE);
67
69 void Detach();
70
73 UInt_64 GetStackSize() const;
74
77 THandle GetHandle() const;
78
81 UInt_32 GetId() const;
82
85 bool IsCurrent() const;
86
87 bool IsValid() const;
88
89 #ifdef EHS_OS_WINDOWS
93 void SetTaskType_32(const Str_32& task);
94
98 void SetTaskType_16(const Str_16& task);
99
103 void SetTaskType_8(const Str_8& task);
104
105 void RevertTaskType();
106 #endif
107
109 static UInt_32 GetMainId();
110
113 static UInt_64 GetCurrentId();
114
115 #ifdef EHS_OS_WINDOWS
119 static void SetMainTaskType_32(const Str_32& task);
120
124 static void SetMainTaskType_16(const Str_16& task);
125
129 static void SetMainTaskType_8(const Str_8& task);
130
131 static void RevertMainTaskType();
132 #endif
133
138 static float HardSleepFor(const float seconds);
139
143 static void SleepFor(const UInt_32 miliseconds);
144
145 private:
146 static void* Redirect(void* args);
147 };
148}
#define EHS_INFINITE
Definition: Types.h:35
Definition: Str.h:29
Definition: Thread.h:24
Definition: Anchor.h:6
unsigned int UInt_32
Definition: Types.h:49