Add more POSIX threads APIs

This commit is contained in:
Justine Tunney 2022-09-09 11:30:33 -07:00
parent 1729a8259c
commit c3208eb9d5
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
32 changed files with 592 additions and 87 deletions

View file

View file

@ -0,0 +1,47 @@
/*-*- 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/intrin/pthread.h"
/**
* Sets thread scheduler policy attribute, e.g.
*
* pthread_t id;
* pthread_attr_t attr;
* pthread_attr_init(&attr);
* struct sched_param pri = {sched_get_priority_min(SCHED_OTHER)};
* pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
* pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
* pthread_attr_setschedparam(&attr, &pri);
* pthread_create(&id, &attr, func, 0);
* pthread_attr_destroy(&attr);
* pthread_join(id, 0);
*
* @param policy may be one of:
* - `SCHED_OTHER` the default policy
* - `SCHED_FIFO` for real-time scheduling (usually needs root)
* - `SCHED_RR` for round-robin scheduling (usually needs root)
* - `SCHED_IDLE` for lowest effort (Linux and FreeBSD only)
* - `SCHED_BATCH` for "batch" style execution of processes if
* supported (Linux), otherwise it's treated as `SCHED_OTHER`
* @see sched_setscheduler()
*/
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) {
attr->schedpolicy = policy;
return 0;
}

View file

