Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
1 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
 
1757 jermar 29
/** @addtogroup sync
1702 cejka 30
 * @{
31
 */
32
 
1264 jermar 33
/**
1702 cejka 34
 * @file
1264 jermar 35
 * @brief   Spinlocks.
36
 */
37
 
383 jermar 38
#include <synch/spinlock.h>
1104 jermar 39
#include <atomic.h>
153 jermar 40
#include <arch/barrier.h>
383 jermar 41
#include <arch.h>
223 jermar 42
#include <preemption.h>
195 vana 43
#include <print.h>
383 jermar 44
#include <debug.h>
4132 svoboda 45
 
46
#ifdef CONFIG_SYMTAB
552 palkovsky 47
#include <symtab.h>
4132 svoboda 48
#endif
1 jermar 49
 
1313 jermar 50
#ifdef CONFIG_FB
51
#include <genarch/fb/fb.h>
52
#endif
53
 
458 decky 54
#ifdef CONFIG_SMP
1 jermar 55
 
383 jermar 56
/** Initialize spinlock
57
 *
58
 * Initialize spinlock.
59
 *
60
 * @param sl Pointer to spinlock_t structure.
61
 */
552 palkovsky 62
void spinlock_initialize(spinlock_t *sl, char *name)
1 jermar 63
{
625 palkovsky 64
    atomic_set(&sl->val, 0);
552 palkovsky 65
#ifdef CONFIG_DEBUG_SPINLOCK
66
    sl->name = name;
67
#endif  
1 jermar 68
}
69
 
383 jermar 70
/** Lock spinlock
71
 *
72
 * Lock spinlock.
73
 * This version has limitted ability to report
74
 * possible occurence of deadlock.
75
 *
76
 * @param sl Pointer to spinlock_t structure.
77
 */
1100 palkovsky 78
#ifdef CONFIG_DEBUG_SPINLOCK
79
void spinlock_lock_debug(spinlock_t *sl)
1 jermar 80
{
557 jermar 81
    count_t i = 0;
4132 svoboda 82
    bool deadlock_reported = false;
83
#ifdef CONFIG_SYMTAB
552 palkovsky 84
    char *symbol;
4132 svoboda 85
#endif
1 jermar 86
 
223 jermar 87
    preemption_disable();
1 jermar 88
    while (test_and_set(&sl->val)) {
1313 jermar 89
 
90
        /*
2211 decky 91
         * We need to be careful about printf_lock and fb_lock.
1313 jermar 92
         * Both of them are used to report deadlocks via
93
         * printf() and fb_putchar().
94
         *
95
         * We trust our code that there is no possible deadlock
96
         * caused by these two locks (except when an exception
97
         * is triggered for instance by printf() or fb_putchar()).
98
         * However, we encountered false positives caused by very
99
         * slow VESA framebuffer interaction (especially when
100
         * run in a simulator) that caused problems with both
2211 decky 101
         * printf_lock and fb_lock.
1313 jermar 102
         *
2211 decky 103
         * Possible deadlocks on both printf_lock and fb_lock
1313 jermar 104
         * are therefore not reported as they would cause an
105
         * infinite recursion.
106
         */
2211 decky 107
        if (sl == &printf_lock)
1313 jermar 108
            continue;
109
#ifdef CONFIG_FB
110
        if (sl == &fb_lock)
111
            continue;
112
#endif
1667 jermar 113
        if (i++ > DEADLOCK_THRESHOLD) {
3059 decky 114
            printf("cpu%u: looping on spinlock %" PRIp ":%s, caller=%" PRIp,
115
                CPU->id, sl, sl->name, CALLER);
4132 svoboda 116
#ifdef CONFIG_SYMTAB
622 palkovsky 117
            symbol = get_symtab_entry(CALLER);
552 palkovsky 118
            if (symbol)
119
                printf("(%s)", symbol);
4132 svoboda 120
#endif
552 palkovsky 121
            printf("\n");
1 jermar 122
            i = 0;
557 jermar 123
            deadlock_reported = true;
1 jermar 124
        }
125
    }
383 jermar 126
 
557 jermar 127
    if (deadlock_reported)
3059 decky 128
        printf("cpu%u: not deadlocked\n", CPU->id);
557 jermar 129
 
383 jermar 130
    /*
131
     * Prevent critical section code from bleeding out this way up.
132
     */
153 jermar 133
    CS_ENTER_BARRIER();
1 jermar 134
}
135
#endif
136
 
383 jermar 137
/** Lock spinlock conditionally
138
 *
139
 * Lock spinlock conditionally.
140
 * If the spinlock is not available at the moment,
141
 * signal failure.
142
 *
143
 * @param sl Pointer to spinlock_t structure.
144
 *
145
 * @return Zero on failure, non-zero otherwise.
146
 */
1 jermar 147
int spinlock_trylock(spinlock_t *sl)
148
{
153 jermar 149
    int rc;
150
 
223 jermar 151
    preemption_disable();
153 jermar 152
    rc = !test_and_set(&sl->val);
383 jermar 153
 
154
    /*
155
     * Prevent critical section code from bleeding out this way up.
156
     */
153 jermar 157
    CS_ENTER_BARRIER();
223 jermar 158
 
159
    if (!rc)
160
        preemption_enable();
153 jermar 161
 
162
    return rc;
1 jermar 163
}
164
 
165
#endif
1702 cejka 166
 
1757 jermar 167
/** @}
1702 cejka 168
 */