-#define LOCK(lockptr) do {int v=1;\
- while(v)__asm__ __volatile__("lock xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(lockptr));}while(0)
-#define RELEASE(lockptr) __asm__ __volatile__("lock andl $0, (%%edi)"::"D"(lockptr));
+struct sShortSpinlock {
+ volatile int Lock;
+ int IF;
+};
+/**
+ * \brief Determine if a short spinlock is locked
+ */
+static inline int IS_LOCKED(struct sShortSpinlock *Lock) {
+ return !!Lock->Lock;
+}
+/**
+ * \brief Acquire a Short Spinlock
+ * \note Stops interrupts, so be careful
+ */
+static inline void SHORTLOCK(struct sShortSpinlock *Lock) {
+ int v = 1;
+ __ASM__ ("pushf;\n\tpop %%eax" : "=a"(Lock->IF));
+ Lock->IF &= 0x200;
+ __ASM__ ("cli"); // Stop task switches
+ // Wait for another CPU to release
+ while(v)
+ __ASM__("xchgl %%eax, (%%edi)":"=a"(v):"a"(1),"D"(&Lock->Lock));
+}
+/**
+ * \brief Release a short lock
+ */
+static inline void SHORTREL(struct sShortSpinlock *Lock) {
+ Lock->Lock = 0;
+ #if 0
+ __ASM__ ("pushf;\n\tor %0, (%%esp);\n\tpopf" : : "a"(Lock->IF));
+ #else
+ if(Lock->IF) __ASM__ ("sti");
+ #endif
+}
+/**
+ * \brief Halt the CPU
+ */
+#define HALT() __asm__ __volatile__ ("hlt")
+#define MAGIC_BREAK() __asm__ __volatile__ ("xchg %bx, %bx")