mbedtls_threadlock.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2013 - 2020, 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.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_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 MBEDTLS_MUTEX_T pthread_mutex_t
  30. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  31. # include <process.h>
  32. # define MBEDTLS_MUTEX_T HANDLE
  33. #endif
  34. #include "mbedtls_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 Mbedtls. */
  42. static MBEDTLS_MUTEX_T *mutex_buf = NULL;
  43. int Curl_mbedtlsthreadlock_thread_setup(void)
  44. {
  45. int i;
  46. mutex_buf = calloc(NUMT * sizeof(MBEDTLS_MUTEX_T), 1);
  47. if(!mutex_buf)
  48. return 0; /* error, no number of threads defined */
  49. for(i = 0; i < NUMT; i++) {
  50. int ret;
  51. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  52. ret = pthread_mutex_init(&mutex_buf[i], NULL);
  53. if(ret)
  54. return 0; /* pthread_mutex_init failed */
  55. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  56. mutex_buf[i] = CreateMutex(0, FALSE, 0);
  57. if(mutex_buf[i] == 0)
  58. return 0; /* CreateMutex failed */
  59. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  60. }
  61. return 1; /* OK */
  62. }
  63. int Curl_mbedtlsthreadlock_thread_cleanup(void)
  64. {
  65. int i;
  66. if(!mutex_buf)
  67. return 0; /* error, no threads locks defined */
  68. for(i = 0; i < NUMT; i++) {
  69. int ret;
  70. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  71. ret = pthread_mutex_destroy(&mutex_buf[i]);
  72. if(ret)
  73. return 0; /* pthread_mutex_destroy failed */
  74. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  75. ret = CloseHandle(mutex_buf[i]);
  76. if(!ret)
  77. return 0; /* CloseHandle failed */
  78. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  79. }
  80. free(mutex_buf);
  81. mutex_buf = NULL;
  82. return 1; /* OK */
  83. }
  84. int Curl_mbedtlsthreadlock_lock_function(int n)
  85. {
  86. if(n < NUMT) {
  87. int ret;
  88. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  89. ret = pthread_mutex_lock(&mutex_buf[n]);
  90. if(ret) {
  91. DEBUGF(fprintf(stderr,
  92. "Error: mbedtlsthreadlock_lock_function failed\n"));
  93. return 0; /* pthread_mutex_lock failed */
  94. }
  95. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  96. ret = (WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED?1:0);
  97. if(ret) {
  98. DEBUGF(fprintf(stderr,
  99. "Error: mbedtlsthreadlock_lock_function failed\n"));
  100. return 0; /* pthread_mutex_lock failed */
  101. }
  102. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  103. }
  104. return 1; /* OK */
  105. }
  106. int Curl_mbedtlsthreadlock_unlock_function(int n)
  107. {
  108. if(n < NUMT) {
  109. int ret;
  110. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  111. ret = pthread_mutex_unlock(&mutex_buf[n]);
  112. if(ret) {
  113. DEBUGF(fprintf(stderr,
  114. "Error: mbedtlsthreadlock_unlock_function failed\n"));
  115. return 0; /* pthread_mutex_unlock failed */
  116. }
  117. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  118. ret = ReleaseMutex(mutex_buf[n]);
  119. if(!ret) {
  120. DEBUGF(fprintf(stderr,
  121. "Error: mbedtlsthreadlock_unlock_function failed\n"));
  122. return 0; /* pthread_mutex_lock failed */
  123. }
  124. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  125. }
  126. return 1; /* OK */
  127. }
  128. #endif /* USE_MBEDTLS */