[libbluray-devel] Move code from mutex.h to mutex.c.
hpi1
git at videolan.org
Thu Feb 19 12:20:27 CET 2015
libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Wed Feb 18 23:16:06 2015 +0200| [57baf98967fa2334096a3a361693f41ced8569a1] | committer: hpi1
Move code from mutex.h to mutex.c.
Avoid pulling system-dependent headers to other files.
> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=57baf98967fa2334096a3a361693f41ced8569a1
---
src/util/mutex.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++---
src/util/mutex.h | 100 ++----------------------------------
2 files changed, 147 insertions(+), 104 deletions(-)
diff --git a/src/util/mutex.c b/src/util/mutex.c
index 18f0cf3..c32bb5f 100644
--- a/src/util/mutex.c
+++ b/src/util/mutex.c
@@ -24,36 +24,173 @@
#include "mutex.h"
#include "logging.h"
+#include "macro.h"
+
+#if defined(_WIN32)
+# include <windows.h>
+#elif defined(HAVE_PTHREAD_H)
+# include <pthread.h>
+#else
+# error no mutex support found
+#endif
#if defined(_WIN32)
- /* nothing here */
+
+typedef struct {
+ CRITICAL_SECTION cs;
+} MUTEX_IMPL;
+
+static int _mutex_lock(MUTEX_IMPL *p)
+{
+ EnterCriticalSection(&p->cs);
+ return 0;
+}
+
+static int _mutex_unlock(MUTEX_IMPL *p)
+{
+ LeaveCriticalSection(&p->cs);
+ return 0;
+}
+
+static int _mutex_init(MUTEX_IMPL *p)
+{
+ InitializeCriticalSection(&p->cs);
+ return 0;
+}
+
+static int _mutex_destroy(MUTEX_IMPL *p)
+{
+ DeleteCriticalSection(&p->cs);
+ return 0;
+}
+
+
#elif defined(HAVE_PTHREAD_H)
-#include <pthread.h>
+typedef struct {
+ int lock_count;
+ pthread_t owner;
+ pthread_mutex_t mutex;
+} MUTEX_IMPL;
-int bd_mutex_init(BD_MUTEX *p)
+static int _mutex_init(MUTEX_IMPL *p)
{
p->owner = (pthread_t)-1;
p->lock_count = 0;
if (pthread_mutex_init(&p->mutex, NULL)) {
+ BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_init() failed !\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int _mutex_lock(MUTEX_IMPL *p)
+{
+ if (pthread_equal(p->owner, pthread_self())) {
+ /* recursive lock */
+ p->lock_count++;
+ return 0;
+ }
+
+ if (pthread_mutex_lock(&p->mutex)) {
+ BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_lock() failed !\n");
+ return -1;
+ }
+
+ p->owner = pthread_self();
+ p->lock_count = 1;
+
+ return 0;
+}
+
+static int _mutex_unlock(MUTEX_IMPL *p)
+{
+ if (!pthread_equal(p->owner, pthread_self())) {
+ BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock(): not owner !\n");
+ return -1;
+ }
+
+ p->lock_count--;
+ if (p->lock_count > 0) {
+ return 0;
+ }
+
+ /* unlock */
+
+ p->owner = (pthread_t)-1;
+
+ if (pthread_mutex_unlock(&p->mutex)) {
+ BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_unlock() failed !\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int _mutex_destroy(MUTEX_IMPL *p)
+{
+ _mutex_lock(p);
+ _mutex_unlock(p);
+
+ if (pthread_mutex_destroy(&p->mutex)) {
+ BD_DEBUG(DBG_BLURAY|DBG_CRIT, "pthread_mutex_destroy() failed !\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+#endif /* HAVE_PTHREAD_H */
+
+int bd_mutex_lock(BD_MUTEX *p)
+{
+ if (!p->impl) {
+ BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_lock() failed !\n");
+ return -1;
+ }
+ return _mutex_lock((MUTEX_IMPL*)p->impl);
+}
+
+int bd_mutex_unlock(BD_MUTEX *p)
+{
+ if (!p->impl) {
+ BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock() failed !\n");
+ return -1;
+ }
+ return _mutex_unlock((MUTEX_IMPL*)p->impl);
+}
+
+int bd_mutex_init(BD_MUTEX *p)
+{
+ p->impl = calloc(1, sizeof(MUTEX_IMPL));
+ if (!p->impl) {
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_init() failed !\n");
return -1;
}
+ if (_mutex_init((MUTEX_IMPL*)p->impl) < 0) {
+ X_FREE(p->impl);
+ return -1;
+ }
+
return 0;
}
int bd_mutex_destroy(BD_MUTEX *p)
{
- bd_mutex_lock(p);
- bd_mutex_unlock(p);
- if (pthread_mutex_destroy(&p->mutex)) {
+ if (!p->impl) {
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_destroy() failed !\n");
return -1;
}
+
+ if (_mutex_destroy((MUTEX_IMPL*)p->impl) < 0) {
+ return -1;
+ }
+
+ X_FREE(p->impl);
return 0;
}
-#endif /* HAVE_PTHREAD_H */
diff --git a/src/util/mutex.h b/src/util/mutex.h
index 1e7a8ee..47b766b 100644
--- a/src/util/mutex.h
+++ b/src/util/mutex.h
@@ -20,56 +20,7 @@
#ifndef LIBBLURAY_MUTEX_H_
#define LIBBLURAY_MUTEX_H_
-#if HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#if defined(_WIN32)
-# include <windows.h>
-#elif defined(HAVE_PTHREAD_H)
-# include <pthread.h>
-#else
-# error no mutex support found
-#endif
-
-
-#if defined(_WIN32)
-
-#include <errno.h>
-
-typedef CRITICAL_SECTION BD_MUTEX;
-
-static inline int bd_mutex_lock(BD_MUTEX *p) {
- EnterCriticalSection(p);
- return 0;
-}
-
-static inline int bd_mutex_unlock(BD_MUTEX *p) {
- LeaveCriticalSection(p);
- return 0;
-}
-
-#if 0
-static int bd_mutex_trylock(BD_MUTEX *p) {
- return TryEnterCriticalSection(p) ? 0 : EBUSY;
-}
-#endif
-
-static inline int bd_mutex_init(BD_MUTEX *p) {
- InitializeCriticalSection(p);
- return 0;
-}
-
-static inline int bd_mutex_destroy(BD_MUTEX *p) {
- DeleteCriticalSection(p);
- return 0;
-}
-
-
-#elif defined(HAVE_PTHREAD_H)
-
#include "attributes.h"
-#include "logging.h"
/*
* recursive mutex
@@ -77,58 +28,13 @@ static inline int bd_mutex_destroy(BD_MUTEX *p) {
typedef struct bd_mutex_s BD_MUTEX;
struct bd_mutex_s {
- int lock_count;
- pthread_t owner;
- pthread_mutex_t mutex;
+ void *impl;
};
BD_PRIVATE int bd_mutex_init(BD_MUTEX *p);
BD_PRIVATE int bd_mutex_destroy(BD_MUTEX *p);
-static int bd_mutex_lock(BD_MUTEX *p)
-{
- if (pthread_equal(p->owner, pthread_self())) {
- /* recursive lock */
- p->lock_count++;
- return 0;
- }
-
- if (pthread_mutex_lock(&p->mutex)) {
- BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_lock() failed !\n");
- return -1;
- }
-
- p->owner = pthread_self();
- p->lock_count = 1;
-
- return 0;
-}
-
-static int bd_mutex_unlock(BD_MUTEX *p)
-{
- if (!pthread_equal(p->owner, pthread_self())) {
- BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock(): not owner !\n");
- return -1;
- }
-
- p->lock_count--;
- if (p->lock_count > 0) {
- return 0;
- }
-
- /* unlock */
-
- p->owner = (pthread_t)-1;
-
- if (pthread_mutex_unlock(&p->mutex)) {
- BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_unlock() failed !\n");
- return -1;
- }
-
- return 0;
-}
-
-#endif // HAVE_PTHREAD_H
-
+BD_PRIVATE int bd_mutex_lock(BD_MUTEX *p);
+BD_PRIVATE int bd_mutex_unlock(BD_MUTEX *p);
#endif // LIBBLURAY_MUTEX_H_
More information about the libbluray-devel
mailing list