LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2009, 07:16 PM   #1
ravi.is.chunduru
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Rep: Reputation: 0
Atomic macros versus Spin locks


Once in a while, I do Kernel programming in kernel space. I was told that atomic operations uses LOCK instruction in x86 and hence these are not good to use. I was told that incrementing variable under spin locks is a better option than using atomic_inc.

Based on number of instructions that gets executed in atomic_inc function is very small compared to instructions it takes to do lock, increment variables and release lock. Hence I expect using atomic_inc is better option from performance perspective. My simple experiment also proved this. But my colleague says that when there is any PCI traffic such as Ethernet traffic, then one would see the atomic_inc is not relatively good.

Any comments?

Thanks
Ravi
 
Old 01-10-2009, 07:43 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Well, spin-locks use those same atomic instructions in most architectures.

Code for simplicity and for the general case. Remember that your solution is expected to work equally well on all architectures. It's highly unlikely that the microseconds you may actually save will be noticeable in practice. But the hours you spend trying to troubleshoot a weird timing-bug will be.

Last edited by sundialsvcs; 01-10-2009 at 07:45 PM.
 
Old 01-11-2009, 03:08 PM   #3
ravi.is.chunduru
LQ Newbie
 
Registered: Jan 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you for the quick response.

I agree that many non-x86 kernels have similar implementation between spin locks and atomic operations. It is very clear that atomic operations are better than incrementing a variable under locks in non-x6 (Example: Power PC) environments. My question was specific to x86 based Linux where LOCK instruction is used in atomic operations.

Thanks
Ravi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on asm/atomic.h Lothar Schwab Programming 0 08-15-2007 07:13 PM
Atomic operation question PatrickNew Programming 4 05-12-2007 12:11 AM
bad: scheduling while atomic! ??? okeyla Linux - Newbie 1 09-26-2005 02:41 PM
atomic clock sync salviadud Slackware 8 02-28-2005 11:28 PM
to spin down or not to spin down, that is the question maenho Linux - Software 2 02-05-2005 08:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration