polarssl_threadlock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
  9. * Copyright (C) 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at http://curl.haxx.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. ***************************************************************************/
  23. #include "curl_setup.h"
  24. #if defined(USE_POLARSSL) && \
  25. (defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32))
  26. #if defined(USE_THREADS_POSIX)
  27. # ifdef HAVE_PTHREAD_H
  28. # include <pthread.h>
  29. # endif
  30. #elif defined(USE_THREADS_WIN32)
  31. # ifdef HAVE_PROCESS_H
  32. # include <process.h>
  33. # endif
  34. #endif
  35. #include "polarssl_threadlock.h"
  36. #define _MPRINTF_REPLACE /* use our functions only */
  37. #include <curl/mprintf.h>
  38. #include "curl_memory.h"
  39. /* The last #include file should be: */
  40. #include "memdebug.h"
  41. /* number of thread locks */
  42. #define NUMT 2
  43. /* This array will store all of the mutexes available to PolarSSL. */
  44. static POLARSSL_MUTEX_T *mutex_buf = NULL;
  45. int polarsslthreadlock_thread_setup(void)
  46. {
  47. int i;
  48. int ret;
  49. mutex_buf = malloc(NUMT * sizeof(POLARSSL_MUTEX_T));
  50. if(!mutex_buf)
  51. return 0; /* error, no number of threads defined */
  52. #ifdef HAVE_PTHREAD_H
  53. for(i = 0; i < NUMT; i++) {
  54. ret = pthread_mutex_init(&mutex_buf[i], NULL);
  55. if(ret)
  56. return 0; /* pthread_mutex_init failed */
  57. }
  58. #elif defined(HAVE_PROCESS_H)
  59. for(i = 0; i < NUMT; i++) {
  60. mutex_buf[i] = CreateMutex(0, FALSE, 0);
  61. if(mutex_buf[i] == 0)
  62. return 0; /* CreateMutex failed */
  63. }
  64. #endif /* HAVE_PTHREAD_H */
  65. return 1; /* OK */
  66. }
  67. int polarsslthreadlock_thread_cleanup(void)
  68. {
  69. int i;
  70. int ret;
  71. if(!mutex_buf)
  72. return 0; /* error, no threads locks defined */
  73. #ifdef HAVE_PTHREAD_H
  74. for(i = 0; i < NUMT; i++) {
  75. ret = pthread_mutex_destroy(&mutex_buf[i]);
  76. if(ret)
  77. return 0; /* pthread_mutex_destroy failed */
  78. }
  79. #elif defined(HAVE_PROCESS_H)
  80. for(i = 0; i < NUMT; i++) {
  81. ret = CloseHandle(mutex_buf[i]);
  82. if(!ret)
  83. return 0; /* CloseHandle failed */
  84. }
  85. #endif /* HAVE_PTHREAD_H */
  86. free(mutex_buf);
  87. mutex_buf = NULL;
  88. return 1; /* OK */
  89. }
  90. int polarsslthreadlock_lock_function(int n)
  91. {
  92. int ret;
  93. #ifdef HAVE_PTHREAD_H
  94. if(n < NUMT) {
  95. ret = pthread_mutex_lock(&mutex_buf[n]);
  96. if(ret) {
  97. DEBUGF(fprintf(stderr,
  98. "Error: polarsslthreadlock_lock_function failed\n"));
  99. return 0; /* pthread_mutex_lock failed */
  100. }
  101. }
  102. #elif defined(HAVE_PROCESS_H)
  103. if(n < NUMT) {
  104. ret = (WaitForSingleObject(mutex_buf[n], INFINITE)==WAIT_FAILED?1:0);
  105. if(ret) {
  106. DEBUGF(fprintf(stderr,
  107. "Error: polarsslthreadlock_lock_function failed\n"));
  108. return 0; /* pthread_mutex_lock failed */
  109. }
  110. }
  111. #endif /* HAVE_PTHREAD_H */
  112. return 1; /* OK */
  113. }
  114. int polarsslthreadlock_unlock_function(int n)
  115. {
  116. int ret;
  117. #ifdef HAVE_PTHREAD_H
  118. if(n < NUMT) {
  119. ret = pthread_mutex_unlock(&mutex_buf[n]);
  120. if(ret) {
  121. DEBUGF(fprintf(stderr,
  122. "Error: polarsslthreadlock_unlock_function failed\n"));
  123. return 0; /* pthread_mutex_unlock failed */
  124. }
  125. }
  126. #elif defined(HAVE_PROCESS_H)
  127. if(n < NUMT) {
  128. ret = ReleaseMutex(mutex_buf[n]);
  129. if(!ret) {
  130. DEBUGF(fprintf(stderr,
  131. "Error: polarsslthreadlock_unlock_function failed\n"));
  132. return 0; /* pthread_mutex_lock failed */
  133. }
  134. }
  135. #endif /* HAVE_PTHREAD_H */
  136. return 1; /* OK */
  137. }
  138. #endif /* USE_POLARSSL */