polarssl_threadlock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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(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. #include "curl_printf.h"
  37. #include "curl_memory.h"
  38. /* The last #include file should be: */
  39. #include "memdebug.h"
  40. /* number of thread locks */
  41. #define NUMT 2
  42. /* This array will store all of the mutexes available to PolarSSL. */
  43. static POLARSSL_MUTEX_T *mutex_buf = NULL;
  44. int Curl_polarsslthreadlock_thread_setup(void)
  45. {
  46. int i;
  47. int ret;
  48. mutex_buf = calloc(NUMT * sizeof(POLARSSL_MUTEX_T), 1);
  49. if(!mutex_buf)
  50. return 0; /* error, no number of threads defined */
  51. #ifdef HAVE_PTHREAD_H
  52. for(i = 0; i < NUMT; i++) {
  53. ret = pthread_mutex_init(&mutex_buf[i], NULL);
  54. if(ret)
  55. return 0; /* pthread_mutex_init failed */
  56. }
  57. #elif defined(HAVE_PROCESS_H)
  58. for(i = 0; i < NUMT; i++) {
  59. mutex_buf[i] = CreateMutex(0, FALSE, 0);
  60. if(mutex_buf[i] == 0)
  61. return 0; /* CreateMutex failed */
  62. }
  63. #endif /* HAVE_PTHREAD_H */
  64. return 1; /* OK */
  65. }
  66. int Curl_polarsslthreadlock_thread_cleanup(void)
  67. {
  68. int i;
  69. int ret;
  70. if(!mutex_buf)
  71. return 0; /* error, no threads locks defined */
  72. #ifdef HAVE_PTHREAD_H
  73. for(i = 0; i < NUMT; i++) {
  74. ret = pthread_mutex_destroy(&mutex_buf[i]);
  75. if(ret)
  76. return 0; /* pthread_mutex_destroy failed */
  77. }
  78. #elif defined(HAVE_PROCESS_H)
  79. for(i = 0; i < NUMT; i++) {
  80. ret = CloseHandle(mutex_buf[i]);
  81. if(!ret)
  82. return 0; /* CloseHandle failed */
  83. }
  84. #endif /* HAVE_PTHREAD_H */
  85. free(mutex_buf);
  86. mutex_buf = NULL;
  87. return 1; /* OK */
  88. }
  89. int Curl_polarsslthreadlock_lock_function(int n)
  90. {
  91. int ret;
  92. #ifdef HAVE_PTHREAD_H
  93. if(n < NUMT) {
  94. ret = pthread_mutex_lock(&mutex_buf[n]);
  95. if(ret) {
  96. DEBUGF(fprintf(stderr,
  97. "Error: polarsslthreadlock_lock_function failed\n"));
  98. return 0; /* pthread_mutex_lock failed */
  99. }
  100. }
  101. #elif defined(HAVE_PROCESS_H)
  102. if(n < NUMT) {
  103. ret = (WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED?1:0);
  104. if(ret) {
  105. DEBUGF(fprintf(stderr,
  106. "Error: polarsslthreadlock_lock_function failed\n"));
  107. return 0; /* pthread_mutex_lock failed */
  108. }
  109. }
  110. #endif /* HAVE_PTHREAD_H */
  111. return 1; /* OK */
  112. }
  113. int Curl_polarsslthreadlock_unlock_function(int n)
  114. {
  115. int ret;
  116. #ifdef HAVE_PTHREAD_H
  117. if(n < NUMT) {
  118. ret = pthread_mutex_unlock(&mutex_buf[n]);
  119. if(ret) {
  120. DEBUGF(fprintf(stderr,
  121. "Error: polarsslthreadlock_unlock_function failed\n"));
  122. return 0; /* pthread_mutex_unlock failed */
  123. }
  124. }
  125. #elif defined(HAVE_PROCESS_H)
  126. if(n < NUMT) {
  127. ret = ReleaseMutex(mutex_buf[n]);
  128. if(!ret) {
  129. DEBUGF(fprintf(stderr,
  130. "Error: polarsslthreadlock_unlock_function failed\n"));
  131. return 0; /* pthread_mutex_lock failed */
  132. }
  133. }
  134. #endif /* HAVE_PTHREAD_H */
  135. return 1; /* OK */
  136. }
  137. #endif /* USE_POLARSSL || USE_MBEDTLS */