mbedtls_threadlock.c 4.1 KB

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