Get threads working on all platforms

We now have a high-quality clone() implementation for creating
lightweight threads on Linux/Windows/FreeBSD/NetBSD/OpenBSD.
This commit is contained in:
Justine Tunney 2022-05-12 17:52:13 -07:00
parent 4e62cefa6e
commit fec396037a
43 changed files with 850 additions and 532 deletions

View file

@ -2,27 +2,33 @@
#define COSMOPOLITAN_LIBC_INTRIN_SPINLOCK_H_
#ifdef TINY
#define _spinlock(lock) \
do { \
while (__sync_lock_test_and_set(lock, 1)) { \
__builtin_ia32_pause(); \
} \
#define _spinlock(lock) \
do { \
while (__atomic_test_and_set(lock, __ATOMIC_SEQ_CST)) { \
__builtin_ia32_pause(); \
} \
} while (0)
#else
#define _spinlock(lock) \
do { \
for (;;) { \
typeof(*(lock)) x; \
__atomic_load(lock, &x, __ATOMIC_RELAXED); \
if (!x && !__sync_lock_test_and_set(lock, 1)) { \
break; \
} else { \
__builtin_ia32_pause(); \
} \
} \
#define _spinlock(lock) \
do { \
for (;;) { \
typeof(*(lock)) x; \
__atomic_load(lock, &x, __ATOMIC_RELAXED); \
if (!x && !__atomic_test_and_set(lock, __ATOMIC_SEQ_CST)) { \
break; \
} else { \
__builtin_ia32_pause(); \
} \
} \
} while (0)
#endif
#define _spunlock(lock) __sync_lock_release(lock)
#define _spunlock(lock) __atomic_clear(lock, __ATOMIC_RELAXED)
#define _seizelock(lock) \
do { \
typeof(*(lock)) x = 1; \
__atomic_store(lock, &x, __ATOMIC_SEQ_CST); \
} while (0)
#endif /* COSMOPOLITAN_LIBC_INTRIN_SPINLOCK_H_ */