mbedtls_threadlock.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2013 - 2021, Daniel Stenberg, <[email protected]>, et al.
  9. * Copyright (C) 2010, 2011, Hoi-Ho Chan, <[email protected]>
  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. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  51. if(pthread_mutex_init(&mutex_buf[i], NULL))
  52. return 0; /* pthread_mutex_init failed */
  53. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  54. mutex_buf[i] = CreateMutex(0, FALSE, 0);
  55. if(mutex_buf[i] == 0)
  56. return 0; /* CreateMutex failed */
  57. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  58. }
  59. return 1; /* OK */
  60. }
  61. int Curl_mbedtlsthreadlock_thread_cleanup(void)
  62. {
  63. int i;
  64. if(!mutex_buf)
  65. return 0; /* error, no threads locks defined */
  66. for(i = 0; i < NUMT; i++) {
  67. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  68. if(pthread_mutex_destroy(&mutex_buf[i]))
  69. return 0; /* pthread_mutex_destroy failed */
  70. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  71. if(!CloseHandle(mutex_buf[i]))
  72. return 0; /* CloseHandle failed */
  73. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  74. }
  75. free(mutex_buf);
  76. mutex_buf = NULL;
  77. return 1; /* OK */
  78. }
  79. int Curl_mbedtlsthreadlock_lock_function(int n)
  80. {
  81. if(n < NUMT) {
  82. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  83. if(pthread_mutex_lock(&mutex_buf[n])) {
  84. DEBUGF(fprintf(stderr,
  85. "Error: mbedtlsthreadlock_lock_function failed\n"));
  86. return 0; /* pthread_mutex_lock failed */
  87. }
  88. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  89. if(WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED) {
  90. DEBUGF(fprintf(stderr,
  91. "Error: mbedtlsthreadlock_lock_function failed\n"));
  92. return 0; /* pthread_mutex_lock failed */
  93. }
  94. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  95. }
  96. return 1; /* OK */
  97. }
  98. int Curl_mbedtlsthreadlock_unlock_function(int n)
  99. {
  100. if(n < NUMT) {
  101. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  102. if(pthread_mutex_unlock(&mutex_buf[n])) {
  103. DEBUGF(fprintf(stderr,
  104. "Error: mbedtlsthreadlock_unlock_function failed\n"));
  105. return 0; /* pthread_mutex_unlock failed */
  106. }
  107. #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
  108. if(!ReleaseMutex(mutex_buf[n])) {
  109. DEBUGF(fprintf(stderr,
  110. "Error: mbedtlsthreadlock_unlock_function failed\n"));
  111. return 0; /* pthread_mutex_lock failed */
  112. }
  113. #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
  114. }
  115. return 1; /* OK */
  116. }
  117. #endif /* USE_MBEDTLS */