]> jfr.im git - irc/atheme/libmowgli-2.git/commitdiff
Change mowgli_thread to use create/init/deinit/destroy API style
authorPatrick McFarland <redacted>
Wed, 28 Mar 2012 15:24:45 +0000 (11:24 -0400)
committerPatrick McFarland <redacted>
Wed, 28 Mar 2012 15:24:45 +0000 (11:24 -0400)
src/libmowgli/concurrent/atomic.c
src/libmowgli/core/heap.c
src/libmowgli/eventloop/eventloop.c
src/libmowgli/eventloop/eventloop.h
src/libmowgli/ext/global_storage.c
src/libmowgli/mowgli.h
src/libmowgli/thread/Makefile
src/libmowgli/thread/mutex.c
src/libmowgli/thread/mutex.h [new file with mode: 0644]
src/libmowgli/thread/thread.h

index 3256b71ac98e077d5414249238a108920c4c8689..37b753a0976c3d1abee5d162b78f85ee717e38dc 100644 (file)
@@ -27,5 +27,5 @@ mowgli_mutex_t mowgli_atomic_mutex[256];
 
 void mowgli_atomic_bootstrap() {
        for(int i = 0; i < 256; i++)
-               mowgli_mutex_create(&mowgli_atomic_mutex[i]);
+               mowgli_mutex_init(&mowgli_atomic_mutex[i]);
 }
