2012-12-24 15:36:14 +04:00
|
|
|
#ifndef __CR_LOCK_H__
|
|
|
|
#define __CR_LOCK_H__
|
2011-12-26 20:33:09 +04:00
|
|
|
|
|
|
|
#include <linux/futex.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-01-09 17:02:47 +04:00
|
|
|
#include "asm/types.h"
|
|
|
|
#include "asm/atomic.h"
|
2011-12-26 20:33:09 +04:00
|
|
|
#include "syscall.h"
|
2013-02-26 17:08:44 +04:00
|
|
|
#include "bug.h"
|
2011-12-26 20:33:09 +04:00
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
typedef struct {
|
2012-09-11 17:55:15 +04:00
|
|
|
atomic_t raw;
|
2012-03-26 23:11:00 +04:00
|
|
|
} futex_t;
|
|
|
|
|
2012-04-03 00:52:00 +04:00
|
|
|
#define FUTEX_ABORT_FLAG (0x80000000)
|
|
|
|
#define FUTEX_ABORT_RAW (-1U)
|
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Get current futex @f value */
|
|
|
|
static inline u32 futex_get(futex_t *f)
|
2012-01-02 20:29:02 +04:00
|
|
|
{
|
2012-03-26 23:11:00 +04:00
|
|
|
return atomic_get(&f->raw);
|
2012-01-02 20:29:02 +04:00
|
|
|
}
|
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Set futex @f value to @v */
|
|
|
|
static inline void futex_set(futex_t *f, u32 v)
|
2011-12-26 20:33:09 +04:00
|
|
|
{
|
2013-08-16 19:31:55 +04:00
|
|
|
atomic_set(&f->raw, (int)v);
|
2011-12-26 20:33:09 +04:00
|
|
|
}
|
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
#define futex_init(f) futex_set(f, 0)
|
|
|
|
|
|
|
|
/* Wait on futex @__f value @__v become in condition @__c */
|
|
|
|
#define futex_wait_if_cond(__f, __v, __cond) \
|
|
|
|
do { \
|
|
|
|
int ret; \
|
|
|
|
u32 tmp; \
|
|
|
|
\
|
|
|
|
while (1) { \
|
2013-08-16 19:31:55 +04:00
|
|
|
tmp = (u32)atomic_get(&(__f)->raw); \
|
2012-04-03 00:52:00 +04:00
|
|
|
if ((tmp & FUTEX_ABORT_FLAG) || \
|
|
|
|
(tmp __cond (__v))) \
|
2012-03-26 23:11:00 +04:00
|
|
|
break; \
|
2013-08-16 19:31:55 +04:00
|
|
|
ret = sys_futex((u32 *)&(__f)->raw.counter, FUTEX_WAIT,\
|
2012-03-26 23:11:00 +04:00
|
|
|
tmp, NULL, NULL, 0); \
|
|
|
|
BUG_ON(ret < 0 && ret != -EWOULDBLOCK); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Set futex @f to @v and wake up all waiters */
|
|
|
|
static inline void futex_set_and_wake(futex_t *f, u32 v)
|
2011-12-26 20:33:09 +04:00
|
|
|
{
|
2013-08-16 19:31:55 +04:00
|
|
|
atomic_set(&f->raw, (int)v);
|
|
|
|
BUG_ON(sys_futex((u32 *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
|
2011-12-26 20:33:09 +04:00
|
|
|
}
|
|
|
|
|
2012-04-03 00:52:00 +04:00
|
|
|
/* Mark futex @f as wait abort needed and wake up all waiters */
|
|
|
|
static inline void futex_abort_and_wake(futex_t *f)
|
|
|
|
{
|
|
|
|
BUILD_BUG_ON(!(FUTEX_ABORT_RAW & FUTEX_ABORT_FLAG));
|
|
|
|
futex_set_and_wake(f, FUTEX_ABORT_RAW);
|
|
|
|
}
|
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Decrement futex @f value and wake up all waiters */
|
|
|
|
static inline void futex_dec_and_wake(futex_t *f)
|
2011-12-26 20:33:09 +04:00
|
|
|
{
|
2012-03-26 23:11:00 +04:00
|
|
|
atomic_dec(&f->raw);
|
2013-08-16 19:31:55 +04:00
|
|
|
BUG_ON(sys_futex((u32 *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
|
2011-12-26 20:33:09 +04:00
|
|
|
}
|
|
|
|
|
2013-01-11 14:50:44 +04:00
|
|
|
/* Increment futex @f value and wake up all waiters */
|
|
|
|
static inline void futex_inc_and_wake(futex_t *f)
|
|
|
|
{
|
|
|
|
atomic_inc(&f->raw);
|
2013-08-16 19:31:55 +04:00
|
|
|
BUG_ON(sys_futex((u32 *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
|
2013-01-11 14:50:44 +04:00
|
|
|
}
|
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Plain increment futex @f value */
|
2012-09-11 17:55:15 +04:00
|
|
|
static inline void futex_inc(futex_t *f) { atomic_inc(&f->raw); }
|
2012-01-19 01:33:15 +03:00
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Plain decrement futex @f value */
|
2012-09-11 17:55:15 +04:00
|
|
|
static inline void futex_dec(futex_t *f) { atomic_dec(&f->raw); }
|
2012-01-19 01:33:15 +03:00
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Wait until futex @f value become @v */
|
|
|
|
static inline void futex_wait_until(futex_t *f, u32 v)
|
|
|
|
{ futex_wait_if_cond(f, v, ==); }
|
2012-01-19 01:33:15 +03:00
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Wait while futex @f value is greater than @v */
|
|
|
|
static inline void futex_wait_while_gt(futex_t *f, u32 v)
|
|
|
|
{ futex_wait_if_cond(f, v, <=); }
|
2011-12-26 20:33:09 +04:00
|
|
|
|
2013-01-11 14:50:44 +04:00
|
|
|
/* Wait while futex @f value is less than @v */
|
|
|
|
static inline void futex_wait_while_lt(futex_t *f, u32 v)
|
|
|
|
{ futex_wait_if_cond(f, v, >=); }
|
|
|
|
|
2013-05-24 16:20:07 +04:00
|
|
|
/* Wait while futex @f value is equal to @v */
|
|
|
|
static inline void futex_wait_while_eq(futex_t *f, u32 v)
|
|
|
|
{ futex_wait_if_cond(f, v, !=); }
|
|
|
|
|
2012-03-26 23:11:00 +04:00
|
|
|
/* Wait while futex @f value is @v */
|
|
|
|
static inline void futex_wait_while(futex_t *f, u32 v)
|
|
|
|
{
|
2013-08-16 19:31:55 +04:00
|
|
|
while ((u32)atomic_get(&f->raw) == v) {
|
|
|
|
int ret = sys_futex((u32 *)&f->raw.counter, FUTEX_WAIT, v, NULL, NULL, 0);
|
2011-12-26 20:33:09 +04:00
|
|
|
BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-26 19:38:00 +04:00
|
|
|
typedef struct {
|
2012-09-11 17:55:15 +04:00
|
|
|
atomic_t raw;
|
2012-03-26 19:38:00 +04:00
|
|
|
} mutex_t;
|
|
|
|
|
2013-02-18 17:54:47 +04:00
|
|
|
static inline void mutex_init(mutex_t *m)
|
2011-12-26 20:33:09 +04:00
|
|
|
{
|
|
|
|
u32 c = 0;
|
2013-08-16 19:31:55 +04:00
|
|
|
atomic_set(&m->raw, (int)c);
|
2011-12-26 20:33:09 +04:00
|
|
|
}
|
|
|
|
|
2013-02-18 17:54:47 +04:00
|
|
|
static inline void mutex_lock(mutex_t *m)
|
2011-12-26 20:33:09 +04:00
|
|
|
{
|
|
|
|
u32 c;
|
|
|
|
int ret;
|
|
|
|
|
2013-08-16 19:31:55 +04:00
|
|
|
while ((c = (u32)atomic_inc_return(&m->raw)) != 1) {
|
|
|
|
ret = sys_futex((u32 *)&m->raw.counter, FUTEX_WAIT, c, NULL, NULL, 0);
|
2011-12-26 20:33:09 +04:00
|
|
|
BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-18 17:54:47 +04:00
|
|
|
static inline void mutex_unlock(mutex_t *m)
|
2011-12-26 20:33:09 +04:00
|
|
|
{
|
|
|
|
u32 c = 0;
|
2013-08-16 19:31:55 +04:00
|
|
|
atomic_set(&m->raw, (int)c);
|
|
|
|
BUG_ON(sys_futex((u32 *)&m->raw.counter, FUTEX_WAKE, 1, NULL, NULL, 0) < 0);
|
2011-12-26 20:33:09 +04:00
|
|
|
}
|
|
|
|
|
2012-12-25 22:40:24 +04:00
|
|
|
#endif /* __CR_LOCK_H__ */
|