实验性的代码,保留了Linux kernel下的接口,实现用的是AIX的锁机制(暂未提供基于Complex lock的rw_lock的实现):
-
typedef struct spinlock{
-
Simple_lock lock;
-
int int_pri;
-
}spinlock_t;
-
-
struct mutex{
-
Simple_lock lock;
-
/* 1: unlocked, 0: locked, negative: locked, possible waiters */
-
atomic_t count;
-
};
-
-
static inline void __spin_lock_irq(spinlock_t *lockp)
-
{
-
lockp->int_pri = disable_lock(INTMAX, &lockp->lock);
-
}
-
-
static inline void __spin_unlock_irq(spinlock_t *lockp)
-
{
-
unlock_enable(lockp->int_pri, &lockp->lock);
-
}
-
-
#ifdef LOCK_DATA_INSTRUMENTATION
-
-
#define __SIMPLE_LOCK_COMMON_INIT(_lockp) lock_alloc(_lockp, LOCK_ALLOC_PIN, OEM_CLASS_DEF, INSTANCE)
-
-
#define mutex_destroy(_lockp) lock_free(_lockp)
-
#define spin_lock_destroy(_lockp) mutex_destroy(_lockp)
-
-
#else //LOCK_DATA_INSTRUMENTATION
-
#define __SIMPLE_LOCK_COMMON_INIT(_lockp)
-
#define mutex_destroy(_lockp)
-
#define spin_lock_destroy(_lockp)
-
#endif
-
-
#define spin_lock_init(_lockp) \
-
do { \
-
__SIMPLE_LOCK_COMMON_INIT(&(_lockp)->lock); \
-
simple_lock_init(&(_lockp)->lock); \
-
} while (0)
-
-
#define mutex_init(mutex) \
-
do { \
-
__SIMPLE_LOCK_COMMON_INIT(&(mutex)->lock); \
-
simple_lock_init(&(mutex)->lock); \
-
atomic_set(&(mutex)->count, 1); \
-
} while (0)
-
-
/* define and initialize a spinlock statically, don't call spin_lock_destroy upon this lock var */
-
#define DEFINE_SPINLOCK(x) spinlock_t x = {.lock = {SIMPLE_LOCK_AVAIL}}
-
-
/* define and initialize a mutex statically, don't call mutex_destroy upon this lock var */
-
#define DEFINE_MUTEX(mutexname) \
-
struct mutex mutexname = \
-
{.count = ATOMIC_INIT(1), \
-
.lock = {SIMPLE_LOCK_AVAIL}}
-
-
static inline void mutex_lock(struct mutex *lock)
-
{
-
MUTEX_LOCK_TRACE(lock);
-
simple_lock(&lock->lock);
-
atomic_dec(&lock->count);
-
}
-
-
static inline void mutex_unlock(struct mutex *lock)
-
{
-
MUTEX_UNLOCK_TRACE(lock);
-
atomic_inc(&lock->count);
-
simple_unlock(&lock->lock);
-
}
-
-
static inline bool mutex_trylock(struct mutex *lock)
-
{
-
bool bret = simple_lock_try(&lock->lock);
-
if (bret)
-
atomic_dec(&lock->count);
-
-
return bret;
-
}
-
-
#define spin_lock(lockp) simple_lock(&(lockp)->lock)
-
#define spin_unlock(lockp) simple_unlock(&(lockp)->lock)
-
-
#define spin_lock_irq(lockp) __spin_lock_irq(lockp)
-
#define spin_unlock_irq(lockp) __spin_unlock_irq(lockp)
-
-
/**
-
* mutex_is_locked - is the mutex locked
-
* @lock: the mutex to be queried
-
*
-
* Returns 1 if the mutex is locked, 0 if unlocked.
-
*/
-
static inline int mutex_is_locked(struct mutex *lock)
-
{
-
return atomic_read(&lock->count) != 1;
-
}
阅读(3490) | 评论(0) | 转发(0) |