uclinux-2008R1-RC8(bf561)到VDSP5的移植(30):atomic_t

来源:互联网 发布:spring源码视频教程 编辑:程序博客网 时间:2024/05/11 22:34
 
  
atomic的相关操作定义都在include/asm/atomic.h中,各个平台有不同的实现方法,比如在x86平台上,CPU提供了在指令执行期间对总线加锁的手段。CPU芯片上有一条引线#HLOCK pin,如果汇编语言的程序中在一条指令前面加上前缀"LOCK",经过汇编以后的机器代码就使CPU在执行这条指令的时候把#HLOCK pin的电位拉低,持续到这条指令结束时放开,从而把总线锁住,这样同一总线上别的CPU就暂时不能通过总线访问内存了,保证了这条指令在多处理器环境中的原子性。
在uclinux目前对atomic的实现中,有这样一个注释:
 
/*
 * Atomic operations that C can't guarantee us. Useful for
 * resource counting etc..
 *
 * Generally we do not concern about SMP BFIN systems, so we don't have
 * to deal with that.
 *
 * Tony Kou (tonyko@lineo.ca)   Lineo Inc.   2001
 */
也就是它并不考虑SMP的情况,我希望对此进行适当的修改。但是在bf561中,它并没有提供类似x86这样锁定总线的功能,它有的仅仅是一个TESTSET的指令,而无法在硬件上提供其它的锁定机制,因此我们需要为其添加一个类似于spinlock这样的锁。
查了下linux-2.6.19的内核代码,在asm-sparc/atomic.h中有一个类似的作法:
/* We do the bulk of the actual work out of line in two common
 * routines in assembler, see arch/sparc/lib/atomic.S for the
 * "fun" details.
 *
 * For SMP the trick is you embed the spin lock byte within
 * the word, use the low byte so signedness is easily retained
 * via a quick arithmetic shift. It looks like this:
 *
 *   ----------------------------------------
 *   | signed 24-bit counter value | lock | atomic_t
 *   ----------------------------------------
 *   31                          8 7      0
 */
为省事,干脆单独在atomic_t的结构体中加上一个spinlock:
 
typedef struct {
     int counter;
     testset_t lock;
} atomic_t;
相应的操作函数也作一下修改,如:
#define atomic_add_negative(a, v)    (atomic_add_return((a), (v)) < 0)
static inline int atomic_sub_return(int i, atomic_t * v)
{
     int __temp = 0;
     long flags;
 
     adi_acquire_lock(&v->lock);
 
     local_irq_save(flags);
     v->counter -= i;
     __temp = v->counter;
     local_irq_restore(flags);
 
     adi_release_lock(&v->lock);
 
     return __temp;
}
以后再考虑一下性能问题。