source: mainline/generic/src/synch/spinlock.c@ dabe6333

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dabe6333 was 266294a9, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Added constructor/destructor calls to SLAB.
Changed allocation of thread_t structure to use SLAB.

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[f761f1eb]1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[5e04b48d]29#include <synch/spinlock.h>
[e3f41b6]30#include <arch/atomic.h>
[7dd56f1]31#include <arch/barrier.h>
[5e04b48d]32#include <arch.h>
[c842f04]33#include <preemption.h>
[9c0a9b3]34#include <print.h>
[5e04b48d]35#include <debug.h>
[2d93f1f9]36#include <symtab.h>
[f761f1eb]37
[5f85c91]38#ifdef CONFIG_SMP
[f761f1eb]39
[5e04b48d]40/** Initialize spinlock
41 *
42 * Initialize spinlock.
43 *
44 * @param sl Pointer to spinlock_t structure.
45 */
[2d93f1f9]46void spinlock_initialize(spinlock_t *sl, char *name)
[f761f1eb]47{
[80d2bdb]48 atomic_set(&sl->val, 0);
[2d93f1f9]49#ifdef CONFIG_DEBUG_SPINLOCK
50 sl->name = name;
51#endif
[f761f1eb]52}
53
[5f85c91]54#ifdef CONFIG_DEBUG_SPINLOCK
[5e04b48d]55/** Lock spinlock
56 *
57 * Lock spinlock.
58 * This version has limitted ability to report
59 * possible occurence of deadlock.
60 *
61 * @param sl Pointer to spinlock_t structure.
62 */
[f761f1eb]63void spinlock_lock(spinlock_t *sl)
64{
[05e2a7ad]65 count_t i = 0;
[2d93f1f9]66 char *symbol;
[05e2a7ad]67 bool deadlock_reported = false;
[f761f1eb]68
[c842f04]69 preemption_disable();
[f761f1eb]70 while (test_and_set(&sl->val)) {
[266294a9]71 if (i++ > 300000 && sl!=&printflock) {
[2d93f1f9]72 printf("cpu%d: looping on spinlock %p:%s, caller=%p",
[3fc03fd]73 CPU->id, sl, sl->name, CALLER);
74 symbol = get_symtab_entry(CALLER);
[2d93f1f9]75 if (symbol)
76 printf("(%s)", symbol);
77 printf("\n");
[f761f1eb]78 i = 0;
[05e2a7ad]79 deadlock_reported = true;
[f761f1eb]80 }
81 }
[5e04b48d]82
[05e2a7ad]83 if (deadlock_reported)
84 printf("cpu%d: not deadlocked\n", CPU->id);
85
[5e04b48d]86 /*
87 * Prevent critical section code from bleeding out this way up.
88 */
[7dd56f1]89 CS_ENTER_BARRIER();
[f761f1eb]90
91}
[5e04b48d]92
[f761f1eb]93#else
[5e04b48d]94
95/** Lock spinlock
96 *
97 * Lock spinlock.
98 *
99 * @param sl Pointer to spinlock_t structure.
100 */
[f761f1eb]101void spinlock_lock(spinlock_t *sl)
102{
[c842f04]103 preemption_disable();
104
[f761f1eb]105 /*
106 * Each architecture has its own efficient/recommended
107 * implementation of spinlock.
108 */
109 spinlock_arch(&sl->val);
[5e04b48d]110
111 /*
112 * Prevent critical section code from bleeding out this way up.
113 */
[7dd56f1]114 CS_ENTER_BARRIER();
[f761f1eb]115}
116#endif
117
[5e04b48d]118/** Lock spinlock conditionally
119 *
120 * Lock spinlock conditionally.
121 * If the spinlock is not available at the moment,
122 * signal failure.
123 *
124 * @param sl Pointer to spinlock_t structure.
125 *
126 * @return Zero on failure, non-zero otherwise.
127 */
[f761f1eb]128int spinlock_trylock(spinlock_t *sl)
129{
[7dd56f1]130 int rc;
131
[c842f04]132 preemption_disable();
[7dd56f1]133 rc = !test_and_set(&sl->val);
[5e04b48d]134
135 /*
136 * Prevent critical section code from bleeding out this way up.
137 */
[7dd56f1]138 CS_ENTER_BARRIER();
[c842f04]139
140 if (!rc)
141 preemption_enable();
[7dd56f1]142
143 return rc;
[f761f1eb]144}
145
[5e04b48d]146/** Unlock spinlock
147 *
148 * Unlock spinlock.
149 *
150 * @param sl Pointer to spinlock_t structure.
151 */
[f761f1eb]152void spinlock_unlock(spinlock_t *sl)
153{
[80d2bdb]154 ASSERT(atomic_get(&sl->val) != 0);
[5e04b48d]155
156 /*
157 * Prevent critical section code from bleeding out this way down.
158 */
[7dd56f1]159 CS_LEAVE_BARRIER();
[5e04b48d]160
[80d2bdb]161 atomic_set(&sl->val,0);
[c842f04]162 preemption_enable();
[f761f1eb]163}
164
165#endif
Note: See TracBrowser for help on using the repository browser.