|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2010-2014 Petri Hintukainen <phintuka@users.sourceforge.net>
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is free software; you can redistribute it and/or
|
|
Packit |
5e46da |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License as published by the Free Software Foundation; either
|
|
Packit |
5e46da |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
5e46da |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5e46da |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
5e46da |
* Lesser General Public License for more details.
|
|
Packit |
5e46da |
*
|
|
Packit |
5e46da |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
5e46da |
* License along with this library. If not, see
|
|
Packit |
5e46da |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
5e46da |
*/
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#if HAVE_CONFIG_H
|
|
Packit |
5e46da |
#include "config.h"
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include "mutex.h"
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include "logging.h"
|
|
Packit |
5e46da |
#include "macro.h"
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#if defined(_WIN32)
|
|
Packit |
5e46da |
# include <windows.h>
|
|
Packit |
5e46da |
#elif defined(HAVE_PTHREAD_H)
|
|
Packit |
5e46da |
# include <pthread.h>
|
|
Packit |
5e46da |
#else
|
|
Packit |
5e46da |
# error no mutex support found
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#if defined(_WIN32)
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
typedef struct {
|
|
Packit |
5e46da |
CRITICAL_SECTION cs;
|
|
Packit |
5e46da |
} MUTEX_IMPL;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_lock(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
EnterCriticalSection(&p->cs);
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_unlock(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
LeaveCriticalSection(&p->cs);
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_init(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
InitializeCriticalSection(&p->cs);
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_destroy(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
DeleteCriticalSection(&p->cs);
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#elif defined(HAVE_PTHREAD_H)
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
typedef struct {
|
|
Packit |
5e46da |
int lock_count;
|
|
Packit |
5e46da |
pthread_t owner;
|
|
Packit |
5e46da |
pthread_mutex_t mutex;
|
|
Packit |
5e46da |
} MUTEX_IMPL;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_init(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
p->owner = (pthread_t)-1;
|
|
Packit |
5e46da |
p->lock_count = 0;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (pthread_mutex_init(&p->mutex, NULL)) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_init() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_lock(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (pthread_equal(p->owner, pthread_self())) {
|
|
Packit |
5e46da |
/* recursive lock */
|
|
Packit |
5e46da |
p->lock_count++;
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (pthread_mutex_lock(&p->mutex)) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_lock() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
p->owner = pthread_self();
|
|
Packit |
5e46da |
p->lock_count = 1;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_unlock(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (!pthread_equal(p->owner, pthread_self())) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock(): not owner !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
p->lock_count--;
|
|
Packit |
5e46da |
if (p->lock_count > 0) {
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
/* unlock */
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
p->owner = (pthread_t)-1;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (pthread_mutex_unlock(&p->mutex)) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_unlock() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _mutex_destroy(MUTEX_IMPL *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
_mutex_lock(p);
|
|
Packit |
5e46da |
_mutex_unlock(p);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (pthread_mutex_destroy(&p->mutex)) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_destroy() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#endif /* HAVE_PTHREAD_H */
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
int bd_mutex_lock(BD_MUTEX *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (!p->impl) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_lock() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return _mutex_lock((MUTEX_IMPL*)p->impl);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
int bd_mutex_unlock(BD_MUTEX *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (!p->impl) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return _mutex_unlock((MUTEX_IMPL*)p->impl);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
int bd_mutex_init(BD_MUTEX *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
p->impl = calloc(1, sizeof(MUTEX_IMPL));
|
|
Packit |
5e46da |
if (!p->impl) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_init() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (_mutex_init((MUTEX_IMPL*)p->impl) < 0) {
|
|
Packit |
5e46da |
X_FREE(p->impl);
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
int bd_mutex_destroy(BD_MUTEX *p)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (!p->impl) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_destroy() failed !\n");
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (_mutex_destroy((MUTEX_IMPL*)p->impl) < 0) {
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
X_FREE(p->impl);
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|