> -----Original Message----- > From: kernelnewbies-bounces+jharan=bytemobile.com@xxxxxxxxxxxxxxxxx > [mailto:kernelnewbies- > bounces+jharan=bytemobile.com@xxxxxxxxxxxxxxxxx] On Behalf Of Kai > Meyer > Sent: Wednesday, November 09, 2011 3:12 PM > To: kernelnewbies@xxxxxxxxxxxxxxxxx > Subject: Re: Spinlocks and interrupts > > Ok, I need mutual exclusion on a data structure regardless of interrupts > and core. It sounds like it can be done by using a spinlock and > disabling interrupts, but you mention that "spinlocks are intended to > provide mutual exclsion between interrupt context and non-interrupt > context." Should I be using a semaphore (mutex) instead? > > Perhaps I could explain my problem with some code: > struct my_struct *get_data(spinlock_t *mylock, int ALLOC_DATA) > { > struct my_struct *mydata = NULL; > spin_lock(mylock); > if (test_bit(index, mybitmap)) > mydata = retrieve_data(); > if (!mydata && ALLOC_DATA) { > mydata = alloc_data(); > set_bit(index, mybitmap); > } > spin_unlock(mylock); > return mydata; > } > > I need to prevent retrieve_data from being called if the index bit is > set in mybitmap and alloc_data has not completed, so I use a bitmap to > indicate that alloc_data has completed. I also need to protect > alloc_data from being run multiple times, so I use the spin_lock to > ensure that test_bit (and possibly retrieve_data) is not run while > alloc_data is being run (because it runs while the bit is cleared). > > -Kai Meyer You probably want to lock with spin_lock_irqsave() and unlock with spin_unlock_irqrestore() if you are not sure about what contexts get_data() will be called in. There's plenty of examples of how to use these in the kernel sources. I note you are passing in the address of the spinlock itself. Be wary of deadly embraces where two threads acquire two locks in different order. Jeff Haran _______________________________________________ Kernelnewbies mailing list Kernelnewbies@xxxxxxxxxxxxxxxxx http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies