polarssl_threadlock.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2013-2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
00010  *
00011  * This software is licensed as described in the file COPYING, which
00012  * you should have received as part of this distribution. The terms
00013  * are also available at https://curl.haxx.se/docs/copyright.html.
00014  *
00015  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00016  * copies of the Software, and permit persons to whom the Software is
00017  * furnished to do so, under the terms of the COPYING file.
00018  *
00019  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00020  * KIND, either express or implied.
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 /* The last #include file should be: */
00042 #include "memdebug.h"
00043 
00044 /* number of thread locks */
00045 #define NUMT                    2
00046 
00047 /* This array will store all of the mutexes available to PolarSSL. */
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;     /* error, no number of threads defined */
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; /* pthread_mutex_init failed */
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;  /* CreateMutex failed */
00070   }
00071 #endif /* HAVE_PTHREAD_H */
00072 
00073   return 1; /* OK */
00074 }
00075 
00076 int Curl_polarsslthreadlock_thread_cleanup(void)
00077 {
00078   int i;
00079   int ret;
00080 
00081   if(!mutex_buf)
00082     return 0; /* error, no threads locks defined */
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; /* pthread_mutex_destroy failed */
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; /* CloseHandle failed */
00095   }
00096 #endif /* HAVE_PTHREAD_H */
00097   free(mutex_buf);
00098   mutex_buf = NULL;
00099 
00100   return 1; /* OK */
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; /* pthread_mutex_lock failed */
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; /* pthread_mutex_lock failed */
00122     }
00123   }
00124 #endif /* HAVE_PTHREAD_H */
00125   return 1; /* OK */
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; /* pthread_mutex_unlock failed */
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; /* pthread_mutex_lock failed */
00147     }
00148   }
00149 #endif /* HAVE_PTHREAD_H */
00150   return 1; /* OK */
00151 }
00152 
00153 #endif /* USE_POLARSSL || USE_MBEDTLS */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06