Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "curl_setup.h"
00024
00025 #if (defined(USE_POLARSSL) || defined(USE_MBEDTLS)) && \
00026 (defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32))
00027
00028 #if defined(USE_THREADS_POSIX)
00029 # ifdef HAVE_PTHREAD_H
00030 # include <pthread.h>
00031 # endif
00032 #elif defined(USE_THREADS_WIN32)
00033 # ifdef HAVE_PROCESS_H
00034 # include <process.h>
00035 # endif
00036 #endif
00037
00038 #include "polarssl_threadlock.h"
00039 #include "curl_printf.h"
00040 #include "curl_memory.h"
00041
00042 #include "memdebug.h"
00043
00044
00045 #define NUMT 2
00046
00047
00048 static POLARSSL_MUTEX_T *mutex_buf = NULL;
00049
00050 int Curl_polarsslthreadlock_thread_setup(void)
00051 {
00052 int i;
00053 int ret;
00054
00055 mutex_buf = calloc(NUMT * sizeof(POLARSSL_MUTEX_T), 1);
00056 if(!mutex_buf)
00057 return 0;
00058
00059 #ifdef HAVE_PTHREAD_H
00060 for(i = 0; i < NUMT; i++) {
00061 ret = pthread_mutex_init(&mutex_buf[i], NULL);
00062 if(ret)
00063 return 0;
00064 }
00065 #elif defined(HAVE_PROCESS_H)
00066 for(i = 0; i < NUMT; i++) {
00067 mutex_buf[i] = CreateMutex(0, FALSE, 0);
00068 if(mutex_buf[i] == 0)
00069 return 0;
00070 }
00071 #endif
00072
00073 return 1;
00074 }
00075
00076 int Curl_polarsslthreadlock_thread_cleanup(void)
00077 {
00078 int i;
00079 int ret;
00080
00081 if(!mutex_buf)
00082 return 0;
00083
00084 #ifdef HAVE_PTHREAD_H
00085 for(i = 0; i < NUMT; i++) {
00086 ret = pthread_mutex_destroy(&mutex_buf[i]);
00087 if(ret)
00088 return 0;
00089 }
00090 #elif defined(HAVE_PROCESS_H)
00091 for(i = 0; i < NUMT; i++) {
00092 ret = CloseHandle(mutex_buf[i]);
00093 if(!ret)
00094 return 0;
00095 }
00096 #endif
00097 free(mutex_buf);
00098 mutex_buf = NULL;
00099
00100 return 1;
00101 }
00102
00103 int Curl_polarsslthreadlock_lock_function(int n)
00104 {
00105 int ret;
00106 #ifdef HAVE_PTHREAD_H
00107 if(n < NUMT) {
00108 ret = pthread_mutex_lock(&mutex_buf[n]);
00109 if(ret) {
00110 DEBUGF(fprintf(stderr,
00111 "Error: polarsslthreadlock_lock_function failed\n"));
00112 return 0;
00113 }
00114 }
00115 #elif defined(HAVE_PROCESS_H)
00116 if(n < NUMT) {
00117 ret = (WaitForSingleObject(mutex_buf[n], INFINITE)==WAIT_FAILED?1:0);
00118 if(ret) {
00119 DEBUGF(fprintf(stderr,
00120 "Error: polarsslthreadlock_lock_function failed\n"));
00121 return 0;
00122 }
00123 }
00124 #endif
00125 return 1;
00126 }
00127
00128 int Curl_polarsslthreadlock_unlock_function(int n)
00129 {
00130 int ret;
00131 #ifdef HAVE_PTHREAD_H
00132 if(n < NUMT) {
00133 ret = pthread_mutex_unlock(&mutex_buf[n]);
00134 if(ret) {
00135 DEBUGF(fprintf(stderr,
00136 "Error: polarsslthreadlock_unlock_function failed\n"));
00137 return 0;
00138 }
00139 }
00140 #elif defined(HAVE_PROCESS_H)
00141 if(n < NUMT) {
00142 ret = ReleaseMutex(mutex_buf[n]);
00143 if(!ret) {
00144 DEBUGF(fprintf(stderr,
00145 "Error: polarsslthreadlock_unlock_function failed\n"));
00146 return 0;
00147 }
00148 }
00149 #endif
00150 return 1;
00151 }
00152
00153 #endif