rtems atomic operation (bit set_bit and test_and_clear_bit)

Chan Kim ckim at etri.re.kr
Sun Sep 6 08:32:29 UTC 2015

I'm on sparc v8 architecture and I looked up casa (compare and swap) in the manual, but there seems to be no casa instruction.
I changed not to use bit operation not it works fine. The ISR set bits but the tasklet thread reset the bit while clearing other bit (because I was not using atomic operation). So I just separted event information to separate int veriables and now the problem is gone (it was last night). 


보낸 사람 : "rg at irfu.se" <rg at irfu.se>
보낸 날짜 : 2015-09-05 20:20:30 ( +09:00 )
받는 사람 : 김찬 <ckim at etri.re.kr>
참조 : 
제목 : Re: rtems atomic operation (bit set_bit and test_and_clear_bit)

On the leon3 I'm running on sparc v8 there is a casa instruction from sparc v9.. It works well implement in assembler compile for v9 move the resulting instructions as data into the ordinary code compiling as v8...read sparc v9 spec for casa set the memory access bits correctly forgot details now...  If not casa if think you use ldstub.. 
Sent from my Cyanogen phone
Sent from my Cyanogen phone
On 5 Sep 2015 11:52 am, Chan Kim <ckim at etri.re.kr> wrote:

Dear rtems users, 

I have an ISR which sets some bits according to interrupt and a thread which tests the bit and if it is '1' clears it. 
The setting and testing/clearing  of the bits should be done using atomic operation. 

In linux case, there are conveinient functions 
- static inline void set_bit(unsigned long nr, volatile unsigned long *addr) and 
- static inline int test_and_clear_bit(unsigned long nr, volatile unsigned long *addr) 
they provide atomic operations. (nr is the bit number and addr is the address of the unsigned long. 

I impleted this function without atomicity and it's now causing problems. So I have to fix it now. 
What are the equivalents in rtems? (I tried using spinlock around accessing the variable but the task can go to sleep holding the lock) 
I found  _Atomic_Flag_test_and_set and _Atomic_Flag_clear in rtems-4.10.99-src/cpukit/score/include/rtems/score/atomic.h 
But couldn't find any example on how to use these functions. 
(BTW, I'm looking for set and test_and_clear, but rtems provides clear and test_and_set!!  :) ) 

Please somebody tell me how to implement set_bit and test_and_clear_bit in rtems. 

Chan Kim 
users mailing list 
users at rtems.org 

More information about the users mailing list