From 3411c1060056e46a52d6fbbcf0b172346eecce54 Mon Sep 17 00:00:00 2001 From: John Birrell Date: Tue, 9 Jun 1998 23:13:10 +0000 Subject: Add support for compile time debug. This is enabled if libc_r is built with -D_LOCK_DEBUG. This adds the file name and line number to each lock call and these are stored in the spinlock structure. When using debug mode, the lock function will check if the thread is trying to lock something it has already locked. This is not supposed to happen because the lock will be freed too early. Without lock debug, libc_r should be smaller and slightly faster. --- lib/libpthread/thread/thr_spinlock.c | 72 ++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 20 deletions(-) (limited to 'lib/libpthread') diff --git a/lib/libpthread/thread/thr_spinlock.c b/lib/libpthread/thread/thr_spinlock.c index 01c29bdd12da..9da115e5eedb 100644 --- a/lib/libpthread/thread/thr_spinlock.c +++ b/lib/libpthread/thread/thr_spinlock.c @@ -29,7 +29,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_spinlock.c,v 1.2 1998/05/05 21:47:58 jb Exp $ + * $Id: uthread_spinlock.c,v 1.3 1998/06/06 07:27:06 jb Exp $ * */ @@ -38,9 +38,10 @@ #include #include #include -#include "spinlock.h" #include "pthread_private.h" +extern char *__progname; + /* * Lock a location for the running thread. Yield to allow other * threads to run if this thread is blocked because the lock is @@ -48,29 +49,60 @@ * assumes that the lock will be available very soon. */ void -_spinlock(volatile long * volatile lck) +_spinlock(spinlock_t *lck) { - do { - /* - * Allow other threads to run if the lock is not - * available: - */ - while (*lck != 0) { - /* Check if already locked by the running thread: */ - if (*lck == (long) _thread_run) { - char str[40]; - snprintf(str,sizeof(str),"Warning: Thread %p attempted to lock %p which it had already locked!\n",_thread_run,lck); - _thread_sys_write(2,str,strlen(str)); - return; - } + /* + * Try to grab the lock and loop if another thread grabs + * it before we do. + */ + while(_atomic_lock(&lck->access_lock)) { + /* Give up the time slice: */ + sched_yield(); - /* Give up the time slice: */ - sched_yield(); - } + /* Check if already locked by the running thread: */ + if (lck->lock_owner == (long) _thread_run) + return; + } + /* The running thread now owns the lock: */ + lck->lock_owner = (long) _thread_run; +} + +/* + * Lock a location for the running thread. Yield to allow other + * threads to run if this thread is blocked because the lock is + * not available. Note that this function does not sleep. It + * assumes that the lock will be available very soon. + * + * This function checks if the running thread has already locked the + * location, warns if this occurs and creates a thread dump before + * returning. + */ +void +_spinlock_debug(spinlock_t *lck, char *fname, int lineno) +{ /* * Try to grab the lock and loop if another thread grabs * it before we do. */ - } while(_atomic_lock(lck,(long) _thread_run)); + while(_atomic_lock(&lck->access_lock)) { + /* Give up the time slice: */ + sched_yield(); + + /* Check if already locked by the running thread: */ + if (lck->lock_owner == (long) _thread_run) { + char str[256]; + snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) which it had already locked in %s (%d)\n", __progname, _thread_run, lck, fname, lineno, lck->fname, lck->lineno); + _thread_sys_write(2,str,strlen(str)); + + /* Create a thread dump to help debug this problem: */ + _thread_dump_info(); + return; + } + } + + /* The running thread now owns the lock: */ + lck->lock_owner = (long) _thread_run; + lck->fname = fname; + lck->lineno = lineno; } -- cgit v1.3