mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Add atomics to chibicc
This change also fixes #434 and makes the chibicc assembler better.
This commit is contained in:
parent
5ddf43332e
commit
a988896048
21 changed files with 650 additions and 445 deletions
|
@ -1,7 +0,0 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_INTRIN_REFCOUNT_H_
|
||||
#define COSMOPOLITAN_LIBC_INTRIN_REFCOUNT_H_
|
||||
|
||||
#define _incref(x) __atomic_fetch_add(x, 1, __ATOMIC_RELAXED)
|
||||
#define _decref(x) __atomic_sub_fetch(x, 1, __ATOMIC_SEQ_CST)
|
||||
|
||||
#endif /* COSMOPOLITAN_LIBC_INTRIN_REFCOUNT_H_ */
|
43
libc/intrin/wait0.c
Normal file
43
libc/intrin/wait0.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/wait0.internal.h"
|
||||
#include "libc/linux/futex.h"
|
||||
|
||||
/**
|
||||
* Blocks until memory location becomes zero.
|
||||
*
|
||||
* This is intended to be used on the child thread id, which is updated
|
||||
* by the clone() system call when a thread terminates. The purpose of
|
||||
* this operation is to know when it's safe to munmap() a thread stack.
|
||||
*/
|
||||
void _wait0(int *ptid) {
|
||||
int x;
|
||||
for (;;) {
|
||||
if (!(x = atomic_load_explicit(ptid, memory_order_relaxed))) {
|
||||
break;
|
||||
} else if (IsLinux()) {
|
||||
LinuxFutexWait(ptid, x, 0);
|
||||
} else {
|
||||
sched_yield();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,20 +1,10 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_INTRIN_WAIT0_H_
|
||||
#define COSMOPOLITAN_LIBC_INTRIN_WAIT0_H_
|
||||
#include "libc/bits/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/linux/futex.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define _wait0(ptid) \
|
||||
do { \
|
||||
int x; \
|
||||
if (!(x = atomic_load_explicit(ptid, memory_order_relaxed))) { \
|
||||
break; \
|
||||
} else if (IsLinux()) { \
|
||||
LinuxFutexWait(ptid, x, 0); \
|
||||
} else { \
|
||||
sched_yield(); \
|
||||
} \
|
||||
} while (1)
|
||||
void _wait0(int *) hidden;
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_INTRIN_WAIT0_H_ */
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_LINUX_FUTEX_H_
|
||||
#define COSMOPOLITAN_LIBC_LINUX_FUTEX_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
forceinline int LinuxFutexWait(void *addr, int expect,
|
||||
struct timespec *timeout) {
|
||||
int ax;
|
||||
register void *r10 asm("r10") = timeout;
|
||||
asm volatile("syscall"
|
||||
asm volatile("mov\t%5,%%r10\n\t"
|
||||
"syscall"
|
||||
: "=a"(ax)
|
||||
: "0"(202), "D"(addr), "S"(0), "d"(expect), "r"(r10)
|
||||
: "rcx", "r11", "memory");
|
||||
: "0"(202), "D"(addr), "S"(0), "d"(expect), "g"(timeout)
|
||||
: "rcx", "r10", "r11", "memory");
|
||||
return ax;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue