curl_threads.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #if defined(USE_THREADS_POSIX)
  25. # ifdef HAVE_PTHREAD_H
  26. # include <pthread.h>
  27. # endif
  28. #elif defined(USE_THREADS_WIN32)
  29. # ifdef HAVE_PROCESS_H
  30. # include <process.h>
  31. # endif
  32. #endif
  33. #include "curl_threads.h"
  34. #define _MPRINTF_REPLACE /* use our functions only */
  35. #include <curl/mprintf.h>
  36. #include "curl_memory.h"
  37. /* The last #include file should be: */
  38. #include "memdebug.h"
  39. #if defined(USE_THREADS_POSIX)
  40. struct curl_actual_call {
  41. unsigned int (*func)(void *);
  42. void *arg;
  43. };
  44. static void *curl_thread_create_thunk(void *arg)
  45. {
  46. struct curl_actual_call * ac = arg;
  47. unsigned int (*func)(void *) = ac->func;
  48. void *real_arg = ac->arg;
  49. free(ac);
  50. (*func)(real_arg);
  51. return 0;
  52. }
  53. curl_thread_t Curl_thread_create(unsigned int (*func) (void*), void *arg)
  54. {
  55. curl_thread_t t;
  56. struct curl_actual_call *ac = malloc(sizeof(struct curl_actual_call));
  57. if(!ac)
  58. return curl_thread_t_null;
  59. ac->func = func;
  60. ac->arg = arg;
  61. if(pthread_create(&t, NULL, curl_thread_create_thunk, ac) != 0) {
  62. free(ac);
  63. return curl_thread_t_null;
  64. }
  65. return t;
  66. }
  67. void Curl_thread_destroy(curl_thread_t hnd)
  68. {
  69. if(hnd != curl_thread_t_null)
  70. pthread_detach(hnd);
  71. }
  72. int Curl_thread_join(curl_thread_t *hnd)
  73. {
  74. int ret = (pthread_join(*hnd, NULL) == 0);
  75. *hnd = curl_thread_t_null;
  76. return ret;
  77. }
  78. #elif defined(USE_THREADS_WIN32)
  79. curl_thread_t Curl_thread_create(unsigned int (CURL_STDCALL *func) (void*), void *arg)
  80. {
  81. #ifdef _WIN32_WCE
  82. return CreateThread(NULL, 0, func, arg, 0, NULL);
  83. #else
  84. curl_thread_t t;
  85. t = (curl_thread_t)_beginthreadex(NULL, 0, func, arg, 0, NULL);
  86. if((t == 0) || (t == (curl_thread_t)-1L))
  87. return curl_thread_t_null;
  88. return t;
  89. #endif
  90. }
  91. void Curl_thread_destroy(curl_thread_t hnd)
  92. {
  93. CloseHandle(hnd);
  94. }
  95. int Curl_thread_join(curl_thread_t *hnd)
  96. {
  97. int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
  98. Curl_thread_destroy(*hnd);
  99. *hnd = curl_thread_t_null;
  100. return ret;
  101. }
  102. #endif /* USE_THREADS_* */