The code you are looking into is under the #else section of #ifdef CONFIG_SMP. The actual implementation of raw_spin_lock is under asm/spinlock.h For e.g in MIPS asm-mips/spinlock.h has the following code static inline void _raw_spin_lock(spinlock_t *lock) { unsigned int tmp; if (R10000_LLSC_WAR) { __asm__ __volatile__( " .set noreorder # _raw_spin_lock \n" "1: ll %1, %2 \n" " bnez %1, 1b \n" " li %1, 1 \n" " sc %1, %0 \n" " beqzl %1, 1b \n" " nop \n" " sync \n" " .set reorder \n" : "=m" (lock->lock), "=&r" (tmp) : "m" (lock->lock) : "memory"); } else { __asm__ __volatile__( " .set noreorder # _raw_spin_lock \n" "1: ll %1, %2 \n" " bnez %1, 1b \n" " li %1, 1 \n" " sc %1, %0 \n" " beqz %1, 1b \n" " sync \n" " .set reorder \n" : "=m" (lock->lock), "=&r" (tmp) : "m" (lock->lock) : "memory"); } } The ll/sc instructions are used for atomic operations. Sadik. On Thu, 03 Mar 2005 16:29:03 -0800, Om <omanakuttan@xxxxxxx> wrote: > I am looking into spin lock structure and locking mechamism as defined > in inclide/linux/spinlock.h and asm/spinlock.h > typedef struct { > unsigned long magic; > volatile unsigned long lock; > volatile unsigned int babble; > const char *module; > char *owner; > int oline; > } spinlock_t; > > I find all locking/unlocking mechanism is nothing but just decrementing > / incrementing operations. Of course they are done with local interrupts > disabled. > > #define _spin_lock_irqsave(lock, flags) \ > do { \ > local_irq_save(flags); \ > preempt_disable(); \ > _raw_spin_lock(lock); \ > } while (0) > > _raw_spin_lock() is, > > #define _raw_spin_lock(x) \ > do { \ > CHECK_LOCK(x); \ > if ((x)->lock&&(x)->babble) { \ > (x)->babble--; \ > printk("%s:%d: spin_lock(%s:%p) already locked by %s/%d\n", \ > __FILE__,__LINE__, (x)->module, \ > (x), (x)->owner, (x)->oline); \ > } \ > (x)->lock = 1; \ > (x)->owner = __FILE__; \ > (x)->oline = __LINE__; \ > } while (0) > > In an smp environment, what prevents 2 different cpu's from aquiring > same spin lock at exactly the same time? I was expecting `lock` to be > atomic, and the inc and dec operations were also atomic operations. > > Regards, > Om. > > -- > Kernelnewbies: Help each other learn about the Linux kernel. > Archive: http://mail.nl.linux.org/kernelnewbies/ > FAQ: http://kernelnewbies.org/faq/ > > -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/