TYPEDEF int uid_t;
TYPEDEF int gid_t;
TYPEDEF int key_t;
+
TYPEDEF struct __pthread * pthread_t;
+TYPEDEF int pthread_once_t;
+TYPEDEF int pthread_key_t;
+TYPEDEF int pthread_spinlock_t;
+
+TYPEDEF struct { union { int __i[9]; size_t __s[2]; } __u; } pthread_attr_t;
+TYPEDEF unsigned pthread_mutexattr_t;
+TYPEDEF unsigned pthread_condattr_t;
+TYPEDEF unsigned pthread_barrierattr_t;
+TYPEDEF struct { unsigned __attr[2]; } pthread_rwlockattr_t;
+
+TYPEDEF struct { union { int __i[6]; void *__p[1]; } __u; } pthread_mutex_t;
+TYPEDEF struct { union { int __i[12]; void *__p[1]; } __u; } pthread_cond_t;
+TYPEDEF struct { union { int __i[8]; void *__p[1]; } __u; } pthread_rwlock_t;
+TYPEDEF struct { union { int __i[5]; void *__p[1]; } __u; } pthread_barrier_t;
TYPEDEF long long off_t;
TYPEDEF unsigned int uid_t;
TYPEDEF unsigned int gid_t;
TYPEDEF int key_t;
+
TYPEDEF struct __pthread * pthread_t;
+TYPEDEF int pthread_once_t;
+TYPEDEF int pthread_key_t;
+TYPEDEF int pthread_spinlock_t;
+
+TYPEDEF struct { union { int __i[14]; size_t __s[2]; } __u; } pthread_attr_t;
+TYPEDEF unsigned pthread_mutexattr_t;
+TYPEDEF unsigned pthread_condattr_t;
+TYPEDEF unsigned pthread_barrierattr_t;
+TYPEDEF struct { unsigned __attr[2]; } pthread_rwlockattr_t;
+
+TYPEDEF struct { union { int __i[10]; void *__p[1]; } __u; } pthread_mutex_t;
+TYPEDEF struct { union { int __i[12]; void *__p[1]; } __u; } pthread_cond_t;
+TYPEDEF struct { union { int __i[14]; void *__p[1]; } __u; } pthread_rwlock_t;
+TYPEDEF struct { union { int __i[8]; void *__p[1]; } __u; } pthread_barrier_t;
TYPEDEF long off_t;
#define __NEED_struct_timespec
#define __NEED_sigset_t
#define __NEED_pthread_t
+#define __NEED_pthread_attr_t
+#define __NEED_pthread_mutexattr_t
+#define __NEED_pthread_condattr_t
+#define __NEED_pthread_rwlockattr_t
+#define __NEED_pthread_barrierattr_t
+#define __NEED_pthread_mutex_t
+#define __NEED_pthread_cond_t
+#define __NEED_pthread_rwlock_t
+#define __NEED_pthread_barrier_t
+#define __NEED_pthread_spinlock_t
+#define __NEED_pthread_key_t
+#define __NEED_pthread_once_t
#define __NEED_size_t
#include <bits/alltypes.h>
#include <sched.h>
#include <time.h>
-typedef int pthread_once_t, pthread_key_t, pthread_spinlock_t;
-typedef int pthread_mutexattr_t, pthread_condattr_t, pthread_barrierattr_t;
-
-typedef struct {
- size_t __guardsize;
- size_t __stacksize;
- unsigned __detach : 1;
- unsigned __pad : 31;
- int __attr[6];
-} pthread_attr_t;
-
-typedef struct {
- int __attr[2];
-} pthread_rwlockattr_t;
-
-typedef struct {
- int __type;
- int __lock;
- pthread_t __owner;
- int __pad2;
- int __waiters;
- int __pad;
-} pthread_mutex_t;
-
-typedef struct {
- int __block;
- int __pad[11];
-} pthread_cond_t;
-
-typedef struct {
- int __wrlock;
- int __readers;
- int __waiters;
- int __owner;
- int __pad[4];
-} pthread_rwlock_t;
-
-typedef struct {
- int __count;
- int __limit;
- int __left;
- int __waiters;
- int __barrier[1];
-} pthread_barrier_t;
-
#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1
#define __NEED_suseconds_t
#define __NEED_blksize_t
+#define __NEED_pthread_t
+#define __NEED_pthread_attr_t
+#define __NEED_pthread_mutexattr_t
+#define __NEED_pthread_condattr_t
+#define __NEED_pthread_rwlockattr_t
+#define __NEED_pthread_barrierattr_t
+#define __NEED_pthread_mutex_t
+#define __NEED_pthread_cond_t
+#define __NEED_pthread_rwlock_t
+#define __NEED_pthread_barrier_t
+#define __NEED_pthread_spinlock_t
+#define __NEED_pthread_key_t
+#define __NEED_pthread_once_t
+
#include <bits/alltypes.h>
#ifdef _GNU_SOURCE
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
int pthread_attr_getdetachstate(pthread_attr_t *a, int *state)
{
- *state = a->__detach;
+ *state = a->_a_detach;
return 0;
}
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;
}
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;
}
int pthread_attr_setdetachstate(pthread_attr_t *a, int state)
{
- a->__detach = state;
+ a->_a_detach = state;
return 0;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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);
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;
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;
{
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;
}
{
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;
}
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;
}
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;
}
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;
}
{
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;
}
{
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;
}
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;
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;
}
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;
}
{
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;
}