|
MythTV
0.26-pre
|
00001 /* 00002 * This file is part of libbluray 00003 * Copyright (C) 2010 hpi1 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Lesser General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2.1 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library. If not, see 00017 * <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #ifndef LIBBLURAY_MUTEX_H_ 00021 #define LIBBLURAY_MUTEX_H_ 00022 00023 #if HAVE_CONFIG_H 00024 #include "config.h" 00025 #endif 00026 00027 #if defined(_WIN32) 00028 # include <windows.h> 00029 #elif defined(HAVE_PTHREAD_H) 00030 # include <pthread.h> 00031 #else 00032 # error no mutex support found 00033 #endif 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 #if defined(_WIN32) 00040 00041 #include <errno.h> 00042 00043 typedef CRITICAL_SECTION BD_MUTEX; 00044 00045 #define bd_mutex_lock(m) \ 00046 (EnterCriticalSection(m), 0) 00047 00048 #define bd_mutex_unlock(m) \ 00049 (LeaveCriticalSection(m), 0) 00050 00051 #define bd_mutex_trylock(m) \ 00052 (TryEnterCriticalSection(m) ? 0 : EBUSY) 00053 00054 #define bd_mutex_init(m) \ 00055 (InitializeCriticalSection(m), 0) 00056 00057 #define bd_mutex_destroy(m) \ 00058 (DeleteCriticalSection(m), 0) 00059 00060 00061 #elif defined(HAVE_PTHREAD_H) 00062 00063 /* 00064 * recursive mutex 00065 */ 00066 00067 typedef struct bd_mutex_s BD_MUTEX; 00068 struct bd_mutex_s { 00069 int lock_count; 00070 pthread_t owner; 00071 pthread_mutex_t mutex; 00072 }; 00073 00074 static inline int bd_mutex_init(BD_MUTEX *p) 00075 { 00076 p->owner = (pthread_t)-1; 00077 p->lock_count = 0; 00078 00079 if (pthread_mutex_init(&p->mutex, NULL)) { 00080 BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_init() failed !"); 00081 return -1; 00082 } 00083 00084 return 0; 00085 } 00086 00087 static inline int bd_mutex_destroy(BD_MUTEX *p) 00088 { 00089 if (pthread_mutex_destroy(&p->mutex)) { 00090 BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_destroy() failed !"); 00091 return -1; 00092 } 00093 return 0; 00094 } 00095 00096 static int bd_mutex_lock(BD_MUTEX *p) 00097 { 00098 if (pthread_equal(p->owner, pthread_self())) { 00099 /* recursive lock */ 00100 p->lock_count++; 00101 return 0; 00102 } 00103 00104 if (pthread_mutex_lock(&p->mutex)) { 00105 BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_lock() failed !"); 00106 return -1; 00107 } 00108 00109 p->owner = pthread_self(); 00110 p->lock_count = 1; 00111 00112 return 0; 00113 } 00114 00115 static int bd_mutex_unlock(BD_MUTEX *p) 00116 { 00117 if (!pthread_equal(p->owner, pthread_self())) { 00118 BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock(): not owner !"); 00119 return -1; 00120 } 00121 00122 p->lock_count--; 00123 if (p->lock_count > 0) { 00124 return 0; 00125 } 00126 00127 /* unlock */ 00128 00129 p->owner = (pthread_t)-1; 00130 00131 if (pthread_mutex_unlock(&p->mutex)) { 00132 BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock() failed !"); 00133 return -1; 00134 } 00135 00136 return 0; 00137 } 00138 00139 #endif // HAVE_PTHREAD_H 00140 00141 #ifdef __cplusplus 00142 }; 00143 #endif 00144 00145 #endif // LIBBLURAY_MUTEX_H_
1.7.6.1