Subversion Repositories HelenOS

Rev

Rev 4132 | Rev 4220 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4132 Rev 4137
1
/*
1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
2
 * Copyright (c) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup sync
29
/** @addtogroup sync
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Spinlocks.
35
 * @brief   Spinlocks.
36
 */
36
 */
37
 
37
 
38
#include <synch/spinlock.h>
38
#include <synch/spinlock.h>
39
#include <atomic.h>
39
#include <atomic.h>
40
#include <arch/barrier.h>
40
#include <arch/barrier.h>
41
#include <arch.h>
41
#include <arch.h>
42
#include <preemption.h>
42
#include <preemption.h>
43
#include <print.h>
43
#include <print.h>
44
#include <debug.h>
44
#include <debug.h>
45
 
-
 
46
#ifdef CONFIG_SYMTAB
-
 
47
#include <symtab.h>
45
#include <symtab.h>
48
#endif
-
 
49
 
46
 
50
#ifdef CONFIG_FB
47
#ifdef CONFIG_FB
51
#include <genarch/fb/fb.h>
48
#include <genarch/fb/fb.h>
52
#endif
49
#endif
53
 
50
 
54
#ifdef CONFIG_SMP
51
#ifdef CONFIG_SMP
55
 
52
 
56
/** Initialize spinlock
53
/** Initialize spinlock
57
 *
54
 *
58
 * Initialize spinlock.
55
 * Initialize spinlock.
59
 *
56
 *
60
 * @param sl Pointer to spinlock_t structure.
57
 * @param sl Pointer to spinlock_t structure.
61
 */
58
 */
62
void spinlock_initialize(spinlock_t *sl, char *name)
59
void spinlock_initialize(spinlock_t *sl, char *name)
63
{
60
{
64
    atomic_set(&sl->val, 0);
61
    atomic_set(&sl->val, 0);
65
#ifdef CONFIG_DEBUG_SPINLOCK
62
#ifdef CONFIG_DEBUG_SPINLOCK
66
    sl->name = name;
63
    sl->name = name;
67
#endif  
64
#endif  
68
}
65
}
69
 
66
 
70
/** Lock spinlock
67
/** Lock spinlock
71
 *
68
 *
72
 * Lock spinlock.
69
 * Lock spinlock.
73
 * This version has limitted ability to report
70
 * This version has limitted ability to report
74
 * possible occurence of deadlock.
71
 * possible occurence of deadlock.
75
 *
72
 *
76
 * @param sl Pointer to spinlock_t structure.
73
 * @param sl Pointer to spinlock_t structure.
77
 */
74
 */
