22 #include "common/thread/sdl_cond_wrapper.h"
23 #include "common/thread/sdl_mutex_wrapper.h"
25 #include <SDL_thread.h>
48 template<
typename Resource>
52 using ResourceUPtr = std::unique_ptr<Resource>;
53 using ThreadFunctionPtr = void(*)(ResourceUPtr);
56 : m_threadFunction(threadFunction),
57 m_resource(std::move(resource))
64 bool condition =
false;
67 data.resource = std::move(m_resource);
68 data.threadFunction = m_threadFunction;
71 data.condition = &condition;
73 SDL_LockMutex(*mutex);
75 SDL_CreateThread(Run, reinterpret_cast<void*>(&data));
79 SDL_CondWait(*cond, *mutex);
82 SDL_UnlockMutex(*mutex);
86 static int Run(
void* data)
88 ThreadFunctionPtr threadFunction =
nullptr;
89 ResourceUPtr resource;
91 ThreadData* threadData =
reinterpret_cast<ThreadData*
>(data);
92 SDL_LockMutex(**threadData->mutex);
94 threadFunction = threadData->threadFunction;
95 resource = std::move(threadData->resource);
97 *threadData->condition =
true;
98 SDL_CondSignal(**threadData->cond);
99 SDL_UnlockMutex(**threadData->mutex);
101 threadFunction(std::move(resource));
108 ResourceUPtr resource;
111 bool* condition =
nullptr;
112 ThreadFunctionPtr threadFunction =
nullptr;
115 ThreadFunctionPtr m_threadFunction;
116 ResourceUPtr m_resource;
Wrapper around SDL thread allowing passing of resources in safe manner.
Definition: resource_owning_thread.h:49
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