mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-11-04 04:32:24 -05:00 
			
		
		
		
	Allow use of global mutexes with threading_alt
This commit is contained in:
		
							parent
							
								
									f7c2eebfcf
								
							
						
					
					
						commit
						944cfe8899
					
				@ -55,7 +55,11 @@ typedef struct
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * \brief           Set your alternate threading implementation function
 | 
					 * \brief           Set your alternate threading implementation function
 | 
				
			||||||
 *                  pointers
 | 
					 *                  pointers and initialize global mutexes. If used, this
 | 
				
			||||||
 | 
					 *                  function must be called once in the main thread before any
 | 
				
			||||||
 | 
					 *                  other mbed TLS function is called, and
 | 
				
			||||||
 | 
					 *                  mbedtls_threading_free_alt() must be called once in the main
 | 
				
			||||||
 | 
					 *                  thread after all other mbed TLS functions.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * \note            mutex_init() and mutex_free() don't return a status code.
 | 
					 * \note            mutex_init() and mutex_free() don't return a status code.
 | 
				
			||||||
 *                  If mutex_init() fails, it should leave its argument (the
 | 
					 *                  If mutex_init() fails, it should leave its argument (the
 | 
				
			||||||
@ -66,13 +70,16 @@ typedef struct
 | 
				
			|||||||
 * \param mutex_free    the free function implementation
 | 
					 * \param mutex_free    the free function implementation
 | 
				
			||||||
 * \param mutex_lock    the lock function implementation
 | 
					 * \param mutex_lock    the lock function implementation
 | 
				
			||||||
 * \param mutex_unlock  the unlock function implementation
 | 
					 * \param mutex_unlock  the unlock function implementation
 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * \return              0 if successful
 | 
					 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
int mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
 | 
					void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
 | 
				
			||||||
                       void (*mutex_free)( mbedtls_threading_mutex_t * ),
 | 
					                       void (*mutex_free)( mbedtls_threading_mutex_t * ),
 | 
				
			||||||
                       int (*mutex_lock)( mbedtls_threading_mutex_t * ),
 | 
					                       int (*mutex_lock)( mbedtls_threading_mutex_t * ),
 | 
				
			||||||
                       int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
 | 
					                       int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * \brief               Free global mutexes.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void mbedtls_threading_free_alt( void );
 | 
				
			||||||
#endif /* MBEDTLS_THREADING_ALT */
 | 
					#endif /* MBEDTLS_THREADING_ALT */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
@ -85,6 +92,11 @@ extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex );
 | 
				
			|||||||
extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
 | 
					extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
 | 
				
			||||||
extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
 | 
					extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Global mutexes
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef __cplusplus
 | 
					#ifdef __cplusplus
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
				
			|||||||
@ -173,11 +173,6 @@ int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
 | 
				
			|||||||
 *                 of failed certificates it encountered. If none complete
 | 
					 *                 of failed certificates it encountered. If none complete
 | 
				
			||||||
 *                 correctly, the first error is returned.
 | 
					 *                 correctly, the first error is returned.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * \warning        This function is NOT thread-safe unless
 | 
					 | 
				
			||||||
 *                 MBEDTLS_THREADING_PTHREAD is defined. If you're using an
 | 
					 | 
				
			||||||
 *                 alternative threading implementation, you should either use
 | 
					 | 
				
			||||||
 *                 this function only in the main thread, or mutex it.
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * \param chain    points to the start of the chain
 | 
					 * \param chain    points to the start of the chain
 | 
				
			||||||
 * \param path     directory / folder to read the certificate files from
 | 
					 * \param path     directory / folder to read the certificate files from
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 | 
				
			|||||||
@ -73,6 +73,12 @@ void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init
 | 
				
			|||||||
void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
 | 
					void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
 | 
				
			||||||
int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
 | 
					int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
 | 
				
			||||||
int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
 | 
					int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * With phtreads we can statically initialize mutexes
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* MBEDTLS_THREADING_PTHREAD */
 | 
					#endif /* MBEDTLS_THREADING_PTHREAD */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(MBEDTLS_THREADING_ALT)
 | 
					#if defined(MBEDTLS_THREADING_ALT)
 | 
				
			||||||
@ -92,8 +98,11 @@ void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dumm
 | 
				
			|||||||
int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
 | 
					int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
 | 
				
			||||||
int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
 | 
					int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int mbedtls_threading_set_alt( int (*mutex_init)( mbedtls_threading_mutex_t * ),
 | 
					/*
 | 
				
			||||||
                       int (*mutex_free)( mbedtls_threading_mutex_t * ),
 | 
					 * Set functions pointers and initialize global mutexes
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
 | 
				
			||||||
 | 
					                       void (*mutex_free)( mbedtls_threading_mutex_t * ),
 | 
				
			||||||
                       int (*mutex_lock)( mbedtls_threading_mutex_t * ),
 | 
					                       int (*mutex_lock)( mbedtls_threading_mutex_t * ),
 | 
				
			||||||
                       int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
 | 
					                       int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@ -102,8 +111,24 @@ int mbedtls_threading_set_alt( int (*mutex_init)( mbedtls_threading_mutex_t * ),
 | 
				
			|||||||
    mbedtls_mutex_lock = mutex_lock;
 | 
					    mbedtls_mutex_lock = mutex_lock;
 | 
				
			||||||
    mbedtls_mutex_unlock = mutex_unlock;
 | 
					    mbedtls_mutex_unlock = mutex_unlock;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return( 0 );
 | 
					    mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Free global mutexes
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void mbedtls_threading_free_alt( void )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#endif /* MBEDTLS_THREADING_ALT */
 | 
					#endif /* MBEDTLS_THREADING_ALT */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Define global mutexes
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#ifndef MUTEX_INIT
 | 
				
			||||||
 | 
					#define MUTEX_INIT
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* MBEDTLS_THREADING_C */
 | 
					#endif /* MBEDTLS_THREADING_C */
 | 
				
			||||||
 | 
				
			|||||||
@ -963,13 +963,6 @@ int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
 | 
				
			|||||||
    return( ret );
 | 
					    return( ret );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(MBEDTLS_THREADING_PTHREAD)
 | 
					 | 
				
			||||||
static mbedtls_threading_mutex_t readdir_mutex = {
 | 
					 | 
				
			||||||
    PTHREAD_MUTEX_INITIALIZER,
 | 
					 | 
				
			||||||
    1
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
 | 
					int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int ret = 0;
 | 
					    int ret = 0;
 | 
				
			||||||
@ -1040,7 +1033,7 @@ int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
 | 
				
			|||||||
        return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
 | 
					        return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(MBEDTLS_THREADING_PTHREAD)
 | 
					#if defined(MBEDTLS_THREADING_PTHREAD)
 | 
				
			||||||
    if( ( ret = mbedtls_mutex_lock( &readdir_mutex ) ) != 0 )
 | 
					    if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
 | 
				
			||||||
        return( ret );
 | 
					        return( ret );
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -1070,7 +1063,7 @@ int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
cleanup:
 | 
					cleanup:
 | 
				
			||||||
#if defined(MBEDTLS_THREADING_PTHREAD)
 | 
					#if defined(MBEDTLS_THREADING_PTHREAD)
 | 
				
			||||||
    if( mbedtls_mutex_unlock( &readdir_mutex ) != 0 )
 | 
					    if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
 | 
				
			||||||
        ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
 | 
					        ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user