Subversion Repositories HelenOS

Rev

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

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