#ifndef CMA_MUTEX
#define CMA_MUTEX
#include <cma.h>
#include <cma_attr.h>
#include <cma_defs.h>
#include <cma_semaphore_defs.h>
#include <cma_sequence.h>
#include <cma_tcb_defs.h>
#include <cma_stack.h>
typedef struct CMA__T_INT_MUTEX {
cma__t_object header;
cma__t_int_attr *attributes;
cma__t_int_tcb *owner;
cma_t_integer nest_count;
cma__t_atomic_bit *unlock;
cma__t_atomic_bit lock;
struct CMA__T_INT_MUTEX *int_lock;
cma__t_atomic_bit event;
cma__t_atomic_bit waiters;
cma__t_atomic_bit bitbucket;
cma_t_mutex_kind mutex_kind;
cma__t_semaphore semaphore;
} cma__t_int_mutex;
#ifdef NDEBUG
# define cma__int_lock(mutex) { \
if (cma__test_and_set (&((cma__t_int_mutex *)mutex)->lock)) { \
cma_t_status res;\
res = cma__int_mutex_block ((cma__t_int_mutex *)mutex); \
if (res != cma_s_normal) cma__error (res); \
} \
}
#else
# define cma__int_lock(mutex) { \
cma__t_int_tcb *__ltcb__; \
__ltcb__ = cma__get_self_tcb (); \
if (cma__test_and_set (&((cma__t_int_mutex *)mutex)->lock)) { \
cma_t_status res;\
res = cma__int_mutex_block ((cma__t_int_mutex *)mutex); \
if (res != cma_s_normal) cma__error (res); \
} \
((cma__t_int_mutex *)mutex)->owner = __ltcb__; \
}
#endif
#ifdef NDEBUG
# define cma__int_unlock(mutex) { \
cma__unset (((cma__t_int_mutex *)mutex)->unlock); \
if (!cma__test_and_set (&((cma__t_int_mutex *)mutex)->event)) { \
cma_t_status res;\
res = cma__int_mutex_unblock ((cma__t_int_mutex *)mutex); \
if (res != cma_s_normal) cma__error (res); \
} \
}
#else
# define cma__int_unlock(mutex) { \
cma__t_int_tcb *__utcb__; \
__utcb__ = cma__get_self_tcb (); \
if (((cma__t_int_mutex *)mutex)->mutex_kind == cma_c_mutex_fast) { \
cma__assert_warn ( \
(__utcb__ == ((cma__t_int_mutex *)mutex)->owner), \
"attempt to release mutx owned by another thread"); \
((cma__t_int_mutex *)mutex)->owner = (cma__t_int_tcb *)cma_c_null_ptr; \
} \
cma__unset (((cma__t_int_mutex *)mutex)->unlock); \
if (!cma__test_and_set (&((cma__t_int_mutex *)mutex)->event)) { \
cma_t_status res;\
res = cma__int_mutex_unblock ((cma__t_int_mutex *)mutex); \
if (res != cma_s_normal) cma__error (res); \
} \
}
#endif
#define cma__int_mutex_delete(_mutex_) { \
cma__t_int_mutex *_int_mutex_; \
_int_mutex_ = cma__validate_null_mutex (_mutex_); \
if (_int_mutex_ == (cma__t_int_mutex *)cma_c_null_ptr) \
return; \
if (cma__int_mutex_locked (_int_mutex_)) \
cma__error (cma_s_in_use); \
cma__free_mutex (_int_mutex_); \
cma__clear_handle (_mutex_); \
}
extern cma__t_sequence cma__g_mutex_seq;
extern cma__t_int_mutex *cma__g_global_lock;
extern void cma__destroy_mutex (cma__t_int_mutex *);
extern void cma__free_mutex (cma__t_int_mutex *);
extern void cma__free_mutex_nolock (cma__t_int_mutex *);
extern cma__t_int_mutex * cma__get_first_mutex (cma__t_int_attr *);
extern cma__t_int_mutex * cma__get_mutex (cma__t_int_attr *);
extern void cma__init_mutex (void);
extern cma_t_status cma__int_mutex_block (cma__t_int_mutex *);
extern cma_t_boolean cma__int_mutex_locked (cma__t_int_mutex *);
extern cma_t_boolean cma__int_try_lock (cma__t_int_mutex *);
extern cma_t_status cma__int_mutex_unblock (cma__t_int_mutex *);
extern cma_t_boolean cma__mutex_locked (cma_t_mutex);
extern void cma__reinit_mutex (cma_t_integer);
#endif