mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-02 17:28:30 +00:00
Add more POSIX threads APIs
This commit is contained in:
parent
1729a8259c
commit
c3208eb9d5
32 changed files with 592 additions and 87 deletions
|
@ -1,5 +1,6 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_
|
||||
#include "libc/calls/struct/sched_param.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
@ -68,9 +69,11 @@ struct PosixThread {
|
|||
|
||||
void _pthread_free(struct PosixThread *) hidden;
|
||||
void _pthread_wait(struct PosixThread *) hidden;
|
||||
int _pthread_reschedule(struct PosixThread *) hidden;
|
||||
void _pthread_zombies_add(struct PosixThread *) hidden;
|
||||
void _pthread_zombies_decimate(void) hidden;
|
||||
void _pthread_zombies_harvest(void) hidden;
|
||||
int _pthread_setschedparam_freebsd(int, int, const struct sched_param *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/futex.internal.h"
|
||||
|
@ -77,11 +76,7 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
|||
rel = _timespec_sub(*abstime, now);
|
||||
tsp = &rel;
|
||||
}
|
||||
if (IsLinux() || IsOpenbsd()) {
|
||||
_futex_wait(&cond->seq, seq, cond->pshared, tsp);
|
||||
} else {
|
||||
sched_yield();
|
||||
}
|
||||
_futex_wait(&cond->seq, seq, cond->pshared, tsp);
|
||||
} while (seq == atomic_load_explicit(&cond->seq, memory_order_relaxed));
|
||||
|
||||
atomic_fetch_sub(&cond->waits, 1);
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/sched-sysv.internal.h"
|
||||
#include "libc/calls/syscall-sysv.internal.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
|
@ -37,6 +38,7 @@
|
|||
#include "libc/sysv/consts/clone.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/internal.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/spawn.h"
|
||||
|
@ -62,6 +64,9 @@ void _pthread_free(struct PosixThread *pt) {
|
|||
static int PosixThread(void *arg, int tid) {
|
||||
struct PosixThread *pt = arg;
|
||||
enum PosixThreadStatus status;
|
||||
if (pt->attr.inheritsched == PTHREAD_EXPLICIT_SCHED) {
|
||||
_pthread_reschedule(pt);
|
||||
}
|
||||
if (!setjmp(pt->exiter)) {
|
||||
((cthread_t)__get_tls())->pthread = (pthread_t)pt;
|
||||
pt->rc = pt->start_routine(pt->arg);
|
||||
|
|
46
libc/thread/pthread_getaffinity_np.c
Normal file
46
libc/thread/pthread_getaffinity_np.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*-*- 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/cpuset.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
/**
|
||||
* Gets CPU affinity for thread.
|
||||
*
|
||||
* While Windows allows us to change the thread affinity mask, it's only
|
||||
* possible to read the process affinity mask. Therefore this function
|
||||
* won't reflect the changes made by psched_setaffinity_np() on Windows.
|
||||
*
|
||||
* @param bitsetsize is byte length of bitset, which should be 128
|
||||
* @return 0 on success, or errno on error
|
||||
* @raise ENOSYS if not Linux or Windows
|
||||
*/
|
||||
int pthread_getaffinity_np(pthread_t thread, size_t bitsetsize,
|
||||
cpu_set_t *bitset) {
|
||||
int rc, e = errno;
|
||||
struct PosixThread *pt = (struct PosixThread *)thread;
|
||||
if (!sched_getaffinity(pt->tid, bitsetsize, bitset)) {
|
||||
return 0;
|
||||
} else {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
return rc;
|
||||
}
|
||||
}
|
31
libc/thread/pthread_getschedparam.c
Normal file
31
libc/thread/pthread_getschedparam.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*-*- 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"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
|
||||
/**
|
||||
* Gets most recently set scheduling of thread.
|
||||
*/
|
||||
int pthread_getschedparam(pthread_t thread, int *policy,
|
||||
struct sched_param *param) {
|
||||
struct PosixThread *pt = (struct PosixThread *)thread;
|
||||
*policy = pt->attr.schedpolicy;
|
||||
*param = (struct sched_param){pt->attr.schedparam};
|
||||
return 0;
|
||||
}
|
42
libc/thread/pthread_reschedule.c
Normal file
42
libc/thread/pthread_reschedule.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*-*- 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/sched-sysv.internal.h"
|
||||
#include "libc/calls/struct/sched_param.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
int _pthread_reschedule(struct PosixThread *pt) {
|
||||
int rc, e = errno;
|
||||
struct sched_param param = {pt->attr.schedparam};
|
||||
if (IsNetbsd()) {
|
||||
rc = sys_sched_setparam_netbsd(0, pt->tid, pt->attr.schedpolicy, ¶m);
|
||||
} else if (IsLinux()) {
|
||||
rc = sys_sched_setscheduler(pt->tid, pt->attr.schedpolicy, ¶m);
|
||||
} else if (IsFreebsd()) {
|
||||
rc = _pthread_setschedparam_freebsd(pt->tid, pt->attr.schedpolicy, ¶m);
|
||||
} else {
|
||||
rc = enosys();
|
||||
}
|
||||
rc = rc != -1 ? 0 : errno;
|
||||
errno = e;
|
||||
return rc;
|
||||
}
|
42
libc/thread/pthread_setaffinity_np.c
Normal file
42
libc/thread/pthread_setaffinity_np.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*-*- 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/cpuset.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
/**
|
||||
* Asks kernel to only schedule thread on particular CPUs.
|
||||
*
|
||||
* @param bitsetsize is byte length of bitset, which should be 128
|
||||
* @return 0 on success, or errno on error
|
||||
* @raise ENOSYS if not Linux or Windows
|
||||
*/
|
||||
int pthread_setaffinity_np(pthread_t thread, size_t bitsetsize,
|
||||
const cpu_set_t *bitset) {
|
||||
int rc, e = errno;
|
||||
struct PosixThread *pt = (struct PosixThread *)thread;
|
||||
if (!sched_setaffinity(pt->tid, bitsetsize, bitset)) {
|
||||
return 0;
|
||||
} else {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
return rc;
|
||||
}
|
||||
}
|
51
libc/thread/pthread_setschedparam.c
Normal file
51
libc/thread/pthread_setschedparam.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*-*- 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/errno.h"
|
||||
#include "libc/intrin/pthread2.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
|
||||
/**
|
||||
* Changes scheduling of thread, e.g.
|
||||
*
|
||||
* struct sched_param p = {sched_get_priority_min(SCHED_OTHER)};
|
||||
* pthread_setschedparam(thread, SCHED_OTHER, &p);
|
||||
*
|
||||
* @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`
|
||||
* @raise ENOSYS on XNU, Windows, OpenBSD
|
||||
* @raise EPERM if not authorized to use scheduler in question (e.g.
|
||||
* trying to use a real-time scheduler as non-root on Linux) or
|
||||
* possibly because pledge() was used and isn't allowing this
|
||||
* @see sched_get_priority_min()
|
||||
* @see sched_get_priority_max()
|
||||
* @see sched_setscheduler()
|
||||
*/
|
||||
int pthread_setschedparam(pthread_t thread, int policy,
|
||||
const struct sched_param *param) {
|
||||
struct PosixThread *pt = (struct PosixThread *)thread;
|
||||
if (!param) return EINVAL;
|
||||
pt->attr.schedpolicy = policy;
|
||||
pt->attr.schedparam = param->sched_priority;
|
||||
return _pthread_reschedule(pt);
|
||||
}
|
48
libc/thread/pthread_setschedparam_freebsd.c
Normal file
48
libc/thread/pthread_setschedparam_freebsd.c
Normal 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/sysv/consts/sched.h"
|
||||
#include "libc/thread/freebsd.internal.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
#define RTP_SET_FREEBSD 1
|
||||
#define PRI_REALTIME_FREEBSD 2
|
||||
#define RTP_PRIO_MAX_FREEBSD 31
|
||||
#define PRI_FIFO_BIT_FREEBSD 8
|
||||
#define PRI_FIFO_FREEBSD (PRI_REALTIME_FREEBSD | PRI_FIFO_BIT_FREEBSD)
|
||||
#define PRI_TIMESHARE_FREEBSD 3
|
||||
|
||||
int rtprio_thread(int fun, int tid, struct rtprio *inout_rtp);
|
||||
|
||||
int _pthread_setschedparam_freebsd(int tid, int policy,
|
||||
const struct sched_param *param) {
|
||||
struct rtprio rtp;
|
||||
if (policy == SCHED_RR) {
|
||||
rtp.type = PRI_REALTIME_FREEBSD;
|
||||
rtp.prio = RTP_PRIO_MAX_FREEBSD - param->sched_priority;
|
||||
} else if (policy == SCHED_FIFO) {
|
||||
rtp.type = PRI_FIFO_FREEBSD;
|
||||
rtp.prio = RTP_PRIO_MAX_FREEBSD - param->sched_priority;
|
||||
} else {
|
||||
rtp.type = PRI_TIMESHARE_FREEBSD;
|
||||
rtp.prio = 0;
|
||||
}
|
||||
return rtprio_thread(RTP_SET_FREEBSD, tid, &rtp);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue