2 * Threading abstraction layer
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
27 #include MBEDTLS_CONFIG_FILE
30 #if defined(MBEDTLS_THREADING_C)
32 #include "mbedtls/threading.h"
34 #if defined(MBEDTLS_THREADING_PTHREAD)
35 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t
*mutex
)
40 mutex
->is_valid
= pthread_mutex_init( &mutex
->mutex
, NULL
) == 0;
43 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t
*mutex
)
45 if( mutex
== NULL
|| !mutex
->is_valid
)
48 (void) pthread_mutex_destroy( &mutex
->mutex
);
52 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t
*mutex
)
54 if( mutex
== NULL
|| ! mutex
->is_valid
)
55 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA
);
57 if( pthread_mutex_lock( &mutex
->mutex
) != 0 )
58 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
63 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t
*mutex
)
65 if( mutex
== NULL
|| ! mutex
->is_valid
)
66 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA
);
68 if( pthread_mutex_unlock( &mutex
->mutex
) != 0 )
69 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
74 void (*mbedtls_mutex_init
)( mbedtls_threading_mutex_t
* ) = threading_mutex_init_pthread
;
75 void (*mbedtls_mutex_free
)( mbedtls_threading_mutex_t
* ) = threading_mutex_free_pthread
;
76 int (*mbedtls_mutex_lock
)( mbedtls_threading_mutex_t
* ) = threading_mutex_lock_pthread
;
77 int (*mbedtls_mutex_unlock
)( mbedtls_threading_mutex_t
* ) = threading_mutex_unlock_pthread
;
80 * With phtreads we can statically initialize mutexes
82 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
84 #endif /* MBEDTLS_THREADING_PTHREAD */
86 #if defined(MBEDTLS_THREADING_ALT)
87 static int threading_mutex_fail( mbedtls_threading_mutex_t
*mutex
)
90 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA
);
92 static void threading_mutex_dummy( mbedtls_threading_mutex_t
*mutex
)
98 void (*mbedtls_mutex_init
)( mbedtls_threading_mutex_t
* ) = threading_mutex_dummy
;
99 void (*mbedtls_mutex_free
)( mbedtls_threading_mutex_t
* ) = threading_mutex_dummy
;
100 int (*mbedtls_mutex_lock
)( mbedtls_threading_mutex_t
* ) = threading_mutex_fail
;
101 int (*mbedtls_mutex_unlock
)( mbedtls_threading_mutex_t
* ) = threading_mutex_fail
;
104 * Set functions pointers and initialize global mutexes
106 void mbedtls_threading_set_alt( void (*mutex_init
)( mbedtls_threading_mutex_t
* ),
107 void (*mutex_free
)( mbedtls_threading_mutex_t
* ),
108 int (*mutex_lock
)( mbedtls_threading_mutex_t
* ),
109 int (*mutex_unlock
)( mbedtls_threading_mutex_t
* ) )
111 mbedtls_mutex_init
= mutex_init
;
112 mbedtls_mutex_free
= mutex_free
;
113 mbedtls_mutex_lock
= mutex_lock
;
114 mbedtls_mutex_unlock
= mutex_unlock
;
116 #if defined(MBEDTLS_FS_IO)
117 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex
);
122 * Free global mutexes
124 void mbedtls_threading_free_alt( void )
126 #if defined(MBEDTLS_FS_IO)
127 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex
);
130 #endif /* MBEDTLS_THREADING_ALT */
133 * Define global mutexes
138 #if defined(MBEDTLS_FS_IO)
139 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT
;
142 #endif /* MBEDTLS_THREADING_C */