1
0

xmlrpc_transport.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #undef PACKAGE
  27. #undef VERSION
  28. #include <stddef.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #ifdef WIN32
  32. #ifdef _DEBUG
  33. # include <crtdbg.h>
  34. # define new DEBUG_NEW
  35. # define malloc(size) _malloc_dbg( size, _NORMAL_BLOCK, __FILE__, __LINE__)
  36. # undef THIS_FILE
  37. static char THIS_FILE[] = __FILE__;
  38. #endif
  39. #endif /*WIN32*/
  40. #include "xmlrpc.h"
  41. #include "xmlrpc_client.h"
  42. #if defined (WIN32)
  43. #include <process.h>
  44. #endif
  45. /* For debugging the xmlrpc_transport threading. */
  46. /* #define tdbg_printf printf */
  47. #define tdbg_printf (void *)
  48. /* Lacking from the abyss/thread.c implimentaion. */
  49. void wait_for_asynch_thread(pthread_t *thread)
  50. {
  51. #if WIN32
  52. unsigned long milliseconds = INFINITE;
  53. switch (WaitForSingleObject (
  54. *thread /* handle to object to wait for */,
  55. milliseconds /* time-out interval in milliseconds*/) )
  56. {
  57. /* One may want to handle these cases */
  58. case WAIT_OBJECT_0:
  59. case WAIT_TIMEOUT:
  60. break;
  61. }
  62. #else
  63. void * result;
  64. int success;
  65. success = pthread_join (*thread, &result);
  66. #endif
  67. }
  68. /* MRB-WARNING: Only call when you have successfully
  69. ** acquired the Lock/Unlock mutex! */
  70. void unregister_asynch_thread (running_thread_list *list, pthread_t *thread)
  71. {
  72. running_thread_info * pCur = NULL;
  73. XMLRPC_ASSERT_PTR_OK(thread);
  74. XMLRPC_ASSERT_PTR_OK(list);
  75. tdbg_printf("unregister_asynch_thread: &pthread_id = %08X *(%08X)\n", thread, *thread);
  76. /* Removal */
  77. /* Lock (); */
  78. for (pCur = list->AsyncThreadHead; pCur != NULL; pCur = (running_thread_info *)pCur->Next)
  79. {
  80. if (pCur->_thread == *thread)
  81. {
  82. if (pCur == list->AsyncThreadHead)
  83. list->AsyncThreadHead = pCur->Next;
  84. if (pCur == list->AsyncThreadTail)
  85. list->AsyncThreadTail = pCur->Last;
  86. if (pCur->Last)
  87. ((running_thread_info *)(pCur->Last))->Next = pCur->Next;
  88. if (pCur->Next)
  89. ((running_thread_info *)(pCur->Next))->Last = pCur->Last;
  90. /* Free malloc'd running_thread_info */
  91. free (pCur);
  92. return;
  93. }
  94. }
  95. /* This is a serious progmatic error, since the thread
  96. ** should be in that list! */
  97. XMLRPC_ASSERT_PTR_OK(0x0000);
  98. /* Unlock (); */
  99. }
  100. /* MRB-WARNING: Only call when you have successfully
  101. ** acquired the Lock/Unlock mutex! */
  102. void register_asynch_thread (running_thread_list *list, pthread_t *thread)
  103. {
  104. running_thread_info* info = (running_thread_info *) malloc(sizeof(running_thread_info));
  105. XMLRPC_ASSERT_PTR_OK(thread);
  106. XMLRPC_ASSERT_PTR_OK(list);
  107. tdbg_printf("register_asynch_thread: &pthread_id = %08X *(%08X)\n", thread, *thread);
  108. info->_thread = *thread;
  109. /* Insertion */
  110. /* Lock (); */
  111. if (list->AsyncThreadHead == NULL)
  112. {
  113. list->AsyncThreadHead = list->AsyncThreadTail = info;
  114. list->AsyncThreadTail->Next = list->AsyncThreadHead->Next = NULL;
  115. list->AsyncThreadTail->Last = list->AsyncThreadHead->Last = NULL;
  116. }
  117. else
  118. {
  119. info->Last = list->AsyncThreadTail;
  120. list->AsyncThreadTail->Next = info;
  121. list->AsyncThreadTail = list->AsyncThreadTail->Next;
  122. list->AsyncThreadTail->Next = NULL;
  123. }
  124. /* Unlock (); */
  125. }