Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
Threading.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "Errors.h"
8#include "Threading.h"
9#include <functional>
10
11using namespace Skyfire;
12
14{
15 for (int i = Idle; i < MAXPRIORITYNUM; ++i)
16 m_priority[i] = i;
17}
18
20{
21 if (p < Idle)
22 p = Idle;
23
24 if (p > Realtime)
25 p = Realtime;
26
27 return m_priority[p];
28}
29
31
32Thread::Thread(Runnable* instance) : m_iThreadId(0), m_task(instance), m_started(false)
33{
34 // register reference to m_task to prevent it deletion until destructor
35 if (m_task)
36 m_task->incReference();
37
38 bool _start = start();
39 ASSERT(_start);
40}
41
43{
44 if (m_thread.joinable())
45 m_thread.detach();
46
47 // deleted runnable object (if no other references)
48 if (m_task)
49 m_task->decReference();
50}
51
53{
54 if (m_task == 0 || m_started)
55 return false;
56
57 // incRef before spawning the thread, otherwise Thread::ThreadTask() might call decRef and delete m_task
58 m_task->incReference();
59
60 try
61 {
62 m_thread = std::thread(&Thread::ThreadTask, m_task);
63 m_iThreadId = std::hash<std::thread::id>()(m_thread.get_id());
64 m_started = true;
65 }
66 catch (...)
67 {
68 m_task->decReference();
69 return false;
70 }
71
72 return true;
73}
74
76{
77 if (!m_thread.joinable())
78 return false;
79
80 m_thread.join();
81
82 m_iThreadId = 0;
83 m_started = false;
84
85 return true;
86}
87
89{
90 if (m_thread.joinable())
91 m_thread.detach();
92
93 m_iThreadId = 0;
94 m_started = false;
95}
96
98{
99}
100
102{
103}
104
106{
107 Runnable* _task = param;
108 _task->run();
109
110 // task execution complete, free reference added at start()
111 _task->decReference();
112}
113
115{
116 return std::hash<std::thread::id>()(std::this_thread::get_id());
117}
118
119std::thread::native_handle_type Thread::currentHandle()
120{
121 return std::thread::native_handle_type();
122}
123
125{
126 static thread_local Thread thread;
127 if (!thread.m_started)
128 {
130 thread.m_started = true;
131 }
132
133 return &thread;
134}
135
137{
138 (void)type;
139}
140
141void Thread::Sleep(unsigned long msecs)
142{
144}
std::uint32_t uint32
Definition Define.h:77
#define ASSERT
Definition Errors.h:29
#define MAXPRIORITYNUM
Definition Threading.h:45
void decReference()
Definition Threading.h:25
virtual void run()=0
static Thread * current()
void setPriority(Priority type)
std::thread m_thread
Definition Threading.h:85
static void ThreadTask(Runnable *param)
static std::thread::native_handle_type currentHandle()
uint64_t m_iThreadId
Definition Threading.h:84
static uint64_t currentId()
Runnable * m_task
Definition Threading.h:86
static void Sleep(unsigned long msecs)
int getPriority(Priority p) const
Definition Threading.cpp:19
int m_priority[MAXPRIORITYNUM]
Definition Threading.h:54
@ Realtime
Definition Threading.h:42
void SleepForMilliseconds(uint32 milliseconds)
Definition TimeUtils.h:42