Subversion Repositories HelenOS-historic

Rev

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

Rev 1224 Rev 1264
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
/**
-
 
30
 * @file    spinlock.c
-
 
31
 * @brief   Spinlocks.
-
 
32
 */
-
 
33
 
29
#include <synch/spinlock.h>
34
#include <synch/spinlock.h>
30
#include <atomic.h>
35
#include <atomic.h>
31
#include <arch/barrier.h>
36
#include <arch/barrier.h>
32
#include <arch.h>
37
#include <arch.h>
33
#include <preemption.h>
38
#include <preemption.h>
34
#include <print.h>
39
#include <print.h>
35
#include <debug.h>
40
#include <debug.h>
36
#include <symtab.h>
41
#include <symtab.h>
37
 
42
 
38
#ifdef CONFIG_SMP
43
#ifdef CONFIG_SMP
39
 
44
 
40
/** Initialize spinlock
45
/** Initialize spinlock
41
 *
46
 *
42
 * Initialize spinlock.
47
 * Initialize spinlock.
43
 *
48
 *
44
 * @param sl Pointer to spinlock_t structure.
49
 * @param sl Pointer to spinlock_t structure.
45
 */
50
 */
46
void spinlock_initialize(spinlock_t *sl, char *name)
51
void spinlock_initialize(spinlock_t *sl, char *name)
47
{
52
{
48
    atomic_set(&sl->val, 0);
53
    atomic_set(&sl->val, 0);
49
#ifdef CONFIG_DEBUG_SPINLOCK
54
#ifdef CONFIG_DEBUG_SPINLOCK
50
    sl->name = name;
55
    sl->name = name;
51
#endif  
56
#endif  
52
}
57
}
53
 
58
 
54
/** Lock spinlock
59
/** Lock spinlock
55
 *
60
 *
56
 * Lock spinlock.
61
 * Lock spinlock.
57
 * This version has limitted ability to report
62
 * This version has limitted ability to report
58
 * possible occurence of deadlock.
63
 * possible occurence of deadlock.
59
 *
64
 *
60
 * @param sl Pointer to spinlock_t structure.
65
 * @param sl Pointer to spinlock_t structure.
61
 */
66
 */
62
#ifdef CONFIG_DEBUG_SPINLOCK
67
#ifdef CONFIG_DEBUG_SPINLOCK
63
void spinlock_lock_debug(spinlock_t *sl)
68
void spinlock_lock_debug(spinlock_t *sl)
64
{
69
{
65
    count_t i = 0;
70
    count_t i = 0;
66
    char *symbol;
71
    char *symbol;
67
    bool deadlock_reported = false;
72
    bool deadlock_reported = false;
68
 
73
 
69
    preemption_disable();
74
    preemption_disable();
70
    while (test_and_set(&sl->val)) {
75
    while (test_and_set(&sl->val)) {
71
        if (i++ > 300000 && sl!=&printflock) {
76
        if (i++ > 300000 && sl!=&printflock) {
72
            printf("cpu%d: looping on spinlock %.*p:%s, caller=%.*p",
77
            printf("cpu%d: looping on spinlock %.*p:%s, caller=%.*p",
73
                   CPU->id, sizeof(__address) * 2, sl, sl->name, sizeof(__address) * 2, CALLER);
78
                   CPU->id, sizeof(__address) * 2, sl, sl->name, sizeof(__address) * 2, CALLER);
74
            symbol = get_symtab_entry(CALLER);
79
            symbol = get_symtab_entry(CALLER);
75
            if (symbol)
80
            if (symbol)
76
                printf("(%s)", symbol);
81
                printf("(%s)", symbol);
77
            printf("\n");
82
            printf("\n");
78
            i = 0;
83
            i = 0;
79
            deadlock_reported = true;
84
            deadlock_reported = true;
80
        }
85
        }
81
    }
86
    }
82
 
87
 
83
    if (deadlock_reported)
88
    if (deadlock_reported)
84
        printf("cpu%d: not deadlocked\n", CPU->id);
89
        printf("cpu%d: not deadlocked\n", CPU->id);
85
 
90
 
86
    /*
91
    /*
87
     * Prevent critical section code from bleeding out this way up.
92
     * Prevent critical section code from bleeding out this way up.
88
     */
93
     */
89
    CS_ENTER_BARRIER();
94
    CS_ENTER_BARRIER();
90
}
95
}
91
#endif
96
#endif
92
 
97
 
93
/** Lock spinlock conditionally
98
/** Lock spinlock conditionally
94
 *
99
 *
95
 * Lock spinlock conditionally.
100
 * Lock spinlock conditionally.
96
 * If the spinlock is not available at the moment,
101
 * If the spinlock is not available at the moment,
97
 * signal failure.
102
 * signal failure.
98
 *
103
 *
99
 * @param sl Pointer to spinlock_t structure.
104
 * @param sl Pointer to spinlock_t structure.
100
 *
105
 *
101
 * @return Zero on failure, non-zero otherwise.
106
 * @return Zero on failure, non-zero otherwise.
102
 */
107
 */
103
int spinlock_trylock(spinlock_t *sl)
108
int spinlock_trylock(spinlock_t *sl)
104
{
109
{
105
    int rc;
110
    int rc;
106
   
111
   
107
    preemption_disable();
112
    preemption_disable();
108
    rc = !test_and_set(&sl->val);
113
    rc = !test_and_set(&sl->val);
109
 
114
 
110
    /*
115
    /*
111
     * Prevent critical section code from bleeding out this way up.
116
     * Prevent critical section code from bleeding out this way up.
112
     */
117
     */
113
    CS_ENTER_BARRIER();
118
    CS_ENTER_BARRIER();
114
 
119
 
115
    if (!rc)
120
    if (!rc)
116
        preemption_enable();
121
        preemption_enable();
117
   
122
   
118
    return rc;
123
    return rc;
119
}
124
}
120
 
125
 
121
#endif
126
#endif
122
 
127