index 56fe68c36cef57554092aba4e5ba21d9fdc0017a..c72310d69f06d84bb215a7353642861d58a1c8a7 100644 (file)
@@ -204,7 +204,7 @@ mowgli_heap_create_full(size_t elem_size, size_t mowgli_heap_elems, unsigned int
        bh->use_mmap = allocator != NULL ? FALSE : TRUE;
 #endif
 
-       if(mowgli_mutex_create(&bh->mutex) != 0)
+       if(mowgli_mutex_init(&bh->mutex) != 0)
                mowgli_throw_exception_fatal("heap mutex can't be created");
 
        if (flags & BH_NOW)
@@ -236,7 +236,7 @@ mowgli_heap_destroy(mowgli_heap_t *heap)
        if (heap->empty_block)
                mowgli_heap_shrink(heap, heap->empty_block);
 
-       mowgli_mutex_destroy(&heap->mutex);
+       mowgli_mutex_uninit(&heap->mutex);
 
        /* everything related to heap has gone, time for itself */
        mowgli_free(heap);
index 18adb95a76b3dcef53ce4b8831b40b89959bf6ec..7d5804e55910ffc317d5faffa20a8c9a80527ce6 100644 (file)
@@ -79,7 +79,7 @@ mowgli_eventloop_t *mowgli_eventloop_create(void)
        eventloop->eventloop_ops = &_mowgli_winsock_pollops;
 #endif
 
-       if (mowgli_mutex_create(&eventloop->mutex) != 0)
+       if (mowgli_mutex_init(&eventloop->mutex) != 0)
        {
                mowgli_log("couldn't create mutex for eventloop %p, aborting...", eventloop);
                abort();
@@ -96,7 +96,7 @@ void mowgli_eventloop_destroy(mowgli_eventloop_t *eventloop)
 {
        eventloop->eventloop_ops->pollshutdown(eventloop);
 
-       mowgli_mutex_destroy(&eventloop->mutex);
+       mowgli_mutex_uninit(&eventloop->mutex);
        mowgli_heap_free(eventloop_heap, eventloop);
 }
 
index d0e75a3f176ebfb0d4c561d54331b8fb547e5658..cc7fbd26bfd3bdab3c99ef463aa67c8fd91b57d4 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef __MOWGLI_EVENTLOOP_EVENTLOOP_H__
 #define __MOWGLI_EVENTLOOP_EVENTLOOP_H__
 
-#include "thread/thread.h"
-
 #ifndef _WIN32
 
 typedef int mowgli_descriptor_t;
index 53183678852f65c20f025587155b37e79de5fd16..4fec74de054d88aaa7ed06247f87f8334144f67d 100644 (file)
@@ -36,7 +36,7 @@ mowgli_global_storage_bootstrap(void)
 {
        mowgli_global_storage_dict = mowgli_patricia_create(_storage_key_canon);
 
-       mowgli_mutex_create(&mowgli_global_storage_lock);
+       mowgli_mutex_init(&mowgli_global_storage_lock);
 }
 
 void *
index b024fc1c55b65a06ecc78b34c5dddc60e6deeb9d..0982fe4ac83563c5471fa0c772968337d56f12b1 100644 (file)
@@ -50,8 +50,13 @@ MOWGLI_DECLS_START
 #include "container/list.h"
 #include "object/class.h"
 #include "object/object.h"
+
 #include "core/allocation_policy.h"
 #include "core/alloc.h"
+
+#include "thread/thread.h"
+#include "thread/mutex.h"
+
 #include "base/memslice.h"
 #include "container/patricia.h"
 #include "module/module.h"
@@ -75,7 +80,7 @@ MOWGLI_DECLS_START
 #include "core/allocator.h"
 #include "base/formatter.h"
 #include "container/index.h"
-#include "thread/thread.h"
+
 #include "ext/confparse.h"
 #include "ext/program_opts.h"
 #include "concurrent/atomic.h"
index 8452fba74660727cbd4e3bd124746b0c09536c6c..769deb467123228a46f730d365112ba95eabfff6 100644 (file)
@@ -8,7 +8,7 @@ SRCS = mutex.c                  \
        posix_mutexops.c                \
        win32_mutexops.c
 
-INCLUDES = thread.h
+INCLUDES = thread.h mutex.h
 
 include ../../../buildsys.mk
 
index 5be11431695e2df05a33fdc5947ccb0c60841dc6..1a2e9807d7ce54fcef61a69d521f714a6d2d9dc7 100644 (file)
@@ -56,7 +56,21 @@ static inline mowgli_mutex_ops_t *get_mutex_platform(void)
        return &_mowgli_null_mutex_ops;
 }
 
-int mowgli_mutex_create(mowgli_mutex_t *mutex)
+mowgli_mutex_t *mowgli_mutex_create(void)
+{
+       mowgli_mutex_t *mutex = mowgli_alloc(sizeof(mowgli_mutex_t));
+
+       return_val_if_fail(mutex != NULL, NULL);
+
+       if(mowgli_mutex_init(mutex)) {
+               return mutex;
+       } else {
+               mowgli_free(mutex);
+               return NULL;
+       }
+}
+
+int mowgli_mutex_init(mowgli_mutex_t *mutex)
 {
        static bool initialized = false;
        mowgli_mutex_ops_t *mutex_ops = get_mutex_platform();
@@ -96,7 +110,7 @@ int mowgli_mutex_unlock(mowgli_mutex_t *mutex)
        return mutex_ops->mutex_unlock(mutex);
 }
 
-int mowgli_mutex_destroy(mowgli_mutex_t *mutex)
+int mowgli_mutex_uninit(mowgli_mutex_t *mutex)
 {
        mowgli_mutex_ops_t *mutex_ops = get_mutex_platform();
 
@@ -105,6 +119,17 @@ int mowgli_mutex_destroy(mowgli_mutex_t *mutex)
        return mutex_ops->mutex_destroy(mutex);
 }
 
+mowgli_mutex_t *mowgli_mutex_destroy(mowgli_mutex_t *mutex) {
+       if(mutex != NULL)
+               return NULL;
+
+       return_val_if_fail(mowgli_mutex_uninit(mutex) != 0, NULL);
+
+       mowgli_free(mutex);
+
+       return NULL;
+}
+
 void mowgli_mutex_set_policy(mowgli_thread_policy_t policy)
 {
        switch (policy)
diff --git a/src/libmowgli/thread/mutex.h b/src/libmowgli/thread/mutex.h
new file mode 100644 (file)
index 0000000..ed51191
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * libmowgli: A collection of useful routines for programming.
+ * mutex.h: Cross-platform mutexes.
+ *
+ * Copyright (c) 2011 William Pitcock <nenolod@dereferenced.org>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice is present in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MOWGLI_MUTEX_H__
+#define __MOWGLI_MUTEX_H__
+
+#if defined(__sun) || defined(__sco)
+# include <thread.h>
+# include <synch.h>
+# define MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES
+# define MOWGLI_NATIVE_MUTEX_DECL(name)        mutex_t (name)
+#elif defined(_WIN32)
+# define MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES
+# define MOWGLI_NATIVE_MUTEX_DECL(name)        HANDLE (name)
+#else
+# include <pthread.h>
+#endif
+
+typedef struct {
+#ifdef MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES
+       MOWGLI_NATIVE_MUTEX_DECL(mutex);
+#else
+       pthread_mutex_t mutex;
+#endif
+} mowgli_mutex_t;
+
+#ifdef MOWGLI_NATIVE_MUTEX_DECL
+# undef MOWGLI_NATIVE_MUTEX_DECL
+#endif
+
+typedef struct {
+       int (*mutex_create)(mowgli_mutex_t *mutex);
+       int (*mutex_lock)(mowgli_mutex_t *mutex);
+       int (*mutex_trylock)(mowgli_mutex_t *mutex);
+       int (*mutex_unlock)(mowgli_mutex_t *mutex);
+       int (*mutex_destroy)(mowgli_mutex_t *mutex);
+} mowgli_mutex_ops_t;
+
+mowgli_mutex_t *mowgli_mutex_create(void);
+int mowgli_mutex_init(mowgli_mutex_t* mutex);
+int mowgli_mutex_lock(mowgli_mutex_t *mutex);
+int mowgli_mutex_trylock(mowgli_mutex_t *mutex);
+int mowgli_mutex_unlock(mowgli_mutex_t *mutex);
+int mowgli_mutex_uninit(mowgli_mutex_t *mutex);
+mowgli_mutex_t *mowgli_mutex_destroy(mowgli_mutex_t *mutex);
+
+void mowgli_mutex_set_policy(mowgli_thread_policy_t policy);
+
+/* simple dispatch function to set the ops up for the various subsystems. */
+static inline void mowgli_thread_set_policy(mowgli_thread_policy_t policy)
+{
+       mowgli_mutex_set_policy(policy);
+}
+
+#endif
+
index 9c907726faaae6e831934b6ce8adacd2e95c8b27..7ac7da370521066c755cf2c0f423ba342936bad9 100644 (file)
 
 #if defined(__sun) || defined(__sco)
 # include <thread.h>
-# include <synch.h>
-# define MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES
 # define MOWGLI_FEATURE_HAVE_NATIVE_THREADS
-# define MOWGLI_NATIVE_MUTEX_DECL(name)        mutex_t (name)
 # define MOWGLI_NATIVE_THREAD_DECL(name)       thread_t (name)
 #elif defined(_WIN32)
-# define MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES
 # define MOWGLI_FEATURE_HAVE_NATIVE_THREADS
-# define MOWGLI_NATIVE_MUTEX_DECL(name)        HANDLE (name)
 # define MOWGLI_NATIVE_THREAD_DECL(name)       HANDLE (name)
 #else
 # include <pthread.h>
 #endif
 
-typedef struct {
-#ifdef MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES
-       MOWGLI_NATIVE_MUTEX_DECL(mutex);
-#else
-       pthread_mutex_t mutex;
-#endif
-} mowgli_mutex_t;
-
-#ifdef MOWGLI_NATIVE_MUTEX_DECL
-# undef MOWGLI_NATIVE_MUTEX_DECL
-#endif
-
-typedef struct {
-       int (*mutex_create)(mowgli_mutex_t *mutex);
-       int (*mutex_lock)(mowgli_mutex_t *mutex);
-       int (*mutex_trylock)(mowgli_mutex_t *mutex);
-       int (*mutex_unlock)(mowgli_mutex_t *mutex);
-       int (*mutex_destroy)(mowgli_mutex_t *mutex);
-} mowgli_mutex_ops_t;
-
-int mowgli_mutex_create(mowgli_mutex_t *mutex);
-int mowgli_mutex_lock(mowgli_mutex_t *mutex);
-int mowgli_mutex_trylock(mowgli_mutex_t *mutex);
-int mowgli_mutex_unlock(mowgli_mutex_t *mutex);
-int mowgli_mutex_destroy(mowgli_mutex_t *mutex);
-
 typedef struct {
 #ifdef MOWGLI_FEATURE_HAVE_NATIVE_THREADS
        MOWGLI_NATIVE_THREAD_DECL(thread);
@@ -105,12 +74,5 @@ typedef enum {
        MOWGLI_THREAD_POLICY_DISABLED,
 } mowgli_thread_policy_t;
 
-void mowgli_mutex_set_policy(mowgli_thread_policy_t policy);
-
-/* simple dispatch function to set the ops up for the various subsystems. */
-static inline void mowgli_thread_set_policy(mowgli_thread_policy_t policy)
-{
-       mowgli_mutex_set_policy(policy);
-}
+#endif
 
-#endif /* !__MOWGLI_THREAD_H__ */