Subversion Repositories HelenOS-historic

Rev

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

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