00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "polarssl/config.h"
00027
00028 #if defined(POLARSSL_THREADING_C)
00029
00030 #include "polarssl/threading.h"
00031
00032 #if defined(POLARSSL_THREADING_DUMMY)
00033 static int threading_mutex_init_dummy( threading_mutex_t *mutex )
00034 {
00035 ((void) mutex );
00036 return( 0 );
00037 }
00038
00039 static int threading_mutex_free_dummy( threading_mutex_t *mutex )
00040 {
00041 ((void) mutex );
00042 return( 0 );
00043 }
00044
00045 static int threading_mutex_lock_dummy( threading_mutex_t *mutex )
00046 {
00047 ((void) mutex );
00048 return( 0 );
00049 }
00050
00051 static int threading_mutex_unlock_dummy( threading_mutex_t *mutex )
00052 {
00053 ((void) mutex );
00054 return( 0 );
00055 }
00056
00057 int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_dummy;
00058 int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_dummy;
00059 int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_dummy;
00060 int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_dummy;
00061 #endif
00062
00063 #if defined(POLARSSL_THREADING_PTHREAD)
00064 static int threading_mutex_init_pthread( threading_mutex_t *mutex )
00065 {
00066 if( mutex == NULL )
00067 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
00068
00069 if( pthread_mutex_init( mutex, NULL ) != 0 )
00070 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
00071
00072 return( 0 );
00073 }
00074
00075 static int threading_mutex_free_pthread( threading_mutex_t *mutex )
00076 {
00077 if( mutex == NULL )
00078 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
00079
00080 if( pthread_mutex_destroy( mutex ) != 0 )
00081 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
00082
00083 return( 0 );
00084 }
00085
00086 static int threading_mutex_lock_pthread( threading_mutex_t *mutex )
00087 {
00088 if( mutex == NULL )
00089 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
00090
00091 if( pthread_mutex_lock( mutex ) != 0 )
00092 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
00093
00094 return( 0 );
00095 }
00096
00097 static int threading_mutex_unlock_pthread( threading_mutex_t *mutex )
00098 {
00099 if( mutex == NULL )
00100 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
00101
00102 if( pthread_mutex_unlock( mutex ) != 0 )
00103 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
00104
00105 return( 0 );
00106 }
00107
00108 int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
00109 int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
00110 int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
00111 int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
00112 #endif
00113
00114 #if defined(POLARSSL_THREADING_ALT)
00115 int (*polarssl_mutex_init)( threading_mutex_t * ) = NULL;
00116 int (*polarssl_mutex_free)( threading_mutex_t * ) = NULL;
00117 int (*polarssl_mutex_lock)( threading_mutex_t * ) = NULL;
00118 int (*polarssl_mutex_unlock)( threading_mutex_t * ) = NULL;
00119
00120 int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
00121 int (*mutex_free)( threading_mutex_t * ),
00122 int (*mutex_lock)( threading_mutex_t * ),
00123 int (*mutex_unlock)( threading_mutex_t * ) )
00124 {
00125 polarssl_mutex_init = mutex_init;
00126 polarssl_mutex_free = mutex_free;
00127 polarssl_mutex_lock = mutex_lock;
00128 polarssl_mutex_unlock = mutex_unlock;
00129
00130 return( 0 );
00131 }
00132 #endif
00133
00134 #endif