mbedtls_threadlock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2013 - 2022, 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. * 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(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)))
  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(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  33. # include <process.h>
  34. # define MBEDTLS_MUTEX_T HANDLE
  35. #endif
  36. #error #include "mbedtls_threadlock.h"
  37. #include "curl_printf.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 Mbedtls. */
  44. static MBEDTLS_MUTEX_T *mutex_buf = NULL;
  45. int Curl_mbedtlsthreadlock_thread_setup(void)
  46. {
  47. int i;
  48. mutex_buf = calloc(NUMT * sizeof(MBEDTLS_MUTEX_T), 1);
  49. if(!mutex_buf)
  50. return 0; /* error, no number of threads defined */
  51. for(i = 0; i < NUMT; i++) {
  52. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  53. if(pthread_mutex_init(&mutex_buf[i], NULL))
  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. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  70. if(pthread_mutex_destroy(&mutex_buf[i]))
  71. return 0; /* pthread_mutex_destroy failed */
  72. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  73. if(!CloseHandle(mutex_buf[i]))
  74. return 0; /* CloseHandle failed */
  75. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  76. }
  77. free(mutex_buf);
  78. mutex_buf = NULL;
  79. return 1; /* OK */
  80. }
  81. int Curl_mbedtlsthreadlock_lock_function(int n)
  82. {
  83. if(n < NUMT) {
  84. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  85. if(pthread_mutex_lock(&mutex_buf[n])) {
  86. DEBUGF(fprintf(stderr,
  87. "Error: mbedtlsthreadlock_lock_function failed\n"));
  88. return 0; /* pthread_mutex_lock failed */
  89. }
  90. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  91. if(WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED) {
  92. DEBUGF(fprintf(stderr,
  93. "Error: mbedtlsthreadlock_lock_function failed\n"));
  94. return 0; /* pthread_mutex_lock failed */
  95. }
  96. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  97. }
  98. return 1; /* OK */
  99. }
  100. int Curl_mbedtlsthreadlock_unlock_function(int n)
  101. {
  102. if(n < NUMT) {
  103. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  104. if(pthread_mutex_unlock(&mutex_buf[n])) {
  105. DEBUGF(fprintf(stderr,
  106. "Error: mbedtlsthreadlock_unlock_function failed\n"));
  107. return 0; /* pthread_mutex_unlock failed */
  108. }
  109. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  110. if(!ReleaseMutex(mutex_buf[n])) {
  111. DEBUGF(fprintf(stderr,
  112. "Error: mbedtlsthreadlock_unlock_function failed\n"));
  113. return 0; /* pthread_mutex_lock failed */
  114. }
  115. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  116. }
  117. return 1; /* OK */
  118. }
  119. #endif /* USE_MBEDTLS */