I now have an untested spinlock compiling on TCC.
static U8
casb(volatile U8 *p, U8 old, U8 new) {
U8 cur;
while (1) {
asm volatile("ldaxrb %0, %1" : "=r" (cur) : "m" (*p));
if (cur != old) {
asm volatile("dmb ish" : : : "memory");
break;
}
int r;
asm volatile("stlxrb %0, %1, %2" : "=&r" (r) : "r" (new), "m" (*p));
if (r) break;
}
return cur;
}
void
spinlock(Spinlock *s) {
while (1) {
if (casb(s, 0, 1) == 0) break;
asm volatile("wfe");
}
}
void
spinunlock(Spinlock *s) {
casb(s, 1, 0);
asm volatile("sev");
}
it's pretty frustrating when I want to do something in TCC and then it says, "sorry, can't do that thing." but then, I can hack on it and then it does that thing. the only drawback is that now it's 7am.