SDL_sysmutex.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_THREAD_WINDOWS
  20. /* Mutex functions using the Win32 API */
  21. #include "../../core/windows/SDL_windows.h"
  22. #include "SDL_mutex.h"
  23. struct SDL_mutex
  24. {
  25. CRITICAL_SECTION cs;
  26. };
  27. /* Create a mutex */
  28. SDL_mutex *
  29. SDL_CreateMutex(void)
  30. {
  31. SDL_mutex *mutex;
  32. /* Allocate mutex memory */
  33. mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
  34. if (mutex) {
  35. /* Initialize */
  36. /* On SMP systems, a non-zero spin count generally helps performance */
  37. #if __WINRT__
  38. InitializeCriticalSectionEx(&mutex->cs, 2000, 0);
  39. #else
  40. InitializeCriticalSectionAndSpinCount(&mutex->cs, 2000);
  41. #endif
  42. } else {
  43. SDL_OutOfMemory();
  44. }
  45. return (mutex);
  46. }
  47. /* Free the mutex */
  48. void
  49. SDL_DestroyMutex(SDL_mutex * mutex)
  50. {
  51. if (mutex) {
  52. DeleteCriticalSection(&mutex->cs);
  53. SDL_free(mutex);
  54. }
  55. }
  56. /* Lock the mutex */
  57. int
  58. SDL_LockMutex(SDL_mutex * mutex)
  59. {
  60. if (mutex == NULL) {
  61. return SDL_SetError("Passed a NULL mutex");
  62. }
  63. EnterCriticalSection(&mutex->cs);
  64. return (0);
  65. }
  66. /* TryLock the mutex */
  67. int
  68. SDL_TryLockMutex(SDL_mutex * mutex)
  69. {
  70. int retval = 0;
  71. if (mutex == NULL) {
  72. return SDL_SetError("Passed a NULL mutex");
  73. }
  74. if (TryEnterCriticalSection(&mutex->cs) == 0) {
  75. retval = SDL_MUTEX_TIMEDOUT;
  76. }
  77. return retval;
  78. }
  79. /* Unlock the mutex */
  80. int
  81. SDL_UnlockMutex(SDL_mutex * mutex)
  82. {
  83. if (mutex == NULL) {
  84. return SDL_SetError("Passed a NULL mutex");
  85. }
  86. LeaveCriticalSection(&mutex->cs);
  87. return (0);
  88. }
  89. #endif /* SDL_THREAD_WINDOWS */
  90. /* vi: set ts=4 sw=4 expandtab: */