mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 23:08:31 +00:00
Rewrite Windows poll()
We can now await signals, files, pipes, and console simultaneously. This change also gives a deeper review and testing to changes made yesterday.
This commit is contained in:
parent
cceddd21b2
commit
fbdf9d028c
15 changed files with 425 additions and 191 deletions
|
@ -23,6 +23,10 @@
|
|||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/futex.internal.h"
|
||||
|
||||
__static_yoink("nsync_mu_lock");
|
||||
__static_yoink("nsync_mu_unlock");
|
||||
__static_yoink("nsync_mu_trylock");
|
||||
|
||||
/**
|
||||
* Wakes all threads waiting on condition, e.g.
|
||||
*
|
||||
|
@ -43,9 +47,14 @@
|
|||
errno_t pthread_cond_broadcast(pthread_cond_t *cond) {
|
||||
|
||||
#if PTHREAD_USE_NSYNC
|
||||
// do nothing if pthread_cond_timedwait() hasn't been called yet
|
||||
// this is because we dont know for certain if nsync use is safe
|
||||
if (!atomic_load_explicit(&cond->_waited, memory_order_acquire))
|
||||
return 0;
|
||||
|
||||
// favor *NSYNC if this is a process private condition variable
|
||||
// if using Mike Burrows' code isn't possible, use a naive impl
|
||||
if (!cond->_pshared && !IsXnuSilicon()) {
|
||||
if (!cond->_footek) {
|
||||
nsync_cv_broadcast((nsync_cv *)cond);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -30,11 +30,8 @@ errno_t pthread_cond_init(pthread_cond_t *cond,
|
|||
const pthread_condattr_t *attr) {
|
||||
*cond = (pthread_cond_t){0};
|
||||
if (attr) {
|
||||
cond->_footek = IsXnuSilicon() || attr->_pshared;
|
||||
cond->_pshared = attr->_pshared;
|
||||
cond->_clock = attr->_clock;
|
||||
} else {
|
||||
cond->_footek = IsXnuSilicon();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,10 @@
|
|||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/futex.internal.h"
|
||||
|
||||
__static_yoink("nsync_mu_lock");
|
||||
__static_yoink("nsync_mu_unlock");
|
||||
__static_yoink("nsync_mu_trylock");
|
||||
|
||||
/**
|
||||
* Wakes at least one thread waiting on condition, e.g.
|
||||
*
|
||||
|
@ -43,7 +47,7 @@ errno_t pthread_cond_signal(pthread_cond_t *cond) {
|
|||
|
||||
#if PTHREAD_USE_NSYNC
|
||||
// do nothing if pthread_cond_timedwait() hasn't been called yet
|
||||
// this is because we dont know for certain if nsync is safe
|
||||
// this is because we dont know for certain if nsync use is safe
|
||||
if (!atomic_load_explicit(&cond->_waited, memory_order_acquire))
|
||||
return 0;
|
||||
|
||||
|
|
|
@ -31,11 +31,21 @@
|
|||
#include "third_party/nsync/futex.internal.h"
|
||||
#include "third_party/nsync/time.h"
|
||||
|
||||
__static_yoink("nsync_mu_lock");
|
||||
__static_yoink("nsync_mu_unlock");
|
||||
__static_yoink("nsync_mu_trylock");
|
||||
|
||||
struct PthreadWait {
|
||||
pthread_cond_t *cond;
|
||||
pthread_mutex_t *mutex;
|
||||
};
|
||||
|
||||
static bool can_use_nsync(uint64_t muword) {
|
||||
return !IsXnuSilicon() && //
|
||||
MUTEX_TYPE(muword) == PTHREAD_MUTEX_NORMAL &&
|
||||
MUTEX_PSHARED(muword) == PTHREAD_PROCESS_PRIVATE;
|
||||
}
|
||||
|
||||
static void pthread_cond_leave(void *arg) {
|
||||
struct PthreadWait *wait = (struct PthreadWait *)arg;
|
||||
if (pthread_mutex_lock(wait->mutex))
|
||||
|
@ -117,19 +127,20 @@ errno_t pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
|||
MUTEX_OWNER(muword) != gettid())
|
||||
return EPERM;
|
||||
|
||||
// if the cond is process shared then the mutex needs to be too
|
||||
if ((cond->_pshared == PTHREAD_PROCESS_SHARED) ^
|
||||
(MUTEX_PSHARED(muword) == PTHREAD_PROCESS_SHARED))
|
||||
return EINVAL;
|
||||
|
||||
#if PTHREAD_USE_NSYNC
|
||||
// the first time pthread_cond_timedwait() is called we learn if the
|
||||
// associated mutex is normal and private. that means *NSYNC is safe
|
||||
// this decision is permanent. you can't use a recursive mutex later
|
||||
if (!atomic_load_explicit(&cond->_waited, memory_order_acquire)) {
|
||||
if (!cond->_footek)
|
||||
if (MUTEX_TYPE(muword) != PTHREAD_MUTEX_NORMAL ||
|
||||
MUTEX_PSHARED(muword) != PTHREAD_PROCESS_PRIVATE)
|
||||
cond->_footek = true;
|
||||
cond->_footek = !can_use_nsync(muword);
|
||||
atomic_store_explicit(&cond->_waited, true, memory_order_release);
|
||||
} else if (!cond->_footek) {
|
||||
if (MUTEX_TYPE(muword) != PTHREAD_MUTEX_NORMAL ||
|
||||
MUTEX_PSHARED(muword) != PTHREAD_PROCESS_PRIVATE)
|
||||
if (!can_use_nsync(muword))
|
||||
return EINVAL;
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue