| 78 | } |
| 79 | |
| 80 | int32 ThreadBase::Run() |
| 81 | { |
| 82 | // Setup |
| 83 | ASSERT(_runnable); |
| 84 | const auto thread = static_cast<Thread*>(this); |
| 85 | _id = Platform::GetCurrentThreadID(); |
| 86 | #if TRACY_ENABLE |
| 87 | char threadName[100]; |
| 88 | const int32 threadNameLength = Math::Min<int32>(ARRAY_COUNT(threadName) - 1, _name.Length()); |
| 89 | StringUtils::ConvertUTF162ANSI(*_name, threadName, threadNameLength); |
| 90 | threadName[threadNameLength] = 0; |
| 91 | tracy::SetThreadName(threadName); |
| 92 | #endif |
| 93 | ThreadRegistry::Add(thread); |
| 94 | ThreadStarting(thread); |
| 95 | int32 exitCode = 1; |
| 96 | _isRunning = true; |
| 97 | |
| 98 | LOG(Info, "Thread \'{0}\' ID=0x{1:x} started with priority {2}", _name, _id, ::ToString(GetPriority())); |
| 99 | |
| 100 | if (_runnable->Init()) |
| 101 | { |
| 102 | exitCode = _runnable->Run(); |
| 103 | |
| 104 | if (_callAfterWork) // Prevent from calling this after calling AfterWork since object may be deleted |
| 105 | _runnable->Exit(); |
| 106 | } |
| 107 | |
| 108 | LOG(Info, "Thread \'{0}\' ID=0x{1:x} exits with code {2}", _name, _id, exitCode); |
| 109 | |
| 110 | // End |
| 111 | if (_callAfterWork) |
| 112 | { |
| 113 | _callAfterWork = false; |
| 114 | _runnable->AfterWork(false); |
| 115 | } |
| 116 | _isRunning = false; |
| 117 | ThreadExiting(thread, exitCode); |
| 118 | ThreadRegistry::Remove(thread); |
| 119 | MCore::Thread::Exit(); // TODO: use mono_thread_detach instead of ext and unlink mono runtime from thread in ThreadExiting delegate |
| 120 | // mono terminates the native thread.. |
| 121 | |
| 122 | return exitCode; |
| 123 | } |
no test coverage detected