| 1 |
/* |
| 2 |
* This file is part of libbluray |
| 3 |
* Copyright (C) 2010-2014 hpi1 |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library. If not, see |
| 17 |
* <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#if HAVE_CONFIG_H |
| 21 |
#include "config.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "mutex.h" |
| 25 |
|
| 26 |
#include "logging.h" |
| 27 |
#include "macro.h" |
| 28 |
|
| 29 |
#if defined(_WIN32) |
| 30 |
# include <windows.h> |
| 31 |
#elif defined(HAVE_PTHREAD_H) |
| 32 |
# include <pthread.h> |
| 33 |
#else |
| 34 |
# error no mutex support found |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
#if defined(_WIN32) |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
CRITICAL_SECTION cs; |
| 42 |
} MUTEX_IMPL; |
| 43 |
|
| 44 |
static int _mutex_lock(MUTEX_IMPL *p) |
| 45 |
{ |
| 46 |
EnterCriticalSection(&p->cs); |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
static int _mutex_unlock(MUTEX_IMPL *p) |
| 51 |
{ |
| 52 |
LeaveCriticalSection(&p->cs); |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
static int _mutex_init(MUTEX_IMPL *p) |
| 57 |
{ |
| 58 |
InitializeCriticalSection(&p->cs); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
static int _mutex_destroy(MUTEX_IMPL *p) |
| 63 |
{ |
| 64 |
DeleteCriticalSection(&p->cs); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
#elif defined(HAVE_PTHREAD_H) |
| 70 |
|
| 71 |
typedef struct { |
| 72 |
int lock_count; |
| 73 |
pthread_t owner; |
| 74 |
pthread_mutex_t mutex; |
| 75 |
} MUTEX_IMPL; |
| 76 |
|
| 77 |
static int _mutex_init(MUTEX_IMPL *p) |
| 78 |
{ |
| 79 |
p->owner = (pthread_t)-1; |
| 80 |
p->lock_count = 0; |
| 81 |
|
| 82 |
if (pthread_mutex_init(&p->mutex, NULL)) { |
| 83 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_init() failed !\n"); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
static int _mutex_lock(MUTEX_IMPL *p) |
| 91 |
{ |
| 92 |
if (pthread_equal(p->owner, pthread_self())) { |
| 93 |
/* recursive lock */ |
| 94 |
p->lock_count++; |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
if (pthread_mutex_lock(&p->mutex)) { |
| 99 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_lock() failed !\n"); |
| 100 |
return -1; |
| 101 |
} |
| 102 |
|
| 103 |
p->owner = pthread_self(); |
| 104 |
p->lock_count = 1; |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
static int _mutex_unlock(MUTEX_IMPL *p) |
| 110 |
{ |
| 111 |
if (!pthread_equal(p->owner, pthread_self())) { |
| 112 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock(): not owner !\n"); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
p->lock_count--; |
| 117 |
if (p->lock_count > 0) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
/* unlock */ |
| 122 |
|
| 123 |
p->owner = (pthread_t)-1; |
| 124 |
|
| 125 |
if (pthread_mutex_unlock(&p->mutex)) { |
| 126 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_unlock() failed !\n"); |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
static int _mutex_destroy(MUTEX_IMPL *p) |
| 134 |
{ |
| 135 |
_mutex_lock(p); |
| 136 |
_mutex_unlock(p); |
| 137 |
|
| 138 |
if (pthread_mutex_destroy(&p->mutex)) { |
| 139 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_destroy() failed !\n"); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
#endif /* HAVE_PTHREAD_H */ |
| 147 |
|
| 148 |
int bd_mutex_lock(BD_MUTEX *p) |
| 149 |
{ |
| 150 |
if (!p->impl) { |
| 151 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_lock() failed !\n"); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
return _mutex_lock((MUTEX_IMPL*)p->impl); |
| 155 |
} |
| 156 |
|
| 157 |
int bd_mutex_unlock(BD_MUTEX *p) |
| 158 |
{ |
| 159 |
if (!p->impl) { |
| 160 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock() failed !\n"); |
| 161 |
return -1; |
| 162 |
} |
| 163 |
return _mutex_unlock((MUTEX_IMPL*)p->impl); |
| 164 |
} |
| 165 |
|
| 166 |
int bd_mutex_init(BD_MUTEX *p) |
| 167 |
{ |
| 168 |
p->impl = calloc(1, sizeof(MUTEX_IMPL)); |
| 169 |
if (!p->impl) { |
| 170 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_init() failed !\n"); |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
if (_mutex_init((MUTEX_IMPL*)p->impl) < 0) { |
| 175 |
X_FREE(p->impl); |
| 176 |
return -1; |
| 177 |
} |
| 178 |
|
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
int bd_mutex_destroy(BD_MUTEX *p) |
| 183 |
{ |
| 184 |
if (!p->impl) { |
| 185 |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_destroy() failed !\n"); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
if (_mutex_destroy((MUTEX_IMPL*)p->impl) < 0) { |
| 190 |
return -1; |
| 191 |
} |
| 192 |
|
| 193 |
X_FREE(p->impl); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|