Subversion Repositories HelenOS

Rev

Rev 552 | Rev 581 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 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
 
383 jermar 29
#include <synch/spinlock.h>
111 palkovsky 30
#include <arch/atomic.h>
153 jermar 31
#include <arch/barrier.h>
383 jermar 32
#include <arch.h>
223 jermar 33
#include <preemption.h>
195 vana 34
#include <print.h>
383 jermar 35
#include <debug.h>
552 palkovsky 36
#include <symtab.h>
1 jermar 37
 
458 decky 38
#ifdef CONFIG_SMP
1 jermar 39
 
383 jermar 40
/** Initialize spinlock
41
 *
42
 * Initialize spinlock.
43
 *
44
 * @param sl Pointer to spinlock_t structure.
45
 */
552 palkovsky 46
void spinlock_initialize(spinlock_t *sl, char *name)
1 jermar 47
{
48
    sl->val = 0;
552 palkovsky 49
#ifdef CONFIG_DEBUG_SPINLOCK
50
    sl->name = name;
51
#endif  
1 jermar 52
}
53
 
458 decky 54
#ifdef CONFIG_DEBUG_SPINLOCK
383 jermar 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
 */
1 jermar 63
void spinlock_lock(spinlock_t *sl)
64
{
557 jermar 65
    count_t i = 0;
552 palkovsky 66
    __address caller = ((__address *) &sl)[-1];
67
    char *symbol;
557 jermar 68
    bool deadlock_reported = false;
1 jermar 69
 
223 jermar 70
    preemption_disable();
1 jermar 71
    while (test_and_set(&sl->val)) {
72
        if (i++ > 300000) {
552 palkovsky 73
            printf("cpu%d: looping on spinlock %p:%s, caller=%p",
74
                   CPU->id, sl, sl->name, caller);
75
            symbol = get_symtab_entry(caller);
76
            if (symbol)
77
                printf("(%s)", symbol);
78
            printf("\n");
1 jermar 79
            i = 0;
557 jermar 80
            deadlock_reported = true;
1 jermar 81
        }
82
    }
383 jermar 83
 
557 jermar 84
    if (deadlock_reported)
85
        printf("cpu%d: not deadlocked\n", CPU->id);
86
 
383 jermar 87
    /*
88
     * Prevent critical section code from bleeding out this way up.
89
     */
153 jermar 90
    CS_ENTER_BARRIER();
1 jermar 91
 
92
}
383 jermar 93
 
1 jermar 94
#else
383 jermar 95
 
96
/** Lock spinlock
97
 *
98
 * Lock spinlock.
99
 *
100
 * @param sl Pointer to spinlock_t structure.
101
 */
1 jermar 102
void spinlock_lock(spinlock_t *sl)
103
{
223 jermar 104
    preemption_disable();
105
 
1 jermar 106
    /*
107
     * Each architecture has its own efficient/recommended
108
     * implementation of spinlock.
109
     */
110
    spinlock_arch(&sl->val);
383 jermar 111
 
112
    /*
113
     * Prevent critical section code from bleeding out this way up.
114
     */
153 jermar 115
    CS_ENTER_BARRIER();
1 jermar 116
}
117
#endif
118
 
383 jermar 119
/** Lock spinlock conditionally
120
 *
121
 * Lock spinlock conditionally.
122
 * If the spinlock is not available at the moment,
123
 * signal failure.
124
 *
125
 * @param sl Pointer to spinlock_t structure.
126
 *
127
 * @return Zero on failure, non-zero otherwise.
128
 */
1 jermar 129
int spinlock_trylock(spinlock_t *sl)
130
{
153 jermar 131
    int rc;
132
 
223 jermar 133
    preemption_disable();
153 jermar 134
    rc = !test_and_set(&sl->val);
383 jermar 135
 
136
    /*
137
     * Prevent critical section code from bleeding out this way up.
138
     */
153 jermar 139
    CS_ENTER_BARRIER();
223 jermar 140
 
141
    if (!rc)
142
        preemption_enable();
153 jermar 143
 
144
    return rc;
1 jermar 145
}
146
 
383 jermar 147
/** Unlock spinlock
148
 *
149
 * Unlock spinlock.
150
 *
151
 * @param sl Pointer to spinlock_t structure.
152
 */
1 jermar 153
void spinlock_unlock(spinlock_t *sl)
154
{
383 jermar 155
    ASSERT(sl->val != 0);
156
 
157
    /*
158
     * Prevent critical section code from bleeding out this way down.
159
     */
153 jermar 160
    CS_LEAVE_BARRIER();
383 jermar 161
 
1 jermar 162
    sl->val = 0;
223 jermar 163
    preemption_enable();
1 jermar 164
}
165
 
166
#endif