| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* mutex.h: Cross-platform mutexes. |
| 4 |
* |
| 5 |
* Copyright (c) 2011 William Pitcock <[email protected]> |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef MOWGLI_SRC_LIBMOWGLI_THREAD_MUTEX_H_INCLUDE_GUARD |
| 25 |
#define MOWGLI_SRC_LIBMOWGLI_THREAD_MUTEX_H_INCLUDE_GUARD 1 |
| 26 |
|
| 27 |
#include "platform/attributes.h" |
| 28 |
#include "thread/thread.h" |
| 29 |
|
| 30 |
#ifdef MOWGLI_OS_UNIX_TYPE |
| 31 |
# include <thread.h> |
| 32 |
# include <synch.h> |
| 33 |
# define MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES |
| 34 |
# define MOWGLI_NATIVE_MUTEX_DECL(name) mutex_t(name) |
| 35 |
#elif defined MOWGLI_OS_WIN |
| 36 |
# define MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES |
| 37 |
# define MOWGLI_NATIVE_MUTEX_DECL(name) HANDLE(name) |
| 38 |
#else |
| 39 |
# include <pthread.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
typedef struct mowgli_mutex_ mowgli_mutex_t; |
| 43 |
|
| 44 |
typedef struct |
| 45 |
{ |
| 46 |
int (*mutex_create)(mowgli_mutex_t *mutex); |
| 47 |
int (*mutex_lock)(mowgli_mutex_t *mutex); |
| 48 |
int (*mutex_trylock)(mowgli_mutex_t *mutex); |
| 49 |
int (*mutex_unlock)(mowgli_mutex_t *mutex); |
| 50 |
int (*mutex_destroy)(mowgli_mutex_t *mutex); |
| 51 |
} mowgli_mutex_ops_t; |
| 52 |
|
| 53 |
struct mowgli_mutex_ |
| 54 |
{ |
| 55 |
#ifdef MOWGLI_FEATURE_HAVE_NATIVE_MUTEXES |
| 56 |
MOWGLI_NATIVE_MUTEX_DECL(mutex); |
| 57 |
#else |
| 58 |
pthread_mutex_t mutex; |
| 59 |
#endif |
| 60 |
const mowgli_mutex_ops_t *ops; |
| 61 |
}; |
| 62 |
|
| 63 |
#ifdef MOWGLI_NATIVE_MUTEX_DECL |
| 64 |
# undef MOWGLI_NATIVE_MUTEX_DECL |
| 65 |
#endif |
| 66 |
|
| 67 |
mowgli_mutex_t *mowgli_mutex_create(void) |
| 68 |
MOWGLI_FATTR_MALLOC; |
| 69 |
|
| 70 |
int mowgli_mutex_init(mowgli_mutex_t *mutex); |
| 71 |
int mowgli_mutex_lock(mowgli_mutex_t *mutex); |
| 72 |
int mowgli_mutex_trylock(mowgli_mutex_t *mutex); |
| 73 |
int mowgli_mutex_unlock(mowgli_mutex_t *mutex); |
| 74 |
int mowgli_mutex_uninit(mowgli_mutex_t *mutex); |
| 75 |
void mowgli_mutex_destroy(mowgli_mutex_t *mutex); |
| 76 |
|
| 77 |
void mowgli_mutex_set_policy(mowgli_thread_policy_t policy); |
| 78 |
|
| 79 |
/* simple dispatch function to set the ops up for the various subsystems. */ |
| 80 |
static inline void |
| 81 |
mowgli_thread_set_policy(mowgli_thread_policy_t policy) |
| 82 |
{ |
| 83 |
mowgli_mutex_set_policy(policy); |
| 84 |
} |
| 85 |
|
| 86 |
#endif /* MOWGLI_SRC_LIBMOWGLI_THREAD_MUTEX_H_INCLUDE_GUARD */ |