]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkse/thread/thr_spinlock.c
This commit was generated by cvs2svn to compensate for changes in r162017,
[FreeBSD/FreeBSD.git] / lib / libkse / thread / thr_spinlock.c
1 /*
2  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  *
34  */
35
36 #include <sys/types.h>
37 #include <machine/atomic.h>
38
39 #include <libc_private.h>
40 #include "spinlock.h"
41 #include "thr_private.h"
42
43 #define MAX_SPINLOCKS   72
44
45 struct spinlock_extra {
46         spinlock_t      *owner;
47         pthread_mutex_t lock;
48 };
49
50 static void     init_spinlock(spinlock_t *lck);
51
52 static struct pthread_mutex_attr static_mutex_attr =
53     PTHREAD_MUTEXATTR_STATIC_INITIALIZER;
54 static pthread_mutexattr_t      static_mattr = &static_mutex_attr;
55
56 static pthread_mutex_t          spinlock_static_lock;
57 static struct spinlock_extra    extra[MAX_SPINLOCKS];
58 static int                      spinlock_count = 0;
59 static int                      initialized = 0;
60
61 LT10_COMPAT_PRIVATE(_spinlock);
62 LT10_COMPAT_PRIVATE(_spinlock_debug);
63 LT10_COMPAT_PRIVATE(_spinunlock);
64
65 /*
66  * These are for compatability only.  Spinlocks of this type
67  * are deprecated.
68  */
69
70 void
71 _spinunlock(spinlock_t *lck)
72 {
73         struct spinlock_extra *extra;
74
75         extra = (struct spinlock_extra *)lck->fname;
76         _pthread_mutex_unlock(&extra->lock);
77 }
78
79 /*
80  * Lock a location for the running thread. Yield to allow other
81  * threads to run if this thread is blocked because the lock is
82  * not available. Note that this function does not sleep. It
83  * assumes that the lock will be available very soon.
84  */
85 void
86 _spinlock(spinlock_t *lck)
87 {
88         struct spinlock_extra *extra;
89
90         if (!__isthreaded)
91                 PANIC("Spinlock called when not threaded.");
92         if (!initialized)
93                 PANIC("Spinlocks not initialized.");
94         /*
95          * Try to grab the lock and loop if another thread grabs
96          * it before we do.
97          */
98         if (lck->fname == NULL)
99                 init_spinlock(lck);
100         extra = (struct spinlock_extra *)lck->fname;
101         _pthread_mutex_lock(&extra->lock);
102 }
103
104 /*
105  * Lock a location for the running thread. Yield to allow other
106  * threads to run if this thread is blocked because the lock is
107  * not available. Note that this function does not sleep. It
108  * assumes that the lock will be available very soon.
109  *
110  * This function checks if the running thread has already locked the
111  * location, warns if this occurs and creates a thread dump before
112  * returning.
113  */
114 void
115 _spinlock_debug(spinlock_t *lck, char *fname, int lineno)
116 {
117         _spinlock(lck);
118 }
119
120 static void
121 init_spinlock(spinlock_t *lck)
122 {
123         _pthread_mutex_lock(&spinlock_static_lock);
124         if ((lck->fname == NULL) && (spinlock_count < MAX_SPINLOCKS)) {
125                 lck->fname = (char *)&extra[spinlock_count];
126                 extra[spinlock_count].owner = lck;
127                 spinlock_count++;
128         }
129         _pthread_mutex_unlock(&spinlock_static_lock);
130         if (lck->fname == NULL)
131                 PANIC("Exceeded max spinlocks");
132 }
133
134 void
135 _thr_spinlock_init(void)
136 {
137         int i;
138
139         if (initialized != 0) {
140                 _thr_mutex_reinit(&spinlock_static_lock);
141                 for (i = 0; i < spinlock_count; i++)
142                         _thr_mutex_reinit(&extra[i].lock);
143         } else {
144                 if (_pthread_mutex_init(&spinlock_static_lock, &static_mattr))
145                         PANIC("Cannot initialize spinlock_static_lock");
146                 for (i = 0; i < MAX_SPINLOCKS; i++) {
147                         if (_pthread_mutex_init(&extra[i].lock, &static_mattr))
148                                 PANIC("Cannot initialize spinlock extra");
149                 }
150                 initialized = 1;
151         }
152 }