1 /*
2  * Copyright (C) 2018 Alibaba Group Holding Limited
3  */
4 
5 /* This file provides threading mutex implementation for AliOS Things. */
6 
7 #if !defined(MBEDTLS_CONFIG_FILE)
8 #include "mbedtls/config.h"
9 #else
10 #include MBEDTLS_CONFIG_FILE
11 #endif
12 
13 #if defined(MBEDTLS_THREADING_ALT)
14 #include <aos/kernel.h>
15 
16 #include "mbedtls/threading.h"
17 
threading_mutex_init(mbedtls_threading_mutex_t * mutex)18 void threading_mutex_init( mbedtls_threading_mutex_t *mutex )
19 {
20     if( mutex == NULL )
21         return;
22 
23     mutex->is_valid = ( aos_mutex_new( &mutex->mutex ) == 0 );
24 }
25 
threading_mutex_free(mbedtls_threading_mutex_t * mutex)26 void threading_mutex_free( mbedtls_threading_mutex_t *mutex )
27 {
28     if( mutex == NULL || !mutex->is_valid )
29         return;
30 
31     aos_mutex_free( &mutex->mutex );
32     mutex->is_valid = 0;
33 }
34 
threading_mutex_lock(mbedtls_threading_mutex_t * mutex)35 int threading_mutex_lock( mbedtls_threading_mutex_t *mutex )
36 {
37     if( mutex == NULL || !mutex->is_valid )
38         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
39 
40     if( aos_mutex_lock( &mutex->mutex, AOS_WAIT_FOREVER ) != 0 )
41         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
42 
43     return( 0 );
44 }
45 
threading_mutex_unlock(mbedtls_threading_mutex_t * mutex)46 int threading_mutex_unlock( mbedtls_threading_mutex_t *mutex )
47 {
48     if( mutex == NULL || ! mutex->is_valid )
49         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
50 
51     if( aos_mutex_unlock( &mutex->mutex ) != 0 )
52         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
53 
54     return( 0 );
55 }
56 #endif /* MBEDTLS_THREADING_ALT */
57 
58