Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement detached threads #88

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading