diff options
Diffstat (limited to 'tinySAK/src/tsk_mutex.c')
-rwxr-xr-x | tinySAK/src/tsk_mutex.c | 139 |
1 files changed, 68 insertions, 71 deletions
diff --git a/tinySAK/src/tsk_mutex.c b/tinySAK/src/tsk_mutex.c index d3eb9f8..8bb8d4e 100755 --- a/tinySAK/src/tsk_mutex.c +++ b/tinySAK/src/tsk_mutex.c @@ -2,19 +2,19 @@ * Copyright (C) 2010-2011 Mamadou Diop. * * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org> -* +* * This file is part of Open Source Doubango Framework. * * DOUBANGO 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 3 of the License, or * (at your option) any later version. -* +* * DOUBANGO 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 DOUBANGO. * @@ -34,13 +34,13 @@ #if TSK_UNDER_WINDOWS # include <windows.h> # include "tsk_errno.h" - typedef HANDLE MUTEX_T; +typedef HANDLE MUTEX_T; # define MUTEX_S void # define TSK_ERROR_NOT_OWNER ERROR_NOT_OWNER #else # include <pthread.h> # define MUTEX_S pthread_mutex_t - typedef MUTEX_S* MUTEX_T; +typedef MUTEX_S* MUTEX_T; # define TSK_ERROR_NOT_OWNER EPERM # if !defined(TSK_RECURSIVE_MUTEXATTR) # if defined(PTHREAD_MUTEX_RECURSIVE) @@ -64,8 +64,8 @@ * @sa @ref tsk_mutex_destroy() @ref tsk_mutex_create_2() */ tsk_mutex_handle_t* tsk_mutex_create() -{ - return tsk_mutex_create_2(tsk_true); +{ + return tsk_mutex_create_2(tsk_true); } /**@ingroup tsk_mutex_group @@ -76,40 +76,40 @@ tsk_mutex_handle_t* tsk_mutex_create() */ tsk_mutex_handle_t* tsk_mutex_create_2(tsk_bool_t recursive) { - MUTEX_T handle = tsk_null; - + MUTEX_T handle = tsk_null; + #if TSK_UNDER_WINDOWS # if TSK_UNDER_WINDOWS_RT - handle = CreateMutexEx(NULL, NULL, 0x00000000, MUTEX_ALL_ACCESS); + handle = CreateMutexEx(NULL, NULL, 0x00000000, MUTEX_ALL_ACCESS); # else - handle = CreateMutex(NULL, FALSE, NULL); + handle = CreateMutex(NULL, FALSE, NULL); # endif #else - int ret; - pthread_mutexattr_t mta; - - if((ret = pthread_mutexattr_init(&mta))){ - TSK_DEBUG_ERROR("pthread_mutexattr_init failed with error code %d", ret); - return tsk_null; - } - if(recursive && (ret = pthread_mutexattr_settype(&mta, TSK_RECURSIVE_MUTEXATTR))){ - TSK_DEBUG_ERROR("pthread_mutexattr_settype failed with error code %d", ret); - pthread_mutexattr_destroy(&mta); - return tsk_null; - } - - /* if we are here: all is ok */ - handle = tsk_calloc(1, sizeof(MUTEX_S)); - if(pthread_mutex_init((MUTEX_T)handle, &mta)){ - TSK_FREE(handle); - } - pthread_mutexattr_destroy(&mta); + int ret; + pthread_mutexattr_t mta; + + if((ret = pthread_mutexattr_init(&mta))) { + TSK_DEBUG_ERROR("pthread_mutexattr_init failed with error code %d", ret); + return tsk_null; + } + if(recursive && (ret = pthread_mutexattr_settype(&mta, TSK_RECURSIVE_MUTEXATTR))) { + TSK_DEBUG_ERROR("pthread_mutexattr_settype failed with error code %d", ret); + pthread_mutexattr_destroy(&mta); + return tsk_null; + } + + /* if we are here: all is ok */ + handle = tsk_calloc(1, sizeof(MUTEX_S)); + if(pthread_mutex_init((MUTEX_T)handle, &mta)) { + TSK_FREE(handle); + } + pthread_mutexattr_destroy(&mta); #endif - - if(!handle){ - TSK_DEBUG_ERROR("Failed to create new mutex."); - } - return handle; + + if(!handle) { + TSK_DEBUG_ERROR("Failed to create new mutex."); + } + return handle; } /**@ingroup tsk_mutex_group @@ -120,24 +120,23 @@ tsk_mutex_handle_t* tsk_mutex_create_2(tsk_bool_t recursive) */ int tsk_mutex_lock(tsk_mutex_handle_t* handle) { - int ret = EINVAL; - if(handle) - { + int ret = EINVAL; + if(handle) { #if TSK_UNDER_WINDOWS # if TSK_UNDER_WINDOWS_RT - if((ret = WaitForSingleObjectEx((MUTEX_T)handle, INFINITE, TRUE)) == WAIT_FAILED) + if((ret = WaitForSingleObjectEx((MUTEX_T)handle, INFINITE, TRUE)) == WAIT_FAILED) # else - if((ret = WaitForSingleObject((MUTEX_T)handle, INFINITE)) == WAIT_FAILED) + if((ret = WaitForSingleObject((MUTEX_T)handle, INFINITE)) == WAIT_FAILED) #endif #else - if((ret = pthread_mutex_lock((MUTEX_T)handle))) + if((ret = pthread_mutex_lock((MUTEX_T)handle))) #endif - { - TSK_DEBUG_ERROR("Failed to lock the mutex: %d", ret); - } - } - return ret; + { + TSK_DEBUG_ERROR("Failed to lock the mutex: %d", ret); + } + } + return ret; } /**@ingroup tsk_mutex_group @@ -148,25 +147,23 @@ int tsk_mutex_lock(tsk_mutex_handle_t* handle) */ int tsk_mutex_unlock(tsk_mutex_handle_t* handle) { - int ret = EINVAL; - if(handle) - { + int ret = EINVAL; + if(handle) { #if TSK_UNDER_WINDOWS - if((ret = ReleaseMutex((MUTEX_T)handle) ? 0 : -1)){ - ret = GetLastError(); + if((ret = ReleaseMutex((MUTEX_T)handle) ? 0 : -1)) { + ret = GetLastError(); #else - if((ret = pthread_mutex_unlock((MUTEX_T)handle))) - { + if((ret = pthread_mutex_unlock((MUTEX_T)handle))) { #endif - if(ret == TSK_ERROR_NOT_OWNER){ - TSK_DEBUG_WARN("The calling thread does not own the mutex: %d", ret); - } - else{ - TSK_DEBUG_ERROR("Failed to unlock the mutex: %d", ret); - } - } - } - return ret; + if(ret == TSK_ERROR_NOT_OWNER) { + TSK_DEBUG_WARN("The calling thread does not own the mutex: %d", ret); + } + else { + TSK_DEBUG_ERROR("Failed to unlock the mutex: %d", ret); + } + } + } + return ret; } /**@ingroup tsk_mutex_group @@ -176,17 +173,17 @@ int tsk_mutex_unlock(tsk_mutex_handle_t* handle) */ void tsk_mutex_destroy(tsk_mutex_handle_t** handle) { - if(handle && *handle){ + if(handle && *handle) { #if TSK_UNDER_WINDOWS - CloseHandle((MUTEX_T)*handle); - *handle = tsk_null; + CloseHandle((MUTEX_T)*handle); + *handle = tsk_null; #else - pthread_mutex_destroy((MUTEX_T)*handle); - tsk_free(handle); + pthread_mutex_destroy((MUTEX_T)*handle); + tsk_free(handle); #endif - } - else{ - TSK_DEBUG_WARN("Cannot free an uninitialized mutex"); - } + } + else { + TSK_DEBUG_WARN("Cannot free an uninitialized mutex"); + } } |