polarssl_threadlock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2013-2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
  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 https://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) || defined(USE_MBEDTLS)) && \
  25. ((defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)) || \
  26. (defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)))
  27. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  28. # include <pthread.h>
  29. # define POLARSSL_MUTEX_T pthread_mutex_t
  30. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  31. # include <process.h>
  32. # define POLARSSL_MUTEX_T HANDLE
  33. #endif
  34. #include "polarssl_threadlock.h"
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. /* The last #include file should be: */
  38. #include "memdebug.h"
  39. /* number of thread locks */
  40. #define NUMT 2
  41. /* This array will store all of the mutexes available to PolarSSL. */
  42. static POLARSSL_MUTEX_T *mutex_buf = NULL;
  43. int Curl_polarsslthreadlock_thread_setup(void)
  44. {
  45. int i;
  46. int ret;
  47. mutex_buf = calloc(NUMT * sizeof(POLARSSL_MUTEX_T), 1);
  48. if(!mutex_buf)
  49. return 0; /* error, no number of threads defined */
  50. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  51. for(i = 0; i < NUMT; i++) {
  52. ret = pthread_mutex_init(&mutex_buf[i], NULL);
  53. if(ret)
  54. return 0; /* pthread_mutex_init failed */
  55. }
  56. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  57. for(i = 0; i < NUMT; i++) {
  58. mutex_buf[i] = CreateMutex(0, FALSE, 0);
  59. if(mutex_buf[i] == 0)
  60. return 0; /* CreateMutex failed */
  61. }
  62. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  63. return 1; /* OK */
  64. }
  65. int Curl_polarsslthreadlock_thread_cleanup(void)
  66. {
  67. int i;
  68. int ret;
  69. if(!mutex_buf)
  70. return 0; /* error, no threads locks defined */
  71. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  72. for(i = 0; i < NUMT; i++) {
  73. ret = pthread_mutex_destroy(&mutex_buf[i]);
  74. if(ret)
  75. return 0; /* pthread_mutex_destroy failed */
  76. }
  77. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  78. for(i = 0; i < NUMT; i++) {
  79. ret = CloseHandle(mutex_buf[i]);
  80. if(!ret)
  81. return 0; /* CloseHandle failed */
  82. }
  83. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  84. free(mutex_buf);
  85. mutex_buf = NULL;
  86. return 1; /* OK */
  87. }
  88. int Curl_polarsslthreadlock_lock_function(int n)
  89. {
  90. int ret;
  91. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  92. if(n < NUMT) {
  93. ret = pthread_mutex_lock(&mutex_buf[n]);
  94. if(ret) {
  95. DEBUGF(fprintf(stderr,
  96. "Error: polarsslthreadlock_lock_function failed\n"));
  97. return 0; /* pthread_mutex_lock failed */
  98. }
  99. }
  100. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  101. if(n < NUMT) {
  102. ret = (WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED?1:0);
  103. if(ret) {
  104. DEBUGF(fprintf(stderr,
  105. "Error: polarsslthreadlock_lock_function failed\n"));
  106. return 0; /* pthread_mutex_lock failed */
  107. }
  108. }
  109. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  110. return 1; /* OK */
  111. }
  112. int Curl_polarsslthreadlock_unlock_function(int n)
  113. {
  114. int ret;
  115. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  116. if(n < NUMT) {
  117. ret = pthread_mutex_unlock(&mutex_buf[n]);
  118. if(ret) {
  119. DEBUGF(fprintf(stderr,
  120. "Error: polarsslthreadlock_unlock_function failed\n"));
  121. return 0; /* pthread_mutex_unlock failed */
  122. }
  123. }
  124. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  125. if(n < NUMT) {
  126. ret = ReleaseMutex(mutex_buf[n]);
  127. if(!ret) {
  128. DEBUGF(fprintf(stderr,
  129. "Error: polarsslthreadlock_unlock_function failed\n"));
  130. return 0; /* pthread_mutex_lock failed */
  131. }
  132. }
  133. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  134. return 1; /* OK */
  135. }
  136. #endif /* USE_POLARSSL || USE_MBEDTLS */