How to use spinlocks

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

How to use spinlocks

Post by Jeko »

I'm writing multiprocessing support for my OS.

I don't know very well how to use spinlocks, I have some doubts...
For example, if i have a list of tasks, I must acquire a spinlock only when I modify the list (adding or removing a entry) or also when I read data from the list?
And if I have a list of threads inside each task structure, I must acquire a spinlock for each list of thread, or for the list of tasks?

Another small question... Must I disable interrupts when I acquire a spinlock? If yes, why? The spinlock locks the modifying and reading of shared structures, so an interrupt routine can't modify a shared structure (for example the list of tasks) if there is a spinlock that locks the operation. Am I wrong?

I've read a tutorial on spinlocks, but I can't understand very well these two things.
However the tutorial is really good
http://osdev.berlios.de/spinlock_part1.html
http://osdev.berlios.de/spinlock_part2.html
http://osdev.berlios.de/spinlock_part3.html
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: How to use spinlocks

Post by salil_bhagurkar »

1. Even when you are reading the list, you require spinlocks. This is because if some other control is modifying the list then your control which is only reading the list will probably read crap. For example, if there is a string pointer in a list node. Suppose a control of execution is allocating the pointer and then initializing it to a string. Suppose another control which is reading the list reads this nodes and prints out the string. If it prints the string when it just got allocated then, the string does not contain anything and will possibly create undesired behaviour in the system. You could have separate read/write and read-only spinlocks. If there is a rw lock acquired then reads or rws are not allowed. But if a read lock is acquired then other reads are allowed. So summing up, you need a spinlock for a read if somebody is modifying the list.

2. This does not have any problems though whatever you implement. If you go for a lock for each list of threads, then that will be a bit of more code/overhead. But when it comes to performance, you might get a better performance when you go for separate locks for each list of threads This is because you allow two execution controls to modify two list of threads independently acquiring two different locks. If you were to have a single lock, then the threads would interlock and you won't benefit on an SMP machine with more than one processors. But again you could go for the simpler version of having a single lock initially.

3. Depends on your implementation IMHO... You could analyze and figure out.. If you are not modifying any lists through the IRQs then theres no need to disable interrupts..
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: How to use spinlocks

Post by Jeko »

This thing of Read/Write Spinlocks is really interesting... I will implement it! Thank you!
Rewriting virtual memory manager - Working on ELF support - Working on Device Drivers Handling

http://sourceforge.net/projects/jeko - Jeko Operating System
Post Reply