Chapter 5. Cheat Sheet For Locking

Table of Contents

Chapter 6. The trylock Functions

Chapter 6. The trylock Functions

There are functions that try to acquire a lock only once and immediately return a value telling about success or failure to acquire the lock. They can be used if you need no access to the data protected with the lock when some other thread is holding the lock. You should acquire the lock later if you then need access to the data protected with the lock.

spin_trylock() does not spin but returns non-zero if it acquires the spinlock on the first try or 0 if not. This function can be used in all contexts like spin_lock: you must have disabled the contexts that might interrupt you and acquire the spin lock.

mutex_trylock() does not suspend your task but returns non-zero if it could lock the mutex on the first try or 0 if not. This function cannot be safely used in hardware or software interrupt contexts despite not sleeping.

./usr/share/doc/linux-doc-2.6.32/html/kernel-locking/ch10s02.html0000644000000000000000000000473312334427700022650 0ustar rootroot Some Functions Which Don't Sleep

Some Functions Which Don't Sleep

Some functions are safe to call from any context, or holding almost any lock.

  • printk()

  • kfree()

  • add_timer() and del_timer()