78
#ifdef CONFIG_DEBUG_SPINLOCK
75
#ifdef CONFIG_DEBUG_SPINLOCK
79
void spinlock_lock_debug(spinlock_t *sl)
76
void spinlock_lock_debug(spinlock_t *sl)
80
{
77
{
81
    count_t i = 0;
78
    count_t i = 0;
82
    bool deadlock_reported = false;
79
    bool deadlock_reported = false;
83
#ifdef CONFIG_SYMTAB
-
 
84
    char *symbol;
-
 
85
#endif
-
 
86
 
80
 
87
    preemption_disable();
81
    preemption_disable();
88
    while (test_and_set(&sl->val)) {
82
    while (test_and_set(&sl->val)) {
89
 
83
 
90
        /*
84
        /*
91
         * We need to be careful about printf_lock and fb_lock.
85
         * We need to be careful about printf_lock and fb_lock.
92
         * Both of them are used to report deadlocks via
86
         * Both of them are used to report deadlocks via
93
         * printf() and fb_putchar().
87
         * printf() and fb_putchar().
94
         *
88
         *
95
         * We trust our code that there is no possible deadlock
89
         * We trust our code that there is no possible deadlock
96
         * caused by these two locks (except when an exception
90
         * caused by these two locks (except when an exception
97
         * is triggered for instance by printf() or fb_putchar()).
91
         * is triggered for instance by printf() or fb_putchar()).
98
         * However, we encountered false positives caused by very
92
         * However, we encountered false positives caused by very
99
         * slow VESA framebuffer interaction (especially when
93
         * slow VESA framebuffer interaction (especially when
100
         * run in a simulator) that caused problems with both
94
         * run in a simulator) that caused problems with both
101
         * printf_lock and fb_lock.
95
         * printf_lock and fb_lock.
102
         *
96
         *
103
         * Possible deadlocks on both printf_lock and fb_lock
97
         * Possible deadlocks on both printf_lock and fb_lock
104
         * are therefore not reported as they would cause an
98
         * are therefore not reported as they would cause an
105
         * infinite recursion.
99
         * infinite recursion.
106
         */
100
         */
107
        if (sl == &printf_lock)
101
        if (sl == &printf_lock)
108
            continue;
102
            continue;
109
#ifdef CONFIG_FB
103
#ifdef CONFIG_FB
110
        if (sl == &fb_lock)
104
        if (sl == &fb_lock)
111
            continue;
105
            continue;
112
#endif
106
#endif
113
        if (i++ > DEADLOCK_THRESHOLD) {
107
        if (i++ > DEADLOCK_THRESHOLD) {
114
            printf("cpu%u: looping on spinlock %" PRIp ":%s, caller=%" PRIp,
108
            printf("cpu%u: looping on spinlock %" PRIp ":%s, "
115
                CPU->id, sl, sl->name, CALLER);
109
                "caller=%" PRIp "(%s)", CPU->id, sl, sl->name,
116
#ifdef CONFIG_SYMTAB
-
 
117
            symbol = get_symtab_entry(CALLER);
110
                CALLER, symtab_fmt_name_lookup(CALLER));
118
            if (symbol)
-
 
119
                printf("(%s)", symbol);
-
 
120
#endif
111
 
121
            printf("\n");
-
 
122
            i = 0;
112
            i = 0;
123
            deadlock_reported = true;
113
            deadlock_reported = true;
124
        }
114
        }
125
    }
115
    }
126
 
116
 
127
    if (deadlock_reported)
117
    if (deadlock_reported)
128
        printf("cpu%u: not deadlocked\n", CPU->id);
118
        printf("cpu%u: not deadlocked\n", CPU->id);
129
 
119
 
130
    /*
120
    /*
131
     * Prevent critical section code from bleeding out this way up.
121
     * Prevent critical section code from bleeding out this way up.
132
     */
122
     */
133
    CS_ENTER_BARRIER();
123
    CS_ENTER_BARRIER();
134
}
124
}
135
#endif
125
#endif
136
 
126
 
137
/** Lock spinlock conditionally
127
/** Lock spinlock conditionally
138
 *
128
 *
139
 * Lock spinlock conditionally.
129
 * Lock spinlock conditionally.
140
 * If the spinlock is not available at the moment,
130
 * If the spinlock is not available at the moment,
141
 * signal failure.
131
 * signal failure.
142
 *
132
 *
143
 * @param sl Pointer to spinlock_t structure.
133
 * @param sl Pointer to spinlock_t structure.
144
 *
134
 *
145
 * @return Zero on failure, non-zero otherwise.
135
 * @return Zero on failure, non-zero otherwise.
146
 */
136
 */
147
int spinlock_trylock(spinlock_t *sl)
137
int spinlock_trylock(spinlock_t *sl)
148
{
138
{
149
    int rc;
139
    int rc;
150
   
140
   
151
    preemption_disable();
141
    preemption_disable();
152
    rc = !test_and_set(&sl->val);
142
    rc = !test_and_set(&sl->val);
153
 
143
 
154
    /*
144
    /*
155
     * Prevent critical section code from bleeding out this way up.
145
     * Prevent critical section code from bleeding out this way up.
156
     */
146
     */
157
    CS_ENTER_BARRIER();
147
    CS_ENTER_BARRIER();
158
 
148
 
159
    if (!rc)
149
    if (!rc)
160
        preemption_enable();
150
        preemption_enable();
161
   
151
   
162
    return rc;
152
    return rc;
163
}
153
}
164
 
154
 
165
#endif
155
#endif
166
 
156
 
167
/** @}
157
/** @}
168
 */
158
 */
169
 
159