added semaphores

This commit is contained in:
KingOfSpadesJFK 2024-04-11 19:07:52 -07:00
parent 1154914e9a
commit 9317c77ecd
2 changed files with 15 additions and 4 deletions

View File

@ -591,7 +591,7 @@ void game_loop_one_iteration(void) {
osContStartReadData(&gSIEventMesgQueue); osContStartReadData(&gSIEventMesgQueue);
} }
// audio_game_loop_tick(); audio_game_loop_tick();
config_gfx_pool(); config_gfx_pool();
read_controller_inputs(); read_controller_inputs();
levelCommandAddr = level_script_execute(levelCommandAddr); levelCommandAddr = level_script_execute(levelCommandAddr);

View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details. #include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h> #include <pthread.h>
#include <semaphore.h>
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
@ -60,8 +61,10 @@ static struct AudioAPI *audio_api;
static struct GfxWindowManagerAPI *wm_api; static struct GfxWindowManagerAPI *wm_api;
static struct GfxRenderingAPI *rendering_api; static struct GfxRenderingAPI *rendering_api;
#define AUDIO_SEMAPHORE_VALUE 0
pthread_t audio_thread_id; pthread_t audio_thread_id;
bool audio_thread_running = false; bool audio_thread_running = false;
sem_t audio_sem;
extern void gfx_run(Gfx *commands); extern void gfx_run(Gfx *commands);
extern void thread5_game_loop(void *arg); extern void thread5_game_loop(void *arg);
@ -93,6 +96,7 @@ void send_display_list(struct SPTask *spTask) {
#endif #endif
void produce_one_frame(void) { void produce_one_frame(void) {
sem_wait(&audio_sem);
gfx_start_frame(); gfx_start_frame();
game_loop_one_iteration(); game_loop_one_iteration();
@ -115,7 +119,7 @@ void* audio_thread() {
set_sequence_player_volume(SEQ_PLAYER_SFX, (f32)configSfxVolume / 127.0f * master_mod); set_sequence_player_volume(SEQ_PLAYER_SFX, (f32)configSfxVolume / 127.0f * master_mod);
set_sequence_player_volume(SEQ_PLAYER_ENV, (f32)configEnvVolume / 127.0f * master_mod); set_sequence_player_volume(SEQ_PLAYER_ENV, (f32)configEnvVolume / 127.0f * master_mod);
audio_game_loop_tick(); // audio_game_loop_tick();
int samples_left = audio_api->buffered(); int samples_left = audio_api->buffered();
u32 num_audio_samples = samples_left < audio_api->get_desired_buffered() ? SAMPLES_HIGH : SAMPLES_LOW; u32 num_audio_samples = samples_left < audio_api->get_desired_buffered() ? SAMPLES_HIGH : SAMPLES_LOW;
@ -140,10 +144,17 @@ void* audio_thread() {
if (napResult == -1) { if (napResult == -1) {
printf("interrupted by a signal handler\n"); printf("interrupted by a signal handler\n");
} }
sem_post(&audio_sem);
} }
return NULL; return NULL;
} }
void audio_thread_init() {
audio_thread_running = true;
sem_init(&audio_sem, 0, AUDIO_SEMAPHORE_VALUE);
pthread_create(&audio_thread_id, NULL, audio_thread, NULL);
}
void audio_shutdown(void) { void audio_shutdown(void) {
if (audio_api) { if (audio_api) {
if (audio_api->shutdown) audio_api->shutdown(); if (audio_api->shutdown) audio_api->shutdown();
@ -151,6 +162,7 @@ void audio_shutdown(void) {
} }
audio_thread_running = false; audio_thread_running = false;
pthread_join(audio_thread_id, NULL); pthread_join(audio_thread_id, NULL);
sem_destroy(&audio_sem);
} }
void game_deinit(void) { void game_deinit(void) {
@ -291,8 +303,7 @@ void main_func(void) {
emscripten_set_main_loop(em_main_loop, 0, 0); emscripten_set_main_loop(em_main_loop, 0, 0);
request_anim_frame(on_anim_frame); request_anim_frame(on_anim_frame);
#else #else
audio_thread_running = true; audio_thread_init();
pthread_create(&audio_thread_id, NULL, audio_thread, NULL);
while (true) { while (true) {
wm_api->main_loop(produce_one_frame); wm_api->main_loop(produce_one_frame);
#ifdef DISCORDRPC #ifdef DISCORDRPC