| 137 | |
| 138 | |
| 139 | void CThreadScheduler::CreateSchedulerThread() |
| 140 | { |
| 141 | if ((m_thread && m_thread->IsAlive()) || m_tasks.empty()) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // A thread can only be run once, so the old one must be safely disposed of |
| 146 | if (m_thread) { |
| 147 | AddDebugLogLineN(logThreads, "CreateSchedulerThread: Disposing of old thread."); |
| 148 | m_thread->Stop(); |
| 149 | delete m_thread; |
| 150 | } |
| 151 | |
| 152 | m_thread = new CTaskThread(this); |
| 153 | |
| 154 | wxThreadError err = m_thread->Create(); |
| 155 | if (err == wxTHREAD_NO_ERROR) { |
| 156 | // Try to avoid reducing the latency of the main thread |
| 157 | m_thread->SetPriority(WXTHREAD_MIN_PRIORITY); |
| 158 | |
| 159 | err = m_thread->Run(); |
| 160 | if (err == wxTHREAD_NO_ERROR) { |
| 161 | AddDebugLogLineN(logThreads, "Scheduler thread started"); |
| 162 | return; |
| 163 | } else { |
| 164 | AddDebugLogLineC(logThreads, "Error while starting scheduler thread: " + GetErrMsg(err)); |
| 165 | } |
| 166 | } else { |
| 167 | AddDebugLogLineC(logThreads, "Error while creating scheduler thread: " + GetErrMsg(err)); |
| 168 | } |
| 169 | |
| 170 | // Creation or running failed. |
| 171 | m_thread->Stop(); |
| 172 | delete m_thread; |
| 173 | m_thread = NULL; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** This is the sorter functor for the task-queue. */ |