Subversion Repositories HelenOS

Rev

Rev 1224 | Rev 1313 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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