pthreadx_win32.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
  2. **
  3. ** Redistribution and use in source and binary forms, with or without
  4. ** modification, are permitted provided that the following conditions
  5. ** are met:
  6. ** 1. Redistributions of source code must retain the above copyright
  7. ** notice, this list of conditions and the following disclaimer.
  8. ** 2. Redistributions in binary form must reproduce the above copyright
  9. ** notice, this list of conditions and the following disclaimer in the
  10. ** documentation and/or other materials provided with the distribution.
  11. ** 3. The name of the author may not be used to endorse or promote products
  12. ** derived from this software without specific prior written permission.
  13. **
  14. ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ** SUCH DAMAGE. */
  25. #include "xmlrpc_config.h"
  26. #include <process.h>
  27. #include "mallocvar.h"
  28. #include "pthreadx.h"
  29. #undef PACKAGE
  30. #undef VERSION
  31. struct winStartArg {
  32. pthread_func * func;
  33. void * arg;
  34. };
  35. static unsigned int __stdcall
  36. winThreadStart(void * const arg) {
  37. /*----------------------------------------------------------------------------
  38. This is a thread start/root function for the Windows threading facility
  39. (i.e. this can be an argument to _beginthreadex()).
  40. All we do is call the real start/root function, which expects to be
  41. called in the pthread format.
  42. -----------------------------------------------------------------------------*/
  43. struct winStartArg * const winStartArgP = arg;
  44. winStartArgP->func(winStartArgP->arg);
  45. free(winStartArgP);
  46. return 0;
  47. }
  48. int
  49. pthread_create(pthread_t * const newThreadIdP,
  50. const pthread_attr_t * const attr,
  51. pthread_func * func,
  52. void * const arg) {
  53. HANDLE hThread;
  54. DWORD dwThreadID;
  55. struct winStartArg * winStartArgP;
  56. MALLOCVAR_NOFAIL(winStartArgP);
  57. winStartArgP->func = func;
  58. winStartArgP->arg = arg;
  59. hThread = (HANDLE) _beginthreadex(
  60. NULL, 0, &winThreadStart, (LPVOID)winStartArgP, CREATE_SUSPENDED,
  61. &dwThreadID);
  62. SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);
  63. ResumeThread(hThread);
  64. *newThreadIdP = hThread;
  65. return hThread ? 0 : -1;
  66. }
  67. /* Just kill it. */
  68. int
  69. pthread_cancel(pthread_t const target_thread) {
  70. CloseHandle(target_thread);
  71. return 0;
  72. }
  73. /* Waits for the thread to exit before continuing. */
  74. int
  75. pthread_join(pthread_t const target_thread,
  76. void ** const statusP) {
  77. DWORD dwResult = WaitForSingleObject(target_thread, INFINITE);
  78. *statusP = (void *)dwResult;
  79. return 0;
  80. }
  81. int
  82. pthread_detach(pthread_t const target_thread) {
  83. return 0;
  84. }
  85. int
  86. pthread_mutex_init(pthread_mutex_t * const mp,
  87. const pthread_mutexattr_t * const attr) {
  88. InitializeCriticalSection(mp);
  89. return 0;
  90. }
  91. int
  92. pthread_mutex_lock(pthread_mutex_t * const mp) {
  93. EnterCriticalSection(mp);
  94. return 0;
  95. }
  96. int
  97. pthread_mutex_unlock(pthread_mutex_t * const mp) {
  98. LeaveCriticalSection(mp);
  99. return 0;
  100. }
  101. int
  102. pthread_mutex_destroy(pthread_mutex_t * const mp) {
  103. DeleteCriticalSection(mp);
  104. return 0;
  105. }