2005-04-17 00:20:36 +02: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 02:00:51 +01:00
|
|
|
#include <linux/compiler.h>
|
2009-01-06 23:40:39 +01:00
|
|
|
#include <linux/types.h>
|
2012-03-28 19:30:02 +02:00
|
|
|
#include <asm/cmpxchg.h>
|
2008-10-21 17:45:58 +02:00
|
|
|
#include <arch/atomic.h>
|
2014-03-13 19:00:37 +01:00
|
|
|
#include <arch/system.h>
|
|
|
|
#include <asm/barrier.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Atomic operations that C can't guarantee us. Useful for
|
|
|
|
* resource counting etc..
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ATOMIC_INIT(i) { (i) }
|
|
|
|
|
2014-09-23 16:29:50 +02:00
|
|
|
#define atomic_read(v) ACCESS_ONCE((v)->counter)
|
2005-04-17 00:20:36 +02: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-23 18:19:25 +01: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 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2014-03-23 18:19:25 +01:00
|
|
|
#define ATOMIC_OPS(op, c_op) ATOMIC_OP(op, c_op) ATOMIC_OP_RETURN(op, c_op)
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2014-03-23 18:19:25 +01:00
|
|
|
ATOMIC_OPS(add, +=)
|
|
|
|
ATOMIC_OPS(sub, -=)
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2014-03-23 18:19:25 +01:00
|
|
|
#undef ATOMIC_OPS
|
|
|
|
#undef ATOMIC_OP_RETURN
|
|
|
|
#undef ATOMIC_OP
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2014-03-23 18:19:25 +01:00
|
|
|
#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-11-07 09:58:44 +01:00
|
|
|
static inline int atomic_sub_and_test(int i, volatile atomic_t *v)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
unsigned long flags;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
retval = (v->counter -= i) == 0;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-07 09:58:44 +01:00
|
|
|
static inline void atomic_inc(volatile atomic_t *v)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
(v->counter)++;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-11-07 09:58:44 +01:00
|
|
|
static inline void atomic_dec(volatile atomic_t *v)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
(v->counter)--;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2005-11-07 09:58:44 +01:00
|
|
|
static inline int atomic_inc_return(volatile atomic_t *v)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int retval;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_save(v, flags);
|
2008-01-21 17:01:31 +01:00
|
|
|
retval = ++(v->counter);
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-07 09:58:44 +01:00
|
|
|
static inline int atomic_dec_return(volatile atomic_t *v)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int retval;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_save(v, flags);
|
2008-01-21 17:01:31 +01:00
|
|
|
retval = --(v->counter);
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
return retval;
|
|
|
|
}
|
2005-11-07 09:58:44 +01:00
|
|
|
static inline int atomic_dec_and_test(volatile atomic_t *v)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
unsigned long flags;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
retval = --(v->counter) == 0;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-07 09:58:44 +01:00
|
|
|
static inline int atomic_inc_and_test(volatile atomic_t *v)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
unsigned long flags;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_save(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
retval = ++(v->counter) == 0;
|
2005-07-27 20:44:42 +02:00
|
|
|
cris_atomic_restore(v, flags);
|
2005-04-17 00:20:36 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-11-14 01:07:24 +01: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 00:59:17 +01:00
|
|
|
#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
|
|
|
|
|
2011-07-27 01:09:07 +02:00
|
|
|
static inline int __atomic_add_unless(atomic_t *v, int a, int u)
|
2005-11-14 01:07:25 +01: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 01:09:07 +02:00
|
|
|
return ret;
|
2005-11-14 01:07:25 +01:00
|
|
|
}
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
#endif
|