From 128a5e113acfa4012d88137bf2010007e735e22b Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Sun, 19 Jan 2014 02:24:04 -0500 Subject: [PATCH] Implement POSIX support --- limelight-common/ControlStream.cpp | 18 ++--- limelight-common/LinkedBlockingQueue.cpp | 16 ++--- limelight-common/Platform.h | 4 ++ limelight-common/PlatformSockets.cpp | 2 +- limelight-common/PlatformSockets.h | 5 ++ limelight-common/PlatformThreads.cpp | 85 ++++++++++++++++++------ limelight-common/PlatformThreads.h | 27 +++++--- 7 files changed, 108 insertions(+), 49 deletions(-) diff --git a/limelight-common/ControlStream.cpp b/limelight-common/ControlStream.cpp index fd4e0ce..7b8685d 100644 --- a/limelight-common/ControlStream.cpp +++ b/limelight-common/ControlStream.cpp @@ -45,7 +45,7 @@ int initializeControlStream(IP_ADDRESS host, PSTREAM_CONFIGURATION streamConfigP } void requestIdrFrame(void) { - PltSetEvent(resyncEvent); + PltSetEvent(&resyncEvent); } static PNVCTL_PACKET_HEADER readNvctlPacket(void) { @@ -143,7 +143,7 @@ static void resyncThreadFunc(void* context) { header.type = PTYPE_RESYNC; header.payloadLength = PPAYLEN_RESYNC; for (;;) { - PltWaitForEvent(resyncEvent); + PltWaitForEvent(&resyncEvent); err = send(ctlSock, (char*) &header, sizeof(header), 0); if (err != sizeof(header)) { @@ -161,20 +161,20 @@ static void resyncThreadFunc(void* context) { return; } - PltClearEvent(resyncEvent); + PltClearEvent(&resyncEvent); } } int stopControlStream(void) { closesocket(ctlSock); - PltJoinThread(heartbeatThread); - PltJoinThread(jitterThread); - PltJoinThread(resyncThread); + PltJoinThread(&heartbeatThread); + PltJoinThread(&jitterThread); + PltJoinThread(&resyncThread); - PltCloseThread(heartbeatThread); - PltCloseThread(jitterThread); - PltCloseThread(resyncThread); + PltCloseThread(&heartbeatThread); + PltCloseThread(&jitterThread); + PltCloseThread(&resyncThread); return 0; } diff --git a/limelight-common/LinkedBlockingQueue.cpp b/limelight-common/LinkedBlockingQueue.cpp index 1030d7a..d7a04da 100644 --- a/limelight-common/LinkedBlockingQueue.cpp +++ b/limelight-common/LinkedBlockingQueue.cpp @@ -30,7 +30,7 @@ int offerQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) { entry->next = NULL; entry->data = data; - PltLockMutex(queueHead->mutex); + PltLockMutex(&queueHead->mutex); if (queueHead->head == NULL) { queueHead->head = entry; @@ -43,9 +43,9 @@ int offerQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) { lastEntry->next = entry; } - PltSetEvent(queueHead->containsDataEvent); + PltSetEvent(&queueHead->containsDataEvent); - PltUnlockMutex(queueHead->mutex); + PltUnlockMutex(&queueHead->mutex); return 1; } @@ -55,12 +55,12 @@ void* waitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead) { void* data; for (;;) { - PltWaitForEvent(queueHead->containsDataEvent); + PltWaitForEvent(&queueHead->containsDataEvent); - PltLockMutex(queueHead->mutex); + PltLockMutex(&queueHead->mutex); if (queueHead->head == NULL) { - PltUnlockMutex(queueHead->mutex); + PltUnlockMutex(&queueHead->mutex); continue; } @@ -72,10 +72,10 @@ void* waitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead) { free(entry); if (queueHead->head == NULL) { - PltClearEvent(queueHead->containsDataEvent); + PltClearEvent(&queueHead->containsDataEvent); } - PltUnlockMutex(queueHead->mutex); + PltUnlockMutex(&queueHead->mutex); break; } diff --git a/limelight-common/Platform.h b/limelight-common/Platform.h index 8ca710b..00de7b2 100644 --- a/limelight-common/Platform.h +++ b/limelight-common/Platform.h @@ -3,4 +3,8 @@ #ifdef _WIN32 #include #else +#include +#include +#include +#include #endif \ No newline at end of file diff --git a/limelight-common/PlatformSockets.cpp b/limelight-common/PlatformSockets.cpp index 612e81f..f0af9ca 100644 --- a/limelight-common/PlatformSockets.cpp +++ b/limelight-common/PlatformSockets.cpp @@ -20,7 +20,7 @@ SOCKET bindUdpSocket(unsigned short port) { } val = 65536; - int err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*) &val, sizeof(val)); + setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*) &val, sizeof(val)); return s; } diff --git a/limelight-common/PlatformSockets.h b/limelight-common/PlatformSockets.h index 6dce63a..340d5a5 100644 --- a/limelight-common/PlatformSockets.h +++ b/limelight-common/PlatformSockets.h @@ -4,6 +4,11 @@ #include #define LastSocketError() WSAGetLastError() #else +#include +#include +#include +#include +#include #define SOCKET int #define LastSocketError() errno #define INVALID_SOCKET -1 diff --git a/limelight-common/PlatformThreads.cpp b/limelight-common/PlatformThreads.cpp index 18ffe2c..ca41a30 100644 --- a/limelight-common/PlatformThreads.cpp +++ b/limelight-common/PlatformThreads.cpp @@ -1,4 +1,5 @@ #include "PlatformThreads.h" +#include "Platform.h" struct thread_context { ThreadEntry entry; @@ -16,13 +17,23 @@ DWORD WINAPI ThreadProc(LPVOID lpParameter) { return 0; } #else -#error POSIX threads not implemented +void* ThreadProc(void* context) { + struct thread_context *ctx = (struct thread_context *)context; + + ctx->entry(ctx->context); + + free(ctx); + + return NULL; +} #endif void PltSleepMs(int ms) { #ifdef _WIN32 Sleep(ms); #else + long usecs = (long)ms * 1000; + usleep(usecs); #endif } @@ -34,40 +45,45 @@ int PltCreateMutex(PLT_MUTEX *mutex) { } return 0; #else + return pthread_mutex_init(mutex, NULL); #endif } -void PltDeleteMutex(PLT_MUTEX mutex) { +void PltDeleteMutex(PLT_MUTEX *mutex) { #ifdef _WIN32 - CloseHandle(mutex); + CloseHandle(*mutex); #else + pthread_mutex_destroy(mutex); #endif } -void PltLockMutex(PLT_MUTEX mutex) { +void PltLockMutex(PLT_MUTEX *mutex) { #ifdef _WIN32 - WaitForSingleObject(mutex, INFINITE); + WaitForSingleObject(*mutex, INFINITE); #else + pthread_mutex_lock(mutex); #endif } -void PltUnlockMutex(PLT_MUTEX mutex) { +void PltUnlockMutex(PLT_MUTEX *mutex) { #ifdef _WIN32 - ReleaseMutex(mutex); + ReleaseMutex(*mutex); #else + pthread_mutex_unlock(mutex); #endif } -void PltJoinThread(PLT_THREAD thread) { +void PltJoinThread(PLT_THREAD *thread) { #ifdef _WIN32 - WaitForSingleObject(thread, INFINITE); + WaitForSingleObject(*thread, INFINITE); #else + pthread_join(*thread, NULL); #endif } -void PltCloseThread(PLT_THREAD thread) { +void PltCloseThread(PLT_THREAD *thread) { #ifdef _WIN32 - CloseHandle(thread); + CloseHandle(*thread); #else #endif } @@ -97,6 +113,12 @@ int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD *thread) { } } #else + { + err = pthread_create(thread, NULL, ThreadProc, ctx); + if (err != 0) { + free(ctx); + } + } #endif return err; @@ -111,41 +133,62 @@ int PltCreateEvent(PLT_EVENT *event) { return 0; #else + pthread_mutex_init(&event->mutex, NULL); + pthread_cond_init(&event->cond, NULL); + event->signalled = 0; + return 0; #endif } -void PltCloseEvent(PLT_EVENT event) { +void PltCloseEvent(PLT_EVENT *event) { #ifdef _WIN32 - CloseHandle(event); + CloseHandle(*event); #else + pthread_mutex_destroy(&event->mutex); + pthread_cond_destroy(&event->cond); #endif } -void PltSetEvent(PLT_EVENT event) { +void PltSetEvent(PLT_EVENT *event) { #ifdef _WIN32 - SetEvent(event); + SetEvent(*event); #else + pthread_mutex_lock(&event->mutex); + event->signalled = 1; + pthread_cond_signal(&event->cond); + pthread_mutex_unlock(&event->mutex); #endif } -void PltClearEvent(PLT_EVENT event) { +void PltClearEvent(PLT_EVENT *event) { #ifdef _WIN32 - ResetEvent(event); + ResetEvent(*event); #else + pthread_mutex_lock(&event->mutex); + event->signalled = 0; + pthread_mutex_unlock(&event->mutex); #endif } -void PltPulseEvent(PLT_EVENT event) { +void PltPulseEvent(PLT_EVENT *event) { #ifdef _WIN32 - PulseEvent(event); + PulseEvent(*event); #else + pthread_mutex_lock(&event->mutex); + event->signalled = 1; + pthread_cond_signal(&event->cond); + event->signalled = 0; + pthread_mutex_unlock(&event->mutex); #endif } -void PltWaitForEvent(PLT_EVENT event) { +void PltWaitForEvent(PLT_EVENT *event) { #ifdef _WIN32 - WaitForSingleObject(event, INFINITE); + WaitForSingleObject(*event, INFINITE); #else + while (!event->signalled) { + pthread_cond_wait(&event->cond, &event->mutex); + } #endif } diff --git a/limelight-common/PlatformThreads.h b/limelight-common/PlatformThreads.h index 732a8e8..9326cf8 100644 --- a/limelight-common/PlatformThreads.h +++ b/limelight-common/PlatformThreads.h @@ -9,22 +9,29 @@ typedef HANDLE PLT_THREAD; typedef HANDLE PLT_MUTEX; typedef HANDLE PLT_EVENT; #else +typedef pthread_t PLT_THREAD; +typedef pthread_mutex_t PLT_MUTEX; +typedef struct _PLT_EVENT { + pthread_mutex_t mutex; + pthread_cond_t cond; + int signalled; +} PLT_EVENT; #endif int PltCreateMutex(PLT_MUTEX *mutex); -void PltDeleteMutex(PLT_MUTEX mutex); -void PltLockMutex(PLT_MUTEX mutex); -void PltUnlockMutex(PLT_MUTEX mutex); +void PltDeleteMutex(PLT_MUTEX *mutex); +void PltLockMutex(PLT_MUTEX *mutex); +void PltUnlockMutex(PLT_MUTEX *mutex); int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD *thread); -void PltCloseThread(PLT_THREAD thread); -void PltJoinThread(PLT_THREAD thread); +void PltCloseThread(PLT_THREAD *thread); +void PltJoinThread(PLT_THREAD *thread); int PltCreateEvent(PLT_EVENT *event); -void PltCloseEvent(PLT_EVENT event); -void PltSetEvent(PLT_EVENT event); -void PltClearEvent(PLT_EVENT event); -void PltPulseEvent(PLT_EVENT event); -void PltWaitForEvent(PLT_EVENT event); +void PltCloseEvent(PLT_EVENT *event); +void PltSetEvent(PLT_EVENT *event); +void PltClearEvent(PLT_EVENT *event); +void PltPulseEvent(PLT_EVENT *event); +void PltWaitForEvent(PLT_EVENT *event); void PltSleepMs(int ms); \ No newline at end of file