2 * Copyright (c) 2002 Frodo
3 * Portions Copyright (c) by the authors of ffmpeg and xvid
4 * Copyright (C) 2002-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
23 #include "threads/SystemClock.h"
25 #include "threads/ThreadLocal.h"
26 #include "threads/SingleLock.h"
27 #include "commons/Exception.h"
29 #define __STDC_FORMAT_MACROS
32 static XbmcThreads::ThreadLocal<CThread> currentThread;
34 XbmcCommons::ILogger* CThread::logger = NULL;
36 #include "threads/platform/ThreadImpl.cpp"
38 //////////////////////////////////////////////////////////////////////
39 // Construction/Destruction
40 //////////////////////////////////////////////////////////////////////
42 #define LOG if(logger) logger->Log
44 CThread::CThread(const char* ThreadName)
45 : m_StopEvent(true,true), m_TermEvent(true), m_StartEvent(true)
49 m_bAutoDelete = false;
58 m_ThreadName = ThreadName;
61 CThread::CThread(IRunnable* pRunnable, const char* ThreadName)
62 : m_StopEvent(true,true), m_TermEvent(true), m_StartEvent(true)
66 m_bAutoDelete = false;
72 m_pRunnable=pRunnable;
75 m_ThreadName = ThreadName;
83 void CThread::Create(bool bAutoDelete, unsigned stacksize)
87 LOG(LOGERROR, "%s - fatal error creating thread- old thread id not null", __FUNCTION__);
90 m_iLastTime = XbmcThreads::SystemClockMillis() * 10000;
93 m_bAutoDelete = bAutoDelete;
99 SpawnThread(stacksize);
102 bool CThread::IsRunning() const
104 return m_ThreadId ? true : false;
107 THREADFUNC CThread::staticThread(void* data)
109 CThread* pThread = (CThread*)(data);
115 LOG(LOGERROR,"%s, sanity failed. thread is NULL.",__FUNCTION__);
119 name = pThread->m_ThreadName;
120 id = pThread->m_ThreadId;
121 autodelete = pThread->m_bAutoDelete;
123 pThread->SetThreadInfo();
125 LOG(LOGNOTICE,"Thread %s start, auto delete: %s", name.c_str(), (autodelete ? "true" : "false"));
127 currentThread.set(pThread);
128 pThread->m_StartEvent.Set();
132 // lock during termination
133 CSingleLock lock(pThread->m_CriticalSection);
135 pThread->m_ThreadId = 0;
136 pThread->m_TermEvent.Set();
137 pThread->TermHandler();
143 LOG(LOGDEBUG,"Thread %s %"PRIu64" terminating (autodelete)", name.c_str(), (uint64_t)id);
148 LOG(LOGDEBUG,"Thread %s %"PRIu64" terminating", name.c_str(), (uint64_t)id);
153 bool CThread::IsAutoDelete() const
155 return m_bAutoDelete;
158 void CThread::StopThread(bool bWait /*= true*/)
162 CSingleLock lock(m_CriticalSection);
163 if (m_ThreadId && bWait)
166 WaitForThreadExit(0xFFFFFFFF);
170 ThreadIdentifier CThread::ThreadId() const
175 void CThread::Process()
181 bool CThread::IsCurrentThread() const
183 return IsCurrentThread(ThreadId());
186 CThread* CThread::GetCurrentThread()
188 return currentThread.get();
191 void CThread::Sleep(unsigned int milliseconds)
193 if(milliseconds > 10 && IsCurrentThread())
194 m_StopEvent.WaitMSec(milliseconds);
196 XbmcThreads::ThreadSleep(milliseconds);
199 void CThread::Action()
205 catch (const XbmcCommons::UncheckedException &e)
207 e.LogThrowMessage("OnStartup");
213 LOG(LOGERROR, "%s - thread %s, Unhandled exception caught in thread startup, aborting. auto delete: %d", __FUNCTION__, m_ThreadName.c_str(), IsAutoDelete());
222 catch (const XbmcCommons::UncheckedException &e)
224 e.LogThrowMessage("Process");
228 LOG(LOGERROR, "%s - thread %s, Unhandled exception caught in thread process, aborting. auto delete: %d", __FUNCTION__, m_ThreadName.c_str(), IsAutoDelete());
235 catch (const XbmcCommons::UncheckedException &e)
237 e.LogThrowMessage("OnExit");
241 LOG(LOGERROR, "%s - thread %s, Unhandled exception caught in thread OnExit, aborting. auto delete: %d", __FUNCTION__, m_ThreadName.c_str(), IsAutoDelete());