You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. threads.c
  3. Portable threads and locks API implementation
  4. gSOAP XML Web services tools
  5. Copyright (C) 2000-2010, Robert van Engelen, Genivia Inc., All Rights Reserved.
  6. This part of the software is released under one of the following licenses:
  7. GPL, the gSOAP public license, or Genivia's license for commercial use.
  8. --------------------------------------------------------------------------------
  9. gSOAP public license.
  10. The contents of this file are subject to the gSOAP Public License Version 1.3
  11. (the "License"); you may not use this file except in compliance with the
  12. License. You may obtain a copy of the License at
  13. http://www.cs.fsu.edu/~engelen/soaplicense.html
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the License.
  17. The Initial Developer of the Original Code is Robert A. van Engelen.
  18. Copyright (C) 2000-2010, Robert van Engelen, Genivia Inc., All Rights Reserved.
  19. --------------------------------------------------------------------------------
  20. GPL license.
  21. This program is free software; you can redistribute it and/or modify it under
  22. the terms of the GNU General Public License as published by the Free Software
  23. Foundation; either version 2 of the License, or (at your option) any later
  24. version.
  25. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  26. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  27. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  28. You should have received a copy of the GNU General Public License along with
  29. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  30. Place, Suite 330, Boston, MA 02111-1307 USA
  31. Author contact information:
  32. engelen@genivia.com / engelen@acm.org
  33. This program is released under the GPL with the additional exemption that
  34. compiling, linking, and/or using OpenSSL is allowed.
  35. --------------------------------------------------------------------------------
  36. A commercial use license is available from Genivia, Inc., contact@genivia.com
  37. --------------------------------------------------------------------------------
  38. */
  39. #include "threads.h"
  40. #ifdef WIN32
  41. /******************************************************************************\
  42. *
  43. * Emulation of POSIX condition variables for WIN32
  44. *
  45. \******************************************************************************/
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. int emulate_pthread_mutex_lock(volatile MUTEX_TYPE *mx)
  50. {
  51. if (*mx == NULL) /* static initializer? */
  52. {
  53. HANDLE p = CreateMutex(NULL, FALSE, NULL);
  54. if (InterlockedCompareExchangePointer((PVOID*)mx, (PVOID)p, NULL) != NULL)
  55. CloseHandle(p);
  56. }
  57. return WaitForSingleObject(*mx, INFINITE) == WAIT_FAILED;
  58. }
  59. int emulate_pthread_cond_init(COND_TYPE *cv)
  60. {
  61. cv->waiters_count_ = 0;
  62. cv->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
  63. InitializeCriticalSection(&cv->waiters_count_lock_);
  64. return 0;
  65. }
  66. int emulate_pthread_cond_destroy(COND_TYPE *cv)
  67. {
  68. CloseHandle(cv->signal_event_);
  69. DeleteCriticalSection(&cv->waiters_count_lock_);
  70. return 0;
  71. }
  72. int emulate_pthread_cond_signal(COND_TYPE *cv)
  73. {
  74. int have_waiters;
  75. EnterCriticalSection(&cv->waiters_count_lock_);
  76. have_waiters = cv->waiters_count_ > 0;
  77. LeaveCriticalSection(&cv->waiters_count_lock_);
  78. if (have_waiters)
  79. return SetEvent(cv->signal_event_) == 0;
  80. return 0;
  81. }
  82. int emulate_pthread_cond_wait(COND_TYPE *cv, MUTEX_TYPE *cs)
  83. {
  84. int result;
  85. EnterCriticalSection(&cv->waiters_count_lock_);
  86. cv->waiters_count_++;
  87. LeaveCriticalSection(&cv->waiters_count_lock_);
  88. ReleaseMutex(*cs);
  89. result = (WaitForSingleObject(cv->signal_event_, INFINITE) == WAIT_FAILED);
  90. if (!result)
  91. {
  92. EnterCriticalSection(&cv->waiters_count_lock_);
  93. cv->waiters_count_--;
  94. LeaveCriticalSection(&cv->waiters_count_lock_);
  95. result = (WaitForSingleObject(*cs, INFINITE) == WAIT_FAILED);
  96. }
  97. return result;
  98. }
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif