PolarSSL v1.3.3
threading.c
Go to the documentation of this file.
1 /*
2  * Threading abstraction layer
3  *
4  * Copyright (C) 2006-2013, 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 #include "polarssl/config.h"
27 
28 #if defined(POLARSSL_THREADING_C)
29 
30 #include "polarssl/threading.h"
31 
32 #if defined(POLARSSL_THREADING_PTHREAD)
33 static int threading_mutex_init_pthread( threading_mutex_t *mutex )
34 {
35  if( mutex == NULL )
37 
38  if( pthread_mutex_init( mutex, NULL ) != 0 )
40 
41  return( 0 );
42 }
43 
44 static int threading_mutex_free_pthread( threading_mutex_t *mutex )
45 {
46  if( mutex == NULL )
48 
49  if( pthread_mutex_destroy( mutex ) != 0 )
51 
52  return( 0 );
53 }
54 
55 static int threading_mutex_lock_pthread( threading_mutex_t *mutex )
56 {
57  if( mutex == NULL )
59 
60  if( pthread_mutex_lock( mutex ) != 0 )
62 
63  return( 0 );
64 }
65 
66 static int threading_mutex_unlock_pthread( threading_mutex_t *mutex )
67 {
68  if( mutex == NULL )
70 
71  if( pthread_mutex_unlock( mutex ) != 0 )
73 
74  return( 0 );
75 }
76 
77 int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
78 int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
79 int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
80 int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
81 #endif /* POLARSSL_THREADING_PTHREAD */
82 
83 #if defined(POLARSSL_THREADING_ALT)
84 static int threading_mutex_fail( threading_mutex_t *mutex )
85 {
86  ((void) mutex );
88 }
89 
90 int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail;
91 int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail;
92 int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail;
93 int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail;
94 
95 int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
96  int (*mutex_free)( threading_mutex_t * ),
97  int (*mutex_lock)( threading_mutex_t * ),
98  int (*mutex_unlock)( threading_mutex_t * ) )
99 {
100  polarssl_mutex_init = mutex_init;
101  polarssl_mutex_free = mutex_free;
102  polarssl_mutex_lock = mutex_lock;
103  polarssl_mutex_unlock = mutex_unlock;
104 
105  return( 0 );
106 }
107 #endif /* POLARSSL_THREADING_ALT_C */
108 
109 #endif /* POLARSSL_THREADING_C */
int(* polarssl_mutex_lock)(threading_mutex_t *mutex)
Configuration options (set of defines)
Threading abstraction layer.
#define POLARSSL_ERR_THREADING_BAD_INPUT_DATA
Bad input parameters to function.
Definition: threading.h:39
int(* polarssl_mutex_free)(threading_mutex_t *mutex)
int(* polarssl_mutex_unlock)(threading_mutex_t *mutex)
int(* polarssl_mutex_init)(threading_mutex_t *mutex)
#define POLARSSL_ERR_THREADING_MUTEX_ERROR
Locking / unlocking / free failed with error code.
Definition: threading.h:40