Skip to content

Commit

Permalink
threads: remove WINAPI, implement priority for pthreads
Browse files Browse the repository at this point in the history
Also:
[fix] threads: fix race condition when stopping thread - cherry pick merge

Also, from jimfcarroll:
[fixed] AirPlayServer now complies with the new CThread constructor requirements (that is, you must supply a thread name).
replace custom thread local storage with the XbmcThreads::ThreadLocal template.
[cosmetic] removed incorrect hungarian notation.
[cleanup] Removed XSyncUtils. Moved the GlobalMemoryStatus call to XMemUtil. Cleaned up code that used the #define INFINITE from XSyncUtils.h
[cleanup] Some miscellaneous cleanup. Fixed initialization warning in LCD. Removed some unused functions and members.
[cleanup] PAPlayer warning due to constructor initialization list order.
  • Loading branch information
FernetMenta authored and Jim Carroll committed Apr 21, 2012
1 parent 8d73c5d commit 64427d4
Show file tree
Hide file tree
Showing 85 changed files with 966 additions and 1,114 deletions.
4 changes: 0 additions & 4 deletions lib/UnrarXLib/extract.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "rar.hpp"
#include "Util.h"
#ifdef _LINUX
#include "XSyncUtils.h"
#include "XEventUtils.h"
#endif

// a cautious wrapper around strncpy
char *strncpy_null_terminated(char *dest, const char *src, size_t n)
Expand Down
5 changes: 0 additions & 5 deletions lib/UnrarXLib/unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
#include "unpack20.cpp"
#endif

#ifdef _LINUX
#include "XSyncUtils.h"
#include "XEventUtils.h"
#endif

Unpack::Unpack(ComprDataIO *DataIO)
{
UnpIO=DataIO;
Expand Down
4 changes: 0 additions & 4 deletions lib/UnrarXLib/unpack15.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#ifdef _LINUX
#include "XSyncUtils.h"
#endif

#define STARTL1 2
static unsigned int DecL1[]={0x8000,0xa000,0xc000,0xd000,0xe000,0xea00,
0xee00,0xf000,0xf200,0xf200,0xffff};
Expand Down
2 changes: 1 addition & 1 deletion xbmc/ApplicationMessenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

using namespace std;

CDelayedMessage::CDelayedMessage(ThreadMessage& msg, unsigned int delay)
CDelayedMessage::CDelayedMessage(ThreadMessage& msg, unsigned int delay) : CThread("CDelayedMessage")
{
m_msg.dwMessage = msg.dwMessage;
m_msg.dwParam1 = msg.dwParam1;
Expand Down
2 changes: 1 addition & 1 deletion xbmc/cdrip/CDDAReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#define SECTOR_COUNT 52

CCDDAReader::CCDDAReader()
CCDDAReader::CCDDAReader() : CThread("CCDDAReader")
{
m_sRipBuffer[0].pbtStream = NULL;
m_sRipBuffer[1].pbtStream = NULL;
Expand Down
9 changes: 0 additions & 9 deletions xbmc/cores/DllLoader/exports/emu_kernel32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,6 @@ extern "C" DWORD WINAPI dllGetCurrentProcessId(void)
#endif
}

extern "C" BOOL WINAPI dllGetProcessTimes(HANDLE hProcess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime)
{
// since the xbox has only one process, we just take the current thread
HANDLE h = GetCurrentThread();
BOOL res = GetThreadTimes(h, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);

return res;
}

extern "C" int WINAPI dllDuplicateHandle(HANDLE hSourceProcessHandle, // handle to source process
HANDLE hSourceHandle, // handle to duplicate
HANDLE hTargetProcessHandle, // handle to target process
Expand Down
1 change: 0 additions & 1 deletion xbmc/cores/DllLoader/exports/emu_kernel32.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,6 @@ extern "C" BOOL WINAPI dllGetProcessAffinityMask(HANDLE hProcess, LPDWORD lpProc

extern "C" HGLOBAL WINAPI dllLoadResource(HMODULE module, HRSRC res);
extern "C" HRSRC WINAPI dllFindResourceA(HMODULE module, LPCTSTR name, LPCTSTR type);
extern "C" BOOL WINAPI dllGetProcessTimes(HANDLE hProcess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime);
extern "C" int WINAPI dllGetLocaleInfoA(LCID Locale, LCTYPE LCType, LPTSTR lpLCData, int cchData);
extern "C" UINT WINAPI dllGetConsoleCP();
extern "C" UINT WINAPI dllGetConsoleOutputCP();
Expand Down
2 changes: 1 addition & 1 deletion xbmc/cores/DummyVideoPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

CDummyVideoPlayer::CDummyVideoPlayer(IPlayerCallback& callback)
: IPlayer(callback),
CThread()
CThread("CDummyVideoPlayer")
{
m_paused = false;
m_clock = 0;
Expand Down
2 changes: 1 addition & 1 deletion xbmc/cores/dvdplayer/DVDCodecs/Video/CrystalHD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ CPictureBuffer::~CPictureBuffer()
#pragma mark -
#endif
CMPCOutputThread::CMPCOutputThread(void *device, DllLibCrystalHD *dll, bool has_bcm70015) :
CThread(),
CThread("CMPCOutputThread"),
m_dll(dll),
m_device(device),
m_has_bcm70015(has_bcm70015),
Expand Down
26 changes: 11 additions & 15 deletions xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*
*/

#include "threads/SystemClock.h"
#include "system.h"
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
Expand All @@ -45,6 +44,7 @@
#include "filesystem/Directory.h"
#include "utils/log.h"
#include "threads/Thread.h"
#include "threads/SystemClock.h"
#include "utils/TimeUtils.h"

void CDemuxStreamAudioFFmpeg::GetStreamInfo(std::string& strInfo)
Expand Down Expand Up @@ -155,16 +155,12 @@ static void ff_flush_avutil_log_buffers(void)
++it;
}

#ifdef _MSC_VER
static __declspec(thread) CDVDDemuxFFmpeg* g_demuxer = 0;
#else
static TLS g_tls;
#define g_demuxer (*((CDVDDemuxFFmpeg**)g_tls.Get()))
#endif
static XbmcThreads::ThreadLocal<CDVDDemuxFFmpeg> g_demuxer;

static int interrupt_cb(void* unused)
{
if(g_demuxer && g_demuxer->Aborted())
CDVDDemuxFFmpeg* demuxer = g_demuxer.get();
if(demuxer && demuxer->Aborted())
return 1;
return 0;
}
Expand Down Expand Up @@ -236,7 +232,7 @@ bool CDVDDemuxFFmpeg::Open(CDVDInputStream* pInput)
std::string strFile;
m_iCurrentPts = DVD_NOPTS_VALUE;
m_speed = DVD_PLAYSPEED_NORMAL;
g_demuxer = this;
g_demuxer.set(this);
m_program = UINT_MAX;
const AVIOInterruptCB int_cb = { interrupt_cb, NULL };

Expand Down Expand Up @@ -500,7 +496,7 @@ bool CDVDDemuxFFmpeg::Open(CDVDInputStream* pInput)

void CDVDDemuxFFmpeg::Dispose()
{
g_demuxer = this;
g_demuxer.set(this);

if (m_pFormatContext)
{
Expand Down Expand Up @@ -541,7 +537,7 @@ void CDVDDemuxFFmpeg::Reset()

void CDVDDemuxFFmpeg::Flush()
{
g_demuxer = this;
g_demuxer.set(this);

// naughty usage of an internal ffmpeg function
if (m_pFormatContext)
Expand All @@ -557,7 +553,7 @@ void CDVDDemuxFFmpeg::Abort()

void CDVDDemuxFFmpeg::SetSpeed(int iSpeed)
{
g_demuxer = this;
g_demuxer.set(this);

if(!m_pFormatContext)
return;
Expand Down Expand Up @@ -619,7 +615,7 @@ double CDVDDemuxFFmpeg::ConvertTimestamp(int64_t pts, int den, int num)

DemuxPacket* CDVDDemuxFFmpeg::Read()
{
g_demuxer = this;
g_demuxer.set(this);

AVPacket pkt;
DemuxPacket* pPacket = NULL;
Expand Down Expand Up @@ -811,7 +807,7 @@ DemuxPacket* CDVDDemuxFFmpeg::Read()

bool CDVDDemuxFFmpeg::SeekTime(int time, bool backwords, double *startpts)
{
g_demuxer = this;
g_demuxer.set(this);

if(time < 0)
time = 0;
Expand Down Expand Up @@ -871,7 +867,7 @@ bool CDVDDemuxFFmpeg::SeekTime(int time, bool backwords, double *startpts)

bool CDVDDemuxFFmpeg::SeekByte(__int64 pos)
{
g_demuxer = this;
g_demuxer.set(this);

CSingleLock lock(m_critSection);
int ret = m_dllAvFormat.av_seek_frame(m_pFormatContext, -1, pos, AVSEEK_FLAG_BYTE);
Expand Down
13 changes: 4 additions & 9 deletions xbmc/cores/dvdplayer/DVDPerformanceCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "DVDPerformanceCounter.h"
#include "DVDMessageQueue.h"
#include "utils/TimeUtils.h"

#include "dvd_config.h"

Expand Down Expand Up @@ -68,22 +69,16 @@ HRESULT __stdcall DVDPerformanceCounterVideoQueue(PLARGE_INTEGER numerator, PLAR

inline __int64 get_thread_cpu_usage(ProcessPerformance* p)
{
if (p->hThread)
if (p->thread)
{
FILETIME dummy;
FILETIME current_time_thread;
FILETIME current_time_system;
ULARGE_INTEGER old_time_thread;
ULARGE_INTEGER old_time_system;

old_time_thread.QuadPart = p->timer_thread.QuadPart;
old_time_system.QuadPart = p->timer_system.QuadPart;

GetThreadTimes(p->hThread, &dummy, &dummy, &current_time_thread, &dummy);
GetSystemTimeAsFileTime(&current_time_system);

FILETIME_TO_ULARGE_INTEGER(p->timer_thread, current_time_thread);
FILETIME_TO_ULARGE_INTEGER(p->timer_system, current_time_system);
p->timer_thread.QuadPart = p->thread->GetAbsoluteUsage();
p->timer_system.QuadPart = CurrentHostCounter();

__int64 threadTime = (p->timer_thread.QuadPart - old_time_thread.QuadPart);
__int64 systemTime = (p->timer_system.QuadPart - old_time_system.QuadPart);
Expand Down
24 changes: 12 additions & 12 deletions xbmc/cores/dvdplayer/DVDPerformanceCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define FILETIME_TO_ULARGE_INTEGER(ularge, filetime) { ularge.u.HighPart = filetime.dwHighDateTime; ularge.u.LowPart = filetime.dwLowDateTime; }

#include "system.h"

#include "threads/Thread.h"
#include "threads/SingleLock.h"

class CDVDMessageQueue;
Expand All @@ -33,7 +33,7 @@ typedef struct stProcessPerformance
{
ULARGE_INTEGER timer_thread;
ULARGE_INTEGER timer_system;
HANDLE hThread;
CThread* thread;
} ProcessPerformance;

class CDVDPerformanceCounter
Expand All @@ -45,20 +45,20 @@ class CDVDPerformanceCounter
bool Initialize();
void DeInitialize();

void EnableAudioQueue(CDVDMessageQueue* pQueue) { CSingleLock lock(m_critSection); m_pAudioQueue = pQueue; }
void DisableAudioQueue() { CSingleLock lock(m_critSection); m_pAudioQueue = NULL; }
void EnableAudioQueue(CDVDMessageQueue* pQueue) { CSingleLock lock(m_critSection); m_pAudioQueue = pQueue; }
void DisableAudioQueue() { CSingleLock lock(m_critSection); m_pAudioQueue = NULL; }

void EnableVideoQueue(CDVDMessageQueue* pQueue) { CSingleLock lock(m_critSection); m_pVideoQueue = pQueue; }
void DisableVideoQueue() { CSingleLock lock(m_critSection); m_pVideoQueue = NULL; }
void EnableVideoQueue(CDVDMessageQueue* pQueue) { CSingleLock lock(m_critSection); m_pVideoQueue = pQueue; }
void DisableVideoQueue() { CSingleLock lock(m_critSection); m_pVideoQueue = NULL; }

void EnableVideoDecodePerformance(HANDLE hThread) { CSingleLock lock(m_critSection); m_videoDecodePerformance.hThread = hThread; }
void DisableVideoDecodePerformance() { CSingleLock lock(m_critSection); m_videoDecodePerformance.hThread = NULL; }
void EnableVideoDecodePerformance(CThread *thread) { CSingleLock lock(m_critSection); m_videoDecodePerformance.thread = thread; }
void DisableVideoDecodePerformance() { CSingleLock lock(m_critSection); m_videoDecodePerformance.thread = NULL; }

void EnableAudioDecodePerformance(HANDLE hThread) { CSingleLock lock(m_critSection); m_audioDecodePerformance.hThread = hThread; }
void DisableAudioDecodePerformance() { CSingleLock lock(m_critSection); m_audioDecodePerformance.hThread = NULL; }
void EnableAudioDecodePerformance(CThread *thread) { CSingleLock lock(m_critSection); m_audioDecodePerformance.thread = thread; }
void DisableAudioDecodePerformance() { CSingleLock lock(m_critSection); m_audioDecodePerformance.thread = NULL; }

void EnableMainPerformance(HANDLE hThread) { CSingleLock lock(m_critSection); m_mainPerformance.hThread = hThread; }
void DisableMainPerformance() { CSingleLock lock(m_critSection); m_mainPerformance.hThread = NULL; }
void EnableMainPerformance(CThread *thread) { CSingleLock lock(m_critSection); m_mainPerformance.thread = thread; }
void DisableMainPerformance() { CSingleLock lock(m_critSection); m_mainPerformance.thread = NULL; }

CDVDMessageQueue* m_pAudioQueue;
CDVDMessageQueue* m_pVideoQueue;
Expand Down
11 changes: 5 additions & 6 deletions xbmc/cores/dvdplayer/DVDPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ bool CDVDPlayer::OpenFile(const CFileItem& file, const CPlayerOptions &options)

// if playing a file close it first
// this has to be changed so we won't have to close it.
if(ThreadHandle())
if(IsRunning())
CloseFile();

m_bAbortRequest = false;
Expand Down Expand Up @@ -433,8 +433,7 @@ void CDVDPlayer::OnStartup()

m_messenger.Init();

g_dvdPerformanceCounter.EnableMainPerformance(ThreadHandle());

g_dvdPerformanceCounter.EnableMainPerformance(this);
CUtil::ClearTempFonts();
}

Expand Down Expand Up @@ -2726,7 +2725,7 @@ bool CDVDPlayer::OpenAudioStream(int iStream, int source)
m_dvdPlayerAudio.SendMessage(new CDVDMsg(CDVDMsg::PLAYER_STARTED), 1);

/* audio normally won't consume full cpu, so let it have prio */
m_dvdPlayerAudio.SetPriority(GetThreadPriority(*this)+1);
m_dvdPlayerAudio.SetPriority(GetPriority()+1);

return true;
}
Expand Down Expand Up @@ -2794,11 +2793,11 @@ bool CDVDPlayer::OpenVideoStream(int iStream, int source)
// the CoreAudio audio device handler thread. We do the same for
// the DVDPlayerVideo thread so it can run to sleep without getting
// swapped out by a busy OS.
m_dvdPlayerVideo.SetPrioritySched_RR();
m_dvdPlayerVideo.SetPriority(GetSchedRRPriority());
#else
/* use same priority for video thread as demuxing thread, as */
/* otherwise demuxer will starve if video consumes the full cpu */
m_dvdPlayerVideo.SetPriority(GetThreadPriority(*this));
m_dvdPlayerVideo.SetPriority(GetPriority());
#endif
return true;

Expand Down
2 changes: 1 addition & 1 deletion xbmc/cores/dvdplayer/DVDPlayerAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ void CDVDPlayerAudio::OnStartup()
m_decode.msg = NULL;
m_decode.Release();

g_dvdPerformanceCounter.EnableAudioDecodePerformance(ThreadHandle());
g_dvdPerformanceCounter.EnableAudioDecodePerformance(this);

#ifdef _WIN32
CoInitializeEx(NULL, COINIT_MULTITHREADED);
Expand Down
4 changes: 2 additions & 2 deletions xbmc/cores/dvdplayer/DVDPlayerVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool CDVDPlayerVideo::OpenStream( CDVDStreamInfo &hint )
return false;
}

if(g_guiSettings.GetBool("videoplayer.usedisplayasclock") && g_VideoReferenceClock.ThreadHandle() == NULL)
if(g_guiSettings.GetBool("videoplayer.usedisplayasclock") && !g_VideoReferenceClock.IsRunning())
{
g_VideoReferenceClock.Create();
//we have to wait for the clock to start otherwise alsa can cause trouble
Expand Down Expand Up @@ -291,7 +291,7 @@ void CDVDPlayerVideo::OnStartup()
m_iCurrentPts = DVD_NOPTS_VALUE;
m_FlipTimeStamp = m_pClock->GetAbsoluteClock();

g_dvdPerformanceCounter.EnableVideoDecodePerformance(ThreadHandle());
g_dvdPerformanceCounter.EnableVideoDecodePerformance(this);
}

void CDVDPlayerVideo::Process()
Expand Down
4 changes: 2 additions & 2 deletions xbmc/cores/paplayer/PAPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
// Supporting all open audio codec standards.
// First one being nullsoft's nsv audio decoder format

PAPlayer::PAPlayer(IPlayerCallback& callback) : IPlayer(callback)
PAPlayer::PAPlayer(IPlayerCallback& callback) : IPlayer(callback), CThread("PAPlayer")
{
m_bIsPlaying = false;
m_bPaused = false;
Expand Down Expand Up @@ -168,7 +168,7 @@ bool PAPlayer::OpenFile(const CFileItem& file, const CPlayerOptions &options)

*m_currentFile = file;

if (ThreadHandle() == NULL)
if (!IsRunning())
Create();

m_startEvent.Set();
Expand Down
2 changes: 1 addition & 1 deletion xbmc/dialogs/GUIDialogCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "threads/SingleLock.h"
#include "utils/TimeUtils.h"

CGUIDialogCache::CGUIDialogCache(DWORD dwDelay, const CStdString& strHeader, const CStdString& strMsg)
CGUIDialogCache::CGUIDialogCache(DWORD dwDelay, const CStdString& strHeader, const CStdString& strMsg) : CThread("CGUIDialogCache")
{
m_pDlg = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);

Expand Down
4 changes: 2 additions & 2 deletions xbmc/filesystem/FileCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CWriteRate
};


CFileCache::CFileCache()
CFileCache::CFileCache() : CThread("CFileCache")
{
m_bDeleteCache = true;
m_nSeekResult = 0;
Expand All @@ -95,7 +95,7 @@ CFileCache::CFileCache()
m_cacheFull = false;
}

CFileCache::CFileCache(CCacheStrategy *pCache, bool bDeleteCache)
CFileCache::CFileCache(CCacheStrategy *pCache, bool bDeleteCache) : CThread("CFileCache")
{
m_pCache = pCache;
m_bDeleteCache = bDeleteCache;
Expand Down
2 changes: 1 addition & 1 deletion xbmc/filesystem/HTSPDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static SSessions g_sessions;
static CCriticalSection g_section;


CHTSPDirectorySession::CHTSPDirectorySession()
CHTSPDirectorySession::CHTSPDirectorySession() : CThread("CHTSPDirectorySession")
{
}

Expand Down

0 comments on commit 64427d4

Please sign in to comment.