mutex.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "precompile.h"
  2. #include "toolkit.h"
  3. #include "dbgutil.h"
  4. #ifdef _WIN32
  5. int toolkit_mutex_init(toolkit_mutex_t* mutex) {
  6. InitializeCriticalSection(mutex);
  7. return 0;
  8. }
  9. int toolkit_mutex_init_recursive(toolkit_mutex_t* mutex) {
  10. return toolkit_mutex_init(mutex);
  11. }
  12. void toolkit_mutex_destroy(toolkit_mutex_t* mutex) {
  13. DeleteCriticalSection(mutex);
  14. }
  15. void toolkit_mutex_lock(toolkit_mutex_t* mutex) {
  16. EnterCriticalSection(mutex);
  17. }
  18. int toolkit_mutex_trylock(toolkit_mutex_t* mutex) {
  19. if (TryEnterCriticalSection(mutex))
  20. return 0;
  21. else
  22. return TOOLKIT_EBUSY;
  23. }
  24. void toolkit_mutex_unlock(toolkit_mutex_t* mutex) {
  25. LeaveCriticalSection(mutex);
  26. }
  27. static void toolkit__once_inner(toolkit_once_t* guard, void (*callback)(void))
  28. {
  29. DWORD result;
  30. HANDLE existing_event, created_event;
  31. created_event = CreateEvent(NULL, 1, 0, NULL);
  32. if (created_event == 0) {
  33. /* Could fail in a low-memory situation? */
  34. abort();
  35. }
  36. existing_event = InterlockedCompareExchangePointer(&guard->event,
  37. created_event,
  38. NULL);
  39. if (existing_event == NULL) {
  40. /* We won the race */
  41. callback();
  42. result = SetEvent(created_event);
  43. TOOLKIT_ASSERT(result);
  44. guard->ran = 1;
  45. }
  46. else {
  47. /* We lost the race. Destroy the event we created and wait for the existing
  48. * one to become signaled. */
  49. CloseHandle(created_event);
  50. result = WaitForSingleObject(existing_event, INFINITE);
  51. TOOLKIT_ASSERT(result == WAIT_OBJECT_0);
  52. }
  53. }
  54. void toolkit_once(toolkit_once_t* guard, void (*callback)(void))
  55. {
  56. /* Fast case - avoid WaitForSingleObject. */
  57. if (guard->ran) {
  58. return;
  59. }
  60. toolkit__once_inner(guard, callback);
  61. }
  62. #endif //_WIN32