d7dd84588dfc04237572c1b7c14d1232baacac72
7 static inline int atomic_xchg(atomic_t
*v
, int n
)
11 __asm__
__volatile__ (
12 "amoswap.w.aqrl %0, %2, %1"
13 : "=r" (c
), "+A" (v
->counter
)
18 #define csr_read(csr) \
20 register unsigned long __v; \
21 __asm__ __volatile__ ("csrr %0, " #csr \
26 static inline void mb(void)
28 __asm__
__volatile__ ("fence");
31 void get_lock(atomic_t
*lock
)
33 while (atomic_xchg(lock
, 1) == 1)
38 void put_lock(atomic_t
*lock
)
44 static atomic_t buf_lock
= { .counter
= 0 };
46 static int buf_initialized
;
47 static unsigned hart_count
[2];
49 static const char case_bit
= 'a' - 'A';
50 volatile int initialized
;
54 uint32_t hartid
= csr_read(mhartid
);
55 hart_count
[hartid
] = 0;
61 if (!buf_initialized
) {
62 for (unsigned i
= 0; i
< sizeof(buf
); i
++) {
63 buf
[i
] = 'A' + (i
% 26);
69 int offset
= (first
& ~0x20) - 'A';
70 for (unsigned i
= 0; i
< sizeof(buf
); i
++) {
71 while (buf
[i
] != (first
- offset
+ ((offset
+ i
) % 26)))
75 buf
[i
] = 'A' + ((i
+ hartid
+ hart_count
[hartid
]) % 26);
77 buf
[i
] = 'a' + ((i
+ hartid
+ hart_count
[hartid
]) % 26);