Skip to content

N3DS: Semaphore fixes. #6776

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

Merged
merged 5 commits into from
Dec 11, 2022
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
1 change: 1 addition & 0 deletions docs/README-n3ds.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ cmake --install build
- SDL3_main should be used to ensure ROMFS is enabled.
- By default, the extra L2 cache and higher clock speeds of the New 2/3DS lineup are enabled. If you wish to turn it off, use `osSetSpeedupEnable(false)` in your main function.
- `SDL_GetBasePath` returns the romfs root instead of the executable's directory.
- The Nintendo 3DS uses a cooperative threading model on a single core, meaning a thread will never yield unless done manually through the `SDL_Delay` functions, or blocking waits (`SDL_LockMutex`, `SDL_SemWait`, `SDL_CondWait`, `SDL_WaitThread`). To avoid starving other threads, `SDL_SemTryWait` and `SDL_SemWaitTimeout` will yield if they fail to acquire the semaphore, see https://github.com/libsdl-org/SDL/pull/6776 for more information.
53 changes: 28 additions & 25 deletions src/thread/n3ds/SDL_syssem.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@

#include <3ds.h>

int WaitOnSemaphoreFor(SDL_sem *sem, Sint64 timeout);

struct SDL_semaphore
{
LightSemaphore semaphore;
};

SDL_sem *
SDL_CreateSemaphore(Uint32 initial_value)
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem;

Expand All @@ -57,44 +58,46 @@ SDL_CreateSemaphore(Uint32 initial_value)
*/
void SDL_DestroySemaphore(SDL_sem *sem)
{
if (sem) {
SDL_free(sem);
}
SDL_free(sem);
}

int SDL_SemWaitTimeoutNS(SDL_sem *sem, Sint64 timeoutNS)
{
int retval;

if (sem == NULL) {
return SDL_InvalidParamError("sem");
}

if (timeoutNS == SDL_MUTEX_MAXWAIT) {
LightSemaphore_Acquire(&sem->semaphore, 1);
retval = 0;
} else {
int return_code = LightSemaphore_TryAcquire(&sem->semaphore, 1);
if (return_code != 0) {
/* FIXME: Does this code guarantee a wall clock timeout here?
* Can we handle sub-millisecond delays? */
u32 timeout = (u32)SDL_NS_TO_MS(timeoutNS);
for (u32 i = 0; i < timeout; i++) {
svcSleepThread(1000000LL);
return_code = LightSemaphore_TryAcquire(&sem->semaphore, 1);
if (return_code == 0) {
break;
}
}
return 0;
}

if (LightSemaphore_TryAcquire(&sem->semaphore, 1) != 0) {
return WaitOnSemaphoreFor(sem, timeoutNS);
}

return 0;
}

int WaitOnSemaphoreFor(SDL_sem *sem, Sint64 timeout)
{
Uint64 stop_time = SDL_GetTicksNS() + timeout;
Uint64 current_time = SDL_GetTicksNS();
while (current_time < stop_time) {
if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) {
return 0;
}
retval = return_code != 0 ? SDL_MUTEX_TIMEDOUT : 0;
/* 100 microseconds seems to be the sweet spot */
SDL_DelayNS(SDL_US_TO_NS(100));
current_time = SDL_GetTicksNS();
}

return retval;
/* If we failed, yield to avoid starvation on busy waits */
SDL_DelayNS(1);
return SDL_MUTEX_TIMEDOUT;
}

Uint32
SDL_SemValue(SDL_sem *sem)
Uint32 SDL_SemValue(SDL_sem *sem)
{
if (sem == NULL) {
SDL_InvalidParamError("sem");
Expand Down
3 changes: 2 additions & 1 deletion src/thread/n3ds/SDL_systhread.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ static void ThreadEntry(void *arg)

int SDL_SYS_CreateThread(SDL_Thread *thread)
{
s32 priority = N3DS_THREAD_PRIORITY_MEDIUM;
s32 priority;
size_t stack_size = GetStackSize(thread->stacksize);
svcGetThreadPriority(&priority, CUR_THREAD_HANDLE);

thread->handle = threadCreate(ThreadEntry,
thread,
Expand Down
2 changes: 2 additions & 0 deletions test/testsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ TestOverheadContended(SDL_bool try_wait)
}
/* Make sure threads consumed everything */
while (SDL_SemValue(sem)) {
/* Friendlier with cooperative threading models */
SDL_DelayNS(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a longer delay, like 10 ms.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, maybe not, since we're trying to time the whole thing.

}
}
end_ticks = SDL_GetTicks();
Expand Down