mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-04-05 07:16:02 +00:00
Implement POSIX support
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -3,4 +3,8 @@
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
#include <Windows.h>
|
||||
#define LastSocketError() WSAGetLastError()
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#define SOCKET int
|
||||
#define LastSocketError() errno
|
||||
#define INVALID_SOCKET -1
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user