1 /************************************************************************
2 ** **
3 ** The YapTab/YapOr/OPTYap systems **
4 ** **
5 ** YapTab extends the Yap Prolog engine to support sequential tabling **
6 ** YapOr extends the Yap Prolog engine to support or-parallelism **
7 ** OPTYap extends the Yap Prolog engine to support or-parallel tabling **
8 ** **
9 ** **
10 ** Yap Prolog was developed at University of Porto, Portugal **
11 ** **
12 ************************************************************************/
13
14 /************************************************************************
15 ** Atomic locks for MIPS **
16 ************************************************************************/
17
test_and_set_bit(int nr,volatile void * addr)18 static __inline__ int test_and_set_bit(int nr, volatile void *addr)
19 {
20 int mask, retval, mw;
21
22 mask = 1;
23 do {
24 mw = load_linked(addr);
25 retval = (mask & mw) != 0;
26 } while (!store_conditional(addr, mw|mask));
27
28 return retval;
29 }
30
_spin_lock(__dummy_lock_t * lock)31 static inline void _spin_lock(__dummy_lock_t *lock)
32 {
33 unsigned int tmp;
34
35 __asm__ __volatile__(
36 ".set\tnoreorder\t\t\t# spin_lock\n"
37 "1:\tll\t%1, %2\n\t"
38 "bnez\t%1, 1b\n\t"
39 " li\t%1, 1\n\t"
40 "sc\t%1, %0\n\t"
41 "beqz\t%1, 1b\n\t"
42 " sync\n\t"
43 ".set\treorder"
44 : "=o" (__dummy_lock(lock)), "=&r" (tmp)
45 : "o" (__dummy_lock(lock))
46 : "memory");
47 }
48
spin_unlock(__dummy_lock_t * lock)49 static inline void spin_unlock(__dummy_lock_t *lock)
50 {
51 __asm__ __volatile__(
52 ".set\tnoreorder\t\t\t# spin_unlock\n\t"
53 "sync\n\t"
54 "sw\t$0, %0\n\t"
55 ".set\treorder"
56 : "=o" (__dummy_lock(lock))
57 : "o" (__dummy_lock(lock))
58 : "memory");
59 }
60
_read_lock(rwlock_t * rw)61 static inline void _read_lock(rwlock_t *rw)
62 {
63 unsigned int tmp;
64
65 __asm__ __volatile__(
66 ".set\tnoreorder\t\t\t# read_lock\n"
67 "1:\tll\t%1, %2\n\t"
68 "bltz\t%1, 1b\n\t"
69 " addu\t%1, 1\n\t"
70 "sc\t%1, %0\n\t"
71 "beqz\t%1, 1b\n\t"
72 " sync\n\t"
73 ".set\treorder"
74 : "=o" (__dummy_lock(rw)), "=&r" (tmp)
75 : "o" (__dummy_lock(rw))
76 : "memory");
77 }
78
79 /* Note the use of sub, not subu which will make the kernel die with an
80 overflow exception if we ever try to unlock an rwlock that is already
81 unlocked or is being held by a writer. */
_read_unlock(rwlock_t * rw)82 static inline void _read_unlock(rwlock_t *rw)
83 {
84 unsigned int tmp;
85
86 __asm__ __volatile__(
87 ".set\tnoreorder\t\t\t# read_unlock\n"
88 "1:\tll\t%1, %2\n\t"
89 "sub\t%1, 1\n\t"
90 "sc\t%1, %0\n\t"
91 "beqz\t%1, 1b\n\t"
92 ".set\treorder"
93 : "=o" (__dummy_lock(rw)), "=&r" (tmp)
94 : "o" (__dummy_lock(rw))
95 : "memory");
96 }
97
_write_lock(rwlock_t * rw)98 static inline void _write_lock(rwlock_t *rw)
99 {
100 unsigned int tmp;
101
102 __asm__ __volatile__(
103 ".set\tnoreorder\t\t\t# write_lock\n"
104 "1:\tll\t%1, %2\n\t"
105 "bnez\t%1, 1b\n\t"
106 " lui\t%1, 0x8000\n\t"
107 "sc\t%1, %0\n\t"
108 "beqz\t%1, 1b\n\t"
109 " sync\n\t"
110 ".set\treorder"
111 : "=o" (__dummy_lock(rw)), "=&r" (tmp)
112 : "o" (__dummy_lock(rw))
113 : "memory");
114 }
115
_write_unlock(rwlock_t * rw)116 static inline void _write_unlock(rwlock_t *rw)
117 {
118 __asm__ __volatile__(
119 ".set\tnoreorder\t\t\t# write_unlock\n\t"
120 "sync\n\t"
121 "sw\t$0, %0\n\t"
122 ".set\treorder"
123 : "=o" (__dummy_lock(rw))
124 : "o" (__dummy_lock(rw))
125 : "memory");
126 }
127
128
129