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)),
66 bool condition =
false;
69 data.resource = std::move(m_resource);
70 data.threadFunction = m_threadFunction;
73 data.condition = &condition;
75 SDL_LockMutex(*mutex);
77 SDL_CreateThread(Run, !m_name.empty() ? m_name.c_str() :
nullptr,
reinterpret_cast<void*
>(&data));
81 SDL_CondWait(*cond, *mutex);
84 SDL_UnlockMutex(*mutex);
88 static int Run(
void* data)
90 ThreadFunctionPtr threadFunction =
nullptr;
91 ResourceUPtr resource;
93 ThreadData* threadData =
reinterpret_cast<ThreadData*
>(data);
94 SDL_LockMutex(**threadData->mutex);
96 threadFunction = threadData->threadFunction;
97 resource = std::move(threadData->resource);
99 *threadData->condition =
true;
100 SDL_CondSignal(**threadData->cond);
101 SDL_UnlockMutex(**threadData->mutex);
103 threadFunction(std::move(resource));
110 ResourceUPtr resource;
113 bool* condition =
nullptr;
114 ThreadFunctionPtr threadFunction =
nullptr;
117 ThreadFunctionPtr m_threadFunction;
118 ResourceUPtr m_resource;
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:28
Wrapper for safe creation/deletion of SDL_mutex.
Definition: sdl_mutex_wrapper.h:28