mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-07 02:10:27 +00:00
Removed "native" specifier
This commit is contained in:
parent
c9f8973de7
commit
699bbfde97
3 changed files with 26 additions and 29 deletions
|
@ -12,13 +12,13 @@
|
||||||
#include "libc/thread/self.h"
|
#include "libc/thread/self.h"
|
||||||
#include "libc/thread/detach.h"
|
#include "libc/thread/detach.h"
|
||||||
#include "libc/thread/join.h"
|
#include "libc/thread/join.h"
|
||||||
#include "libc/thread/nativesem.h"
|
#include "libc/thread/sem.h"
|
||||||
#include "libc/time/time.h"
|
#include "libc/time/time.h"
|
||||||
|
|
||||||
cthread_native_sem_t semaphore;
|
cthread_sem_t semaphore;
|
||||||
|
|
||||||
int worker(void* arg) {
|
int worker(void* arg) {
|
||||||
cthread_native_sem_signal(&semaphore);
|
cthread_sem_signal(&semaphore);
|
||||||
|
|
||||||
cthread_t self = cthread_self();
|
cthread_t self = cthread_self();
|
||||||
int tid = self->tid;
|
int tid = self->tid;
|
||||||
|
@ -30,12 +30,12 @@ int worker(void* arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
cthread_native_sem_init(&semaphore, 0);
|
cthread_sem_init(&semaphore, 0);
|
||||||
|
|
||||||
cthread_t thread;
|
cthread_t thread;
|
||||||
int rc = cthread_create(&thread, NULL, &worker, NULL);
|
int rc = cthread_create(&thread, NULL, &worker, NULL);
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
cthread_native_sem_wait(&semaphore, 0, NULL);
|
cthread_sem_wait(&semaphore, 0, NULL);
|
||||||
//printf("thread created: %p\n", thread);
|
//printf("thread created: %p\n", thread);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
#if 1
|
#if 1
|
||||||
|
@ -44,8 +44,8 @@ int main() {
|
||||||
rc = cthread_detach(thread);
|
rc = cthread_detach(thread);
|
||||||
sleep(2);
|
sleep(2);
|
||||||
#endif
|
#endif
|
||||||
cthread_native_sem_signal(&semaphore);
|
cthread_sem_signal(&semaphore);
|
||||||
cthread_native_sem_wait(&semaphore, 0, NULL);
|
cthread_sem_wait(&semaphore, 0, NULL);
|
||||||
//printf("thread joined: %p -> %d\n", thread, rc);
|
//printf("thread joined: %p -> %d\n", thread, rc);
|
||||||
} else {
|
} else {
|
||||||
printf("ERROR: thread could not be started: %d\n", rc);
|
printf("ERROR: thread could not be started: %d\n", rc);
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
#include "libc/bits/atomic.h"
|
#include "libc/bits/atomic.h"
|
||||||
#include "libc/sysv/consts/futex.h"
|
#include "libc/sysv/consts/futex.h"
|
||||||
#include "libc/sysv/consts/nr.h"
|
#include "libc/sysv/consts/nr.h"
|
||||||
#include "libc/thread/nativesem.h"
|
#include "libc/thread/sem.h"
|
||||||
#include "libc/thread/yield.h"
|
#include "libc/thread/yield.h"
|
||||||
|
|
||||||
#define CTHREAD_THREAD_VAL_BITS 32
|
#define CTHREAD_THREAD_VAL_BITS 32
|
||||||
|
@ -34,16 +34,16 @@ static void pause(int attempt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int cthread_native_sem_init(cthread_native_sem_t* sem, int count) {
|
int cthread_sem_init(cthread_sem_t* sem, int count) {
|
||||||
sem->linux.count = count;
|
sem->linux.count = count;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int cthread_native_sem_destroy(cthread_native_sem_t* sem) {
|
int cthread_sem_destroy(cthread_sem_t* sem) {
|
||||||
(void)sem;
|
(void)sem;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cthread_native_sem_signal(cthread_native_sem_t* sem) {
|
int cthread_sem_signal(cthread_sem_t* sem) {
|
||||||
uint64_t count;
|
uint64_t count;
|
||||||
asm volatile("lock xadd\t%1, %0"
|
asm volatile("lock xadd\t%1, %0"
|
||||||
: "+m"(sem->linux.count), "=r"(count)
|
: "+m"(sem->linux.count), "=r"(count)
|
||||||
|
@ -64,8 +64,7 @@ int cthread_native_sem_signal(cthread_native_sem_t* sem) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cthread_native_sem_wait_futex(cthread_native_sem_t* sem,
|
int cthread_sem_wait_futex(cthread_sem_t* sem, const struct timespec* timeout) {
|
||||||
const struct timespec* timeout) {
|
|
||||||
uint64_t count;
|
uint64_t count;
|
||||||
|
|
||||||
// record current thread as waiter
|
// record current thread as waiter
|
||||||
|
@ -101,36 +100,34 @@ int cthread_native_sem_wait_futex(cthread_native_sem_t* sem,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cthread_native_sem_wait_spin(cthread_native_sem_t* sem, uint64_t count,
|
int cthread_sem_wait_spin(cthread_sem_t* sem, uint64_t count, int spin,
|
||||||
int spin, const struct timespec* timeout) {
|
const struct timespec* timeout) {
|
||||||
// spin on pause
|
// spin on pause
|
||||||
for (int attempt = 0; attempt < spin; ++attempt) {
|
for (int attempt = 0; attempt < spin; ++attempt) {
|
||||||
//if ((count >> CTHREAD_THREAD_VAL_BITS) != 0) break;
|
//if ((count >> CTHREAD_THREAD_VAL_BITS) != 0) break;
|
||||||
while ((uint32_t)count > 0) {
|
while ((uint32_t)count > 0) {
|
||||||
// spin is useful if multiple waiters can acquire the semaphore at the same time
|
// spin is useful if multiple waiters can acquire the semaphore at the same time
|
||||||
if (atomic_compare_exchange_weak(
|
if (atomic_compare_exchange_weak(&sem->linux.count, count, count - 1)) {
|
||||||
&sem->linux.count, count, count - 1)) {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pause(attempt);
|
pause(attempt);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cthread_native_sem_wait_futex(sem, timeout);
|
return cthread_sem_wait_futex(sem, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cthread_native_sem_wait(cthread_native_sem_t* sem, int spin,
|
int cthread_sem_wait(cthread_sem_t* sem, int spin,
|
||||||
const struct timespec* timeout) {
|
const struct timespec* timeout) {
|
||||||
uint64_t count = atomic_load(&sem->linux.count);
|
uint64_t count = atomic_load(&sem->linux.count);
|
||||||
|
|
||||||
// uncontended
|
// uncontended
|
||||||
while ((uint32_t)count > 0) {
|
while ((uint32_t)count > 0) {
|
||||||
// spin is useful if multiple waiters can acquire the semaphore at the same time
|
// spin is useful if multiple waiters can acquire the semaphore at the same time
|
||||||
if (atomic_compare_exchange_weak(
|
if (atomic_compare_exchange_weak(&sem->linux.count, count, count - 1)) {
|
||||||
&sem->linux.count, count, count - 1)) {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cthread_native_sem_wait_spin(sem, count, spin, timeout);
|
return cthread_sem_wait_spin(sem, count, spin, timeout);
|
||||||
}
|
}
|
|
@ -7,19 +7,19 @@ COSMOPOLITAN_C_START_
|
||||||
* @fileoverview native semaphore for implementation details
|
* @fileoverview native semaphore for implementation details
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef union cthread_native_sem_t {
|
typedef union cthread_sem_t {
|
||||||
struct {
|
struct {
|
||||||
uint64_t count;
|
uint64_t count;
|
||||||
} linux;
|
} linux;
|
||||||
} cthread_native_sem_t;
|
} cthread_sem_t;
|
||||||
|
|
||||||
struct timespec;
|
struct timespec;
|
||||||
|
|
||||||
int cthread_native_sem_init(cthread_native_sem_t*, int);
|
int cthread_sem_init(cthread_sem_t*, int);
|
||||||
int cthread_native_sem_destroy(cthread_native_sem_t*);
|
int cthread_sem_destroy(cthread_sem_t*);
|
||||||
|
|
||||||
int cthread_native_sem_wait(cthread_native_sem_t*, int, const struct timespec*);
|
int cthread_sem_wait(cthread_sem_t*, int, const struct timespec*);
|
||||||
int cthread_native_sem_signal(cthread_native_sem_t*);
|
int cthread_sem_signal(cthread_sem_t*);
|
||||||
|
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
Loading…
Add table
Add a link
Reference in a new issue