Subversion Repositories HelenOS

Rev

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