2005-04-17 06:20:36 +08:00
|
|
|
/* $Id: atomic.h,v 1.3 2001/07/25 16:15:19 bjornw Exp $ */
|
|
|
|
|
|
|
|
#ifndef __ASM_CRIS_ATOMIC__
|
|
|
|
#define __ASM_CRIS_ATOMIC__
|
|
|
|
|
2007-11-15 09:00:51 +08:00
|
|
|
#include <linux/compiler.h>
|
2009-01-07 06:40:39 +08:00
|
|
|
#include <linux/types.h>
|
2012-03-29 01:30:02 +08:00
|
|
|
#include <asm/cmpxchg.h>
|
2008-10-21 23:45:58 +08:00
|
|
|
#include <arch/atomic.h>
|
2014-03-14 02:00:37 +08:00
|
|
|
#include <arch/system.h>
|
|
|
|
#include <asm/barrier.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Atomic operations that C can't guarantee us. Useful for
|
|
|
|
* resource counting etc..
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ATOMIC_INIT(i) { (i) }
|
|
|
|
|
2014-09-23 22:29:50 +08:00
|
|
|
#define atomic_read(v) ACCESS_ONCE((v)->counter)
|
2005-04-17 06:20:36 +08:00
|
|
|
#define atomic_set(v,i) (((v)->counter) = (i))
|
|
|
|
|
|
|
|
/* These should be written in asm but we do it in C for now. */
|
|
|
|
|
2014-03-24 01:19:25 +08:00
|
|
|
#define ATOMIC_OP(op, c_op) \
|
|
|
|
static inline void atomic_##op(int i, volatile atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
unsigned long flags; \
|
|
|
|
cris_atomic_save(v, flags); \
|
|
|
|
v->counter c_op i; \
|
|
|
|
cris_atomic_restore(v, flags); \
|
|
|
|
} \
|
|
|
|
|
|
|
|
#define ATOMIC_OP_RETURN(op, c_op) \
|
|
|
|
static inline int atomic_##op##_return(int i, volatile atomic_t *v) \
|
|
|
|
{ \
|
|
|
|
unsigned long flags; \
|
|
|
|
int retval; \
|
|
|
|
cris_atomic_save(v, flags); \
|
|
|
|
retval = (v->counter c_op i); \
|
|
|
|
cris_atomic_restore(v, flags); \
|
|
|
|
return retval; \
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-03-24 01:19:25 +08:00
|
|
|
#define ATOMIC_OPS(op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-03-24 01:19:25 +08:00
|
|
|
ATOMIC_OPS(add, +=)
|
|
|
|
ATOMIC_OPS(sub, -=)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-03-24 01:19:25 +08:00
|
|
|
#undef ATOMIC_OPS
|
|
|
|
#undef ATOMIC_OP_RETURN
|
|
|
|
#undef ATOMIC_OP
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-03-24 01:19:25 +08:00
|
|
|
#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-07 16:58:44 +08:00
|
|
|
static inline int atomic_sub_and_test(int i, volatile atomic_t *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
unsigned long flags;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = (v->counter -= i) == 0;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-07 16:58:44 +08:00
|
|
|
static inline void atomic_inc(volatile atomic_t *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
(v->counter)++;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-11-07 16:58:44 +08:00
|
|
|
static inline void atomic_dec(volatile atomic_t *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
(v->counter)--;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-11-07 16:58:44 +08:00
|
|
|
static inline int atomic_inc_return(volatile atomic_t *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int retval;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_save(v, flags);
|
2008-01-22 00:01:31 +08:00
|
|
|
retval = ++(v->counter);
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-07 16:58:44 +08:00
|
|
|
static inline int atomic_dec_return(volatile atomic_t *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int retval;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_save(v, flags);
|
2008-01-22 00:01:31 +08:00
|
|
|
retval = --(v->counter);
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2005-11-07 16:58:44 +08:00
|
|
|
static inline int atomic_dec_and_test(volatile atomic_t *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
unsigned long flags;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = --(v->counter) == 0;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-07 16:58:44 +08:00
|
|
|
static inline int atomic_inc_and_test(volatile atomic_t *v)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
unsigned long flags;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = ++(v->counter) == 0;
|
2005-07-28 02:44:42 +08:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-14 08:07:24 +08:00
|
|
|
static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
cris_atomic_save(v, flags);
|
|
|
|
ret = v->counter;
|
|
|
|
if (likely(ret == old))
|
|
|
|
v->counter = new;
|
|
|
|
cris_atomic_restore(v, flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-01-10 07:59:17 +08:00
|
|
|
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
|
|
|
|
|
2011-07-27 07:09:07 +08:00
|
|
|
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
|
2005-11-14 08:07:25 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
cris_atomic_save(v, flags);
|
|
|
|
ret = v->counter;
|
|
|
|
if (ret != u)
|
|
|
|
v->counter += a;
|
|
|
|
cris_atomic_restore(v, flags);
|
2011-07-27 07:09:07 +08:00
|
|
|
return ret;
|
2005-11-14 08:07:25 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|