threading.c 4.25 KB
Newer Older
1 2 3
/*
 *  Threading abstraction layer
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
23
#if !defined(MBEDTLS_CONFIG_FILE)
24
#include "mbedtls/config.h"
25
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
26
#include MBEDTLS_CONFIG_FILE
27
#endif
28

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
29
#if defined(MBEDTLS_THREADING_C)
30

31
#include "mbedtls/threading.h"
32

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
33
#if defined(MBEDTLS_THREADING_PTHREAD)
34
static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
35 36
{
    if( mutex == NULL )
37
        return;
38

39
    mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
40 41
}

42
static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
43 44
{
    if( mutex == NULL )
45
        return;
46

47
    (void) pthread_mutex_destroy( &mutex->mutex );
48 49
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
50
static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
51
{
52
    if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
53
        return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
54

55
    if( pthread_mutex_lock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
56
        return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
57 58 59 60

    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
61
static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
62
{
63
    if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
64
        return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
65

66
    if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
67
        return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
68 69 70 71

    return( 0 );
}

72 73
void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
74 75
int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
76 77 78 79 80 81

/*
 * With phtreads we can statically initialize mutexes
 */
#define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
82
#endif /* MBEDTLS_THREADING_PTHREAD */
83

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
84 85
#if defined(MBEDTLS_THREADING_ALT)
static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
86 87
{
    ((void) mutex );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
88
    return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
89
}
90 91 92 93 94
static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
{
    ((void) mutex );
    return;
}
95

96 97
void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
98 99
int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
100

101 102 103 104 105
/*
 * 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 * ),
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
106 107
                       int (*mutex_lock)( mbedtls_threading_mutex_t * ),
                       int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
108
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
109 110 111 112
    mbedtls_mutex_init = mutex_init;
    mbedtls_mutex_free = mutex_free;
    mbedtls_mutex_lock = mutex_lock;
    mbedtls_mutex_unlock = mutex_unlock;
113

114
    mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
115
    mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
116 117 118 119 120 121 122 123
}

/*
 * Free global mutexes
 */
void mbedtls_threading_free_alt( void )
{
    mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
124
    mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
125
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
126
#endif /* MBEDTLS_THREADING_ALT */
127

128 129 130 131 132 133 134
/*
 * Define global mutexes
 */
#ifndef MUTEX_INIT
#define MUTEX_INIT
#endif
mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
135
mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
136

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
137
#endif /* MBEDTLS_THREADING_C */