Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/arch/mips/include/spinlock.h
2096 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
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. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
#ifndef _MIPS_SPINLOCK_H_
31
#define _MIPS_SPINLOCK_H_
32
33
#include <cdefs.h>
34
35
36
/* Type of value needed to actually spin on */
37
typedef unsigned spinlock_data_t;
38
39
/* Initializer for use by SPINLOCK_INITIALIZER */
40
#define SPINLOCK_DATA_INITIALIZER 0
41
42
/* Atomic operations on spinlock_data_t */
43
void spinlock_data_set(volatile spinlock_data_t *sd, unsigned val);
44
spinlock_data_t spinlock_data_get(volatile spinlock_data_t *sd);
45
spinlock_data_t spinlock_data_testandset(volatile spinlock_data_t *sd);
46
47
////////////////////////////////////////////////////////////
48
49
SPINLOCK_INLINE
50
void
51
spinlock_data_set(volatile spinlock_data_t *sd, unsigned val)
52
{
53
*sd = val;
54
}
55
56
SPINLOCK_INLINE
57
spinlock_data_t
58
spinlock_data_get(volatile spinlock_data_t *sd)
59
{
60
return *sd;
61
}
62
63
SPINLOCK_INLINE
64
spinlock_data_t
65
spinlock_data_testandset(volatile spinlock_data_t *sd)
66
{
67
spinlock_data_t x;
68
spinlock_data_t y;
69
70
/*
71
* Test-and-set using LL/SC.
72
*
73
* Load the existing value into X, and use Y to store 1.
74
* After the SC, Y contains 1 if the store succeeded,
75
* 0 if it failed.
76
*
77
* On failure, return 1 to pretend that the spinlock
78
* was already held.
79
*/
80
81
y = 1;
82
__asm volatile(
83
".set push;" /* save assembler mode */
84
".set mips32;" /* allow MIPS32 instructions */
85
".set volatile;" /* avoid unwanted optimization */
86
"ll %0, 0(%2);" /* x = *sd */
87
"sc %1, 0(%2);" /* *sd = y; y = success? */
88
".set pop" /* restore assembler mode */
89
: "=r" (x), "+r" (y) : "r" (sd));
90
if (y == 0) {
91
return 1;
92
}
93
return x;
94
}
95
96
97
#endif /* _MIPS_SPINLOCK_H_ */
98
99