reorganize pthread data structures and move the definitions to alltypes.h
this allows sys/types.h to provide the pthread types, as required by POSIX. this design also facilitates forcing ABI-compatible sizes in the arch-specific alltypes.h, while eliminating the need for developers changing the internals of the pthread types to poke around with arch-specific headers they may not be able to test.
This commit is contained in:
parent
4fd159568a
commit
e882756311
28 changed files with 158 additions and 128 deletions
|
|
@ -38,6 +38,25 @@ struct pthread {
|
|||
pthread_attr_t attr;
|
||||
};
|
||||
|
||||
#define __SU (sizeof(size_t)/sizeof(int))
|
||||
|
||||
#define _a_stacksize __u.__s[0]
|
||||
#define _a_guardsize __u.__s[1]
|
||||
#define _a_detach __u.__i[2*__SU+0]
|
||||
#define _m_type __u.__i[0]
|
||||
#define _m_lock __u.__i[1]
|
||||
#define _m_waiters __u.__i[2]
|
||||
#define _m_owner __u.__i[3]
|
||||
#define _c_block __u.__i[0]
|
||||
#define _rw_wrlock __u.__i[0]
|
||||
#define _rw_readers __u.__i[1]
|
||||
#define _rw_waiters __u.__i[2]
|
||||
#define _rw_owner __u.__i[3]
|
||||
#define _b_count __u.__i[0]
|
||||
#define _b_limit __u.__i[1]
|
||||
#define _b_left __u.__i[2]
|
||||
#define _b_waiters __u.__i[3]
|
||||
|
||||
#include "pthread_arch.h"
|
||||
|
||||
#define SIGCANCEL 32
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
int pthread_attr_getdetachstate(pthread_attr_t *a, int *state)
|
||||
{
|
||||
*state = a->__detach;
|
||||
*state = a->_a_detach;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
int pthread_attr_getguardsize(pthread_attr_t *a, size_t *size)
|
||||
{
|
||||
*size = a->__guardsize + DEFAULT_GUARD_SIZE;
|
||||
*size = a->_a_guardsize + DEFAULT_GUARD_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
int pthread_attr_getstacksize(pthread_attr_t *a, size_t *size)
|
||||
{
|
||||
*size = a->__stacksize + DEFAULT_STACK_SIZE;
|
||||
*size = a->_a_stacksize + DEFAULT_STACK_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
int pthread_attr_setdetachstate(pthread_attr_t *a, int state)
|
||||
{
|
||||
a->__detach = state;
|
||||
a->_a_detach = state;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@
|
|||
int pthread_attr_setguardsize(pthread_attr_t *a, size_t size)
|
||||
{
|
||||
if (size > SIZE_MAX/8) return EINVAL;
|
||||
a->__guardsize = size - DEFAULT_GUARD_SIZE;
|
||||
a->_a_guardsize = size - DEFAULT_GUARD_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@
|
|||
int pthread_attr_setstacksize(pthread_attr_t *a, size_t size)
|
||||
{
|
||||
if (size-PAGE_SIZE > SIZE_MAX/4) return EINVAL;
|
||||
a->__stacksize = size - DEFAULT_STACK_SIZE;
|
||||
a->_a_stacksize = size - DEFAULT_STACK_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@
|
|||
int pthread_barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *a, unsigned count)
|
||||
{
|
||||
if (!count) return EINVAL;
|
||||
*b = (pthread_barrier_t){ .__limit = count-1 };
|
||||
*b = (pthread_barrier_t){ ._b_limit = count-1 };
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,27 +5,27 @@ int pthread_barrier_wait(pthread_barrier_t *b)
|
|||
int cur;
|
||||
|
||||
/* Trivial case: count was set at 1 */
|
||||
if (!b->__limit) return PTHREAD_BARRIER_SERIAL_THREAD;
|
||||
if (!b->_b_limit) return PTHREAD_BARRIER_SERIAL_THREAD;
|
||||
|
||||
/* Wait for anyone still suspended at previous use of barrier */
|
||||
while ((cur=b->__left))
|
||||
__wait(&b->__left, &b->__waiters, cur, 0);
|
||||
while ((cur=b->_b_left))
|
||||
__wait(&b->_b_left, &b->_b_waiters, cur, 0);
|
||||
|
||||
/* If we are the last to reach barrier, reset it and wake others */
|
||||
if (a_fetch_add(&b->__count, 1) == b->__limit) {
|
||||
b->__left = b->__limit;
|
||||
b->__count = 0;
|
||||
__wake(&b->__count, -1, 0);
|
||||
if (a_fetch_add(&b->_b_count, 1) == b->_b_limit) {
|
||||
b->_b_left = b->_b_limit;
|
||||
b->_b_count = 0;
|
||||
__wake(&b->_b_count, -1, 0);
|
||||
return PTHREAD_BARRIER_SERIAL_THREAD;
|
||||
}
|
||||
|
||||
/* Wait for our peers to reach the barrier */
|
||||
while ((cur=b->__count))
|
||||
__wait(&b->__count, 0, cur, 0);
|
||||
while ((cur=b->_b_count))
|
||||
__wait(&b->_b_count, 0, cur, 0);
|
||||
|
||||
/* If we're the last to wake up and barrier is awaiting reuse */
|
||||
if (a_fetch_add(&b->__left, -1) == 1 && b->__waiters)
|
||||
__wake(&b->__left, -1, 0);
|
||||
if (a_fetch_add(&b->_b_left, -1) == 1 && b->_b_waiters)
|
||||
__wake(&b->_b_left, -1, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
int pthread_cond_broadcast(pthread_cond_t *c)
|
||||
{
|
||||
c->__block = 0;
|
||||
__wake(&c->__block, -1, 0);
|
||||
c->_c_block = 0;
|
||||
__wake(&c->_c_block, -1, 0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
int pthread_cond_signal(pthread_cond_t *c)
|
||||
{
|
||||
c->__block = 0;
|
||||
__wake(&c->__block, 1, 0);
|
||||
c->_c_block = 0;
|
||||
__wake(&c->_c_block, 1, 0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,11 @@ int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct t
|
|||
CANCELPT(0);
|
||||
|
||||
pthread_cleanup_push(relock, m);
|
||||
c->__block = 1;
|
||||
c->_c_block = 1;
|
||||
if ((r=pthread_mutex_unlock(m))) return r;
|
||||
|
||||
CANCELPT(1);
|
||||
e = __timedwait(&c->__block, 1, CLOCK_REALTIME, ts, 0);
|
||||
e = __timedwait(&c->_c_block, 1, CLOCK_REALTIME, ts, 0);
|
||||
CANCELPT(0);
|
||||
|
||||
pthread_cleanup_pop(0);
|
||||
|
|
|
|||
|
|
@ -173,8 +173,8 @@ int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(vo
|
|||
if (!init && ++init) init_threads();
|
||||
|
||||
if (!attr) attr = &default_attr;
|
||||
guard = ROUND(attr->__guardsize + DEFAULT_GUARD_SIZE);
|
||||
size = guard + ROUND(attr->__stacksize + DEFAULT_STACK_SIZE);
|
||||
guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
|
||||
size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
|
||||
size += __pthread_tsd_size;
|
||||
map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
|
||||
if (!map) return EAGAIN;
|
||||
|
|
@ -190,7 +190,7 @@ int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(vo
|
|||
new->start_arg = arg;
|
||||
new->self = new;
|
||||
new->tsd = (void *)tsd;
|
||||
new->detached = attr->__detach;
|
||||
new->detached = attr->_a_detach;
|
||||
new->attr = *attr;
|
||||
memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
|
||||
new->tlsdesc[1] = (uintptr_t)new;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,6 @@ int pthread_mutex_lock(pthread_mutex_t *m)
|
|||
{
|
||||
int r;
|
||||
while ((r=pthread_mutex_trylock(m)) == EBUSY)
|
||||
__wait(&m->__lock, &m->__waiters, 1, 0);
|
||||
__wait(&m->_m_lock, &m->_m_waiters, 1, 0);
|
||||
return r;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *at)
|
|||
{
|
||||
int r, w=0;
|
||||
while ((r=pthread_mutex_trylock(m)) == EBUSY) {
|
||||
if (!w) a_inc(&m->__waiters), w++;
|
||||
if (__timedwait(&m->__lock, 1, CLOCK_REALTIME, at, 0) == ETIMEDOUT) {
|
||||
if (w) a_dec(&m->__waiters);
|
||||
if (!w) a_inc(&m->_m_waiters), w++;
|
||||
if (__timedwait(&m->_m_lock, 1, CLOCK_REALTIME, at, 0) == ETIMEDOUT) {
|
||||
if (w) a_dec(&m->_m_waiters);
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
if (w) a_dec(&m->__waiters);
|
||||
if (w) a_dec(&m->_m_waiters);
|
||||
return r;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,27 +2,27 @@
|
|||
|
||||
int pthread_mutex_trylock(pthread_mutex_t *m)
|
||||
{
|
||||
if (m->__type == PTHREAD_MUTEX_RECURSIVE) {
|
||||
if (m->_m_type == PTHREAD_MUTEX_RECURSIVE) {
|
||||
pthread_t self = pthread_self();
|
||||
if (m->__owner == self) {
|
||||
if ((unsigned)m->__lock >= INT_MAX) return EAGAIN;
|
||||
a_inc(&m->__lock);
|
||||
if (m->_m_owner == self->tid) {
|
||||
if ((unsigned)m->_m_lock >= INT_MAX) return EAGAIN;
|
||||
a_inc(&m->_m_lock);
|
||||
return 0;
|
||||
}
|
||||
if (a_fetch_add(&m->__lock, 1)) {
|
||||
if (a_fetch_add(&m->__lock, -1)==1 && m->__waiters)
|
||||
__wake(&m->__lock, 1, 0);
|
||||
if (a_fetch_add(&m->_m_lock, 1)) {
|
||||
if (a_fetch_add(&m->_m_lock, -1)==1 && m->_m_waiters)
|
||||
__wake(&m->_m_lock, 1, 0);
|
||||
return EBUSY;
|
||||
}
|
||||
m->__owner = self;
|
||||
m->_m_owner = self->tid;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (a_xchg(&m->__lock, 1))
|
||||
if (m->__type == PTHREAD_MUTEX_ERRORCHECK
|
||||
&& m->__owner == pthread_self()) return EDEADLK;
|
||||
if (a_xchg(&m->_m_lock, 1))
|
||||
if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK
|
||||
&& m->_m_owner == pthread_self()->tid) return EDEADLK;
|
||||
else return EBUSY;
|
||||
if (m->__type == PTHREAD_MUTEX_ERRORCHECK)
|
||||
m->__owner = pthread_self();
|
||||
if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK)
|
||||
m->_m_owner = pthread_self()->tid;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
int pthread_mutex_unlock(pthread_mutex_t *m)
|
||||
{
|
||||
if (m->__type == PTHREAD_MUTEX_RECURSIVE) {
|
||||
if (a_fetch_add(&m->__lock, -1)==1 && m->__waiters)
|
||||
__wake(&m->__lock, 1, 0);
|
||||
if (m->_m_type == PTHREAD_MUTEX_RECURSIVE) {
|
||||
if (a_fetch_add(&m->_m_lock, -1)==1 && m->_m_waiters)
|
||||
__wake(&m->_m_lock, 1, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (m->__type == PTHREAD_MUTEX_ERRORCHECK
|
||||
&& m->__owner != pthread_self())
|
||||
if (m->_m_type == PTHREAD_MUTEX_ERRORCHECK
|
||||
&& m->_m_owner != pthread_self()->tid)
|
||||
return EPERM;
|
||||
|
||||
m->__owner = 0;
|
||||
m->__lock = 0;
|
||||
if (m->__waiters) __wake(&m->__lock, 1, 0);
|
||||
m->_m_owner = 0;
|
||||
m->_m_lock = 0;
|
||||
if (m->_m_waiters) __wake(&m->_m_lock, 1, 0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@
|
|||
int pthread_rwlock_rdlock(pthread_rwlock_t *rw)
|
||||
{
|
||||
while (pthread_rwlock_tryrdlock(rw))
|
||||
__wait(&rw->__wrlock, &rw->__waiters, 1, 0);
|
||||
__wait(&rw->_rw_wrlock, &rw->_rw_waiters, 1, 0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rw, const struct timespec *at)
|
|||
{
|
||||
int w=0;
|
||||
while (pthread_rwlock_tryrdlock(rw)) {
|
||||
if (!w) a_inc(&rw->__waiters), w++;
|
||||
if (__timedwait(&rw->__wrlock, 1, CLOCK_REALTIME, at, 0)==ETIMEDOUT) {
|
||||
if (w) a_dec(&rw->__waiters);
|
||||
if (!w) a_inc(&rw->_rw_waiters), w++;
|
||||
if (__timedwait(&rw->_rw_wrlock, 1, CLOCK_REALTIME, at, 0)==ETIMEDOUT) {
|
||||
if (w) a_dec(&rw->_rw_waiters);
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
if (w) a_dec(&rw->__waiters);
|
||||
if (w) a_dec(&rw->_rw_waiters);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,14 +4,14 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rw, const struct timespec *at)
|
|||
{
|
||||
int nr, *p, w=0;
|
||||
while (pthread_rwlock_trywrlock(rw)==EAGAIN) {
|
||||
if (!w) a_inc(&rw->__waiters), w++;
|
||||
if ((nr=rw->__readers)) p = &rw->__readers;
|
||||
else nr=1, p = &rw->__wrlock;
|
||||
if (!w) a_inc(&rw->_rw_waiters), w++;
|
||||
if ((nr=rw->_rw_readers)) p = &rw->_rw_readers;
|
||||
else nr=1, p = &rw->_rw_wrlock;
|
||||
if (__timedwait(p, nr, CLOCK_REALTIME, at, 0)==ETIMEDOUT) {
|
||||
if (w) a_dec(&rw->__waiters);
|
||||
if (w) a_dec(&rw->_rw_waiters);
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
if (w) a_dec(&rw->__waiters);
|
||||
if (w) a_dec(&rw->_rw_waiters);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
|
||||
{
|
||||
a_inc(&rw->__readers);
|
||||
if (rw->__wrlock) {
|
||||
a_dec(&rw->__readers);
|
||||
if (rw->__waiters && !rw->__readers)
|
||||
__wake(&rw->__readers, 1, 0);
|
||||
a_inc(&rw->_rw_readers);
|
||||
if (rw->_rw_wrlock) {
|
||||
a_dec(&rw->_rw_readers);
|
||||
if (rw->_rw_waiters && !rw->_rw_readers)
|
||||
__wake(&rw->_rw_readers, 1, 0);
|
||||
return EAGAIN;
|
||||
}
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
int pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
|
||||
{
|
||||
if (a_xchg(&rw->__wrlock, 1))
|
||||
if (a_xchg(&rw->_rw_wrlock, 1))
|
||||
return EAGAIN;
|
||||
if (rw->__readers) {
|
||||
a_store(&rw->__wrlock, 0);
|
||||
if (rw->_rw_readers) {
|
||||
a_store(&rw->_rw_wrlock, 0);
|
||||
return EAGAIN;
|
||||
}
|
||||
rw->__owner = pthread_self()->tid;
|
||||
rw->_rw_owner = pthread_self()->tid;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,15 +3,15 @@
|
|||
int pthread_rwlock_unlock(pthread_rwlock_t *rw)
|
||||
{
|
||||
struct pthread *self = pthread_self();
|
||||
if (rw->__owner == self->tid) {
|
||||
rw->__owner = 0;
|
||||
a_store(&rw->__wrlock, 0);
|
||||
if (rw->__waiters)
|
||||
__wake(&rw->__wrlock, -1, 0);
|
||||
if (rw->_rw_owner == self->tid) {
|
||||
rw->_rw_owner = 0;
|
||||
a_store(&rw->_rw_wrlock, 0);
|
||||
if (rw->_rw_waiters)
|
||||
__wake(&rw->_rw_wrlock, -1, 0);
|
||||
return 0;
|
||||
}
|
||||
a_dec(&rw->__readers);
|
||||
if (rw->__waiters && !rw->__readers)
|
||||
__wake(&rw->__readers, 1, 0);
|
||||
a_dec(&rw->_rw_readers);
|
||||
if (rw->_rw_waiters && !rw->_rw_readers)
|
||||
__wake(&rw->_rw_readers, 1, 0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *rw)
|
|||
{
|
||||
int nr;
|
||||
while (pthread_rwlock_trywrlock(rw)==EAGAIN) {
|
||||
if ((nr=rw->__readers))
|
||||
__wait(&rw->__readers, &rw->__waiters, nr, 0);
|
||||
if ((nr=rw->_rw_readers))
|
||||
__wait(&rw->_rw_readers, &rw->_rw_waiters, nr, 0);
|
||||
else
|
||||
__wait(&rw->__wrlock, &rw->__waiters, 1, 0);
|
||||
__wait(&rw->_rw_wrlock, &rw->_rw_waiters, 1, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue