diff options
author | Jim Carroll <thecarrolls@jiminger.com> | 2011-06-11 14:08:01 -0400 |
---|---|---|
committer | Jim Carroll <thecarrolls@jiminger.com> | 2011-06-23 10:15:25 -0400 |
commit | 1574345cb36f10c545062ac230d32b6edb83cbb4 (patch) | |
tree | 93e4da2f3bba2b771bee70d00449aaff54567fe1 | |
parent | 16a725f0c87dc8ad1dc7925e2bc52e7ff0c049a6 (diff) |
Removed redundant means of synchronization and collapsed uses to the remaining pattern using CCriticalSection, CSingleLock, CSingleTryLock, CSingleExit, CSharedSection, and CSharedLock
49 files changed, 385 insertions, 755 deletions
diff --git a/xbmc/BackgroundInfoLoader.cpp b/xbmc/BackgroundInfoLoader.cpp index 48f002bbca..cef3f03f56 100644 --- a/xbmc/BackgroundInfoLoader.cpp +++ b/xbmc/BackgroundInfoLoader.cpp @@ -116,7 +116,7 @@ void CBackgroundInfoLoader::Load(CFileItemList& items) if (items.Size() == 0) return; - EnterCriticalSection(m_lock); + CSingleLock lock(m_lock); for (int nItem=0; nItem < items.Size(); nItem++) m_vecItems.push_back(items[nItem]); @@ -144,7 +144,6 @@ void CBackgroundInfoLoader::Load(CFileItemList& items) m_workers.push_back(pThread); } - LeaveCriticalSection(m_lock); } void CBackgroundInfoLoader::StopAsync() diff --git a/xbmc/cores/DllLoader/dll_tracker.h b/xbmc/cores/DllLoader/dll_tracker.h index 33e9cabd67..76667663aa 100644 --- a/xbmc/cores/DllLoader/dll_tracker.h +++ b/xbmc/cores/DllLoader/dll_tracker.h @@ -23,6 +23,7 @@ */ #include "threads/CriticalSection.h" +#include "PlatformDefs.h" #ifdef _WIN32 #include "system.h" // for SOCKET #endif @@ -65,9 +66,6 @@ typedef std::list<HMODULE>::iterator DllListIter; typedef std::list<uintptr_t> DummyList; typedef std::list<uintptr_t>::iterator DummyListIter; -typedef std::list<LPCRITICAL_SECTION> CriticalSectionList; -typedef std::list<LPCRITICAL_SECTION>::iterator CriticalSectionListIter; - typedef std::list<SOCKET> SocketList; typedef std::list<SOCKET>::iterator SocketListIter; @@ -93,7 +91,6 @@ typedef struct _DllTrackInfo FileList fileList; SocketList socketList; - CriticalSectionList criticalSectionList; HeapObjectList heapobjectList; diff --git a/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp b/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp index aea6f1e47d..41e491942b 100644 --- a/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp +++ b/xbmc/cores/DllLoader/exports/emu_msvcrt.cpp @@ -64,6 +64,7 @@ #include "emu_kernel32.h" #include "util/EmuFileWrapper.h" #include "utils/log.h" +#include "threads/SingleLock.h" #ifndef _LINUX #include "utils/CharsetConverter.h" #include "utils/URIUtils.h" @@ -109,13 +110,12 @@ static char *dll__environ_imp[EMU_MAX_ENVIRONMENT_ITEMS + 1]; extern "C" char **dll__environ; char **dll__environ = dll__environ_imp; -CRITICAL_SECTION dll_cs_environ; +CCriticalSection dll_cs_environ; #define dll_environ (*dll___p__environ()) /* pointer to environment table */ extern "C" void __stdcall init_emu_environ() { - InitializeCriticalSection(&dll_cs_environ); memset(dll__environ, 0, EMU_MAX_ENVIRONMENT_ITEMS + 1); // python @@ -1941,44 +1941,45 @@ extern "C" if (size) value[size - 1] = '\0'; - EnterCriticalSection(&dll_cs_environ); - - char** free_position = NULL; - for (int i = 0; i < EMU_MAX_ENVIRONMENT_ITEMS && free_position == NULL; i++) { - if (dll__environ[i] != NULL) + CSingleLock lock(dll_cs_environ); + + char** free_position = NULL; + for (int i = 0; i < EMU_MAX_ENVIRONMENT_ITEMS && free_position == NULL; i++) { - // we only support overwriting the old values - if (strnicmp(dll__environ[i], var, strlen(var)) == 0) + if (dll__environ[i] != NULL) + { + // we only support overwriting the old values + if (strnicmp(dll__environ[i], var, strlen(var)) == 0) + { + // free it first + free(dll__environ[i]); + dll__environ[i] = NULL; + free_position = &dll__environ[i]; + } + } + else { - // free it first - free(dll__environ[i]); - dll__environ[i] = NULL; free_position = &dll__environ[i]; } } - else - { - free_position = &dll__environ[i]; - } - } - if (free_position != NULL) - { - // free position, copy value - size = strlen(var) + strlen(value) + 2; - *free_position = (char*)malloc(size); // for '=' and 0 termination - if ((*free_position)) + if (free_position != NULL) { - strncpy(*free_position, var, size); - (*free_position)[size - 1] = '\0'; - strncat(*free_position, "=", size - strlen(*free_position)); - strncat(*free_position, value, size - strlen(*free_position)); - added = true; + // free position, copy value + size = strlen(var) + strlen(value) + 2; + *free_position = (char*)malloc(size); // for '=' and 0 termination + if ((*free_position)) + { + strncpy(*free_position, var, size); + (*free_position)[size - 1] = '\0'; + strncat(*free_position, "=", size - strlen(*free_position)); + strncat(*free_position, value, size - strlen(*free_position)); + added = true; + } } - } - LeaveCriticalSection(&dll_cs_environ); + } free(value); } @@ -1993,24 +1994,24 @@ extern "C" { char* value = NULL; - EnterCriticalSection(&dll_cs_environ); + { + CSingleLock lock(dll_cs_environ); - update_emu_environ();//apply any changes + update_emu_environ();//apply any changes - for (int i = 0; i < EMU_MAX_ENVIRONMENT_ITEMS && value == NULL; i++) - { - if (dll__environ[i]) + for (int i = 0; i < EMU_MAX_ENVIRONMENT_ITEMS && value == NULL; i++) { - if (strnicmp(dll__environ[i], szKey, strlen(szKey)) == 0) + if (dll__environ[i]) { - // found it - value = dll__environ[i] + strlen(szKey) + 1; + if (strnicmp(dll__environ[i], szKey, strlen(szKey)) == 0) + { + // found it + value = dll__environ[i] + strlen(szKey) + 1; + } } } } - LeaveCriticalSection(&dll_cs_environ); - if (value != NULL) { return value; diff --git a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp index 486808c1d0..d85aed219e 100644 --- a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp +++ b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.cpp @@ -22,13 +22,12 @@ #include "EmuFileWrapper.h" #include "filesystem/File.h" #include "threads/Mutex.h" +#include "threads/SingleLock.h" CEmuFileWrapper g_emuFileWrapper; CEmuFileWrapper::CEmuFileWrapper() { - InitializeCriticalSection(&m_criticalSection); - // since we always use dlls we might just initialize it directly for (int i = 0; i < MAX_EMULATED_FILES; i++) { @@ -40,12 +39,11 @@ CEmuFileWrapper::CEmuFileWrapper() CEmuFileWrapper::~CEmuFileWrapper() { - DeleteCriticalSection(&m_criticalSection); } void CEmuFileWrapper::CleanUp() { - EnterCriticalSection(&m_criticalSection); + CSingleLock lock(m_criticalSection); for (int i = 0; i < MAX_EMULATED_FILES; i++) { if (m_files[i].used) @@ -63,14 +61,13 @@ void CEmuFileWrapper::CleanUp() m_files[i].file_emu._file = -1; } } - LeaveCriticalSection(&m_criticalSection); } EmuFileObject* CEmuFileWrapper::RegisterFileObject(XFILE::CFile* pFile) { EmuFileObject* object = NULL; - EnterCriticalSection(&m_criticalSection); + CSingleLock lock(m_criticalSection); for (int i = 0; i < MAX_EMULATED_FILES; i++) { @@ -86,8 +83,6 @@ EmuFileObject* CEmuFileWrapper::RegisterFileObject(XFILE::CFile* pFile) } } - LeaveCriticalSection(&m_criticalSection); - return object; } @@ -98,7 +93,7 @@ void CEmuFileWrapper::UnRegisterFileObjectByDescriptor(int fd) { if (m_files[i].used) { - EnterCriticalSection(&m_criticalSection); + CSingleLock lock(m_criticalSection); // we assume the emulated function alreay deleted the CFile object if (m_files[i].used) @@ -112,8 +107,6 @@ void CEmuFileWrapper::UnRegisterFileObjectByDescriptor(int fd) m_files[i].used = false; m_files[i].file_emu._file = -1; } - - LeaveCriticalSection(&m_criticalSection); } } } diff --git a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h index c68a43e704..496b870a18 100644 --- a/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h +++ b/xbmc/cores/DllLoader/exports/util/EmuFileWrapper.h @@ -26,6 +26,7 @@ #include <stdio.h> #include "system.h" +#include "threads/CriticalSection.h" class CMutex; @@ -77,7 +78,7 @@ public: bool StreamIsEmulatedFile(FILE* stream); private: EmuFileObject m_files[MAX_EMULATED_FILES]; - CRITICAL_SECTION m_criticalSection; + CCriticalSection m_criticalSection; bool m_initialized; }; diff --git a/xbmc/cores/VideoRenderers/RenderManager.cpp b/xbmc/cores/VideoRenderers/RenderManager.cpp index 8e8486be00..b5c286d2ea 100644 --- a/xbmc/cores/VideoRenderers/RenderManager.cpp +++ b/xbmc/cores/VideoRenderers/RenderManager.cpp @@ -64,18 +64,18 @@ public: CRetakeLock(CSharedSection §ion, bool immidiate = true, CCriticalSection &owned = g_graphicsContext) : m_owned(owned) { - m_count = ExitCriticalSection(m_owned); + m_count = m_owned.exit(); m_lock = new T(section); if(immidiate) { - RestoreCriticalSection(m_owned, m_count); + m_owned.restore(m_count); m_count = 0; } } ~CRetakeLock() { delete m_lock; - RestoreCriticalSection(m_owned, m_count); + m_owned.restore(m_count); } void Leave() { m_lock->Leave(); } void Enter() { m_lock->Enter(); } diff --git a/xbmc/cores/dvdplayer/DVDAudio.h b/xbmc/cores/dvdplayer/DVDAudio.h index 44a20dc028..101d8f72c9 100644 --- a/xbmc/cores/dvdplayer/DVDAudio.h +++ b/xbmc/cores/dvdplayer/DVDAudio.h @@ -27,6 +27,7 @@ #include "cores/AudioRenderers/IAudioRenderer.h" #include "cores/IAudioCallback.h" #include "threads/CriticalSection.h" +#include "PlatformDefs.h" #ifndef _LINUX enum CodecID; diff --git a/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlay.h b/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlay.h index b9aa43f521..b403062d93 100644 --- a/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlay.h +++ b/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlay.h @@ -22,6 +22,7 @@ */ #include "cores/VideoRenderers/OverlayRenderer.h" +#include "threads/Atomics.h" #include <assert.h> #include <vector> @@ -78,7 +79,7 @@ public: */ CDVDOverlay* Acquire() { - InterlockedIncrement(&m_references); + AtomicIncrement(&m_references); return this; } @@ -87,7 +88,7 @@ public: */ long Release() { - long count = InterlockedDecrement(&m_references); + long count = AtomicDecrement(&m_references); if (count == 0) delete this; return count; } diff --git a/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayCodec.h b/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayCodec.h index f0357be45c..4fd55f3e6a 100644 --- a/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayCodec.h +++ b/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayCodec.h @@ -22,6 +22,7 @@ */ #include "DVDOverlay.h" +#include "PlatformDefs.h" #include <string> diff --git a/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayText.h b/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayText.h index 996c3b0540..1bf01c5ebc 100644 --- a/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayText.h +++ b/xbmc/cores/dvdplayer/DVDCodecs/Overlay/DVDOverlayText.h @@ -25,6 +25,7 @@ #ifdef _LINUX #include "utils/CharsetConverter.h" #endif +#include <string.h> class CDVDOverlayText : public CDVDOverlay { diff --git a/xbmc/cores/dvdplayer/DVDCodecs/Video/CrystalHD.h b/xbmc/cores/dvdplayer/DVDCodecs/Video/CrystalHD.h index a1af27a820..b30f269b59 100644 --- a/xbmc/cores/dvdplayer/DVDCodecs/Video/CrystalHD.h +++ b/xbmc/cores/dvdplayer/DVDCodecs/Video/CrystalHD.h @@ -26,42 +26,36 @@ #include "DVDVideoCodec.h" #include "cores/dvdplayer/DVDStreamInfo.h" +#include "threads/SingleLock.h" //////////////////////////////////////////////////////////////////////////////////////////// template <class T> class CSyncPtrQueue { public: - CSyncPtrQueue() - { - InitializeCriticalSection(&m_Lock); - } - virtual ~CSyncPtrQueue() - { - DeleteCriticalSection(&m_Lock); - } + CSyncPtrQueue() { } + virtual ~CSyncPtrQueue() { } void Push(T* p) { - EnterCriticalSection(&m_Lock); + CSingleLock lock(m_Lock); m_Queue.push_back(p); - LeaveCriticalSection(&m_Lock); } + T* Pop() { T* p = NULL; - EnterCriticalSection(&m_Lock); + CSingleLock lock(m_Lock); if (m_Queue.size()) { p = m_Queue.front(); m_Queue.pop_front(); } - LeaveCriticalSection(&m_Lock); return p; } unsigned int Count(){return m_Queue.size();} protected: std::deque<T*> m_Queue; - CRITICAL_SECTION m_Lock; + CCriticalSection m_Lock; }; //////////////////////////////////////////////////////////////////////////////////////////// diff --git a/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.cpp b/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.cpp index c8699aa491..660afdee50 100644 --- a/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.cpp +++ b/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.cpp @@ -208,7 +208,6 @@ CDVDDemuxFFmpeg::CDVDDemuxFFmpeg() : CDVDDemux() m_pFormatContext = NULL; m_pInput = NULL; m_ioContext = NULL; - InitializeCriticalSection(&m_critSection); for (int i = 0; i < MAX_STREAMS; i++) m_streams[i] = NULL; m_iCurrentPts = DVD_NOPTS_VALUE; } @@ -216,7 +215,6 @@ CDVDDemuxFFmpeg::CDVDDemuxFFmpeg() : CDVDDemux() CDVDDemuxFFmpeg::~CDVDDemuxFFmpeg() { Dispose(); - DeleteCriticalSection(&m_critSection); ff_flush_avutil_log_buffers(); } @@ -637,130 +635,131 @@ DemuxPacket* CDVDDemuxFFmpeg::Read() // on some cases where the received packet is invalid we will need to return an empty packet (0 length) otherwise the main loop (in CDVDPlayer) // would consider this the end of stream and stop. bool bReturnEmpty = false; - Lock(); - if (m_pFormatContext) { - // assume we are not eof - if(m_pFormatContext->pb) - m_pFormatContext->pb->eof_reached = 0; - - // keep track if ffmpeg doesn't always set these - pkt.size = 0; - pkt.data = NULL; - pkt.stream_index = MAX_STREAMS; - - // timeout reads after 100ms - m_timeout = CTimeUtils::GetTimeMS() + 20000; - int result = 0; - try - { - result = m_dllAvFormat.av_read_frame(m_pFormatContext, &pkt); - } - catch(const win32_exception &e) + CSingleLock lock(m_critSection); + if (m_pFormatContext) { - e.writelog(__FUNCTION__); - result = AVERROR(EFAULT); - } - m_timeout = 0; + // assume we are not eof + if(m_pFormatContext->pb) + m_pFormatContext->pb->eof_reached = 0; + + // keep track if ffmpeg doesn't always set these + pkt.size = 0; + pkt.data = NULL; + pkt.stream_index = MAX_STREAMS; + + // timeout reads after 100ms + m_timeout = CTimeUtils::GetTimeMS() + 20000; + int result = 0; + try + { + result = m_dllAvFormat.av_read_frame(m_pFormatContext, &pkt); + } + catch(const win32_exception &e) + { + e.writelog(__FUNCTION__); + result = AVERROR(EFAULT); + } + m_timeout = 0; - if (result == AVERROR(EINTR) || result == AVERROR(EAGAIN)) - { - // timeout, probably no real error, return empty packet - bReturnEmpty = true; - } - else if (result < 0) - { - Flush(); - } - else if (pkt.size < 0 || pkt.stream_index >= MAX_STREAMS) - { - // XXX, in some cases ffmpeg returns a negative packet size - if(m_pFormatContext->pb && !m_pFormatContext->pb->eof_reached) + if (result == AVERROR(EINTR) || result == AVERROR(EAGAIN)) { - CLog::Log(LOGERROR, "CDVDDemuxFFmpeg::Read() no valid packet"); + // timeout, probably no real error, return empty packet bReturnEmpty = true; + } + else if (result < 0) + { Flush(); } - else - CLog::Log(LOGERROR, "CDVDDemuxFFmpeg::Read() returned invalid packet and eof reached"); - - m_dllAvCodec.av_free_packet(&pkt); - } - else - { - AVStream *stream = m_pFormatContext->streams[pkt.stream_index]; - - if (m_program != UINT_MAX) + else if (pkt.size < 0 || pkt.stream_index >= MAX_STREAMS) { - /* check so packet belongs to selected program */ - for (unsigned int i = 0; i < m_pFormatContext->programs[m_program]->nb_stream_indexes; i++) + // XXX, in some cases ffmpeg returns a negative packet size + if(m_pFormatContext->pb && !m_pFormatContext->pb->eof_reached) { - if(pkt.stream_index == (int)m_pFormatContext->programs[m_program]->stream_index[i]) - { - pPacket = CDVDDemuxUtils::AllocateDemuxPacket(pkt.size); - break; - } + CLog::Log(LOGERROR, "CDVDDemuxFFmpeg::Read() no valid packet"); + bReturnEmpty = true; + Flush(); } + else + CLog::Log(LOGERROR, "CDVDDemuxFFmpeg::Read() returned invalid packet and eof reached"); - if (!pPacket) - bReturnEmpty = true; + m_dllAvCodec.av_free_packet(&pkt); } else - pPacket = CDVDDemuxUtils::AllocateDemuxPacket(pkt.size); - - if (pPacket) { - // lavf sometimes bugs out and gives 0 dts/pts instead of no dts/pts - // since this could only happens on initial frame under normal - // circomstances, let's assume it is wrong all the time - if(pkt.dts == 0) - pkt.dts = AV_NOPTS_VALUE; - if(pkt.pts == 0) - pkt.pts = AV_NOPTS_VALUE; - - if(m_bMatroska && stream->codec && stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) - { // matroska can store different timestamps - // for different formats, for native stored - // stuff it is pts, but for ms compatibility - // tracks, it is really dts. sadly ffmpeg - // sets these two timestamps equal all the - // time, so we select it here instead - if(stream->codec->codec_tag == 0) + AVStream *stream = m_pFormatContext->streams[pkt.stream_index]; + + if (m_program != UINT_MAX) + { + /* check so packet belongs to selected program */ + for (unsigned int i = 0; i < m_pFormatContext->programs[m_program]->nb_stream_indexes; i++) + { + if(pkt.stream_index == (int)m_pFormatContext->programs[m_program]->stream_index[i]) + { + pPacket = CDVDDemuxUtils::AllocateDemuxPacket(pkt.size); + break; + } + } + + if (!pPacket) + bReturnEmpty = true; + } + else + pPacket = CDVDDemuxUtils::AllocateDemuxPacket(pkt.size); + + if (pPacket) + { + // lavf sometimes bugs out and gives 0 dts/pts instead of no dts/pts + // since this could only happens on initial frame under normal + // circomstances, let's assume it is wrong all the time + if(pkt.dts == 0) pkt.dts = AV_NOPTS_VALUE; - else + if(pkt.pts == 0) pkt.pts = AV_NOPTS_VALUE; - } - // we need to get duration slightly different for matroska embedded text subtitels - if(m_bMatroska && stream->codec->codec_id == CODEC_ID_TEXT && pkt.convergence_duration != 0) + if(m_bMatroska && stream->codec && stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) + { // matroska can store different timestamps + // for different formats, for native stored + // stuff it is pts, but for ms compatibility + // tracks, it is really dts. sadly ffmpeg + // sets these two timestamps equal all the + // time, so we select it here instead + if(stream->codec->codec_tag == 0) + pkt.dts = AV_NOPTS_VALUE; + else + pkt.pts = AV_NOPTS_VALUE; + } + + // we need to get duration slightly different for matroska embedded text subtitels + if(m_bMatroska && stream->codec->codec_id == CODEC_ID_TEXT && pkt.convergence_duration != 0) pkt.duration = pkt.convergence_duration; - if(m_bAVI && stream->codec && stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) - { - // AVI's always have borked pts, specially if m_pFormatContext->flags includes - // AVFMT_FLAG_GENPTS so always use dts - pkt.pts = AV_NOPTS_VALUE; - } + if(m_bAVI && stream->codec && stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) + { + // AVI's always have borked pts, specially if m_pFormatContext->flags includes + // AVFMT_FLAG_GENPTS so always use dts + pkt.pts = AV_NOPTS_VALUE; + } - // copy contents into our own packet - pPacket->iSize = pkt.size; + // copy contents into our own packet + pPacket->iSize = pkt.size; - // maybe we can avoid a memcpy here by detecting where pkt.destruct is pointing too? - if (pkt.data) - memcpy(pPacket->pData, pkt.data, pPacket->iSize); + // maybe we can avoid a memcpy here by detecting where pkt.destruct is pointing too? + if (pkt.data) + memcpy(pPacket->pData, pkt.data, pPacket->iSize); - pPacket->pts = ConvertTimestamp(pkt.pts, stream->time_base.den, stream->time_base.num); - pPacket->dts = ConvertTimestamp(pkt.dts, stream->time_base.den, stream->time_base.num); - pPacket->duration = DVD_SEC_TO_TIME((double)pkt.duration * stream->time_base.num / stream->time_base.den); + pPacket->pts = ConvertTimestamp(pkt.pts, stream->time_base.den, stream->time_base.num); + pPacket->dts = ConvertTimestamp(pkt.dts, stream->time_base.den, stream->time_base.num); + pPacket->duration = DVD_SEC_TO_TIME((double)pkt.duration * stream->time_base.num / stream->time_base.den); - // used to guess streamlength - if (pPacket->dts != DVD_NOPTS_VALUE && (pPacket->dts > m_iCurrentPts || m_iCurrentPts == DVD_NOPTS_VALUE)) - m_iCurrentPts = pPacket->dts; + // used to guess streamlength + if (pPacket->dts != DVD_NOPTS_VALUE && (pPacket->dts > m_iCurrentPts || m_iCurrentPts == DVD_NOPTS_VALUE)) + m_iCurrentPts = pPacket->dts; - // check if stream has passed full duration, needed for live streams - if(pkt.dts != (int64_t)AV_NOPTS_VALUE) - { + // check if stream has passed full duration, needed for live streams + if(pkt.dts != (int64_t)AV_NOPTS_VALUE) + { int64_t duration; duration = pkt.dts; if(stream->start_time != (int64_t)AV_NOPTS_VALUE) @@ -771,25 +770,24 @@ DemuxPacket* CDVDDemuxFFmpeg::Read() stream->duration = duration; duration = m_dllAvUtil.av_rescale_rnd(stream->duration, stream->time_base.num * AV_TIME_BASE, stream->time_base.den, AV_ROUND_NEAR_INF); if ((m_pFormatContext->duration == (int64_t)AV_NOPTS_VALUE && m_pFormatContext->file_size > 0) - || (m_pFormatContext->duration != (int64_t)AV_NOPTS_VALUE && duration > m_pFormatContext->duration)) + || (m_pFormatContext->duration != (int64_t)AV_NOPTS_VALUE && duration > m_pFormatContext->duration)) m_pFormatContext->duration = duration; } - } + } - // check if stream seem to have grown since start - if(m_pFormatContext->file_size > 0 && m_pFormatContext->pb) - { - if(m_pFormatContext->pb->pos > m_pFormatContext->file_size) - m_pFormatContext->file_size = m_pFormatContext->pb->pos; - } + // check if stream seem to have grown since start + if(m_pFormatContext->file_size > 0 && m_pFormatContext->pb) + { + if(m_pFormatContext->pb->pos > m_pFormatContext->file_size) + m_pFormatContext->file_size = m_pFormatContext->pb->pos; + } - pPacket->iStreamId = pkt.stream_index; // XXX just for now + pPacket->iStreamId = pkt.stream_index; // XXX just for now + } + m_dllAvCodec.av_free_packet(&pkt); } - m_dllAvCodec.av_free_packet(&pkt); } } - Unlock(); - if (bReturnEmpty && !pPacket) pPacket = CDVDDemuxUtils::AllocateDemuxPacket(0); @@ -863,12 +861,14 @@ bool CDVDDemuxFFmpeg::SeekTime(int time, bool backwords, double *startpts) if (m_pFormatContext->start_time != (int64_t)AV_NOPTS_VALUE) seek_pts += m_pFormatContext->start_time; - Lock(); - int ret = m_dllAvFormat.av_seek_frame(m_pFormatContext, -1, seek_pts, backwords ? AVSEEK_FLAG_BACKWARD : 0); + int ret; + { + CSingleLock lock(m_critSection); + ret = m_dllAvFormat.av_seek_frame(m_pFormatContext, -1, seek_pts, backwords ? AVSEEK_FLAG_BACKWARD : 0); - if(ret >= 0) - UpdateCurrentPTS(); - Unlock(); + if(ret >= 0) + UpdateCurrentPTS(); + } if(m_iCurrentPts == DVD_NOPTS_VALUE) CLog::Log(LOGDEBUG, "%s - unknown position after seek", __FUNCTION__); @@ -890,13 +890,12 @@ bool CDVDDemuxFFmpeg::SeekByte(__int64 pos) { g_demuxer = this; - Lock(); + CSingleLock lock(m_critSection); int ret = m_dllAvFormat.av_seek_frame(m_pFormatContext, -1, pos, AVSEEK_FLAG_BYTE); if(ret >= 0) UpdateCurrentPTS(); - Unlock(); return (ret >= 0); } diff --git a/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.h b/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.h index c68b357d67..b757a26020 100644 --- a/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.h +++ b/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxFFmpeg.h @@ -26,6 +26,8 @@ #include "DllAvCodec.h" #include "DllAvUtil.h" +#include "threads/CriticalSection.h" + class CDVDDemuxFFmpeg; class CDemuxStreamVideoFFmpeg @@ -117,13 +119,13 @@ protected: int ReadFrame(AVPacket *packet); void AddStream(int iId); - void Lock() { EnterCriticalSection(&m_critSection); } - void Unlock() { LeaveCriticalSection(&m_critSection); } + // void Lock() { EnterCriticalSection(&m_critSection); } + // void Unlock() { LeaveCriticalSection(&m_critSection); } double ConvertTimestamp(int64_t pts, int den, int num); void UpdateCurrentPTS(); - CRITICAL_SECTION m_critSection; + CCriticalSection m_critSection; #define MAX_STREAMS 100 CDemuxStream* m_streams[MAX_STREAMS]; // maximum number of streams that ffmpeg can handle diff --git a/xbmc/cores/dvdplayer/DVDInputStreams/DVDInputStreamNavigator.cpp b/xbmc/cores/dvdplayer/DVDInputStreams/DVDInputStreamNavigator.cpp index 565c346d62..35d0c95a26 100644 --- a/xbmc/cores/dvdplayer/DVDInputStreams/DVDInputStreamNavigator.cpp +++ b/xbmc/cores/dvdplayer/DVDInputStreams/DVDInputStreamNavigator.cpp @@ -279,13 +279,15 @@ int CDVDInputStreamNavigator::ProcessBlock(BYTE* dest_buffer, int* read) result = m_dll.dvdnav_get_next_cache_block(m_dvdnav, &buf, &m_lastevent, &len); } + // this appears to be impossible as nothing in the try ever throws + // TODO: validate the above catch (...) { CLog::Log(LOGERROR, "CDVDInputStreamNavigator::ProcessBlock - exception thrown in dvdnav_get_next_cache_block."); // okey, we are probably holding a vm_lock here so leave it.. this could potentialy cause problems if we aren't holding it // but it's more likely that we do - LeaveCriticalSection((LPCRITICAL_SECTION)&(m_dvdnav->vm_lock)); +// LeaveCriticalSection((LPCRITICAL_SECTION)&(m_dvdnav->vm_lock)); m_bEOF = true; return NAVRESULT_ERROR; } diff --git a/xbmc/cores/dvdplayer/DVDMessageTracker.cpp b/xbmc/cores/dvdplayer/DVDMessageTracker.cpp index 3d06e3785f..0f03b38f08 100644 --- a/xbmc/cores/dvdplayer/DVDMessageTracker.cpp +++ b/xbmc/cores/dvdplayer/DVDMessageTracker.cpp @@ -21,6 +21,7 @@ #include "DVDMessageTracker.h" #include "DVDMessage.h" +#include "threads/SingleLock.h" #ifdef DVDDEBUG_MESSAGE_TRACKER @@ -56,15 +57,14 @@ void CDVDMessageTracker::Register(CDVDMsg* pMsg) { if (m_bInitialized) { - EnterCriticalSection(&m_critSection); + CSingleLock lock(m_critSection); m_messageList.push_back(new CDVDMessageTrackerItem(pMsg)); - LeaveCriticalSection(&m_critSection); } } void CDVDMessageTracker::UnRegister(CDVDMsg* pMsg) { - EnterCriticalSection(&m_critSection); + CSingleLock lock(m_critSection); std::list<CDVDMessageTrackerItem*>::iterator iter = m_messageList.begin(); while (iter != m_messageList.end()) @@ -79,7 +79,6 @@ void CDVDMessageTracker::UnRegister(CDVDMsg* pMsg) iter++; } - LeaveCriticalSection(&m_critSection); } void CDVDMessageTracker::OnStartup() @@ -93,7 +92,7 @@ void CDVDMessageTracker::Process() { Sleep(1000); - EnterCriticalSection(&m_critSection); + CSingleLock lock(m_critSection); std::list<CDVDMessageTrackerItem*>::iterator iter = m_messageList.begin(); while (!m_bStop && iter != m_messageList.end()) @@ -112,7 +111,6 @@ void CDVDMessageTracker::Process() iter++; } - LeaveCriticalSection(&m_critSection); } } diff --git a/xbmc/cores/dvdplayer/DVDMessageTracker.h b/xbmc/cores/dvdplayer/DVDMessageTracker.h index 52fc31758e..080237b830 100644 --- a/xbmc/cores/dvdplayer/DVDMessageTracker.h +++ b/xbmc/cores/dvdplayer/DVDMessageTracker.h @@ -26,6 +26,7 @@ #ifdef DVDDEBUG_MESSAGE_TRACKER #include "utils/thread.h" +#include "threads/CriticalSection.h" class CDVDMsg; @@ -65,7 +66,7 @@ protected: private: bool m_bInitialized; std::list<CDVDMessageTrackerItem*> m_messageList; - CRITICAL_SECTION m_critSection; + CCriticalSection m_critSection; }; extern CDVDMessageTracker g_dvdMessageTracker; diff --git a/xbmc/cores/dvdplayer/DVDOverlayContainer.cpp b/xbmc/cores/dvdplayer/DVDOverlayContainer.cpp index f6340cddb4..ac093fbf8a 100644 --- a/xbmc/cores/dvdplayer/DVDOverlayContainer.cpp +++ b/xbmc/cores/dvdplayer/DVDOverlayContainer.cpp @@ -21,26 +21,23 @@ #include "DVDOverlayContainer.h" #include "DVDInputStreams/DVDInputStreamNavigator.h" - +#include "threads/SingleLock.h" CDVDOverlayContainer::CDVDOverlayContainer() { m_overlays.clear(); - InitializeCriticalSection(&m_critSection); } CDVDOverlayContainer::~CDVDOverlayContainer() { Clear(); - - DeleteCriticalSection(&m_critSection); } void CDVDOverlayContainer::Add(CDVDOverlay* pOverlay) { pOverlay->Acquire(); - EnterCriticalSection(&m_critSection); + CSingleLock lock(*this); // markup any non ending overlays, to finish // when this new one starts, there can be @@ -63,7 +60,6 @@ void CDVDOverlayContainer::Add(CDVDOverlay* pOverlay) m_overlays.push_back(pOverlay); - LeaveCriticalSection(&m_critSection); } VecOverlays* CDVDOverlayContainer::GetOverlays() @@ -76,9 +72,10 @@ VecOverlaysIter CDVDOverlayContainer::Remove(VecOverlaysIter itOverlay) VecOverlaysIter itNext; CDVDOverlay* pOverlay = *itOverlay; - EnterCriticalSection(&m_critSection); - itNext = m_overlays.erase(itOverlay); - LeaveCriticalSection(&m_critSection); + { + CSingleLock lock(*this); + itNext = m_overlays.erase(itOverlay); + } pOverlay->Release(); @@ -89,7 +86,7 @@ void CDVDOverlayContainer::CleanUp(double pts) { CDVDOverlay* pOverlay = NULL; - EnterCriticalSection(&m_critSection); + CSingleLock lock(*this); VecOverlaysIter it = m_overlays.begin(); while (it != m_overlays.end()) @@ -128,7 +125,6 @@ void CDVDOverlayContainer::CleanUp(double pts) it++; } - LeaveCriticalSection(&m_critSection); } void CDVDOverlayContainer::Remove() @@ -137,13 +133,12 @@ void CDVDOverlayContainer::Remove() { CDVDOverlay* pOverlay; - EnterCriticalSection(&m_critSection); - - pOverlay = m_overlays.front(); - m_overlays.erase(m_overlays.begin()); - - LeaveCriticalSection(&m_critSection); + { + CSingleLock lock(*this); + pOverlay = m_overlays.front(); + m_overlays.erase(m_overlays.begin()); + } pOverlay->Release(); } } @@ -162,7 +157,7 @@ bool CDVDOverlayContainer::ContainsOverlayType(DVDOverlayType type) { bool result = false; - EnterCriticalSection(&m_critSection); + CSingleLock lock(*this); VecOverlaysIter it = m_overlays.begin(); while (!result && it != m_overlays.end()) @@ -171,8 +166,6 @@ bool CDVDOverlayContainer::ContainsOverlayType(DVDOverlayType type) it++; } - LeaveCriticalSection(&m_critSection); - return result; } @@ -181,7 +174,7 @@ bool CDVDOverlayContainer::ContainsOverlayType(DVDOverlayType type) */ void CDVDOverlayContainer::UpdateOverlayInfo(CDVDInputStreamNavigator* pStream, CDVDDemuxSPU *pSpu, int iAction) { - EnterCriticalSection(&m_critSection); + CSingleLock lock(*this); //Update any forced overlays. for(VecOverlays::iterator it = m_overlays.begin(); it != m_overlays.end(); it++ ) @@ -211,5 +204,4 @@ void CDVDOverlayContainer::UpdateOverlayInfo(CDVDInputStreamNavigator* pStream, } } } - LeaveCriticalSection(&m_critSection); } diff --git a/xbmc/cores/dvdplayer/DVDOverlayContainer.h b/xbmc/cores/dvdplayer/DVDOverlayContainer.h index 8860632ca2..d4def6dd86 100644 --- a/xbmc/cores/dvdplayer/DVDOverlayContainer.h +++ b/xbmc/cores/dvdplayer/DVDOverlayContainer.h @@ -22,19 +22,17 @@ */ #include "DVDCodecs/Overlay/DVDOverlay.h" +#include "threads/CriticalSection.h" class CDVDInputStreamNavigator; class CDVDDemuxSPU; -class CDVDOverlayContainer +class CDVDOverlayContainer : public CCriticalSection { public: CDVDOverlayContainer(); virtual ~CDVDOverlayContainer(); - void Lock() { EnterCriticalSection(&m_critSection); } - void Unlock() { LeaveCriticalSection(&m_critSection); } - void Add(CDVDOverlay* pPicture); // add a overlay to the fifo VecOverlays* GetOverlays(); // get the first overlay in this fifo @@ -51,6 +49,4 @@ private: VecOverlaysIter Remove(VecOverlaysIter itOverlay); // removes a specific overlay VecOverlays m_overlays; - - CRITICAL_SECTION m_critSection; }; diff --git a/xbmc/cores/dvdplayer/DVDPerformanceCounter.cpp b/xbmc/cores/dvdplayer/DVDPerformanceCounter.cpp index 0d624f68bb..23c5e3eecd 100644 --- a/xbmc/cores/dvdplayer/DVDPerformanceCounter.cpp +++ b/xbmc/cores/dvdplayer/DVDPerformanceCounter.cpp @@ -128,20 +128,17 @@ CDVDPerformanceCounter::CDVDPerformanceCounter() memset(&m_audioDecodePerformance, 0, sizeof(m_audioDecodePerformance)); // audio decoding + output to audio device memset(&m_mainPerformance, 0, sizeof(m_mainPerformance)); // reading files, demuxing, decoding of subtitles + menu overlays - InitializeCriticalSection(&m_critSection); - Initialize(); } CDVDPerformanceCounter::~CDVDPerformanceCounter() { DeInitialize(); - DeleteCriticalSection(&m_critSection); } bool CDVDPerformanceCounter::Initialize() { - Lock(); + CSingleLock lock(m_critSection); #ifdef DVDDEBUG_WITH_PERFORMANCE_COUNTER @@ -153,15 +150,11 @@ bool CDVDPerformanceCounter::Initialize() #endif - Unlock(); - return true; } void CDVDPerformanceCounter::DeInitialize() { - Lock(); - Unlock(); } diff --git a/xbmc/cores/dvdplayer/DVDPerformanceCounter.h b/xbmc/cores/dvdplayer/DVDPerformanceCounter.h index 302f7e463f..2f0b5acf4e 100644 --- a/xbmc/cores/dvdplayer/DVDPerformanceCounter.h +++ b/xbmc/cores/dvdplayer/DVDPerformanceCounter.h @@ -25,6 +25,8 @@ #include "system.h" +#include "threads/SingleLock.h" + class CDVDMessageQueue; typedef struct stProcessPerformance @@ -43,23 +45,20 @@ public: bool Initialize(); void DeInitialize(); - void Lock() { EnterCriticalSection(&m_critSection); } - void Unlock() { LeaveCriticalSection(&m_critSection); } - - void EnableAudioQueue(CDVDMessageQueue* pQueue) { Lock(); m_pAudioQueue = pQueue; Unlock(); } - void DisableAudioQueue() { Lock(); m_pAudioQueue = NULL; Unlock(); } + void EnableAudioQueue(CDVDMessageQueue* pQueue) { CSingleLock lock(m_critSection); m_pAudioQueue = pQueue; } + void DisableAudioQueue() { CSingleLock lock(m_critSection); m_pAudioQueue = NULL; } - void EnableVideoQueue(CDVDMessageQueue* pQueue) { Lock(); m_pVideoQueue = pQueue; Unlock(); } - void DisableVideoQueue() { Lock(); m_pVideoQueue = NULL; Unlock(); } + void EnableVideoQueue(CDVDMessageQueue* pQueue) { CSingleLock lock(m_critSection); m_pVideoQueue = pQueue; } + void DisableVideoQueue() { CSingleLock lock(m_critSection); m_pVideoQueue = NULL; } - void EnableVideoDecodePerformance(HANDLE hThread) { Lock(); m_videoDecodePerformance.hThread = hThread; Unlock(); } - void DisableVideoDecodePerformance() { Lock(); m_videoDecodePerformance.hThread = NULL; Unlock(); } + void EnableVideoDecodePerformance(HANDLE hThread) { CSingleLock lock(m_critSection); m_videoDecodePerformance.hThread = hThread; } + void DisableVideoDecodePerformance() { CSingleLock lock(m_critSection); m_videoDecodePerformance.hThread = NULL; } - void EnableAudioDecodePerformance(HANDLE hThread) { Lock(); m_audioDecodePerformance.hThread = hThread; Unlock(); } - void DisableAudioDecodePerformance() { Lock(); m_audioDecodePerformance.hThread = NULL; Unlock(); } + void EnableAudioDecodePerformance(HANDLE hThread) { CSingleLock lock(m_critSection); m_audioDecodePerformance.hThread = hThread; } + void DisableAudioDecodePerformance() { CSingleLock lock(m_critSection); m_audioDecodePerformance.hThread = NULL; } - void EnableMainPerformance(HANDLE hThread) { Lock(); m_mainPerformance.hThread = hThread; Unlock(); } - void DisableMainPerformance() { Lock(); m_mainPerformance.hThread = NULL; Unlock(); } + void EnableMainPerformance(HANDLE hThread) { CSingleLock lock(m_critSection); m_mainPerformance.hThread = hThread; } + void DisableMainPerformance() { CSingleLock lock(m_critSection); m_mainPerformance.hThread = NULL; } CDVDMessageQueue* m_pAudioQueue; CDVDMessageQueue* m_pVideoQueue; @@ -69,7 +68,7 @@ public: ProcessPerformance m_mainPerformance; private: - CRITICAL_SECTION m_critSection; + CCriticalSection m_critSection; }; extern CDVDPerformanceCounter g_dvdPerformanceCounter; diff --git a/xbmc/cores/dvdplayer/DVDPlayer.cpp b/xbmc/cores/dvdplayer/DVDPlayer.cpp index 85e89e3df1..e6a55994e2 100644 --- a/xbmc/cores/dvdplayer/DVDPlayer.cpp +++ b/xbmc/cores/dvdplayer/DVDPlayer.cpp @@ -296,8 +296,6 @@ CDVDPlayer::CDVDPlayer(IPlayerCallback& callback) m_pSubtitleDemuxer = NULL; m_pInputStream = NULL; - InitializeCriticalSection(&m_critStreamSection); - m_dvd.Clear(); m_State.Clear(); m_UpdateApplication = 0; @@ -316,7 +314,6 @@ CDVDPlayer::~CDVDPlayer() { CloseFile(); - DeleteCriticalSection(&m_critStreamSection); #ifdef DVDDEBUG_MESSAGE_TRACKER g_dvdMessageTracker.DeInit(); #endif @@ -1120,7 +1117,7 @@ void CDVDPlayer::Process() void CDVDPlayer::ProcessPacket(CDemuxStream* pStream, DemuxPacket* pPacket) { /* process packet if it belongs to selected stream. for dvd's don't allow automatic opening of streams*/ - LockStreams(); + StreamLock lock(this); try { @@ -1143,7 +1140,6 @@ void CDVDPlayer::ProcessPacket(CDemuxStream* pStream, DemuxPacket* pPacket) CLog::Log(LOGERROR, "%s - Exception thrown when processing demux packet", __FUNCTION__); } - UnlockStreams(); } void CDVDPlayer::ProcessAudioData(CDemuxStream* pStream, DemuxPacket* pPacket) @@ -1874,7 +1870,7 @@ void CDVDPlayer::OnExit() void CDVDPlayer::HandleMessages() { CDVDMsg* pMsg; - LockStreams(); + StreamLock lock(this); while (m_messenger.Get(&pMsg, 0) == MSGQ_OK) { @@ -2121,7 +2117,6 @@ void CDVDPlayer::HandleMessages() pMsg->Release(); } - UnlockStreams(); } @@ -2452,9 +2447,8 @@ float CDVDPlayer::GetSubTitleDelay() // priority: 1: libdvdnav, 2: external subtitles, 3: muxed subtitles int CDVDPlayer::GetSubtitleCount() { - LockStreams(); + StreamLock lock(this); m_SelectionStreams.Update(m_pInputStream, m_pDemuxer); - UnlockStreams(); return m_SelectionStreams.Count(STREAM_SUBTITLE); } @@ -2503,9 +2497,8 @@ void CDVDPlayer::SetSubtitleVisible(bool bVisible) int CDVDPlayer::GetAudioStreamCount() { - LockStreams(); + StreamLock lock(this); m_SelectionStreams.Update(m_pInputStream, m_pDemuxer); - UnlockStreams(); return m_SelectionStreams.Count(STREAM_AUDIO); } diff --git a/xbmc/cores/dvdplayer/DVDPlayer.h b/xbmc/cores/dvdplayer/DVDPlayer.h index 88c3bd0362..72f3d3bf85 100644 --- a/xbmc/cores/dvdplayer/DVDPlayer.h +++ b/xbmc/cores/dvdplayer/DVDPlayer.h @@ -39,6 +39,7 @@ #include "Edl.h" #include "FileItem.h" +#include "threads/SingleLock.h" class CDVDInputStream; @@ -233,8 +234,12 @@ public: virtual int OnDVDNavResult(void* pData, int iMessage); protected: friend class CSelectionStreams; - void LockStreams() { EnterCriticalSection(&m_critStreamSection); } - void UnlockStreams() { LeaveCriticalSection(&m_critStreamSection); } + + class StreamLock : public CSingleLock + { + public: + inline StreamLock(CDVDPlayer* cdvdplayer) : CSingleLock(cdvdplayer->m_critStreamSection) {} + }; virtual void OnStartup(); virtual void OnExit(); @@ -405,7 +410,7 @@ protected: CCriticalSection m_StateSection; CEvent m_ready; - CRITICAL_SECTION m_critStreamSection; // need to have this lock when switching streams (audio / video) + CCriticalSection m_critStreamSection; // need to have this lock when switching streams (audio / video) CEdl m_Edl; diff --git a/xbmc/cores/dvdplayer/DVDPlayerSubtitle.cpp b/xbmc/cores/dvdplayer/DVDPlayerSubtitle.cpp index 14ac1ea22c..7a3177cb85 100644 --- a/xbmc/cores/dvdplayer/DVDPlayerSubtitle.cpp +++ b/xbmc/cores/dvdplayer/DVDPlayerSubtitle.cpp @@ -34,6 +34,7 @@ #include "DVDCodecs/DVDFactoryCodec.h" #include "DVDDemuxers/DVDDemuxUtils.h" #include "utils/log.h" +#include "threads/SingleLock.h" #ifdef _LINUX #include "config.h" #endif @@ -248,7 +249,7 @@ void CDVDPlayerSubtitle::GetCurrentSubtitle(CStdString& strSubtitle, double pts) Process(pts); // TODO: move to separate thread? - m_pOverlayContainer->Lock(); + CSingleLock lock(*m_pOverlayContainer); VecOverlays* pOverlays = m_pOverlayContainer->GetOverlays(); if (pOverlays) { @@ -274,6 +275,5 @@ void CDVDPlayerSubtitle::GetCurrentSubtitle(CStdString& strSubtitle, double pts) } } } - m_pOverlayContainer->Unlock(); strSubtitle.TrimRight('\n'); } diff --git a/xbmc/cores/dvdplayer/DVDPlayerVideo.cpp b/xbmc/cores/dvdplayer/DVDPlayerVideo.cpp index f3d014805c..29bd9ce48d 100644 --- a/xbmc/cores/dvdplayer/DVDPlayerVideo.cpp +++ b/xbmc/cores/dvdplayer/DVDPlayerVideo.cpp @@ -878,41 +878,42 @@ void CDVDPlayerVideo::ProcessOverlays(DVDVideoPicture* pSource, YV12Image* pDest } } - m_pOverlayContainer->Lock(); - - VecOverlays* pVecOverlays = m_pOverlayContainer->GetOverlays(); - VecOverlaysIter it = pVecOverlays->begin(); - - //Check all overlays and render those that should be rendered, based on time and forced - //Both forced and subs should check timeing, pts == 0 in the stillframe case - while (it != pVecOverlays->end()) { - CDVDOverlay* pOverlay = *it++; - if(!pOverlay->bForced && !m_bRenderSubs) - continue; - - if(pOverlay->iGroupId != pSource->iGroupId) - continue; + CSingleLock lock(*m_pOverlayContainer); - double pts2 = pOverlay->bForced ? pts : pts - m_iSubtitleDelay; + VecOverlays* pVecOverlays = m_pOverlayContainer->GetOverlays(); + VecOverlaysIter it = pVecOverlays->begin(); - if((pOverlay->iPTSStartTime <= pts2 && (pOverlay->iPTSStopTime > pts2 || pOverlay->iPTSStopTime == 0LL)) || pts == 0) + //Check all overlays and render those that should be rendered, based on time and forced + //Both forced and subs should check timeing, pts == 0 in the stillframe case + while (it != pVecOverlays->end()) { - if (render == OVERLAY_GPU) - g_renderManager.AddOverlay(pOverlay, pts2); + CDVDOverlay* pOverlay = *it++; + if(!pOverlay->bForced && !m_bRenderSubs) + continue; + + if(pOverlay->iGroupId != pSource->iGroupId) + continue; - if(pSource->format == DVDVideoPicture::FMT_YUV420P) + double pts2 = pOverlay->bForced ? pts : pts - m_iSubtitleDelay; + + if((pOverlay->iPTSStartTime <= pts2 && (pOverlay->iPTSStopTime > pts2 || pOverlay->iPTSStopTime == 0LL)) || pts == 0) { - if (render == OVERLAY_BUF) - CDVDOverlayRenderer::Render(m_pTempOverlayPicture, pOverlay, pts2); - else if(render == OVERLAY_VID) - CDVDOverlayRenderer::Render(pDest, pOverlay, pts2); - } + if (render == OVERLAY_GPU) + g_renderManager.AddOverlay(pOverlay, pts2); + + if(pSource->format == DVDVideoPicture::FMT_YUV420P) + { + if (render == OVERLAY_BUF) + CDVDOverlayRenderer::Render(m_pTempOverlayPicture, pOverlay, pts2); + else if(render == OVERLAY_VID) + CDVDOverlayRenderer::Render(pDest, pOverlay, pts2); + } + } } - } - m_pOverlayContainer->Unlock(); + } if(pSource->format == DVDVideoPicture::FMT_YUV420P) { diff --git a/xbmc/dialogs/GUIDialogProgress.cpp b/xbmc/dialogs/GUIDialogProgress.cpp index 0a19d35834..a0e7904d8b 100644 --- a/xbmc/dialogs/GUIDialogProgress.cpp +++ b/xbmc/dialogs/GUIDialogProgress.cpp @@ -52,7 +52,8 @@ void CGUIDialogProgress::SetCanCancel(bool bCanCancel) { m_bCanCancel = bCanCancel; CGUIMessage msg(bCanCancel ? GUI_MSG_VISIBLE : GUI_MSG_HIDDEN, GetID(), CONTROL_CANCEL_BUTTON); - if(OwningCriticalSection(g_graphicsContext)) + CSingleTryLock tryLock(g_graphicsContext); + if(tryLock.IsOwner()) OnMessage(msg); else g_windowManager.SendThreadMessage(msg, GetID()); @@ -196,7 +197,8 @@ bool CGUIDialogProgress::Abort() void CGUIDialogProgress::ShowProgressBar(bool bOnOff) { CGUIMessage msg(bOnOff ? GUI_MSG_VISIBLE : GUI_MSG_HIDDEN, GetID(), CONTROL_PROGRESS_BAR); - if(OwningCriticalSection(g_graphicsContext)) + CSingleTryLock tryLock(g_graphicsContext); + if(tryLock.IsOwner()) OnMessage(msg); else g_windowManager.SendThreadMessage(msg, GetID()); diff --git a/xbmc/filesystem/PluginDirectory.cpp b/xbmc/filesystem/PluginDirectory.cpp index 53e025e0ae..8488017572 100644 --- a/xbmc/filesystem/PluginDirectory.cpp +++ b/xbmc/filesystem/PluginDirectory.cpp @@ -454,15 +454,15 @@ bool CPluginDirectory::WaitOnScriptResult(const CStdString &scriptPath, const CS CLog::Log(LOGDEBUG, "%s - waiting on the %s plugin...", __FUNCTION__, scriptName.c_str()); while (true) { - CSingleExit ex(g_graphicsContext); - // check if the python script is finished - if (WaitForSingleObject(m_fetchComplete, 20) == WAIT_OBJECT_0) - { // python has returned - CLog::Log(LOGDEBUG, "%s- plugin returned %s", __FUNCTION__, m_success ? "successfully" : "failure"); - break; + { + CSingleExit ex(g_graphicsContext); + // check if the python script is finished + if (WaitForSingleObject(m_fetchComplete, 20) == WAIT_OBJECT_0) + { // python has returned + CLog::Log(LOGDEBUG, "%s- plugin returned %s", __FUNCTION__, m_success ? "successfully" : "failure"); + break; + } } - ex.Restore(); - // check our script is still running #ifdef HAS_PYTHON if (!g_pythonParser.isRunning(g_pythonParser.getScriptId(scriptPath.c_str()))) diff --git a/xbmc/filesystem/PluginDirectory.h b/xbmc/filesystem/PluginDirectory.h index 0dc17229f7..f08f9d0674 100644 --- a/xbmc/filesystem/PluginDirectory.h +++ b/xbmc/filesystem/PluginDirectory.h @@ -29,6 +29,7 @@ #include <vector> #include "threads/CriticalSection.h" #include "addons/IAddon.h" +#include "PlatformDefs.h" class CURL; class CFileItemList; diff --git a/xbmc/filesystem/iso9660.cpp b/xbmc/filesystem/iso9660.cpp index b9b6e719ea..2110ddbe32 100644 --- a/xbmc/filesystem/iso9660.cpp +++ b/xbmc/filesystem/iso9660.cpp @@ -20,6 +20,7 @@ #include "system.h" #include "utils/log.h" + /* Redbook : CDDA Yellowbook : CDROM @@ -43,6 +44,7 @@ ISO9660 #include "iso9660.h" #include "storage/IoSupport.h" #include "utils/CharsetConverter.h" +#include "threads/SingleLock.h" #include "IFile.h" #ifndef _WIN32 @@ -51,7 +53,7 @@ ISO9660 #include <cdio/bytesex.h> //#define _DEBUG_OUTPUT 1 -static CRITICAL_SECTION m_critSection; +static CCriticalSection m_critSection; class iso9660 m_isoReader; #define BUFFER_SIZE MODE2_DATA_SIZE #define RET_ERR -1 @@ -440,7 +442,6 @@ struct iso_dirtree *iso9660::ReadRecursiveDirFromSector( DWORD sector, const cha iso9660::iso9660( ) { memset(m_isoFiles, 0, sizeof(m_isoFiles)); - InitializeCriticalSection(&m_critSection); m_hCDROM = NULL; m_lastpath = NULL; m_searchpointer = NULL; @@ -463,7 +464,7 @@ void iso9660::Scan() m_info.Curr_dir = (char*)malloc( 4096 ); strcpy( m_info.Curr_dir, "\\" ); - EnterCriticalSection(&m_critSection); + CSingleLock lock(m_critSection); DWORD lpNumberOfBytesRead = 0; ::SetFilePointer( m_info.ISO_HANDLE, 0x8000, 0, FILE_BEGIN ); @@ -477,7 +478,6 @@ void iso9660::Scan() m_info.ISO_HANDLE = NULL; m_hCDROM = NULL; m_info.iso9660 = 0; - LeaveCriticalSection(&m_critSection); return ; } else @@ -533,13 +533,11 @@ void iso9660::Scan() memcpy( &m_info.isodir, &m_info.iso.szRootDir, sizeof(m_info.isodir) ); ReadRecursiveDirFromSector( from_733(m_info.isodir.extent), "\\" ); - LeaveCriticalSection(&m_critSection); } //****************************************************************************************************************** iso9660::~iso9660( ) { - DeleteCriticalSection(&m_critSection); Reset(); } @@ -762,7 +760,7 @@ HANDLE iso9660::OpenFile(const char *filename) bool bError; - EnterCriticalSection(&m_critSection); + CSingleLock lock(m_critSection); bError = (CIoSupport::ReadSector(m_info.ISO_HANDLE, pContext->m_dwStartBlock, (char*) & (pContext->m_pBuffer[0])) < 0); if ( bError ) { @@ -770,7 +768,6 @@ HANDLE iso9660::OpenFile(const char *filename) if ( !bError ) pContext->m_bUseMode2 = true; } - LeaveCriticalSection(&m_critSection); if (pContext->m_bUseMode2) pContext->m_dwFileSize = (pContext->m_dwFileSize / 2048) * MODE2_DATA_SIZE; @@ -844,16 +841,17 @@ bool iso9660::ReadSectorFromCache(iso9660::isofile* pContext, DWORD sector, byte } // Ok, we're ready to read the sector into the cache bool bError; - EnterCriticalSection(&m_critSection); - if ( pContext->m_bUseMode2 ) { - bError = (CIoSupport::ReadSectorMode2(m_info.ISO_HANDLE, sector, (char*) & (pContext->m_pBuffer[pContext->m_dwCircBuffEnd])) < 0); - } - else - { - bError = (CIoSupport::ReadSector(m_info.ISO_HANDLE, sector, (char*) & (pContext->m_pBuffer[pContext->m_dwCircBuffEnd])) < 0); + CSingleLock lock(m_critSection); + if ( pContext->m_bUseMode2 ) + { + bError = (CIoSupport::ReadSectorMode2(m_info.ISO_HANDLE, sector, (char*) & (pContext->m_pBuffer[pContext->m_dwCircBuffEnd])) < 0); + } + else + { + bError = (CIoSupport::ReadSector(m_info.ISO_HANDLE, sector, (char*) & (pContext->m_pBuffer[pContext->m_dwCircBuffEnd])) < 0); + } } - LeaveCriticalSection(&m_critSection); if ( bError ) return false; *ppBuffer = &(pContext->m_pBuffer[pContext->m_dwCircBuffEnd]); diff --git a/xbmc/guilib/GUIDialog.cpp b/xbmc/guilib/GUIDialog.cpp index 3f7ca4ca33..786fc414be 100644 --- a/xbmc/guilib/GUIDialog.cpp +++ b/xbmc/guilib/GUIDialog.cpp @@ -243,9 +243,8 @@ void CGUIDialog::Close(bool forceClose /* = false */) if (!g_application.IsCurrentThread()) { // make sure graphics lock is not held - int nCount = ExitCriticalSection(g_graphicsContext); + CSingleExit leaveIt(g_graphicsContext); g_application.getApplicationMessenger().Close(this, forceClose); - RestoreCriticalSection(g_graphicsContext, nCount); } else g_application.getApplicationMessenger().Close(this, forceClose); diff --git a/xbmc/guilib/GUIWindowManager.cpp b/xbmc/guilib/GUIWindowManager.cpp index 895f97875b..cc80be3b0f 100644 --- a/xbmc/guilib/GUIWindowManager.cpp +++ b/xbmc/guilib/GUIWindowManager.cpp @@ -343,9 +343,8 @@ void CGUIWindowManager::ActivateWindow(int iWindowID, const vector<CStdString>& if (!g_application.IsCurrentThread()) { // make sure graphics lock is not held - int nCount = ExitCriticalSection(g_graphicsContext); + CSingleExit leaveIt(g_graphicsContext); g_application.getApplicationMessenger().ActivateWindow(iWindowID, params, swappingWindows); - RestoreCriticalSection(g_graphicsContext, nCount); } else ActivateWindow_Internal(iWindowID, params, swappingWindows); diff --git a/xbmc/guilib/GraphicContext.h b/xbmc/guilib/GraphicContext.h index 4a9c3916bb..04092cf193 100644 --- a/xbmc/guilib/GraphicContext.h +++ b/xbmc/guilib/GraphicContext.h @@ -100,8 +100,8 @@ public: void ResetOverscan(RESOLUTION res, OVERSCAN &overscan); void ResetOverscan(RESOLUTION_INFO &resinfo); void ResetScreenParameters(RESOLUTION res); - void Lock() { EnterCriticalSection(*this); } - void Unlock() { LeaveCriticalSection(*this); } + void Lock() { lock(); } + void Unlock() { unlock(); } float GetPixelRatio(RESOLUTION iRes) const; void CaptureStateBlock(); void ApplyStateBlock(); diff --git a/xbmc/guilib/XBTFReader.cpp b/xbmc/guilib/XBTFReader.cpp index a6946dd0c9..f803035987 100644 --- a/xbmc/guilib/XBTFReader.cpp +++ b/xbmc/guilib/XBTFReader.cpp @@ -28,6 +28,9 @@ #include "PlatformDefs.h" //for PRIdS, PRId64 #endif +#include <string.h> +#include "PlatformDefs.h" + #define READ_STR(str, size, file) \ if (!fread(str, size, 1, file)) \ return false; diff --git a/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowDialog.cpp b/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowDialog.cpp index f6edcbd0c3..2ad8b0a2c1 100644 --- a/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowDialog.cpp +++ b/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowDialog.cpp @@ -22,6 +22,7 @@ #include "GUIPythonWindowDialog.h" #include "guilib/GUIWindowManager.h" #include "Application.h" +#include "threads/SingleLock.h" CGUIPythonWindowDialog::CGUIPythonWindowDialog(int id) :CGUIPythonWindow(id) @@ -58,11 +59,10 @@ bool CGUIPythonWindowDialog::OnMessage(CGUIMessage& message) void CGUIPythonWindowDialog::Show(bool show /* = true */) { - int count = ExitCriticalSection(g_graphicsContext); + CSingleExit leaveIt(g_graphicsContext); ThreadMessage tMsg = {TMSG_GUI_PYTHON_DIALOG, 0, show ? 1 : 0}; tMsg.lpVoid = this; g_application.getApplicationMessenger().SendMessage(tMsg, true); - RestoreCriticalSection(g_graphicsContext, count); } void CGUIPythonWindowDialog::Show_Internal(bool show /* = true */) diff --git a/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowXMLDialog.cpp b/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowXMLDialog.cpp index 6badfdcede..f59eebd647 100644 --- a/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowXMLDialog.cpp +++ b/xbmc/interfaces/python/xbmcmodule/GUIPythonWindowXMLDialog.cpp @@ -22,6 +22,7 @@ #include "GUIPythonWindowXMLDialog.h" #include "guilib/GUIWindowManager.h" #include "Application.h" +#include "threads/SingleLock.h" CGUIPythonWindowXMLDialog::CGUIPythonWindowXMLDialog(int id, CStdString strXML, CStdString strFallBackPath) : CGUIPythonWindowXML(id,strXML,strFallBackPath) @@ -48,11 +49,10 @@ bool CGUIPythonWindowXMLDialog::OnMessage(CGUIMessage &message) void CGUIPythonWindowXMLDialog::Show(bool show /* = true */) { - int count = ExitCriticalSection(g_graphicsContext); + CSingleExit leaveIt(g_graphicsContext); ThreadMessage tMsg = {TMSG_GUI_PYTHON_DIALOG, 1, show ? 1 : 0}; tMsg.lpVoid = this; g_application.getApplicationMessenger().SendMessage(tMsg, true); - RestoreCriticalSection(g_graphicsContext, count); } void CGUIPythonWindowXMLDialog::Show_Internal(bool show /* = true */) diff --git a/xbmc/linux/ConvUtils.cpp b/xbmc/linux/ConvUtils.cpp index 539b058bf6..372befbeae 100644 --- a/xbmc/linux/ConvUtils.cpp +++ b/xbmc/linux/ConvUtils.cpp @@ -27,8 +27,6 @@ #include <ctype.h> #include <errno.h> -#include <utils/log.h> - /* ** The following two functions together make up an itoa() diff --git a/xbmc/linux/PlatformDefs.h b/xbmc/linux/PlatformDefs.h index 7318ebf3dc..a12393ed0e 100644 --- a/xbmc/linux/PlatformDefs.h +++ b/xbmc/linux/PlatformDefs.h @@ -333,10 +333,6 @@ typedef struct _TIME_ZONE_INFORMATION { typedef int SOCKET; -class CCriticalSection; -#define CRITICAL_SECTION XCriticalSection -#define LPCRITICAL_SECTION XCriticalSection* - // Thread typedef int (*LPTHREAD_START_ROUTINE)(void *); diff --git a/xbmc/linux/XCriticalSection.cpp b/xbmc/linux/XCriticalSection.cpp deleted file mode 100644 index 42a7e9f05d..0000000000 --- a/xbmc/linux/XCriticalSection.cpp +++ /dev/null @@ -1,239 +0,0 @@ -/* -* XBMC Media Center -* Copyright (c) 2002 Frodo -* Portions Copyright (c) by the authors of ffmpeg and xvid -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#include "system.h" -#include "threads/CriticalSection.h" -#include "utils/log.h" -#include "threads/Thread.h" -#define SAFELY(expr) \ -{ \ - int err = 0; \ - if ((err=expr) != 0) \ - { CLog::Log(LOGERROR, "(%s): [%s:%d] %d", #expr, __FILE__, __LINE__, err); } \ -} - -////////////////////////////////////////////////////////////////////// -XCriticalSection::XCriticalSection() - : m_ownerThread(0) - , m_count(0) - , m_isDestroyed(false) - , m_isInitialized(false) -{ -} - -////////////////////////////////////////////////////////////////////// -void XCriticalSection::Initialize() -{ - if (m_isDestroyed) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to initialize destroyed section.", (void *)this); - return; - } - - if (m_isInitialized) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to initialze initialized section.", (void *)this); - return; - } - - // Setup for recursive locks. - pthread_mutexattr_t attr; - SAFELY(pthread_mutexattr_init(&attr)); - SAFELY(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)); - - // Create the mutexes - SAFELY(pthread_mutex_init(&m_mutex, &attr)); - SAFELY(pthread_mutex_init(&m_countMutex, &attr)); - - SAFELY(pthread_mutexattr_destroy(&attr)); - m_isInitialized = true; -} - -////////////////////////////////////////////////////////////////////// -void XCriticalSection::Destroy() -{ - if (m_isDestroyed) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to destroy destroyed section.", (void *)this); - return; - } - - if (!m_isInitialized) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to destroy uninitialized section.", (void *)this); - return; - } - - SAFELY(pthread_mutex_destroy(&m_mutex)); - SAFELY(pthread_mutex_destroy(&m_countMutex)); - m_isDestroyed = true; -} - -////////////////////////////////////////////////////////////////////// -XCriticalSection::~XCriticalSection() -{ -} - -////////////////////////////////////////////////////////////////////// -void XCriticalSection::Enter() -{ - if (m_isDestroyed) - { -#ifdef _DEBUG - // This has to be a printf, otherwise we may recurse. - printf("CRITSEC[%p]: Trying to enter destroyed section.\n", (void *)this); -#endif - return; - } - - if (!m_isInitialized) - { -#ifdef _DEBUG - // This has to be a printf, otherwise we may recurse. - printf("CRITSEC[%p]: Trying to enter uninitialized section.\n", (void *)this); -#endif - return; - } - - // Lock the mutex, bump the count. - SAFELY(pthread_mutex_lock(&m_mutex)); - - pthread_mutex_lock(&m_countMutex); - - // Save the owner, bump the count. - m_count++; - m_ownerThread = CThread::GetCurrentThreadId(); - - pthread_mutex_unlock(&m_countMutex); -} - -////////////////////////////////////////////////////////////////////// -void XCriticalSection::Leave() -{ - if (m_isDestroyed) - { -#ifdef _DEBUG - printf("CRITSEC[%p]: Trying to leave destroyed section.\n", (void *)this); -#endif - return; - } - - if (!m_isInitialized) - { -#ifdef _DEBUG - printf("CRITSEC[%p]: Trying to leave uninitialized section.\n", (void *)this); -#endif - return; - } - - if (!Owning()) - { -#ifdef _DEBUG - printf("CRITSEC[%p]: Some other thread trying to leave our critical section.\n", (void *)this); -#endif - return; - } - - pthread_mutex_lock(&m_countMutex); - - // Decrease the count, unlock mutex. - if (m_count > 0) - { - m_count--; - if(m_count == 0) - m_ownerThread = 0; - SAFELY(pthread_mutex_unlock(&m_mutex)); - } - else - { -#ifdef _DEBUG - printf("RITSEC[%p]: Trying to leave, already left.\n", (void *)this); -#endif - } - - pthread_mutex_unlock(&m_countMutex); -} - -////////////////////////////////////////////////////////////////////// -DWORD XCriticalSection::Exit() -{ - if (m_isDestroyed) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to exit destroyed section.", (void *)this); - return 0; - } - - if (!m_isInitialized) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to exit uninitialized section.", (void *)this); - return 0; - } - - // Only an owning thread can get out of a critical section. - if (!Owning()) - return 0; - - pthread_mutex_lock(&m_countMutex); - - // As many times as we've entered, leave. - DWORD count = m_count; - for (DWORD i=0; i<count; i++) - Leave(); - - pthread_mutex_unlock(&m_countMutex); - - return count; -} - -////////////////////////////////////////////////////////////////////// -BOOL XCriticalSection::Owning() -{ - return CThread::IsCurrentThread(m_ownerThread); -} - -////////////////////////////////////////////////////////////////////// -void XCriticalSection::Restore(DWORD count) -{ - if (m_isDestroyed) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to restore destroyed section.", (void *)this); - return; - } - - if (!m_isInitialized) - { - CLog::Log(LOGWARNING, "CRITSEC[%p]: Trying to restore uninitialized section.", (void *)this); - return; - } - - // Restore the specified count. - for (DWORD i=0; i<count; i++) - Enter(); -} - -// The C API. -void InitializeCriticalSection(XCriticalSection* section) { section->Initialize(); } -void DeleteCriticalSection(XCriticalSection* section) { section->Destroy(); } -BOOL OwningCriticalSection(XCriticalSection* section) { return section->Owning(); } -DWORD ExitCriticalSection(XCriticalSection* section) { return section->Exit(); } -void RestoreCriticalSection(XCriticalSection* section, DWORD count) { return section->Restore(count); } -void EnterCriticalSection(XCriticalSection* section) { section->Enter(); } -void LeaveCriticalSection(XCriticalSection* section) { section->Leave(); } - diff --git a/xbmc/linux/XCriticalSection.h b/xbmc/linux/XCriticalSection.h deleted file mode 100644 index 3865435066..0000000000 --- a/xbmc/linux/XCriticalSection.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef _XCRITICAL_SECTION_H_ -#define _XCRITICAL_SECTION_H_ - -/* - * Copyright (C) 2005-2008 Team XBMC - * http://www.xbmc.org - * - * This Program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This Program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with XBMC; see the file COPYING. If not, write to - * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -#ifdef _LINUX -#include "PlatformDefs.h" -#include "linux/XSyncUtils.h" -#endif - -class XCriticalSection -{ - public: - - // Constructor/destructor. - XCriticalSection(); - virtual ~XCriticalSection(); - - // Initialize a critical section. - void Initialize(); - - // Destroy a critical section. - void Destroy(); - - // Enter a critical section. - void Enter(); - - // Leave a critical section. - void Leave(); - - // Checks if current thread owns the critical section. - BOOL Owning(); - - // Exits a critical section. - DWORD Exit(); - - // Restores critical section count. - void Restore(DWORD count); - - private: - - ThreadIdentifier m_ownerThread; - pthread_mutex_t m_mutex; - pthread_mutex_t m_countMutex; - int m_count; - bool m_isDestroyed; - bool m_isInitialized; -}; - -// Define the C API. -void InitializeCriticalSection(XCriticalSection* section); -void DeleteCriticalSection(XCriticalSection* section); -BOOL OwningCriticalSection(XCriticalSection* section); -DWORD ExitCriticalSection(XCriticalSection* section); -void RestoreCriticalSection(XCriticalSection* section, DWORD count); -void EnterCriticalSection(XCriticalSection* section); -void LeaveCriticalSection(XCriticalSection* section); - -#endif diff --git a/xbmc/linux/XSyncUtils.h b/xbmc/linux/XSyncUtils.h index 974f4b0b1d..ab395b8c08 100644 --- a/xbmc/linux/XSyncUtils.h +++ b/xbmc/linux/XSyncUtils.h @@ -22,7 +22,7 @@ * */ -#include "XCriticalSection.h" +#include "PlatformDefs.h" #include "XHandlePublic.h" #ifdef _LINUX diff --git a/xbmc/music/karaoke/karaokewindowbackground.cpp b/xbmc/music/karaoke/karaokewindowbackground.cpp index 815a4a4c5d..44b566f4c2 100644 --- a/xbmc/music/karaoke/karaokewindowbackground.cpp +++ b/xbmc/music/karaoke/karaokewindowbackground.cpp @@ -143,9 +143,11 @@ void CKaraokeWindowBackground::Render() { // We cannot use CSingleLock on m_CritSectionVideoEnded, because OnPlayBackEnded() might be // called from a different thread, and since we cannot proceed on m_CritSectionShared, we'll deadlock - EnterCriticalSection( m_CritSectionVideoEnded ); - bool ended = m_videoEnded; - LeaveCriticalSection( m_CritSectionVideoEnded ); + bool ended; + { + CSingleLock lock( m_CritSectionVideoEnded ); + ended = m_videoEnded; + } CSingleLock lock (m_CritSectionShared); // Do we need to restart video? @@ -346,9 +348,10 @@ void CKaraokeWindowBackground::NextVideo() // Only one video selected, restarting m_videoLastTime = 0; - EnterCriticalSection( m_CritSectionVideoEnded ); - m_videoEnded = false; - LeaveCriticalSection( m_CritSectionVideoEnded ); + { + CSingleLock lock(m_CritSectionVideoEnded ); + m_videoEnded = false; + } StartVideo( m_path, m_videoLastTime ); CLog::Log( LOGDEBUG, "KaraokeVideoBackground: restarting video from the beginning" ); diff --git a/xbmc/network/EventClient.cpp b/xbmc/network/EventClient.cpp index 7cbb2c6ca5..38a31ddee3 100644 --- a/xbmc/network/EventClient.cpp +++ b/xbmc/network/EventClient.cpp @@ -199,19 +199,17 @@ void CEventClient::ProcessEvents() bool CEventClient::GetNextAction(CEventAction &action) { - EnterCriticalSection(&m_critSection); + CSingleLock lock(m_critSection); if (m_actionQueue.size() > 0) { // grab the next action in line action = m_actionQueue.front(); m_actionQueue.pop(); - LeaveCriticalSection(&m_critSection); return true; } else { // we got nothing - LeaveCriticalSection(&m_critSection); return false; } } @@ -650,9 +648,10 @@ bool CEventClient::OnPacketACTION(CEventPacket *packet) { case AT_EXEC_BUILTIN: case AT_BUTTON: - EnterCriticalSection(&m_critSection); - m_actionQueue.push(CEventAction(actionString.c_str(), actionType)); - LeaveCriticalSection(&m_critSection); + { + CSingleLock lock(m_critSection); + m_actionQueue.push(CEventAction(actionString.c_str(), actionType)); + } break; default: diff --git a/xbmc/network/EventServer.cpp b/xbmc/network/EventServer.cpp index 418cc7d45c..d75eae38f5 100644 --- a/xbmc/network/EventServer.cpp +++ b/xbmc/network/EventServer.cpp @@ -327,7 +327,7 @@ void CEventServer::ProcessEvents() bool CEventServer::ExecuteNextAction() { - EnterCriticalSection(&m_critSection); + CSingleLock lock(m_critSection); CEventAction actionEvent; map<unsigned long, CEventClient*>::iterator iter = m_clients.begin(); @@ -337,7 +337,7 @@ bool CEventServer::ExecuteNextAction() if (iter->second->GetNextAction(actionEvent)) { // Leave critical section before processing action - LeaveCriticalSection(&m_critSection); + lock.Leave(); switch(actionEvent.actionType) { case AT_EXEC_BUILTIN: @@ -358,7 +358,7 @@ bool CEventServer::ExecuteNextAction() } iter++; } - LeaveCriticalSection(&m_critSection); + return false; } diff --git a/xbmc/network/UdpClient.cpp b/xbmc/network/UdpClient.cpp index 23ad81fb4e..95220db84f 100644 --- a/xbmc/network/UdpClient.cpp +++ b/xbmc/network/UdpClient.cpp @@ -28,6 +28,8 @@ #include "utils/log.h" #include "utils/TimeUtils.h" +#include "threads/SingleLock.h" + #include <arpa/inet.h> #define UDPCLIENT_DEBUG_LEVEL LOGDEBUG @@ -43,8 +45,6 @@ bool CUdpClient::Create(void) { m_bStop = false; - InitializeCriticalSection(&critical_section); - CLog::Log(UDPCLIENT_DEBUG_LEVEL, "UDPCLIENT: Creating UDP socket..."); // Create a UDP socket @@ -81,7 +81,6 @@ void CUdpClient::Destroy() { StopThread(); closesocket(client_socket); - DeleteCriticalSection(&critical_section); } void CUdpClient::OnStartup() @@ -91,7 +90,7 @@ void CUdpClient::OnStartup() bool CUdpClient::Broadcast(int aPort, CStdString& aMessage) { - EnterCriticalSection(&critical_section); + CSingleLock lock(critical_section); SOCKADDR_IN addr; addr.sin_family = AF_INET; @@ -102,14 +101,13 @@ bool CUdpClient::Broadcast(int aPort, CStdString& aMessage) UdpCommand broadcast = {addr, aMessage, NULL, 0}; commands.push_back(broadcast); - LeaveCriticalSection(&critical_section); return true; } bool CUdpClient::Send(CStdString aIpAddress, int aPort, CStdString& aMessage) { - EnterCriticalSection(&critical_section); + CSingleLock lock(critical_section); SOCKADDR_IN addr; addr.sin_family = AF_INET; @@ -120,29 +118,26 @@ bool CUdpClient::Send(CStdString aIpAddress, int aPort, CStdString& aMessage) UdpCommand transmit = {addr, aMessage, NULL, 0}; commands.push_back(transmit); - LeaveCriticalSection(&critical_section); return true; } bool CUdpClient::Send(SOCKADDR_IN aAddress, CStdString& aMessage) { - EnterCriticalSection(&critical_section); + CSingleLock lock(critical_section); UdpCommand transmit = {aAddress, aMessage, NULL, 0}; commands.push_back(transmit); - LeaveCriticalSection(&critical_section); return true; } bool CUdpClient::Send(SOCKADDR_IN aAddress, LPBYTE pMessage, DWORD dwSize) { - EnterCriticalSection(&critical_section); + CSingleLock lock(critical_section); UdpCommand transmit = {aAddress, "", pMessage, dwSize}; commands.push_back(transmit); - LeaveCriticalSection(&critical_section); return true; } @@ -229,18 +224,17 @@ void CUdpClient::Process() bool CUdpClient::DispatchNextCommand() { - EnterCriticalSection(&critical_section); - - if (commands.size() <= 0) + UdpCommand command; { - LeaveCriticalSection(&critical_section); - return false; - } + CSingleLock lock(critical_section); + + if (commands.size() <= 0) + return false; - COMMANDITERATOR it = commands.begin(); - UdpCommand command = *it; - commands.erase(it); - LeaveCriticalSection(&critical_section); + COMMANDITERATOR it = commands.begin(); + command = *it; + commands.erase(it); + } int ret; if (command.binarySize > 0) diff --git a/xbmc/network/UdpClient.h b/xbmc/network/UdpClient.h index 14b304e41c..97acb28434 100644 --- a/xbmc/network/UdpClient.h +++ b/xbmc/network/UdpClient.h @@ -22,6 +22,7 @@ #include "utils/StdString.h" #include "threads/Thread.h" +#include "threads/CriticalSection.h" #include <sys/socket.h> #include <netinet/in.h> @@ -63,5 +64,5 @@ protected: std::vector<UdpCommand> commands; typedef std::vector<UdpCommand> ::iterator COMMANDITERATOR; - CRITICAL_SECTION critical_section; + CCriticalSection critical_section; }; diff --git a/xbmc/utils/DownloadQueue.cpp b/xbmc/utils/DownloadQueue.cpp index e9367a8e26..be65eb67ba 100644 --- a/xbmc/utils/DownloadQueue.cpp +++ b/xbmc/utils/DownloadQueue.cpp @@ -25,6 +25,7 @@ #include "filesystem/File.h" #include "filesystem/FileCurl.h" +#include "threads/SingleLock.h" using namespace std; using namespace XFILE; @@ -33,7 +34,6 @@ WORD CDownloadQueue::m_wNextQueueId = 0; CDownloadQueue::CDownloadQueue(void) : CThread() { - InitializeCriticalSection(&m_critical); m_bStop = false; m_wQueueId = m_wNextQueueId++; m_dwNextItemId = 0; @@ -47,39 +47,35 @@ void CDownloadQueue::OnStartup() CDownloadQueue::~CDownloadQueue(void) { - DeleteCriticalSection(&m_critical); } TICKET CDownloadQueue::RequestContent(const CStdString& aUrl, IDownloadQueueObserver* aObserver) { - EnterCriticalSection(&m_critical); - + CSingleLock lock(m_critical); TICKET ticket(m_wQueueId, m_dwNextItemId++); Command request = {ticket, aUrl, "", aObserver}; m_queue.push(request); - LeaveCriticalSection(&m_critical); return request.ticket; } TICKET CDownloadQueue::RequestFile(const CStdString& aUrl, const CStdString& aFilePath, IDownloadQueueObserver* aObserver) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); TICKET ticket(m_wQueueId, m_dwNextItemId++); Command request = {ticket, aUrl, aFilePath, aObserver}; m_queue.push(request); - LeaveCriticalSection(&m_critical); return request.ticket; } void CDownloadQueue::CancelRequests(IDownloadQueueObserver *aObserver) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); CLog::Log(LOGDEBUG, "CancelRequests from observer at %p", aObserver); // run through our queue, and create a new queue with the requests @@ -94,12 +90,11 @@ void CDownloadQueue::CancelRequests(IDownloadQueueObserver *aObserver) newQueue.push(request); } m_queue = newQueue; - LeaveCriticalSection(&m_critical); } TICKET CDownloadQueue::RequestFile(const CStdString& aUrl, IDownloadQueueObserver* aObserver) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); CLog::Log(LOGDEBUG, "RequestFile from observer at %p", aObserver); // create a temporary destination @@ -114,17 +109,15 @@ TICKET CDownloadQueue::RequestFile(const CStdString& aUrl, IDownloadQueueObserve Command request = {ticket, aUrl, strFilePath, aObserver}; m_queue.push(request); - LeaveCriticalSection(&m_critical); return request.ticket; } VOID CDownloadQueue::Flush() { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); m_queue.empty(); - LeaveCriticalSection(&m_critical); } void CDownloadQueue::Process() @@ -138,13 +131,12 @@ void CDownloadQueue::Process() { while ( CDownloadQueue::Size() > 0 ) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); // get the first item, but don't pop it from our queue // so that the download can be interrupted Command request = m_queue.front(); - - LeaveCriticalSection(&m_critical); + lock.Leave(); bool bFileRequest = request.content.length() > 0; DWORD dwSize = 0; @@ -161,38 +153,39 @@ void CDownloadQueue::Process() // now re-grab the item as we may have cancelled our download // while we were working - EnterCriticalSection(&m_critical); + { + CSingleLock lock2(m_critical); - request = m_queue.front(); - m_queue.pop(); + request = m_queue.front(); + m_queue.pop(); - // if the request has been cancelled our observer will be NULL - if (NULL != request.observer) - { - try + // if the request has been cancelled our observer will be NULL + if (NULL != request.observer) { - if (bFileRequest) + try { - request.observer->OnFileComplete(request.ticket, request.content, dwSize, - bSuccess ? IDownloadQueueObserver::Succeeded : IDownloadQueueObserver::Failed ); + if (bFileRequest) + { + request.observer->OnFileComplete(request.ticket, request.content, dwSize, + bSuccess ? IDownloadQueueObserver::Succeeded : IDownloadQueueObserver::Failed ); + } + else + { + request.observer->OnContentComplete(request.ticket, request.content, + bSuccess ? IDownloadQueueObserver::Succeeded : IDownloadQueueObserver::Failed ); + } } - else + catch (...) { - request.observer->OnContentComplete(request.ticket, request.content, - bSuccess ? IDownloadQueueObserver::Succeeded : IDownloadQueueObserver::Failed ); - } - } - catch (...) - { - CLog::Log(LOGERROR, "exception while updating download observer."); + CLog::Log(LOGERROR, "exception while updating download observer."); - if (bFileRequest) - { - CFile::Delete(request.content); + if (bFileRequest) + { + CFile::Delete(request.content); + } } } } - LeaveCriticalSection(&m_critical); } Sleep(500); @@ -203,12 +196,10 @@ void CDownloadQueue::Process() INT CDownloadQueue::Size() { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); int sizeOfQueue = m_queue.size(); - LeaveCriticalSection(&m_critical); - return sizeOfQueue; } diff --git a/xbmc/utils/DownloadQueue.h b/xbmc/utils/DownloadQueue.h index b6420244c2..c314792853 100644 --- a/xbmc/utils/DownloadQueue.h +++ b/xbmc/utils/DownloadQueue.h @@ -23,6 +23,7 @@ #include <queue> #include "threads/Thread.h" +#include "threads/CriticalSection.h" #include "StdString.h" struct TICKET @@ -76,7 +77,7 @@ protected: typedef std::queue<Command> COMMANDQUEUE; COMMANDQUEUE m_queue; - CRITICAL_SECTION m_critical; + CCriticalSection m_critical; WORD m_wQueueId; DWORD m_dwNextItemId; diff --git a/xbmc/utils/DownloadQueueManager.cpp b/xbmc/utils/DownloadQueueManager.cpp index 76c14de07d..c1e73f65e9 100644 --- a/xbmc/utils/DownloadQueueManager.cpp +++ b/xbmc/utils/DownloadQueueManager.cpp @@ -20,17 +20,15 @@ */ #include "DownloadQueueManager.h" - +#include "threads/SingleLock.h" #include <assert.h> CDownloadQueueManager::CDownloadQueueManager() { - InitializeCriticalSection(&m_critical); } CDownloadQueueManager::~CDownloadQueueManager(void) { - DeleteCriticalSection(&m_critical); } VOID CDownloadQueueManager::Initialize() @@ -40,38 +38,34 @@ VOID CDownloadQueueManager::Initialize() TICKET CDownloadQueueManager::RequestContent(const CStdString& aUrl, IDownloadQueueObserver* aObserver) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); TICKET ticket = GetNextDownloadQueue()->RequestContent(aUrl, aObserver); - LeaveCriticalSection(&m_critical); return ticket; } TICKET CDownloadQueueManager::RequestFile(const CStdString& aUrl, const CStdString& aFilePath, IDownloadQueueObserver* aObserver) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); TICKET ticket = GetNextDownloadQueue()->RequestFile(aUrl, aFilePath, aObserver); - LeaveCriticalSection(&m_critical); return ticket; } TICKET CDownloadQueueManager::RequestFile(const CStdString& aUrl, IDownloadQueueObserver* aObserver) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); TICKET ticket = GetNextDownloadQueue()->RequestFile(aUrl, aObserver); - LeaveCriticalSection(&m_critical); return ticket; } void CDownloadQueueManager::CancelRequests(IDownloadQueueObserver *aObserver) { - EnterCriticalSection(&m_critical); + CSingleLock lock(m_critical); // run through all our queues and remove all requests from this observer for (QUEUEPOOL::iterator it = m_queues.begin(); it != m_queues.end(); ++it) { CDownloadQueue* downloadQueue = *it; downloadQueue->CancelRequests(aObserver); } - LeaveCriticalSection(&m_critical); } CDownloadQueue* CDownloadQueueManager::GetNextDownloadQueue() diff --git a/xbmc/utils/DownloadQueueManager.h b/xbmc/utils/DownloadQueueManager.h index 4474f97144..3fb0d9f4ae 100644 --- a/xbmc/utils/DownloadQueueManager.h +++ b/xbmc/utils/DownloadQueueManager.h @@ -44,7 +44,7 @@ protected: typedef std::vector<CDownloadQueue*> QUEUEPOOL; QUEUEPOOL m_queues; - CRITICAL_SECTION m_critical; + CCriticalSection m_critical; }; // Single global instance of class is in cpp file diff --git a/xbmc/utils/RssReader.cpp b/xbmc/utils/RssReader.cpp index c0e4aab348..2a6d848a87 100644 --- a/xbmc/utils/RssReader.cpp +++ b/xbmc/utils/RssReader.cpp @@ -117,7 +117,7 @@ void CRssReader::Process() { while (GetQueueSize()) { - EnterCriticalSection(*this); + CSingleLock lock(*this); int iFeed = m_vecQueue.front(); m_vecQueue.erase(m_vecQueue.begin()); @@ -130,8 +130,7 @@ void CRssReader::Process() http.SetTimeout(2); CStdString strXML; CStdString strUrl = m_vecUrls[iFeed]; - - LeaveCriticalSection(*this); + lock.Leave(); int nRetries = 3; CURL url(strUrl); @@ -414,10 +413,9 @@ void CRssReader::UpdateObserver() getFeed(feed); if (feed.size() > 0) { - g_graphicsContext.Lock(); + CSingleLock lock(g_graphicsContext); if (m_pObserver) // need to check again when locked to make sure observer wasnt removed m_pObserver->OnFeedUpdate(feed); - g_graphicsContext.Unlock(); } } |