windows-recmutex.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Plain recursive mutexes (native Windows implementation).
  2. Copyright (C) 2005-2020 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2005.
  14. Based on GCC's gthr-win32.h. */
  15. #include <config.h>
  16. /* Specification. */
  17. #include "windows-recmutex.h"
  18. #include <errno.h>
  19. void
  20. glwthread_recmutex_init (glwthread_recmutex_t *mutex)
  21. {
  22. mutex->owner = 0;
  23. mutex->depth = 0;
  24. InitializeCriticalSection (&mutex->lock);
  25. mutex->guard.done = 1;
  26. }
  27. int
  28. glwthread_recmutex_lock (glwthread_recmutex_t *mutex)
  29. {
  30. if (!mutex->guard.done)
  31. {
  32. if (InterlockedIncrement (&mutex->guard.started) == 0)
  33. /* This thread is the first one to need this mutex. Initialize it. */
  34. glwthread_recmutex_init (mutex);
  35. else
  36. {
  37. /* Don't let mutex->guard.started grow and wrap around. */
  38. InterlockedDecrement (&mutex->guard.started);
  39. /* Yield the CPU while waiting for another thread to finish
  40. initializing this mutex. */
  41. while (!mutex->guard.done)
  42. Sleep (0);
  43. }
  44. }
  45. {
  46. DWORD self = GetCurrentThreadId ();
  47. if (mutex->owner != self)
  48. {
  49. EnterCriticalSection (&mutex->lock);
  50. mutex->owner = self;
  51. }
  52. if (++(mutex->depth) == 0) /* wraparound? */
  53. {
  54. mutex->depth--;
  55. return EAGAIN;
  56. }
  57. }
  58. return 0;
  59. }
  60. int
  61. glwthread_recmutex_trylock (glwthread_recmutex_t *mutex)
  62. {
  63. if (!mutex->guard.done)
  64. {
  65. if (InterlockedIncrement (&mutex->guard.started) == 0)
  66. /* This thread is the first one to need this mutex. Initialize it. */
  67. glwthread_recmutex_init (mutex);
  68. else
  69. {
  70. /* Don't let mutex->guard.started grow and wrap around. */
  71. InterlockedDecrement (&mutex->guard.started);
  72. /* Let another thread finish initializing this mutex, and let it also
  73. lock this mutex. */
  74. return EBUSY;
  75. }
  76. }
  77. {
  78. DWORD self = GetCurrentThreadId ();
  79. if (mutex->owner != self)
  80. {
  81. if (!TryEnterCriticalSection (&mutex->lock))
  82. return EBUSY;
  83. mutex->owner = self;
  84. }
  85. if (++(mutex->depth) == 0) /* wraparound? */
  86. {
  87. mutex->depth--;
  88. return EAGAIN;
  89. }
  90. }
  91. return 0;
  92. }
  93. int
  94. glwthread_recmutex_unlock (glwthread_recmutex_t *mutex)
  95. {
  96. if (mutex->owner != GetCurrentThreadId ())
  97. return EPERM;
  98. if (mutex->depth == 0)
  99. return EINVAL;
  100. if (--(mutex->depth) == 0)
  101. {
  102. mutex->owner = 0;
  103. LeaveCriticalSection (&mutex->lock);
  104. }
  105. return 0;
  106. }
  107. int
  108. glwthread_recmutex_destroy (glwthread_recmutex_t *mutex)
  109. {
  110. if (mutex->owner != 0)
  111. return EBUSY;
  112. DeleteCriticalSection (&mutex->lock);
  113. mutex->guard.done = 0;
  114. return 0;
  115. }