@ -21,6 +21,9 @@
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1
#define PTHREAD_INHERIT_SCHED 0
#define PTHREAD_EXPLICIT_SCHED 1
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -83,12 +86,13 @@ typedef struct pthread_rwlock_s {
typedef struct pthread_attr_s {
char detachstate;
size_t stacksize;
size_t guardsize;
char *stackaddr;
int scope;
char inheritsched;
int schedparam;
int schedpolicy;
int inheritsched;
int scope;
unsigned guardsize;
unsigned stacksize;
char *stackaddr;
} pthread_attr_t;
int pthread_yield(void);

View file

@ -1,5 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_PTHREAD2_H_
#define COSMOPOLITAN_LIBC_INTRIN_PTHREAD2_H_
#include "libc/calls/struct/cpuset.h"
#include "libc/calls/struct/sched_param.h"
#include "libc/calls/struct/timespec.h"
#include "libc/intrin/pthread.h"
@ -11,6 +12,10 @@ int pthread_attr_getschedparam(const pthread_attr_t *, struct sched_param *);
int pthread_attr_setschedparam(pthread_attr_t *, const struct sched_param *);
int pthread_cond_timedwait(pthread_cond_t *, pthread_mutex_t *,
const struct timespec *);
int pthread_setaffinity_np(pthread_t, size_t, const cpu_set_t *);
int pthread_getaffinity_np(pthread_t, size_t, cpu_set_t *);
int pthread_setschedparam(pthread_t, int, const struct sched_param *);
int pthread_getschedparam(pthread_t, int *, struct sched_param *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -0,0 +1,28 @@
/*-*- 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/intrin/pthread.h"
/**
* Returns thread inherit schedule attribute.
*/
int pthread_attr_getinheritsched(const pthread_attr_t *attr,
int *inheritsched) {
*inheritsched = attr->inheritsched;
return 0;
}

View file

@ -0,0 +1,28 @@
/*-*- 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/intrin/pthread2.h"
/**
* Gets thread scheduler parameter attribute.
*/
int pthread_attr_getschedparam(const pthread_attr_t *attr,
struct sched_param *param) {
*param = (struct sched_param){attr->schedparam};
return 0;
}

View file

@ -0,0 +1,27 @@
/*-*- 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/intrin/pthread.h"
/**
* Gets thread scheduler policy attribute
*/
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) {
*policy = attr->schedpolicy;
return 0;
}

View file

@ -26,7 +26,6 @@
*/
int pthread_attr_init(pthread_attr_t *attr) {
*attr = (pthread_attr_t){
.detachstate = PTHREAD_CREATE_JOINABLE,
.stacksize = GetStackSize(),
.guardsize = PAGESIZE,
};

View file

@ -0,0 +1,53 @@
/*-*- 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/assert.h"
#include "libc/errno.h"
#include "libc/intrin/pthread.h"
/**
* Sets thread scheduler inheritance attribute, e.g.
*
* pthread_t id;
* pthread_attr_t attr;
* pthread_attr_init(&attr);
* struct sched_param pri = {sched_get_priority_min(SCHED_OTHER)};
* pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
* pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
* pthread_attr_setschedparam(&attr, &pri);
* pthread_create(&id, &attr, func, 0);
* pthread_attr_destroy(&attr);
* pthread_join(id, 0);
*
* @param inheritsched may be one of:
* - `PTHREAD_INHERIT_SCHED` the default
* - `PTHREAD_EXPLICIT_SCHED` to enable rescheduling
* @return 0 on success, or errno on error
* @raise EINVAL on bad value
*/
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched) {
switch (inheritsched) {
case PTHREAD_INHERIT_SCHED:
case PTHREAD_EXPLICIT_SCHED:
attr->inheritsched = inheritsched;
return 0;
default:
assert(!"badval");
return EINVAL;
}
}

View file

@ -0,0 +1,48 @@
/*-*- 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/calls/struct/sched_param.h"
#include "libc/errno.h"
#include "libc/intrin/pthread2.h"
/**
* Sets thread scheduler parameter attribute, e.g.
*
* pthread_t id;
* pthread_attr_t attr;
* pthread_attr_init(&attr);
* struct sched_param pri = {sched_get_priority_min(SCHED_OTHER)};
* pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
* pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
* pthread_attr_setschedparam(&attr, &pri);
* pthread_create(&id, &attr, func, 0);
* pthread_attr_destroy(&attr);
* pthread_join(id, 0);
*
* @param param specifies priority on scheduling policies that need it
* @see pthread_attr_setschedpolicy()
* @see sched_get_priority_min()
* @see sched_get_priority_max()
* @see sched_setparam()
*/
int pthread_attr_setschedparam(pthread_attr_t *attr,
const struct sched_param *param) {
if (!param) return EINVAL;
attr->schedparam = param->sched_priority;
return 0;
}

View file

@ -16,12 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/futex.internal.h"
#include "libc/intrin/intrin.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/pthread.h"
#include "libc/limits.h"
@ -39,13 +35,9 @@
int pthread_barrier_wait(pthread_barrier_t *barrier) {
if (atomic_fetch_add(&barrier->waits, 1) + 1 == barrier->count) {
if (atomic_fetch_add(&barrier->waits, 1) + 1 < barrier->count * 2) {
atomic_store(&barrier->popped, 1);
atomic_store_explicit(&barrier->popped, 1, memory_order_relaxed);
do {
if (IsLinux() || IsOpenbsd()) {
_futex_wake(&barrier->popped, INT_MAX, barrier->pshared);
} else {
pthread_yield();
}
_futex_wake(&barrier->popped, INT_MAX, barrier->pshared);
} while (atomic_load_explicit(&barrier->waits, memory_order_relaxed) <
barrier->count * 2);
atomic_store_explicit(&barrier->popped, 0, memory_order_relaxed);
@ -54,11 +46,7 @@ int pthread_barrier_wait(pthread_barrier_t *barrier) {
return PTHREAD_BARRIER_SERIAL_THREAD;
}
do {
if (IsLinux() || IsOpenbsd()) {
_futex_wait(&barrier->popped, 0, barrier->pshared, 0);
} else {
pthread_yield();
}
_futex_wait(&barrier->popped, 0, barrier->pshared, 0);
} while (atomic_load_explicit(&barrier->waits, memory_order_relaxed) <
barrier->count);
atomic_fetch_add(&barrier->waits, 1);

View file

@ -18,7 +18,6 @@
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/asmflag.h"
#include "libc/intrin/atomic.h"
@ -36,12 +35,10 @@ static int pthread_mutex_lock_spin(pthread_mutex_t *mutex, int expect,
for (i = 0; i != 1 << tries; i++) {
}
tries++;
} else if (IsLinux() || IsOpenbsd()) {
} else {
atomic_fetch_add(&mutex->waits, 1);
_futex_wait(&mutex->lock, expect, mutex->pshared, &(struct timespec){1});
atomic_fetch_sub(&mutex->waits, 1);
} else {
pthread_yield();
}
return tries;
}

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/futex.internal.h"
#include "libc/intrin/pthread.h"
@ -28,12 +27,10 @@ static int pthread_rwlock_rdlock_spin(pthread_rwlock_t *rwlock, int expect,
for (i = 0; i != 1 << tries; i++) {
}
tries++;
} else if (IsLinux() || IsOpenbsd()) {
} else {
atomic_fetch_add(&rwlock->waits, 1);
_futex_wait(&rwlock->lock, expect, rwlock->pshared, &(struct timespec){1});
atomic_fetch_sub(&rwlock->waits, 1);
} else {
pthread_yield();
}
return tries;
}

View file

@ -21,7 +21,6 @@
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/futex.internal.h"
#include "libc/intrin/intrin.h"
#include "libc/intrin/pthread.h"
/**

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/futex.internal.h"
#include "libc/intrin/pthread.h"
@ -28,12 +27,10 @@ static int pthread_rwlock_wrlock_spin(pthread_rwlock_t *rwlock, int expect,
for (i = 0; i != 1 << tries; i++) {
}
tries++;
} else if (IsLinux() || IsOpenbsd()) {
} else {
atomic_fetch_add(&rwlock->waits, 1);
_futex_wait(&rwlock->lock, expect, rwlock->pshared, &(struct timespec){1});
atomic_fetch_sub(&rwlock->waits, 1);
} else {
pthread_yield();
}
return tries;
}

View file

@ -18,7 +18,6 @@
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/futex.internal.h"
#include "libc/intrin/pthread.h"
@ -37,10 +36,8 @@ void _wait0(const int *ctid) {
for (;;) {
if (!(x = atomic_load_explicit(ctid, memory_order_relaxed))) {
break;
} else if (IsLinux() || IsOpenbsd()) {
_futex_wait(ctid, x, PTHREAD_PROCESS_SHARED, &(struct timespec){2});
} else {
pthread_yield();
_futex_wait(ctid, x, PTHREAD_PROCESS_SHARED, &(struct timespec){2});
}
}
if (IsOpenbsd()) {