mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-06 09:50:28 +00:00
Abstract away Futex for cthread
This commit is contained in:
parent
699bbfde97
commit
9cd5c6a27d
3 changed files with 69 additions and 16 deletions
|
@ -17,9 +17,8 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/atomic.h"
|
||||
#include "libc/sysv/consts/futex.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
#include "libc/thread/sem.h"
|
||||
#include "libc/thread/wait.h"
|
||||
#include "libc/thread/yield.h"
|
||||
|
||||
#define CTHREAD_THREAD_VAL_BITS 32
|
||||
|
@ -51,14 +50,9 @@ int cthread_sem_signal(cthread_sem_t* sem) {
|
|||
: "cc");
|
||||
|
||||
if ((count >> CTHREAD_THREAD_VAL_BITS)) {
|
||||
int flags = FUTEX_WAKE;
|
||||
|
||||
// WARNING: an offset of 4 bytes would be required on little-endian archs
|
||||
void* wait_address = &sem->linux.count;
|
||||
asm volatile("syscall"
|
||||
: /* no outputs */
|
||||
: "a"(__NR_futex), "D"(wait_address), "S"(flags), "d"(1)
|
||||
: "rcx", "r11", "cc", "memory");
|
||||
cthread_memory_wake32(wait_address, 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -84,16 +78,9 @@ int cthread_sem_wait_futex(cthread_sem_t* sem, const struct timespec* timeout) {
|
|||
}
|
||||
}
|
||||
|
||||
int flags = FUTEX_WAIT;
|
||||
register struct timespec* timeout_ asm("r10") = timeout;
|
||||
|
||||
// WARNING: an offset of 4 bytes would be required on little-endian archs
|
||||
void* wait_address = &sem->linux.count;
|
||||
asm volatile("syscall"
|
||||
: /* no outputs */
|
||||
: "a"(__NR_futex), "D"(wait_address), "S"(flags), "d"(count),
|
||||
"r"(timeout_)
|
||||
: "rcx", "r11", "cc", "memory");
|
||||
cthread_memory_wait32(wait_address, count, timeout);
|
||||
count = atomic_load(&sem->linux.count);
|
||||
}
|
||||
|
||||
|
|
50
libc/thread/wait.c
Normal file
50
libc/thread/wait.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*-*- 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 2020 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/sysv/consts/futex.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
#include "libc/thread/wait.h"
|
||||
|
||||
int cthread_memory_wait32(uint32_t* addr, uint32_t val,
|
||||
const struct timespec* timeout) {
|
||||
if (__NR_futex != 0xfff) {
|
||||
int flags = FUTEX_WAIT;
|
||||
int rc;
|
||||
register struct timespec* timeout_ asm("r10") = timeout;
|
||||
asm volatile("syscall"
|
||||
: "=a"(rc)
|
||||
: "0"(__NR_futex), "D"(addr), "S"(flags), "d"(val),
|
||||
"r"(timeout_)
|
||||
: "rcx", "r11", "cc", "memory");
|
||||
return rc;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int cthread_memory_wake32(uint32_t* addr, int n) {
|
||||
if (__NR_futex != 0xfff) {
|
||||
int flags = FUTEX_WAKE;
|
||||
int rc;
|
||||
asm volatile("syscall"
|
||||
: "=a"(rc)
|
||||
: "0"(__NR_futex), "D"(addr), "S"(flags), "d"(n)
|
||||
: "rcx", "r11", "cc", "memory");
|
||||
return rc;
|
||||
}
|
||||
return -1;
|
||||
}
|
16
libc/thread/wait.h
Normal file
16
libc/thread/wait.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_THREAD_WAIT_H_
|
||||
#define COSMOPOLITAN_LIBC_THREAD_WAIT_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
/**
|
||||
* @fileoverview wait on memory
|
||||
*/
|
||||
struct timespec;
|
||||
|
||||
int cthread_memory_wait32(uint32_t*, uint32_t, const struct timespec*);
|
||||
int cthread_memory_wake32(uint32_t*, int);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_THREAD_WAIT_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue