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

Subversion Repositories mips32r1

[/] [mips32r1/] [trunk/] [Software/] [demos/] [XD5_Threads/] [src/] [os/] [lock.c] - Blame information for rev 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ayersg
#include <stdlib.h> /* only for NULL */
2
#include "lock.h"
3
 
4
void Lock(volatile int *lock, int *load_count, int *store_count)
5
{
6
        int l_tries = 0;
7
        int s_tries = 0;
8
 
9
        asm volatile(
10
                ".set noreorder\n\t"
11
                "addiu  $8, $0, 1\n\t"
12
                "addu   %[l_tries], $0, $0\n\t"
13
                "addu   %[s_tries], $0, $0\n\t"
14
                "1:\n\t"
15
                "ll     $9, %[lock]\n\t"
16
                "bne    $9, $0, 1b\n\t"
17
                "addiu  %[l_tries], %[l_tries], 1\n\t"
18
                "sc     $8, %[lock]\n\t"
19
                "beq    $8, $0, 1b\n\t"
20
                "addiu  %[s_tries], %[s_tries], 1\n\t"
21
                ".set reorder\n\t"
22
                : [l_tries] "=&r"(l_tries), [s_tries] "=&r"(s_tries),
23
                [lock] "+m"(*lock)
24
                :
25
                : "$8", "$9", "memory"
26
        );
27
 
28
        if (load_count != NULL) {
29
                *load_count = l_tries;
30
        }
31
        if (store_count != NULL) {
32
                *store_count = s_tries;
33
        }
34
}
35
 
36
void LockAlmost(volatile int *lock, int *load_count, int *store_count)
37
{
38
        int l_tries = 0;
39
        int s_tries = 0;
40
 
41
        asm volatile(
42
                ".set noreorder\n\t"
43
                "addiu  $8, $0, 1\n\t"
44
                "addu   %[l_tries], $0, $0\n\t"
45
                "addu   %[s_tries], $0, $0\n\t"
46
                "1:\n\t"
47
                "ll     $9, %[lock]\n\t"
48
                "bne    $9, $0, 1b\n\t"
49
                "addiu  %[l_tries], %[l_tries], 1\n\t"
50
 
51
                "nop\n\t"
52
                "nop\n\t"
53
 
54
                "sc     $8, %[lock]\n\t"
55
                "addiu  %[s_tries], %[s_tries], 1\n\t"
56
                ".set reorder\n\t"
57
                : [l_tries] "=&r"(l_tries), [s_tries] "=&r"(s_tries),
58
                [lock] "+m"(*lock)
59
                :
60
                : "$8", "$9", "memory"
61
        );
62
 
63
        if (load_count != NULL) {
64
                *load_count = l_tries;
65
        }
66
        if (store_count != NULL) {
67
                *store_count = s_tries;
68
        }
69
}
70
 
71
 
72
void LockNull(volatile int *lock, int *load_count, int *store_count)
73
{
74
        if (load_count != NULL) {
75
                *load_count = 1;
76
        }
77
        if (store_count != NULL) {
78
                *store_count = 1;
79
        }
80
}
81
 
82
void Unlock(volatile int *lock)
83
{
84
        *lock = 0;
85
}
86
 

powered by: WebSVN 2.1.0

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