赞
踩
内核支持名为atomic_t的32位的原子数据类型。一个这种类型的变量可以被task,fiber或者ISR以中断的方式进行读操作。这保证了目标操作不受高优先级上下文的调度的干扰,甚至当高优先级上下文操作同一个变量。
Use the atomic services to implement critical section processing that only requires the manipulation of a single 32-bit data item.
注意:使用原子变量通常比使用其他技术来实现对临界区域的访问更为高效,比如用微内核锁(a microkernel mutex),将处理降级到fiber级别(offloading the processing to a fiber)或者锁中断(locking interrupts)
This code shows how a function can keep track of the number of times it has been invoked. Since the count is incremented atomically, there is no risk that it will become corrupted in mid-increment if the routine is interrupted by the scheduling of a higher priority context that also calls the routine.
atomic_t call_count;
int call_counting_routine(void)
{
/* increment invocation counter */
atomic_inc(&call_count);
/* do rest of routine's processing */
...
}
The following atomic operation APIs are provided by atomic.h
:
atomic_get()
atomic_set()
atomic_clear()
atomic_add()
atomic_sub()
atomic_inc()
atomic_dec()
atomic_and()
atomic_or()
atomic_xor()
atomic_nand()
atomic_cas()
atomic_set_bit()
atomic_clear_bit()
atomic_test_bit()
atomic_test_and_set_bit()
atomic_test_and_clear_bit()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。