diff options
-rw-r--r-- | opl/opl_timer.c | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/opl/opl_timer.c b/opl/opl_timer.c index 519dbbd9..62ffbd37 100644 --- a/opl/opl_timer.c +++ b/opl/opl_timer.c @@ -149,19 +149,34 @@ static int ThreadFunction(void *unused) return 0; } -int OPL_Timer_StartThread(void) +static void InitResources(void) { - timer_thread_state = THREAD_STATE_RUNNING; - timer_thread = SDL_CreateThread(ThreadFunction, NULL); - timer_mutex = SDL_CreateMutex(); - callback_queue = OPL_Queue_Create(); + timer_mutex = SDL_CreateMutex(); callback_queue_mutex = SDL_CreateMutex(); +} + +static void FreeResources(void) +{ + OPL_Queue_Destroy(callback_queue); + SDL_DestroyMutex(callback_queue_mutex); + SDL_DestroyMutex(timer_mutex); +} + +int OPL_Timer_StartThread(void) +{ + InitResources(); + + timer_thread_state = THREAD_STATE_RUNNING; current_time = SDL_GetTicks(); + timer_thread = SDL_CreateThread(ThreadFunction, NULL); + if (timer_thread == NULL) { timer_thread_state = THREAD_STATE_STOPPED; + FreeResources(); + return 0; } @@ -176,6 +191,8 @@ void OPL_Timer_StopThread(void) { SDL_Delay(1); } + + FreeResources(); } void OPL_Timer_SetCallback(unsigned int ms, opl_callback_t callback, void *data) |