1112918Sjeff/* 2112918Sjeff * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>. 3112918Sjeff * All rights reserved. 4112918Sjeff * 5112918Sjeff * Redistribution and use in source and binary forms, with or without 6112918Sjeff * modification, are permitted provided that the following conditions 7112918Sjeff * are met: 8112918Sjeff * 1. Redistributions of source code must retain the above copyright 9112918Sjeff * notice, this list of conditions and the following disclaimer. 10112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright 11112918Sjeff * notice, this list of conditions and the following disclaimer in the 12112918Sjeff * documentation and/or other materials provided with the distribution. 13165967Simp * 3. Neither the name of the author nor the names of any co-contributors 14112918Sjeff * may be used to endorse or promote products derived from this software 15112918Sjeff * without specific prior written permission. 16112918Sjeff * 17112918Sjeff * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 18112918Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20112918Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21112918Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22112918Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23112918Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24112918Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25112918Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26112918Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27112918Sjeff * SUCH DAMAGE. 28112918Sjeff * 29112918Sjeff * $FreeBSD$ 30112918Sjeff * 31112918Sjeff */ 32112918Sjeff 33124837Smtm#include <sys/types.h> 34112918Sjeff#include <pthread.h> 35112918Sjeff#include <libc_private.h> 36144518Sdavidxu#include <spinlock.h> 37112918Sjeff 38112918Sjeff#include "thr_private.h" 39112918Sjeff 40154288Sjasone#define MAX_SPINLOCKS 72 41124837Smtm 42144518Sdavidxu/* 43144518Sdavidxu * These data structures are used to trace all spinlocks 44144518Sdavidxu * in libc. 45144518Sdavidxu */ 46144518Sdavidxustruct spinlock_extra { 47144518Sdavidxu spinlock_t *owner; 48162061Sdavidxu struct umutex lock; 49144518Sdavidxu}; 50124837Smtm 51162061Sdavidxustatic struct umutex spinlock_static_lock = DEFAULT_UMUTEX; 52144518Sdavidxustatic struct spinlock_extra extra[MAX_SPINLOCKS]; 53144518Sdavidxustatic int spinlock_count; 54144518Sdavidxustatic int initialized; 55124837Smtm 56144518Sdavidxustatic void init_spinlock(spinlock_t *lck); 57124837Smtm 58124837Smtm/* 59144518Sdavidxu * These are for compatability only. Spinlocks of this type 60144518Sdavidxu * are deprecated. 61124837Smtm */ 62124837Smtm 63144518Sdavidxuvoid 64144518Sdavidxu_spinunlock(spinlock_t *lck) 65124837Smtm{ 66178446Sdelphij struct spinlock_extra *_extra; 67162061Sdavidxu 68178446Sdelphij _extra = (struct spinlock_extra *)lck->fname; 69178446Sdelphij THR_UMUTEX_UNLOCK(_get_curthread(), &_extra->lock); 70124837Smtm} 71124837Smtm 72112918Sjeffvoid 73144518Sdavidxu_spinlock(spinlock_t *lck) 74112918Sjeff{ 75178446Sdelphij struct spinlock_extra *_extra; 76162061Sdavidxu 77144518Sdavidxu if (!__isthreaded) 78144518Sdavidxu PANIC("Spinlock called when not threaded."); 79144518Sdavidxu if (!initialized) 80144518Sdavidxu PANIC("Spinlocks not initialized."); 81144518Sdavidxu if (lck->fname == NULL) 82144518Sdavidxu init_spinlock(lck); 83178446Sdelphij _extra = (struct spinlock_extra *)lck->fname; 84178446Sdelphij THR_UMUTEX_LOCK(_get_curthread(), &_extra->lock); 85112918Sjeff} 86112918Sjeff 87112918Sjeffvoid 88157457Sdavidxu_spinlock_debug(spinlock_t *lck, char *fname __unused, int lineno __unused) 89112918Sjeff{ 90144518Sdavidxu _spinlock(lck); 91115271Smtm} 92115271Smtm 93144518Sdavidxustatic void 94144518Sdavidxuinit_spinlock(spinlock_t *lck) 95123350Smtm{ 96162061Sdavidxu struct pthread *curthread = _get_curthread(); 97123350Smtm 98162061Sdavidxu THR_UMUTEX_LOCK(curthread, &spinlock_static_lock); 99144518Sdavidxu if ((lck->fname == NULL) && (spinlock_count < MAX_SPINLOCKS)) { 100144518Sdavidxu lck->fname = (char *)&extra[spinlock_count]; 101162061Sdavidxu _thr_umutex_init(&extra[spinlock_count].lock); 102144518Sdavidxu extra[spinlock_count].owner = lck; 103144518Sdavidxu spinlock_count++; 104144518Sdavidxu } 105162061Sdavidxu THR_UMUTEX_UNLOCK(curthread, &spinlock_static_lock); 106162061Sdavidxu if (lck->fname == NULL) 107162061Sdavidxu PANIC("Warning: exceeded max spinlocks"); 108123350Smtm} 109123350Smtm 110112918Sjeffvoid 111144518Sdavidxu_thr_spinlock_init(void) 112112918Sjeff{ 113144518Sdavidxu int i; 114144518Sdavidxu 115162061Sdavidxu _thr_umutex_init(&spinlock_static_lock); 116144518Sdavidxu if (initialized != 0) { 117144518Sdavidxu /* 118144518Sdavidxu * called after fork() to reset state of libc spin locks, 119144518Sdavidxu * it is not quite right since libc may be in inconsistent 120144518Sdavidxu * state, resetting the locks to allow current thread to be 121144518Sdavidxu * able to hold them may not help things too much, but 122144518Sdavidxu * anyway, we do our best. 123144518Sdavidxu * it is better to do pthread_atfork in libc. 124144518Sdavidxu */ 125144518Sdavidxu for (i = 0; i < spinlock_count; i++) 126162061Sdavidxu _thr_umutex_init(&extra[i].lock); 127144518Sdavidxu } else { 128144518Sdavidxu initialized = 1; 129144518Sdavidxu } 130112918Sjeff} 131