mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-06-17 14:21:30 +00:00
Simplify thread management since we don't need thread alerts to cancel anymore
This commit is contained in:
Regular → Executable
+8
-106
@@ -4,13 +4,13 @@
|
|||||||
int initializePlatformSockets(void);
|
int initializePlatformSockets(void);
|
||||||
void cleanupPlatformSockets(void);
|
void cleanupPlatformSockets(void);
|
||||||
|
|
||||||
|
|
||||||
struct thread_context {
|
struct thread_context {
|
||||||
ThreadEntry entry;
|
ThreadEntry entry;
|
||||||
void* context;
|
void* context;
|
||||||
PLT_THREAD* thread;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int running_threads = 0;
|
||||||
|
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
void LimelogWindows(char* Format, ...) {
|
void LimelogWindows(char* Format, ...) {
|
||||||
va_list va;
|
va_list va;
|
||||||
@@ -24,9 +24,6 @@ void LimelogWindows(char* Format, ...) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
PLT_MUTEX thread_list_lock;
|
|
||||||
PLT_THREAD* thread_head;
|
|
||||||
|
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
|
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
|
||||||
struct thread_context* ctx = (struct thread_context*)lpParameter;
|
struct thread_context* ctx = (struct thread_context*)lpParameter;
|
||||||
@@ -35,15 +32,6 @@ void* ThreadProc(void* context) {
|
|||||||
struct thread_context* ctx = (struct thread_context*)context;
|
struct thread_context* ctx = (struct thread_context*)context;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Add this thread to the thread list
|
|
||||||
PltLockMutex(&thread_list_lock);
|
|
||||||
ctx->thread->next = thread_head;
|
|
||||||
thread_head = ctx->thread;
|
|
||||||
PltUnlockMutex(&thread_list_lock);
|
|
||||||
|
|
||||||
// Signal the event since the thread is now inserted
|
|
||||||
PltSetEvent(&ctx->thread->insertedEvent);
|
|
||||||
|
|
||||||
ctx->entry(ctx->context);
|
ctx->entry(ctx->context);
|
||||||
|
|
||||||
free(ctx);
|
free(ctx);
|
||||||
@@ -64,31 +52,6 @@ void PltSleepMs(int ms) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(LC_WINDOWS)
|
|
||||||
static PLT_THREAD* findCurrentThread(void) {
|
|
||||||
PLT_THREAD* current_thread;
|
|
||||||
|
|
||||||
PltLockMutex(&thread_list_lock);
|
|
||||||
current_thread = thread_head;
|
|
||||||
while (current_thread != NULL) {
|
|
||||||
#if defined(LC_WINDOWS)
|
|
||||||
if (current_thread->tid == GetCurrentThreadId()) {
|
|
||||||
#else
|
|
||||||
if (pthread_equal(current_thread->thread, pthread_self())) {
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
current_thread = current_thread->next;
|
|
||||||
}
|
|
||||||
PltUnlockMutex(&thread_list_lock);
|
|
||||||
|
|
||||||
LC_ASSERT(current_thread != NULL);
|
|
||||||
|
|
||||||
return current_thread;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int PltCreateMutex(PLT_MUTEX* mutex) {
|
int PltCreateMutex(PLT_MUTEX* mutex) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
*mutex = CreateMutexEx(NULL, NULL, 0, MUTEX_ALL_ACCESS);
|
*mutex = CreateMutexEx(NULL, NULL, 0, MUTEX_ALL_ACCESS);
|
||||||
@@ -139,39 +102,8 @@ void PltJoinThread(PLT_THREAD* thread) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PltCloseThread(PLT_THREAD* thread) {
|
void PltCloseThread(PLT_THREAD* thread) {
|
||||||
PLT_THREAD* current_thread;
|
running_threads--;
|
||||||
|
|
||||||
PltLockMutex(&thread_list_lock);
|
|
||||||
|
|
||||||
if (thread_head == thread)
|
|
||||||
{
|
|
||||||
// Remove the thread from the head
|
|
||||||
thread_head = thread_head->next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Find the thread in the list
|
|
||||||
current_thread = thread_head;
|
|
||||||
while (current_thread != NULL) {
|
|
||||||
if (current_thread->next == thread) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
current_thread = current_thread->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
LC_ASSERT(current_thread != NULL);
|
|
||||||
|
|
||||||
// Unlink this thread
|
|
||||||
current_thread->next = thread->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
PltUnlockMutex(&thread_list_lock);
|
|
||||||
|
|
||||||
PltCloseEvent(&thread->insertedEvent);
|
|
||||||
|
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
CloseHandle(thread->termRequested);
|
|
||||||
CloseHandle(thread->handle);
|
CloseHandle(thread->handle);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -189,7 +121,6 @@ void PltInterruptThread(PLT_THREAD* thread) {
|
|||||||
|
|
||||||
int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD* thread) {
|
int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD* thread) {
|
||||||
struct thread_context* ctx;
|
struct thread_context* ctx;
|
||||||
int err;
|
|
||||||
|
|
||||||
ctx = (struct thread_context*)malloc(sizeof(*ctx));
|
ctx = (struct thread_context*)malloc(sizeof(*ctx));
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
@@ -198,49 +129,28 @@ int PltCreateThread(ThreadEntry entry, void* context, PLT_THREAD* thread) {
|
|||||||
|
|
||||||
ctx->entry = entry;
|
ctx->entry = entry;
|
||||||
ctx->context = context;
|
ctx->context = context;
|
||||||
ctx->thread = thread;
|
|
||||||
|
|
||||||
err = PltCreateEvent(&thread->insertedEvent);
|
|
||||||
if (err != 0) {
|
|
||||||
free(ctx);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
thread->cancelled = 0;
|
thread->cancelled = 0;
|
||||||
|
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
{
|
{
|
||||||
thread->termRequested = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
|
|
||||||
if (thread->termRequested == NULL) {
|
|
||||||
PltCloseEvent(&thread->insertedEvent);
|
|
||||||
free(ctx);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
thread->handle = CreateThread(NULL, 0, ThreadProc, ctx, 0, &thread->tid);
|
thread->handle = CreateThread(NULL, 0, ThreadProc, ctx, 0, &thread->tid);
|
||||||
if (thread->handle == NULL) {
|
if (thread->handle == NULL) {
|
||||||
PltCloseEvent(&thread->insertedEvent);
|
|
||||||
CloseHandle(thread->termRequested);
|
|
||||||
free(ctx);
|
free(ctx);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
err = pthread_create(&thread->thread, NULL, ThreadProc, ctx);
|
int err = pthread_create(&thread->thread, NULL, ThreadProc, ctx);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
PltCloseEvent(&thread->insertedEvent);
|
|
||||||
free(ctx);
|
free(ctx);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// We shouldn't get this far with an error
|
running_threads++;
|
||||||
LC_ASSERT(err == 0);
|
|
||||||
|
|
||||||
// Wait for the thread to be started and inserted into the active threads list
|
|
||||||
PltWaitForEvent(&thread->insertedEvent);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -290,17 +200,11 @@ void PltClearEvent(PLT_EVENT* event) {
|
|||||||
int PltWaitForEvent(PLT_EVENT* event) {
|
int PltWaitForEvent(PLT_EVENT* event) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
DWORD error;
|
DWORD error;
|
||||||
HANDLE objects[2];
|
|
||||||
|
|
||||||
objects[0] = *event;
|
error = WaitForSingleObjectEx(*event, INFINITE, FALSE);
|
||||||
objects[1] = findCurrentThread()->termRequested;
|
|
||||||
error = WaitForMultipleObjectsEx(2, objects, FALSE, INFINITE, FALSE);
|
|
||||||
if (error == WAIT_OBJECT_0) {
|
if (error == WAIT_OBJECT_0) {
|
||||||
return PLT_WAIT_SUCCESS;
|
return PLT_WAIT_SUCCESS;
|
||||||
}
|
}
|
||||||
else if (error == WAIT_OBJECT_0 + 1) {
|
|
||||||
return PLT_WAIT_INTERRUPTED;
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
LC_ASSERT(0);
|
LC_ASSERT(0);
|
||||||
return -1;
|
return -1;
|
||||||
@@ -336,13 +240,11 @@ int initializePlatform(void) {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
return PltCreateMutex(&thread_list_lock);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanupPlatform(void) {
|
void cleanupPlatform(void) {
|
||||||
cleanupPlatformSockets();
|
cleanupPlatformSockets();
|
||||||
|
|
||||||
LC_ASSERT(thread_head == NULL);
|
LC_ASSERT(running_threads == 0);
|
||||||
|
|
||||||
PltDeleteMutex(&thread_list_lock);
|
|
||||||
}
|
}
|
||||||
Regular → Executable
-8
@@ -11,11 +11,6 @@ typedef HANDLE PLT_EVENT;
|
|||||||
typedef struct _PLT_THREAD {
|
typedef struct _PLT_THREAD {
|
||||||
HANDLE handle;
|
HANDLE handle;
|
||||||
int cancelled;
|
int cancelled;
|
||||||
DWORD tid;
|
|
||||||
HANDLE termRequested;
|
|
||||||
PLT_EVENT insertedEvent;
|
|
||||||
|
|
||||||
struct _PLT_THREAD* next;
|
|
||||||
} PLT_THREAD;
|
} PLT_THREAD;
|
||||||
#elif defined (LC_POSIX)
|
#elif defined (LC_POSIX)
|
||||||
typedef pthread_mutex_t PLT_MUTEX;
|
typedef pthread_mutex_t PLT_MUTEX;
|
||||||
@@ -27,9 +22,6 @@ typedef struct _PLT_EVENT {
|
|||||||
typedef struct _PLT_THREAD {
|
typedef struct _PLT_THREAD {
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
int cancelled;
|
int cancelled;
|
||||||
PLT_EVENT insertedEvent;
|
|
||||||
|
|
||||||
struct _PLT_THREAD* next;
|
|
||||||
} PLT_THREAD;
|
} PLT_THREAD;
|
||||||
#else
|
#else
|
||||||
#error Unsupported platform
|
#error Unsupported platform
|
||||||
|
|||||||
Reference in New Issue
Block a user