Re: race conditions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jun 14, 2004 at 19:40:12 -0400, Ed L Cashin wrote:
> Hmm.  What about this (untested, but simple) code?  It saves the flags
> in one function and restores them in another, but the flags variable
> is not shared with anyone else.

It's perfectly ok. No problem. You can safely pass flags around.

> I think I've heard that you aren't supposed to do this, but I'm
> interested to know why.  (And whether it's true!)

You are not supposed to. But it's not because it wouldn't work. It's
because it's prone to errors. Generaly, for any and all locks, you
should always lock and unlock in the same function. That way it's easy
to see that the locks are properly matched with unlocks.

> static spinlock_t mylock = SPIN_LOCK_UNLOCKED;
> 
> static void unlock(unsigned long flags)
> {
>     spin_unlock_irqrestore(&mylock, flags);
> }
> 
> void demo(void)
> {
>     unsigned long flags;
>     
>     spin_lock_irqsave(&mylock, flags);
>     /* use locked resource ... */
>     unlock(flags);
> }
> 
> -- 
> --Ed L Cashin            |   PGP public key:
>   ecashin@uga.edu        |   http://noserose.net/e/pgp/
> 
> 
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 
-------------------------------------------------------------------------------
						 Jan 'Bulb' Hudec <bulb@ucw.cz>

Attachment: signature.asc
Description: Digital signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux