1 /*------------------------------------------------------------------------- 2 * drawElements Thread Library 3 * --------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief Unix implementation of mutex. 22 *//*--------------------------------------------------------------------*/ 23 24 #include "deMutex.h" 25 26 #if (DE_OS == DE_OS_UNIX || DE_OS == DE_OS_ANDROID || DE_OS == DE_OS_SYMBIAN || DE_OS == DE_OS_QNX || \ 27 DE_OS == DE_OS_OSX || DE_OS == DE_OS_IOS) || \ 28 (DE_OS == DE_OS_FUCHSIA) 29 30 #include "deMemory.h" 31 32 #include <pthread.h> 33 34 /* \todo [2009-11-12 pyry] It is quite nasty to allocate mutex structs from heap. */ 35 36 DE_STATIC_ASSERT(sizeof(deMutex) >= sizeof(pthread_mutex_t *)); 37 deMutex_create(const deMutexAttributes * attributes)38deMutex deMutex_create(const deMutexAttributes *attributes) 39 { 40 pthread_mutexattr_t attr; 41 int ret; 42 pthread_mutex_t *mutex = deMalloc(sizeof(pthread_mutex_t)); 43 44 if (!mutex) 45 return 0; 46 47 if (pthread_mutexattr_init(&attr) != 0) 48 { 49 deFree(mutex); 50 return 0; 51 } 52 53 #if defined(DE_DEBUG) 54 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) 55 #else 56 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL) != 0) 57 #endif 58 { 59 pthread_mutexattr_destroy(&attr); 60 deFree(mutex); 61 return 0; 62 } 63 64 if (attributes) 65 { 66 if (attributes->flags & DE_MUTEX_RECURSIVE) 67 { 68 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) 69 { 70 pthread_mutexattr_destroy(&attr); 71 deFree(mutex); 72 return 0; 73 } 74 } 75 } 76 77 ret = pthread_mutex_init(mutex, &attr); 78 if (ret != 0) 79 { 80 pthread_mutexattr_destroy(&attr); 81 deFree(mutex); 82 return 0; 83 } 84 85 pthread_mutexattr_destroy(&attr); 86 87 return (deMutex)mutex; 88 } 89 deMutex_destroy(deMutex mutex)90void deMutex_destroy(deMutex mutex) 91 { 92 pthread_mutex_t *pMutex = (pthread_mutex_t *)mutex; 93 DE_ASSERT(pMutex); 94 pthread_mutex_destroy(pMutex); 95 deFree(pMutex); 96 } 97 deMutex_lock(deMutex mutex)98void deMutex_lock(deMutex mutex) 99 { 100 int ret = pthread_mutex_lock((pthread_mutex_t *)mutex); 101 DE_ASSERT(ret == 0); 102 DE_UNREF(ret); 103 } 104 deMutex_unlock(deMutex mutex)105void deMutex_unlock(deMutex mutex) 106 { 107 int ret = pthread_mutex_unlock((pthread_mutex_t *)mutex); 108 DE_ASSERT(ret == 0); 109 DE_UNREF(ret); 110 } 111 deMutex_tryLock(deMutex mutex)112bool deMutex_tryLock(deMutex mutex) 113 { 114 return (pthread_mutex_trylock((pthread_mutex_t *)mutex) == 0); 115 } 116 117 #endif /* DE_OS */ 118