1 #include "compat.h"
2 #include "mutex.h"
3 
mutex_init(mutex_t * mutex)4 int32_t mutex_init(mutex_t *mutex)
5 {
6 #if SDL_MAJOR_VERSION >= 2
7     // SDL2's spinlock is most preferable
8     *mutex = 0;
9     return 0;
10 #elif defined _WIN32
11     // Windows' "Critical Section" is preferable to "Mutex" -- within a single process only
12     // https://preshing.com/20111124/always-use-a-lightweight-mutex/
13     InitializeCriticalSection(mutex);
14     return 0;
15 #elif SDL_MAJOR_VERSION == 1
16     if (mutex)
17     {
18         *mutex = SDL_CreateMutex();
19         if (*mutex != NULL)
20             return 0;
21     }
22     return -1;
23 #else
24     return -1;
25 #endif
26 }
27 
mutex_destroy(mutex_t * mutex)28 void mutex_destroy(mutex_t *mutex)
29 {
30 #if SDL_MAJOR_VERSION >= 2
31     *mutex = 0;
32 #elif defined _WIN32
33     DeleteCriticalSection(mutex);
34 #elif SDL_MAJOR_VERSION == 1
35     if (mutex)
36     {
37         SDL_DestroyMutex(*mutex);
38         *mutex = nullptr;
39     }
40 #endif
41 }
42