diff --git a/xbmc/cores/paplayer/SIDCodec/Makefile.in b/xbmc/cores/paplayer/SIDCodec/Makefile.in index fd77476..a434b95 100644 --- a/xbmc/cores/paplayer/SIDCodec/Makefile.in +++ b/xbmc/cores/paplayer/SIDCodec/Makefile.in @@ -1,6 +1,6 @@ ARCH=@ARCH@ -CFLAGS +=-D_LINUX -DHAVE_UNIX -DHAVE_STRCASECMP -DHAVE_STRNCASECMP -DHAVE_IOS_OPENMODE -fPIC -Ilibsidplay/win/VC -Ibuilders/resid-builder/include/sidplay/builders -Ilibsidplay/include/sidplay -Iresid -Ilibsidplay/include -I../../../linux -I../../../ -I../../../../guilib/ -fPIC +CFLAGS +=-D_LINUX -DHAVE_UNIX -DHAVE_STRCASECMP -DHAVE_STRNCASECMP -DHAVE_IOS_OPENMODE -fPIC -Ilibsidplay/win/VC -Ibuilders/resid-builder/include/sidplay/builders -Ilibsidplay/include/sidplay -Iresid -Ilibsidplay/include -I../../../linux -I../../../utils -I../../../ -I../../../../guilib/ -fPIC CXXFLAGS += $(CFLAGS) ifeq ($(ARCH), powerpc-osx) BUNDLE1_O=-lbundle1.o -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/ -lgcc diff --git a/xbmc/linux/XHandle.cpp b/xbmc/linux/XHandle.cpp index 5a314f6..59f7c1c 100644 --- a/xbmc/linux/XHandle.cpp +++ b/xbmc/linux/XHandle.cpp @@ -47,8 +47,8 @@ CXHandle::CXHandle(const CXHandle &src) CLog::Log(LOGWARNING,"%s, copy handle.", __FUNCTION__); Init(); - if (src.m_hSem) - m_hSem = SDL_CreateSemaphore(SDL_SemValue(src.m_hSem)); + if (src.m_pSem) + m_pSem = new CSemaphore(*src.m_pSem); if (m_threadValid) { @@ -83,10 +83,8 @@ CXHandle::~CXHandle() CLog::Log(LOGERROR,"%s, destroying handle with ref count %d", __FUNCTION__, m_nRefCount); assert(false); } - - if (m_hSem) { - SDL_DestroySemaphore(m_hSem); - } + + delete m_pSem; if (m_hMutex) { SDL_DestroyMutex(m_hMutex); @@ -113,7 +111,7 @@ CXHandle::~CXHandle() void CXHandle::Init() { fd=0; - m_hSem=NULL; + m_pSem=NULL; m_hMutex=NULL; m_threadValid=false; m_hCond=NULL; diff --git a/xbmc/linux/XHandle.h b/xbmc/linux/XHandle.h index db6fcc1..85799e2 100644 --- a/xbmc/linux/XHandle.h +++ b/xbmc/linux/XHandle.h @@ -30,6 +30,7 @@ #include "PlatformDefs.h" #include "XHandlePublic.h" +#include "Semaphore.h" #include struct CXHandle { @@ -46,7 +47,7 @@ public: inline HandleType GetType() { return m_type; } void ChangeType(HandleType newType); - SDL_sem *m_hSem; + CSemaphore *m_pSem; ThreadIdentifier m_hThread; bool m_threadValid; SDL_cond *m_hCond; diff --git a/xbmc/linux/XSyncUtils.cpp b/xbmc/linux/XSyncUtils.cpp index cfad28c..f110eca 100644 --- a/xbmc/linux/XSyncUtils.cpp +++ b/xbmc/linux/XSyncUtils.cpp @@ -53,7 +53,7 @@ bool InitializeRecursiveMutex(HANDLE hMutex, BOOL bInitialOwner) { // we use semaphores instead of the mutex because in SDL we CANT wait for a mutex // to lock with timeout. - hMutex->m_hSem = SDL_CreateSemaphore(bInitialOwner?0:1); + hMutex->m_pSem = new CSemaphore(bInitialOwner?0:1); hMutex->m_hMutex = SDL_CreateMutex(); hMutex->ChangeType(CXHandle::HND_MUTEX); @@ -66,14 +66,14 @@ bool InitializeRecursiveMutex(HANDLE hMutex, BOOL bInitialOwner) { } bool DestroyRecursiveMutex(HANDLE hMutex) { - if (hMutex == NULL || hMutex->m_hMutex == NULL || hMutex->m_hSem == NULL) + if (hMutex == NULL || hMutex->m_hMutex == NULL || hMutex->m_pSem == NULL) return false; - SDL_DestroySemaphore(hMutex->m_hSem); + delete hMutex->m_pSem; SDL_DestroyMutex(hMutex->m_hMutex); hMutex->m_hMutex = NULL; - hMutex->m_hSem = NULL; + hMutex->m_pSem = NULL; return true; } @@ -87,7 +87,7 @@ HANDLE WINAPI CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInit } bool WINAPI ReleaseMutex( HANDLE hMutex ) { - if (hMutex == NULL || hMutex->m_hSem == NULL || hMutex->m_hMutex == NULL) + if (hMutex == NULL || hMutex->m_pSem == NULL || hMutex->m_hMutex == NULL) return false; BOOL bOk = false; @@ -97,7 +97,7 @@ bool WINAPI ReleaseMutex( HANDLE hMutex ) { bOk = true; if (--hMutex->RecursionCount == 0) { hMutex->OwningThread = 0; - SDL_SemPost(hMutex->m_hSem); + hMutex->m_pSem->Post(); } } SDL_mutexV(hMutex->m_hMutex); diff --git a/xbmc/posix/SemaphorePOSIX.cpp b/xbmc/posix/SemaphorePOSIX.cpp new file mode 100644 index 0000000..094c622 --- /dev/null +++ b/xbmc/posix/SemaphorePOSIX.cpp @@ -0,0 +1,72 @@ + +#include "SemaphorePOSIX.h" +#include +#include +#define __STDC_FORMAT_MACROS +#include +#include +#include +#include +#include +#include + +#define SEM_NAME_LEN (NAME_MAX-4) + +CSemaphorePOSIX::CSemaphorePOSIX(uint32_t initialCount) + : ISemaphore() +{ + struct timespec now = {}; + char name[SEM_NAME_LEN] = {}; + + clock_gettime(CLOCK_MONOTONIC, &now); + + snprintf(name, SEM_NAME_LEN, "/xbmc-sem-%u-%" PRIu64 "-%" PRIu64, (uint32_t)getpid(), (uint64_t)now.tv_sec, (uint64_t)now.tv_nsec); + + m_pSem = sem_open(name, O_CREAT, 0600, initialCount); +} + +CSemaphorePOSIX::~CSemaphorePOSIX() +{ + sem_close(m_pSem); +} + +bool CSemaphorePOSIX::Wait() +{ + return (0 == sem_wait(m_pSem)); +} + +SEM_GRAB CSemaphorePOSIX::TimedWait(uint32_t millis) +{ + struct timespec to = + { + millis / 1000, + (millis % 1000) * 1000000 + }; + + if (0 == sem_timedwait(m_pSem, &to)) + return SEM_GRAB_SUCCESS; + + if (errno == ETIMEDOUT) + return SEM_GRAB_TIMEOUT; + + return SEM_GRAB_FAILED; +} + +bool CSemaphorePOSIX::TryWait() +{ + return (0 == sem_trywait(m_pSem)); +} + +bool CSemaphorePOSIX::Post() +{ + return (0 == sem_post(m_pSem)); +} + +int CSemaphorePOSIX::GetCount() const +{ + int val; + if (0 == sem_getvalue(m_pSem, &val)) + return val; + return -1; +} + diff --git a/xbmc/posix/SemaphorePOSIX.h b/xbmc/posix/SemaphorePOSIX.h new file mode 100644 index 0000000..020ce31 --- /dev/null +++ b/xbmc/posix/SemaphorePOSIX.h @@ -0,0 +1,22 @@ + +#ifndef SEMAPHORE_POSIX_H__ +#define SEMAPHORE_POSIX_H__ + +#include "ISemaphore.h" +#include + +class CSemaphorePOSIX : public ISemaphore +{ + sem_t* m_pSem; + public: + CSemaphorePOSIX(uint32_t initialCount); + virtual ~CSemaphorePOSIX(); + virtual bool Wait(); + virtual SEM_GRAB TimedWait(uint32_t millis); + virtual bool TryWait(); + virtual bool Post(); + virtual int GetCount() const; +}; + +#endif // SEMAPHORE_POSIX_H__ + diff --git a/xbmc/utils/ISemaphore.h b/xbmc/utils/ISemaphore.h new file mode 100644 index 0000000..a260640 --- /dev/null +++ b/xbmc/utils/ISemaphore.h @@ -0,0 +1,27 @@ + +#ifndef ISEMAPHORE_H__ +#define ISEMAPHORE_H__ + +#include + +enum SEM_GRAB +{ + SEM_GRAB_SUCCESS, + SEM_GRAB_FAILED, + SEM_GRAB_TIMEOUT +}; + +class ISemaphore +{ + public: + ISemaphore() {} + virtual ~ISemaphore() {} + virtual bool Wait() = 0; + virtual SEM_GRAB TimedWait(uint32_t millis) = 0; + virtual bool TryWait() = 0; + virtual bool Post() = 0; + virtual int GetCount() const = 0; +}; + +#endif // ISEMAPHORE_H__ + diff --git a/xbmc/utils/Makefile b/xbmc/utils/Makefile index d208da2..77a6090 100644 --- a/xbmc/utils/Makefile +++ b/xbmc/utils/Makefile @@ -1,4 +1,4 @@ -INCLUDES=-I. -I.. -I../../ -I../linux -I../cores -I../../guilib +INCLUDES=-I. -I.. -I../../ -I../linux -I../cores -I../../guilib -I../posix SRCS=AlarmClock.cpp \ Archive.cpp \ @@ -59,7 +59,8 @@ SRCS=AlarmClock.cpp \ Teletext.cpp \ fastmemcpy.cpp \ PasswordManager.cpp \ - AliasShortcutUtils.cpp + AliasShortcutUtils.cpp \ + Semaphore.cpp LIB=utils.a diff --git a/xbmc/utils/Semaphore.cpp b/xbmc/utils/Semaphore.cpp new file mode 100644 index 0000000..1407ae8 --- /dev/null +++ b/xbmc/utils/Semaphore.cpp @@ -0,0 +1,56 @@ + +#include "Semaphore.h" +#ifdef __linux__ +#include "SemaphorePOSIX.h" +#endif + +CSemaphore::CSemaphore(uint32_t initialCount/*=1*/) + : ISemaphore() +{ +#ifdef _SEMAPHORE_H + m_pSemaphore = new CSemaphorePOSIX(initialCount); +#else +#error No supported semaphore implementation available +#endif +} + +CSemaphore::CSemaphore(const CSemaphore& sem) + : ISemaphore() +{ +#ifdef _SEMAPHORE_H + m_pSemaphore = new CSemaphorePOSIX(sem.GetCount()); +#else +#error No supported semaphore implementation available +#endif +} + +CSemaphore::~CSemaphore() +{ + delete m_pSemaphore; +} + +bool CSemaphore::Wait() +{ + return m_pSemaphore->Wait(); +} + +SEM_GRAB CSemaphore::TimedWait(uint32_t millis) +{ + return m_pSemaphore->TimedWait(millis); +} + +bool CSemaphore::TryWait() +{ + return m_pSemaphore->TryWait(); +} + +bool CSemaphore::Post() +{ + return m_pSemaphore->Post(); +} + +int CSemaphore::GetCount() const +{ + return m_pSemaphore->GetCount(); +} + diff --git a/xbmc/utils/Semaphore.h b/xbmc/utils/Semaphore.h new file mode 100644 index 0000000..47214dc --- /dev/null +++ b/xbmc/utils/Semaphore.h @@ -0,0 +1,22 @@ + +#ifndef SEMAPHORE_H__ +#define SEMAPHORE_H__ + +#include "ISemaphore.h" + +class CSemaphore : public ISemaphore +{ + ISemaphore* m_pSemaphore; + public: + CSemaphore(uint32_t initialCount=1); + CSemaphore(const CSemaphore& sem); + virtual ~CSemaphore(); + virtual bool Wait(); + virtual SEM_GRAB TimedWait(uint32_t millis); + virtual bool TryWait(); + virtual bool Post(); + virtual int GetCount() const; +}; + +#endif // SEMAPHORE_H__ +