On Thu, Feb 13, 2003 at 09:09:18PM +0100, Adam ENDRODI wrote: > > Hi list, > > > I came across a situation when I needed to hold a read-only lock > (in fact, a semaphore) but I haven't found a way to upgrade it > to act as a write lock. I mean, something like this: > > struct rwsemaphore sem; > > void foo(struct bar *data) > { > down(&sem); > if (!data->bar) { > down_rw(&sem); > data->bar = kmalloc(); > } > up(&sem); > } > > Is it possible to achieve via rwsemaphores? If not, is there a > way to emulate the same effect by using atomic_* and semaphore > operations, other than releasing ``sem'' and reaquiring it by > down_write()? No. Readlock can't be upgraded to a write one and the reason for this is principial. Consider two concurent readers. Both hold the read lock and then both decide to upgrade it to a write lock. But one can only be granted a write lock if noone is holding a read lock. So you have a deadlock. ------------------------------------------------------------------------------- Jan 'Bulb' Hudec <bulb@ucw.cz> -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/