3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL 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 * CyaSSL 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <cyassl/ctaocrypt/settings.h>
27 #include <cyassl/ctaocrypt/types.h>
28 #include <cyassl/ctaocrypt/error-crypt.h>
32 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
33 #pragma warning(disable: 4996)
38 #ifdef SINGLE_THREADED
40 int InitMutex(CyaSSL_Mutex* m)
47 int FreeMutex(CyaSSL_Mutex *m)
54 int LockMutex(CyaSSL_Mutex *m)
61 int UnLockMutex(CyaSSL_Mutex *m)
67 #else /* MULTI_THREAD */
71 int InitMutex(CyaSSL_Mutex* m)
75 *m = ( CyaSSL_Mutex ) xSemaphoreCreateMutex();
79 iReturn = BAD_MUTEX_E;
84 int FreeMutex(CyaSSL_Mutex* m)
86 vSemaphoreDelete( *m );
90 int LockMutex(CyaSSL_Mutex* m)
92 /* Assume an infinite block, or should there be zero block? */
93 xSemaphoreTake( *m, portMAX_DELAY );
97 int UnLockMutex(CyaSSL_Mutex* m)
103 #elif defined(CYASSL_SAFERTOS)
105 int InitMutex(CyaSSL_Mutex* m)
107 vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
108 if (m->mutex == NULL)
114 int FreeMutex(CyaSSL_Mutex* m)
120 int LockMutex(CyaSSL_Mutex* m)
122 /* Assume an infinite block */
123 xSemaphoreTake(m->mutex, portMAX_DELAY);
127 int UnLockMutex(CyaSSL_Mutex* m)
129 xSemaphoreGive(m->mutex);
134 #elif defined(USE_WINDOWS_API)
136 int InitMutex(CyaSSL_Mutex* m)
138 InitializeCriticalSection(m);
143 int FreeMutex(CyaSSL_Mutex* m)
145 DeleteCriticalSection(m);
150 int LockMutex(CyaSSL_Mutex* m)
152 EnterCriticalSection(m);
157 int UnLockMutex(CyaSSL_Mutex* m)
159 LeaveCriticalSection(m);
163 #elif defined(CYASSL_PTHREADS)
165 int InitMutex(CyaSSL_Mutex* m)
167 if (pthread_mutex_init(m, 0) == 0)
174 int FreeMutex(CyaSSL_Mutex* m)
176 if (pthread_mutex_destroy(m) == 0)
183 int LockMutex(CyaSSL_Mutex* m)
185 if (pthread_mutex_lock(m) == 0)
192 int UnLockMutex(CyaSSL_Mutex* m)
194 if (pthread_mutex_unlock(m) == 0)
200 #elif defined(THREADX)
202 int InitMutex(CyaSSL_Mutex* m)
204 if (tx_mutex_create(m, "CyaSSL Mutex", TX_NO_INHERIT) == 0)
211 int FreeMutex(CyaSSL_Mutex* m)
213 if (tx_mutex_delete(m) == 0)
220 int LockMutex(CyaSSL_Mutex* m)
222 if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
229 int UnLockMutex(CyaSSL_Mutex* m)
231 if (tx_mutex_put(m) == 0)
237 #elif defined(MICRIUM)
239 int InitMutex(CyaSSL_Mutex* m)
241 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
242 if (NetSecure_OS_MutexCreate(m) == 0)
252 int FreeMutex(CyaSSL_Mutex* m)
254 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
255 if (NetSecure_OS_FreeMutex(m) == 0)
265 int LockMutex(CyaSSL_Mutex* m)
267 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
268 if (NetSecure_OS_LockMutex(m) == 0)
278 int UnLockMutex(CyaSSL_Mutex* m)
280 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
281 if (NetSecure_OS_UnLockMutex(m) == 0)
291 #elif defined(EBSNET)
293 int InitMutex(CyaSSL_Mutex* m)
295 if (rtp_sig_mutex_alloc(m, "CyaSSL Mutex") == -1)
301 int FreeMutex(CyaSSL_Mutex* m)
303 rtp_sig_mutex_free(*m);
307 int LockMutex(CyaSSL_Mutex* m)
309 if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
315 int UnLockMutex(CyaSSL_Mutex* m)
317 rtp_sig_mutex_release(*m);
321 #elif defined(FREESCALE_MQX)
323 int InitMutex(CyaSSL_Mutex* m)
325 if (_mutex_init(m, NULL) == MQX_EOK)
331 int FreeMutex(CyaSSL_Mutex* m)
333 if (_mutex_destroy(m) == MQX_EOK)
339 int LockMutex(CyaSSL_Mutex* m)
341 if (_mutex_lock(m) == MQX_EOK)
347 int UnLockMutex(CyaSSL_Mutex* m)
349 if (_mutex_unlock(m) == MQX_EOK)
355 #elif defined(CYASSL_MDK_ARM)|| defined(CYASSL_CMSIS_RTOS)
357 #if defined(CYASSL_CMSIS_RTOS)
358 #include "cmsis_os.h"
359 #define CMSIS_NMUTEX 10
360 osMutexDef(CyaSSL_mt0) ; osMutexDef(CyaSSL_mt1) ; osMutexDef(CyaSSL_mt2) ;
361 osMutexDef(CyaSSL_mt3) ; osMutexDef(CyaSSL_mt4) ; osMutexDef(CyaSSL_mt5) ;
362 osMutexDef(CyaSSL_mt6) ; osMutexDef(CyaSSL_mt7) ; osMutexDef(CyaSSL_mt8) ;
363 osMutexDef(CyaSSL_mt9) ;
365 static const osMutexDef_t *CMSIS_mutex[] = { osMutex(CyaSSL_mt0),
366 osMutex(CyaSSL_mt1), osMutex(CyaSSL_mt2), osMutex(CyaSSL_mt3),
367 osMutex(CyaSSL_mt4), osMutex(CyaSSL_mt5), osMutex(CyaSSL_mt6),
368 osMutex(CyaSSL_mt7), osMutex(CyaSSL_mt8), osMutex(CyaSSL_mt9) } ;
370 static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0} ;
372 int InitMutex(CyaSSL_Mutex* m)
375 for (i=0; i<CMSIS_NMUTEX; i++) {
376 if(CMSIS_mutexID[i] == 0) {
377 CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]) ;
378 (*m) = CMSIS_mutexID[i] ;
385 int FreeMutex(CyaSSL_Mutex* m)
389 for (i=0; i<CMSIS_NMUTEX; i++) {
390 if(CMSIS_mutexID[i] == (*m)) {
391 CMSIS_mutexID[i] = 0 ;
398 int LockMutex(CyaSSL_Mutex* m)
400 osMutexWait(*m, osWaitForever) ;
404 int UnLockMutex(CyaSSL_Mutex* m)
411 int InitMutex(CyaSSL_Mutex* m)
417 int FreeMutex(CyaSSL_Mutex* m)
422 int LockMutex(CyaSSL_Mutex* m)
424 os_mut_wait (m, 0xffff);
428 int UnLockMutex(CyaSSL_Mutex* m)
434 #endif /* USE_WINDOWS_API */
435 #endif /* SINGLE_THREADED */