Skip to content

Commit

Permalink
Implement detached threads (#88)
Browse files Browse the repository at this point in the history
The `AsyncTerm` thread is never joined but its handle is immediately closed. This works fine on Windows, but causes issues on other platforms where threads need to be explicitly detached if never joined (pthread, wiiu).

This adds a new `PltDetachThread` function, which can be used instead of `PltCloseThread` to explictly detach a thread, requiring it to no longer be closed.
  • Loading branch information
GaryOderNichts authored Feb 17, 2024
1 parent ec171fd commit 9545dd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ static void ClInternalConnectionTerminated(int errorCode)
LC_ASSERT(err == 0);
}

// Close the thread handle since we can never wait on it
PltCloseThread(&terminationCallbackThread);
// Detach the thread since we never wait on it
PltDetachThread(&terminationCallbackThread);
}

static bool parseRtspPortNumberFromUrl(const char* rtspSessionUrl, uint16_t* port)
Expand Down
43 changes: 39 additions & 4 deletions src/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,43 @@ void PltJoinThread(PLT_THREAD* thread) {
OSJoinThread(&thread->thread, NULL);
#elif defined(__3DS__)
threadJoin(thread->thread, U64_MAX);
threadFree(thread->thread);
#else
pthread_join(thread->thread, NULL);
#endif
}

void PltDetachThread(PLT_THREAD* thread)
{
// Assume detached threads are no longer active
activeThreads--;

#if defined(LC_WINDOWS)
// According MSDN:
// "Closing a thread handle does not terminate the associated thread or remove the thread object."
CloseHandle(thread->handle);
#elif defined(__vita__)
sceKernelDeleteThread(thread->handle);
#elif defined(__WIIU__)
OSDetachThread(&thread->thread);
#elif defined(__3DS__)
threadDetach(thread->thread);
#else
pthread_detach(thread->thread);
#endif
}

void PltCloseThread(PLT_THREAD* thread) {
activeThreads--;
#if defined(LC_WINDOWS)
CloseHandle(thread->handle);
#elif defined(__vita__)
sceKernelDeleteThread(thread->handle);
#elif defined(__WIIU__)
// Thread is automatically closed after join
#elif defined(__3DS__)
threadFree(thread->thread);
#else
// Thread is automatically closed after join
#endif
}

Expand Down Expand Up @@ -262,19 +287,29 @@ int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THRE
sceKernelStartThread(thread->handle, sizeof(struct thread_context), ctx);
}
#elif defined(__WIIU__)
int stack_size = 4 * 1024 * 1024;
void* stack_addr = (uint8_t *)memalign(8, stack_size) + stack_size;
memset(&thread->thread, 0, sizeof(thread->thread));

// Allocate stack
const int stack_size = 4 * 1024 * 1024;
uint8_t* stack = (uint8_t*)memalign(16, stack_size);
if (stack == NULL) {
free(ctx);
return -1;
}

// Create thread
if (!OSCreateThread(&thread->thread,
ThreadProc,
0, (char*)ctx,
stack_addr, stack_size,
stack + stack_size, stack_size,
0x10, OS_THREAD_ATTRIB_AFFINITY_ANY))
{
free(ctx);
free(stack);
return -1;
}

OSSetThreadName(&thread->thread, name);
OSSetThreadDeallocator(&thread->thread, thread_deallocator);
OSResumeThread(&thread->thread);
#elif defined(__3DS__)
Expand Down
1 change: 1 addition & 0 deletions src/PlatformThreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void PltCloseThread(PLT_THREAD* thread);
void PltInterruptThread(PLT_THREAD* thread);
bool PltIsThreadInterrupted(PLT_THREAD* thread);
void PltJoinThread(PLT_THREAD* thread);
void PltDetachThread(PLT_THREAD* thread);

int PltCreateEvent(PLT_EVENT* event);
void PltCloseEvent(PLT_EVENT* event);
Expand Down

0 comments on commit 9545dd7

Please sign in to comment.