YAP 7.1.0
locks_sparc.h
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 SPARC **
16************************************************************************/
17
18#define swap_il(adr,reg) \
19({ int _ret; \
20 asm volatile ("swap %1,%0" \
21 : "=r" (_ret), "=m" (*(adr)) /* Output %0,%1 */ \
22 : "m" (*(adr)), "0" (reg)); /* Input (%2),%0 */ \
23 _ret; \
24})
25
26#define TRY_LOCK(LOCK_VAR) (swap_il((LOCK_VAR),1)==0)
27
28#define INIT_LOCK(LOCK_VAR) ((LOCK_VAR) = 0)
29#define LOCK(LOCK_VAR) do { \
30 if (TRY_LOCK(&(LOCK_VAR))) break; \
31 while (IS_LOCKED(LOCK_VAR)) continue; \
32 } while (1)
33#define IS_LOCKED(LOCK_VAR) ((LOCK_VAR) != 0)
34#define IS_UNLOCKED(LOCK_VAR) ((LOCK_VAR) == 0)
35#define UNLOCK(LOCK_VAR) ((LOCK_VAR) = 0)
36
37/* Read-write spinlocks, allowing multiple readers
38 * but only one writer.
39 *
40 */
41
42typedef struct { volatile unsigned int lock; } rwlock_t;
43
44/* Sort of like atomic_t's on Sparc, but even more clever.
45 *
46 * ------------------------------------
47 * | 24-bit counter | wlock | rwlock_t
48 * ------------------------------------
49 * 31 8 7 0
50 *
51 * wlock signifies the one writer is in or somebody is updating
52 * counter. For a writer, if he successfully acquires the wlock,
53 * but counter is non-zero, he has to release the lock and wait,
54 * till both counter and wlock are zero.
55 *
56 * Unfortunately this scheme limits us to ~16,000,000 cpus.
57 */
58
59static __inline__ void _read_lock(rwlock_t *rw)
60{
61 register rwlock_t *lp asm("g1");
62 lp = rw;
63 asm __volatile__("mov %%o7, %%g4\n\t" \
64 "call ___rw_read_enter\n\t" \
65 "ldstub [%%g1 + 3], %%g2\n" \
66 : /* no outputs */ \
67 : "r" (lp) \
68 : "g2", "g4", "g7", "memory", "cc");
69}
70
71
72#define READ_LOCK(lock) \
73do { _read_lock(&(lock)); \
74} while(0)
75
76static __inline__ void _read_unlock(rwlock_t *rw)
77{
78 register rwlock_t *lp asm("g1");
79 lp = rw;
80 asm __volatile__( \
81 "mov %%o7, %%g4\n\t" \
82 "call ___rw_read_exit\n\t" \
83 "ldstub [%%g1 + 3], %%g2\n" \
84 : /* no outputs */ \
85 : "r" (lp) \
86 : "g2", "g4", "g7", "memory", "cc");
87}
88
89#define READ_UNLOCK(lock) \
90do { _read_unlock(&lock); \
91} while(0)
92
93static __inline__ void write_lock(rwlock_t *rw)
94{
95 register rwlock_t *lp asm("g1");
96 lp = rw;
97 asm __volatile__( \
98 "mov %%o7, %%g4\n\t"\
99 "call ___rw_write_enter\n\t" \
100 "ldstub [%%g1 + 3], %%g2\n\t" \
101 : /* no outputs */ \
102 : "r" (lp) \
103 : "g2", "g4", "g7", "memory", "cc");
104}
105
106#define WRITE_LOCK(X) write_lock(&(X))
107
108#define WRITE_UNLOCK(rw) do { (&(rw))->lock = 0; } while(0)
109
110#define INIT_RWLOCK(RW) (RW).lock = 0