PolarSSL v1.3.9
threading.c
Go to the documentation of this file.
1/*
2 * Threading abstraction layer
3 *
4 * Copyright (C) 2006-2014, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#if !defined(POLARSSL_CONFIG_FILE)
27#include "polarssl/config.h"
28#else
29#include POLARSSL_CONFIG_FILE
30#endif
31
32#if defined(POLARSSL_THREADING_C)
33
34#include "polarssl/threading.h"
35
36#if defined(POLARSSL_THREADING_PTHREAD)
37static int threading_mutex_init_pthread( threading_mutex_t *mutex )
38{
39 if( mutex == NULL )
41
42 if( pthread_mutex_init( mutex, NULL ) != 0 )
44
45 return( 0 );
46}
47
48static int threading_mutex_free_pthread( threading_mutex_t *mutex )
49{
50 if( mutex == NULL )
52
53 if( pthread_mutex_destroy( mutex ) != 0 )
55
56 return( 0 );
57}
58
59static int threading_mutex_lock_pthread( threading_mutex_t *mutex )
60{
61 if( mutex == NULL )
63
64 if( pthread_mutex_lock( mutex ) != 0 )
66
67 return( 0 );
68}
69
70static int threading_mutex_unlock_pthread( threading_mutex_t *mutex )
71{
72 if( mutex == NULL )
74
75 if( pthread_mutex_unlock( mutex ) != 0 )
77
78 return( 0 );
79}
80
81int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
82int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
83int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
84int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
85#endif /* POLARSSL_THREADING_PTHREAD */
86
87#if defined(POLARSSL_THREADING_ALT)
88static int threading_mutex_fail( threading_mutex_t *mutex )
89{
90 ((void) mutex );
92}
93
94int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail;
95int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail;
96int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail;
97int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail;
98
99int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
100 int (*mutex_free)( threading_mutex_t * ),
101 int (*mutex_lock)( threading_mutex_t * ),
102 int (*mutex_unlock)( threading_mutex_t * ) )
103{
104 polarssl_mutex_init = mutex_init;
105 polarssl_mutex_free = mutex_free;
106 polarssl_mutex_lock = mutex_lock;
107 polarssl_mutex_unlock = mutex_unlock;
108
109 return( 0 );
110}
111#endif /* POLARSSL_THREADING_ALT_C */
112
113#endif /* POLARSSL_THREADING_C */
Configuration options (set of defines)
Threading abstraction layer.
#define POLARSSL_ERR_THREADING_BAD_INPUT_DATA
Bad input parameters to function.
Definition: threading.h:43
int(* polarssl_mutex_init)(threading_mutex_t *mutex)
int(* polarssl_mutex_free)(threading_mutex_t *mutex)
int(* polarssl_mutex_unlock)(threading_mutex_t *mutex)
int(* polarssl_mutex_lock)(threading_mutex_t *mutex)
#define POLARSSL_ERR_THREADING_MUTEX_ERROR
Locking / unlocking / free failed with error code.
Definition: threading.h:44