OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [src/] [arch/] [arm/] [v5/] [mutex.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * ARM v5 Binary semaphore (mutex) implementation.
3
 *
4
 * Copyright (C) 2007-2010 B Labs Ltd.
5
 * Author: Prem Mallappa <prem.mallappa@b-labs.co.uk>
6
 */
7
 
8
#include <l4/lib/printk.h>
9
 
10
/* Recap on swp:
11
 * swp rx, ry, [rz]
12
 * In one instruction:
13
 * 1) Stores the value in ry into location pointed by rz.
14
 * 2) Loads the value in the location of rz into rx.
15
 * By doing so, in one instruction one can attempt to lock
16
 * a word, and discover whether it was already locked.
17
 */
18
 
19
#define MUTEX_UNLOCKED  0
20
#define MUTEX_LOCKED    1
21
 
22
void __spin_lock(unsigned long *s)
23
{
24
        int tmp = 0, tmp2;
25
        __asm__ __volatile__(
26
                "1: \n"
27
                "swp %0, %1, [%2] \n"
28
                "teq %0, %3 \n"
29
                "bne 1b \n"
30
                : "=&r" (tmp2)
31
                : "r" (tmp), "r"(s), "r"(0)
32
                : "cc", "memory"
33
                );
34
}
35
 
36
void __spin_unlock(unsigned long *s)
37
{
38
        int tmp = 1, tmp2;
39
        __asm__ __volatile__(
40
                "1: \n"
41
                "swp %0, %1, [%2] \n"
42
                "teq %0, %3 \n"
43
                "bne 1b \n"
44
                : "=&r" (tmp2)
45
                : "r" (tmp), "r"(s), "r"(0)
46
                : "cc", "memory"
47
                );
48
}
49
 
50
int __mutex_lock(unsigned long *s)
51
{
52
        int tmp = MUTEX_LOCKED, tmp2;
53
        __asm__ __volatile__(
54
                "swp %0, %1, [%2] \n"
55
                : "=&r"(tmp2)
56
                : "r"(tmp), "r"(s)
57
                : "cc", "memory"
58
                );
59
        if (tmp2 == MUTEX_UNLOCKED)
60
                return 1;
61
 
62
        return 0;
63
}
64
 
65
void __mutex_unlock(unsigned long *s)
66
{
67
        int tmp, tmp2=MUTEX_UNLOCKED;
68
        __asm__ __volatile__(
69
                "swp %0, %1, [%2] \n"
70
                : "=&r"(tmp)
71
                : "r"(tmp2), "r"(s)
72
                : "cc", "memory"
73
                );
74
        BUG_ON(tmp != MUTEX_LOCKED);
75
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.