22 #include "common/thread/sdl_cond_wrapper.h"
23 #include "common/thread/sdl_mutex_wrapper.h"
25 #include <SDL_thread.h>
49 template<
typename Resource>
53 using ResourceUPtr = std::unique_ptr<Resource>;
54 using ThreadFunctionPtr = void(*)(ResourceUPtr);
57 : m_threadFunction(threadFunction),
58 m_resource(std::move(resource)),
64 SDL_DetachThread(m_thread);
71 bool condition =
false;
74 data.resource = std::move(m_resource);
75 data.threadFunction = m_threadFunction;
78 data.condition = &condition;
80 SDL_LockMutex(*mutex);
82 m_thread = SDL_CreateThread(Run, !m_name.empty() ? m_name.c_str() :
nullptr,
reinterpret_cast<void*
>(&data));
86 SDL_CondWait(*cond, *mutex);
89 SDL_UnlockMutex(*mutex);
94 if (m_thread ==
nullptr)
return;
95 SDL_WaitThread(m_thread,
nullptr);
100 static int Run(
void* data)
102 ThreadFunctionPtr threadFunction =
nullptr;
103 ResourceUPtr resource;
105 ThreadData* threadData =
reinterpret_cast<ThreadData*
>(data);
106 SDL_LockMutex(**threadData->mutex);
108 threadFunction = threadData->threadFunction;
109 resource = std::move(threadData->resource);
111 *threadData->condition =
true;
112 SDL_CondSignal(**threadData->cond);
113 SDL_UnlockMutex(**threadData->mutex);
115 threadFunction(std::move(resource));
122 ResourceUPtr resource;
125 bool* condition =
nullptr;
126 ThreadFunctionPtr threadFunction =
nullptr;
129 ThreadFunctionPtr m_threadFunction;
130 ResourceUPtr m_resource;
132 SDL_Thread* m_thread =
nullptr;
Wrapper around SDL thread allowing passing of resources in safe manner.
Definition: resource_owning_thread.h:50
Wrapper for safe creation/deletion of SDL_cond.
Definition: sdl_cond_wrapper.h:30
Wrapper for safe creation/deletion of SDL_mutex.
Definition: sdl_mutex_wrapper.h:28