xref: /freebsd/lib/libthr/thread/thr_spinlock.c (revision d0aa4fd3ca3bc2ff53b230bf224baac8ed886109)
1bb535300SJeff Roberson /*
2bb535300SJeff Roberson  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
3bb535300SJeff Roberson  * All rights reserved.
4bb535300SJeff Roberson  *
5bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
6bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
7bb535300SJeff Roberson  * are met:
8bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
9bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
10bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
12bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
13fed32d75SWarner Losh  * 3. Neither the name of the author nor the names of any co-contributors
14bb535300SJeff Roberson  *    may be used to endorse or promote products derived from this software
15bb535300SJeff Roberson  *    without specific prior written permission.
16bb535300SJeff Roberson  *
17bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bb535300SJeff Roberson  * SUCH DAMAGE.
28bb535300SJeff Roberson  *
29bb535300SJeff Roberson  * $FreeBSD$
30bb535300SJeff Roberson  *
31bb535300SJeff Roberson  */
32bb535300SJeff Roberson 
33dec04f43SMike Makonnen #include <sys/types.h>
34bb535300SJeff Roberson #include <pthread.h>
35bb535300SJeff Roberson #include <libc_private.h>
36a091d823SDavid Xu #include <spinlock.h>
37bb535300SJeff Roberson 
38bb535300SJeff Roberson #include "thr_private.h"
39bb535300SJeff Roberson 
4090dc7952SJason Evans #define	MAX_SPINLOCKS	72
41dec04f43SMike Makonnen 
42dec04f43SMike Makonnen /*
43a091d823SDavid Xu  * These data structures are used to trace all spinlocks
44a091d823SDavid Xu  * in libc.
45dec04f43SMike Makonnen  */
46a091d823SDavid Xu struct spinlock_extra {
47a091d823SDavid Xu 	spinlock_t	*owner;
48bddd24cdSDavid Xu 	struct umutex	lock;
49a091d823SDavid Xu };
50dec04f43SMike Makonnen 
51bddd24cdSDavid Xu static struct umutex		spinlock_static_lock = DEFAULT_UMUTEX;
52a091d823SDavid Xu static struct spinlock_extra	extra[MAX_SPINLOCKS];
53a091d823SDavid Xu static int			spinlock_count;
54a091d823SDavid Xu static int			initialized;
55dec04f43SMike Makonnen 
56a091d823SDavid Xu static void	init_spinlock(spinlock_t *lck);
57a091d823SDavid Xu 
58a091d823SDavid Xu /*
59a091d823SDavid Xu  * These are for compatability only.  Spinlocks of this type
60a091d823SDavid Xu  * are deprecated.
61a091d823SDavid Xu  */
62dec04f43SMike Makonnen 
63bb535300SJeff Roberson void
64bb535300SJeff Roberson _spinunlock(spinlock_t *lck)
65bb535300SJeff Roberson {
66d0aa4fd3SXin LI 	struct spinlock_extra	*_extra;
67bddd24cdSDavid Xu 
68d0aa4fd3SXin LI 	_extra = (struct spinlock_extra *)lck->fname;
69d0aa4fd3SXin LI 	THR_UMUTEX_UNLOCK(_get_curthread(), &_extra->lock);
70bb535300SJeff Roberson }
71bb535300SJeff Roberson 
72bb535300SJeff Roberson void
73bb535300SJeff Roberson _spinlock(spinlock_t *lck)
74bb535300SJeff Roberson {
75d0aa4fd3SXin LI 	struct spinlock_extra *_extra;
76bddd24cdSDavid Xu 
77a091d823SDavid Xu 	if (!__isthreaded)
78a091d823SDavid Xu 		PANIC("Spinlock called when not threaded.");
79a091d823SDavid Xu 	if (!initialized)
80a091d823SDavid Xu 		PANIC("Spinlocks not initialized.");
81a091d823SDavid Xu 	if (lck->fname == NULL)
82a091d823SDavid Xu 		init_spinlock(lck);
83d0aa4fd3SXin LI 	_extra = (struct spinlock_extra *)lck->fname;
84d0aa4fd3SXin LI 	THR_UMUTEX_LOCK(_get_curthread(), &_extra->lock);
8559a47b31SMike Makonnen }
8659a47b31SMike Makonnen 
87bb535300SJeff Roberson void
8837a6356bSDavid Xu _spinlock_debug(spinlock_t *lck, char *fname __unused, int lineno __unused)
89bb535300SJeff Roberson {
90a091d823SDavid Xu 	_spinlock(lck);
91a091d823SDavid Xu }
92a091d823SDavid Xu 
93a091d823SDavid Xu static void
94a091d823SDavid Xu init_spinlock(spinlock_t *lck)
95a091d823SDavid Xu {
96bddd24cdSDavid Xu 	struct pthread *curthread = _get_curthread();
97a091d823SDavid Xu 
98bddd24cdSDavid Xu 	THR_UMUTEX_LOCK(curthread, &spinlock_static_lock);
99a091d823SDavid Xu 	if ((lck->fname == NULL) && (spinlock_count < MAX_SPINLOCKS)) {
100a091d823SDavid Xu 		lck->fname = (char *)&extra[spinlock_count];
101bddd24cdSDavid Xu 		_thr_umutex_init(&extra[spinlock_count].lock);
102a091d823SDavid Xu 		extra[spinlock_count].owner = lck;
103a091d823SDavid Xu 		spinlock_count++;
104a091d823SDavid Xu 	}
105bddd24cdSDavid Xu 	THR_UMUTEX_UNLOCK(curthread, &spinlock_static_lock);
106bddd24cdSDavid Xu 	if (lck->fname == NULL)
107bddd24cdSDavid Xu 		PANIC("Warning: exceeded max spinlocks");
108a091d823SDavid Xu }
109a091d823SDavid Xu 
110a091d823SDavid Xu void
111a091d823SDavid Xu _thr_spinlock_init(void)
112a091d823SDavid Xu {
113a091d823SDavid Xu 	int i;
114a091d823SDavid Xu 
115bddd24cdSDavid Xu 	_thr_umutex_init(&spinlock_static_lock);
116a091d823SDavid Xu 	if (initialized != 0) {
117a091d823SDavid Xu 		/*
118a091d823SDavid Xu 		 * called after fork() to reset state of libc spin locks,
119a091d823SDavid Xu 		 * it is not quite right since libc may be in inconsistent
120a091d823SDavid Xu 		 * state, resetting the locks to allow current thread to be
121a091d823SDavid Xu 		 * able to hold them may not help things too much, but
122a091d823SDavid Xu 		 * anyway, we do our best.
123a091d823SDavid Xu 		 * it is better to do pthread_atfork in libc.
124a091d823SDavid Xu 		 */
125a091d823SDavid Xu 		for (i = 0; i < spinlock_count; i++)
126bddd24cdSDavid Xu 			_thr_umutex_init(&extra[i].lock);
127a091d823SDavid Xu 	} else {
128a091d823SDavid Xu 		initialized = 1;
129a091d823SDavid Xu 	}
130bb535300SJeff Roberson }
131