[fusl] clang-format fusl
R=viettrungluu@chromium.org
Review URL: https://codereview.chromium.org/1714623002 .
diff --git a/fusl/src/thread/__futex.c b/fusl/src/thread/__futex.c
index 96307c0..c836586 100644
--- a/fusl/src/thread/__futex.c
+++ b/fusl/src/thread/__futex.c
@@ -1,7 +1,6 @@
#include "futex.h"
#include "syscall.h"
-int __futex(volatile int *addr, int op, int val, void *ts)
-{
- return syscall(SYS_futex, addr, op, val, ts);
+int __futex(volatile int* addr, int op, int val, void* ts) {
+ return syscall(SYS_futex, addr, op, val, ts);
}
diff --git a/fusl/src/thread/__lock.c b/fusl/src/thread/__lock.c
index 0874c04..f5e1377 100644
--- a/fusl/src/thread/__lock.c
+++ b/fusl/src/thread/__lock.c
@@ -1,15 +1,15 @@
#include "pthread_impl.h"
-void __lock(volatile int *l)
-{
- if (libc.threads_minus_1)
- while (a_swap(l, 1)) __wait(l, l+1, 1, 1);
+void __lock(volatile int* l) {
+ if (libc.threads_minus_1)
+ while (a_swap(l, 1))
+ __wait(l, l + 1, 1, 1);
}
-void __unlock(volatile int *l)
-{
- if (l[0]) {
- a_store(l, 0);
- if (l[1]) __wake(l, 1, 1);
- }
+void __unlock(volatile int* l) {
+ if (l[0]) {
+ a_store(l, 0);
+ if (l[1])
+ __wake(l, 1, 1);
+ }
}
diff --git a/fusl/src/thread/__set_thread_area.c b/fusl/src/thread/__set_thread_area.c
index 152a6a2..55923f3 100644
--- a/fusl/src/thread/__set_thread_area.c
+++ b/fusl/src/thread/__set_thread_area.c
@@ -1,10 +1,9 @@
#include "pthread_impl.h"
-int __set_thread_area(void *p)
-{
+int __set_thread_area(void* p) {
#ifdef SYS_set_thread_area
- return __syscall(SYS_set_thread_area, p);
+ return __syscall(SYS_set_thread_area, p);
#else
- return -ENOSYS;
+ return -ENOSYS;
#endif
}
diff --git a/fusl/src/thread/__syscall_cp.c b/fusl/src/thread/__syscall_cp.c
index 09a2be8..29bc954 100644
--- a/fusl/src/thread/__syscall_cp.c
+++ b/fusl/src/thread/__syscall_cp.c
@@ -1,21 +1,26 @@
#include "pthread_impl.h"
#include "syscall.h"
-__attribute__((__visibility__("hidden")))
-long __syscall_cp_c();
+__attribute__((__visibility__("hidden"))) long __syscall_cp_c();
static long sccp(syscall_arg_t nr,
- syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
- syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
-{
- return (__syscall)(nr, u, v, w, x, y, z);
+ syscall_arg_t u,
+ syscall_arg_t v,
+ syscall_arg_t w,
+ syscall_arg_t x,
+ syscall_arg_t y,
+ syscall_arg_t z) {
+ return (__syscall)(nr, u, v, w, x, y, z);
}
weak_alias(sccp, __syscall_cp_c);
-long (__syscall_cp)(syscall_arg_t nr,
- syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
- syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
-{
- return __syscall_cp_c(nr, u, v, w, x, y, z);
+long(__syscall_cp)(syscall_arg_t nr,
+ syscall_arg_t u,
+ syscall_arg_t v,
+ syscall_arg_t w,
+ syscall_arg_t x,
+ syscall_arg_t y,
+ syscall_arg_t z) {
+ return __syscall_cp_c(nr, u, v, w, x, y, z);
}
diff --git a/fusl/src/thread/__timedwait.c b/fusl/src/thread/__timedwait.c
index 13d8465..cfed587 100644
--- a/fusl/src/thread/__timedwait.c
+++ b/fusl/src/thread/__timedwait.c
@@ -5,42 +5,52 @@
#include "syscall.h"
#include "pthread_impl.h"
-int __pthread_setcancelstate(int, int *);
-int __clock_gettime(clockid_t, struct timespec *);
+int __pthread_setcancelstate(int, int*);
+int __clock_gettime(clockid_t, struct timespec*);
-int __timedwait_cp(volatile int *addr, int val,
- clockid_t clk, const struct timespec *at, int priv)
-{
- int r;
- struct timespec to, *top=0;
+int __timedwait_cp(volatile int* addr,
+ int val,
+ clockid_t clk,
+ const struct timespec* at,
+ int priv) {
+ int r;
+ struct timespec to, *top = 0;
- if (priv) priv = 128;
+ if (priv)
+ priv = 128;
- if (at) {
- if (at->tv_nsec >= 1000000000UL) return EINVAL;
- if (__clock_gettime(clk, &to)) return EINVAL;
- to.tv_sec = at->tv_sec - to.tv_sec;
- if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
- to.tv_sec--;
- to.tv_nsec += 1000000000;
- }
- if (to.tv_sec < 0) return ETIMEDOUT;
- top = &to;
- }
+ if (at) {
+ if (at->tv_nsec >= 1000000000UL)
+ return EINVAL;
+ if (__clock_gettime(clk, &to))
+ return EINVAL;
+ to.tv_sec = at->tv_sec - to.tv_sec;
+ if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
+ to.tv_sec--;
+ to.tv_nsec += 1000000000;
+ }
+ if (to.tv_sec < 0)
+ return ETIMEDOUT;
+ top = &to;
+ }
- r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT|priv, val, top);
- if (r == ENOSYS) r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
- if (r != EINTR && r != ETIMEDOUT && r != ECANCELED) r = 0;
+ r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT | priv, val, top);
+ if (r == ENOSYS)
+ r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
+ if (r != EINTR && r != ETIMEDOUT && r != ECANCELED)
+ r = 0;
- return r;
+ return r;
}
-int __timedwait(volatile int *addr, int val,
- clockid_t clk, const struct timespec *at, int priv)
-{
- int cs, r;
- __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- r = __timedwait_cp(addr, val, clk, at, priv);
- __pthread_setcancelstate(cs, 0);
- return r;
+int __timedwait(volatile int* addr,
+ int val,
+ clockid_t clk,
+ const struct timespec* at,
+ int priv) {
+ int cs, r;
+ __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ r = __timedwait_cp(addr, val, clk, at, priv);
+ __pthread_setcancelstate(cs, 0);
+ return r;
}
diff --git a/fusl/src/thread/__tls_get_addr.c b/fusl/src/thread/__tls_get_addr.c
index 6945faa..91cd3f7 100644
--- a/fusl/src/thread/__tls_get_addr.c
+++ b/fusl/src/thread/__tls_get_addr.c
@@ -2,15 +2,13 @@
#include "pthread_impl.h"
#include "libc.h"
-__attribute__((__visibility__("hidden")))
-void *__tls_get_new(size_t *);
+__attribute__((__visibility__("hidden"))) void* __tls_get_new(size_t*);
-void *__tls_get_addr(size_t *v)
-{
- pthread_t self = __pthread_self();
- if (v[0]<=(size_t)self->dtv[0])
- return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
- return __tls_get_new(v);
+void* __tls_get_addr(size_t* v) {
+ pthread_t self = __pthread_self();
+ if (v[0] <= (size_t)self->dtv[0])
+ return (char*)self->dtv[v[0]] + v[1] + DTP_OFFSET;
+ return __tls_get_new(v);
}
weak_alias(__tls_get_addr, __tls_get_new);
diff --git a/fusl/src/thread/__unmapself.c b/fusl/src/thread/__unmapself.c
index 1d3bee1..14676b5 100644
--- a/fusl/src/thread/__unmapself.c
+++ b/fusl/src/thread/__unmapself.c
@@ -5,25 +5,23 @@
#include "dynlink.h"
static volatile int lock;
-static void *unmap_base;
+static void* unmap_base;
static size_t unmap_size;
static char shared_stack[256];
-static void do_unmap()
-{
- __syscall(SYS_munmap, unmap_base, unmap_size);
- __syscall(SYS_exit);
+static void do_unmap() {
+ __syscall(SYS_munmap, unmap_base, unmap_size);
+ __syscall(SYS_exit);
}
-void __unmapself(void *base, size_t size)
-{
- int tid=__pthread_self()->tid;
- char *stack = shared_stack + sizeof shared_stack;
- stack -= (uintptr_t)stack % 16;
- while (lock || a_cas(&lock, 0, tid))
- a_spin();
- __syscall(SYS_set_tid_address, &lock);
- unmap_base = base;
- unmap_size = size;
- CRTJMP(do_unmap, stack);
+void __unmapself(void* base, size_t size) {
+ int tid = __pthread_self()->tid;
+ char* stack = shared_stack + sizeof shared_stack;
+ stack -= (uintptr_t)stack % 16;
+ while (lock || a_cas(&lock, 0, tid))
+ a_spin();
+ __syscall(SYS_set_tid_address, &lock);
+ unmap_base = base;
+ unmap_size = size;
+ CRTJMP(do_unmap, stack);
}
diff --git a/fusl/src/thread/__wait.c b/fusl/src/thread/__wait.c
index dc33c1a..4eb13dd 100644
--- a/fusl/src/thread/__wait.c
+++ b/fusl/src/thread/__wait.c
@@ -1,17 +1,21 @@
#include "pthread_impl.h"
-void __wait(volatile int *addr, volatile int *waiters, int val, int priv)
-{
- int spins=100;
- if (priv) priv = FUTEX_PRIVATE;
- while (spins-- && (!waiters || !*waiters)) {
- if (*addr==val) a_spin();
- else return;
- }
- if (waiters) a_inc(waiters);
- while (*addr==val) {
- __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS
- || __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0);
- }
- if (waiters) a_dec(waiters);
+void __wait(volatile int* addr, volatile int* waiters, int val, int priv) {
+ int spins = 100;
+ if (priv)
+ priv = FUTEX_PRIVATE;
+ while (spins-- && (!waiters || !*waiters)) {
+ if (*addr == val)
+ a_spin();
+ else
+ return;
+ }
+ if (waiters)
+ a_inc(waiters);
+ while (*addr == val) {
+ __syscall(SYS_futex, addr, FUTEX_WAIT | priv, val, 0) != -ENOSYS ||
+ __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0);
+ }
+ if (waiters)
+ a_dec(waiters);
}
diff --git a/fusl/src/thread/arm/__set_thread_area.c b/fusl/src/thread/arm/__set_thread_area.c
index 61d0282..ea69652 100644
--- a/fusl/src/thread/arm/__set_thread_area.c
+++ b/fusl/src/thread/arm/__set_thread_area.c
@@ -6,44 +6,46 @@
#define HWCAP_TLS (1 << 15)
extern const unsigned char __attribute__((__visibility__("hidden")))
- __a_barrier_dummy[], __a_barrier_oldkuser[],
- __a_barrier_v6[], __a_barrier_v7[],
- __a_cas_dummy[], __a_cas_v6[], __a_cas_v7[],
- __a_gettp_dummy[];
+__a_barrier_dummy[],
+ __a_barrier_oldkuser[], __a_barrier_v6[], __a_barrier_v7[], __a_cas_dummy[],
+ __a_cas_v6[], __a_cas_v7[], __a_gettp_dummy[];
#define __a_barrier_kuser 0xffff0fa0
#define __a_cas_kuser 0xffff0fc0
#define __a_gettp_kuser 0xffff0fe0
-extern uintptr_t __attribute__((__visibility__("hidden")))
- __a_barrier_ptr, __a_cas_ptr, __a_gettp_ptr;
+extern uintptr_t __attribute__((__visibility__("hidden"))) __a_barrier_ptr,
+ __a_cas_ptr, __a_gettp_ptr;
-#define SET(op,ver) (__a_##op##_ptr = \
- (uintptr_t)__a_##op##_##ver - (uintptr_t)__a_##op##_dummy)
+#define SET(op, ver) \
+ (__a_##op##_ptr = (uintptr_t)__a_##op##_##ver - (uintptr_t)__a_##op##_dummy)
-int __set_thread_area(void *p)
-{
+int __set_thread_area(void* p) {
#if !__ARM_ARCH_7A__ && !__ARM_ARCH_7R__ && __ARM_ARCH < 7
- if (__hwcap & HWCAP_TLS) {
- size_t *aux;
- SET(cas, v7);
- SET(barrier, v7);
- for (aux=libc.auxv; *aux; aux+=2) {
- if (*aux != AT_PLATFORM) continue;
- const char *s = (void *)aux[1];
- if (s[0]!='v' || s[1]!='6' || s[2]-'0'<10u) break;
- SET(cas, v6);
- SET(barrier, v6);
- break;
- }
- } else {
- int ver = *(int *)0xffff0ffc;
- SET(gettp, kuser);
- SET(cas, kuser);
- SET(barrier, kuser);
- if (ver < 2) a_crash();
- if (ver < 3) SET(barrier, oldkuser);
- }
+ if (__hwcap & HWCAP_TLS) {
+ size_t* aux;
+ SET(cas, v7);
+ SET(barrier, v7);
+ for (aux = libc.auxv; *aux; aux += 2) {
+ if (*aux != AT_PLATFORM)
+ continue;
+ const char* s = (void*)aux[1];
+ if (s[0] != 'v' || s[1] != '6' || s[2] - '0' < 10u)
+ break;
+ SET(cas, v6);
+ SET(barrier, v6);
+ break;
+ }
+ } else {
+ int ver = *(int*)0xffff0ffc;
+ SET(gettp, kuser);
+ SET(cas, kuser);
+ SET(barrier, kuser);
+ if (ver < 2)
+ a_crash();
+ if (ver < 3)
+ SET(barrier, oldkuser);
+ }
#endif
- return __syscall(0xf0005, p);
+ return __syscall(0xf0005, p);
}
diff --git a/fusl/src/thread/call_once.c b/fusl/src/thread/call_once.c
index a7bc935..4cc2ff4 100644
--- a/fusl/src/thread/call_once.c
+++ b/fusl/src/thread/call_once.c
@@ -1,8 +1,7 @@
#include <threads.h>
-int __pthread_once(once_flag *, void (*)(void));
+int __pthread_once(once_flag*, void (*)(void));
-void call_once(once_flag *flag, void (*func)(void))
-{
- __pthread_once(flag, func);
+void call_once(once_flag* flag, void (*func)(void)) {
+ __pthread_once(flag, func);
}
diff --git a/fusl/src/thread/clone.c b/fusl/src/thread/clone.c
index be80c8e..f25ae47 100644
--- a/fusl/src/thread/clone.c
+++ b/fusl/src/thread/clone.c
@@ -1,7 +1,6 @@
#include <errno.h>
#include "pthread_impl.h"
-int __clone(int (*func)(void *), void *stack, int flags, void *arg, ...)
-{
- return -ENOSYS;
+int __clone(int (*func)(void*), void* stack, int flags, void* arg, ...) {
+ return -ENOSYS;
}
diff --git a/fusl/src/thread/cnd_broadcast.c b/fusl/src/thread/cnd_broadcast.c
index 85d4d3e..ca9dc86 100644
--- a/fusl/src/thread/cnd_broadcast.c
+++ b/fusl/src/thread/cnd_broadcast.c
@@ -1,10 +1,9 @@
#include <threads.h>
-int __private_cond_signal(cnd_t *, int);
+int __private_cond_signal(cnd_t*, int);
-int cnd_broadcast(cnd_t *c)
-{
- /* This internal function never fails, and always returns zero,
- * which matches the value thrd_success is defined with. */
- return __private_cond_signal(c, -1);
+int cnd_broadcast(cnd_t* c) {
+ /* This internal function never fails, and always returns zero,
+ * which matches the value thrd_success is defined with. */
+ return __private_cond_signal(c, -1);
}
diff --git a/fusl/src/thread/cnd_destroy.c b/fusl/src/thread/cnd_destroy.c
index 453c90b..56b9add 100644
--- a/fusl/src/thread/cnd_destroy.c
+++ b/fusl/src/thread/cnd_destroy.c
@@ -1,6 +1,5 @@
#include <threads.h>
-void cnd_destroy(cnd_t *c)
-{
- /* For private cv this is a no-op */
+void cnd_destroy(cnd_t* c) {
+ /* For private cv this is a no-op */
}
diff --git a/fusl/src/thread/cnd_init.c b/fusl/src/thread/cnd_init.c
index 18c5085..4b6852f 100644
--- a/fusl/src/thread/cnd_init.c
+++ b/fusl/src/thread/cnd_init.c
@@ -1,7 +1,6 @@
#include <threads.h>
-int cnd_init(cnd_t *c)
-{
- *c = (cnd_t){ 0 };
- return thrd_success;
+int cnd_init(cnd_t* c) {
+ *c = (cnd_t){0};
+ return thrd_success;
}
diff --git a/fusl/src/thread/cnd_signal.c b/fusl/src/thread/cnd_signal.c
index 1211260..7f81363 100644
--- a/fusl/src/thread/cnd_signal.c
+++ b/fusl/src/thread/cnd_signal.c
@@ -1,10 +1,9 @@
#include <threads.h>
-int __private_cond_signal(cnd_t *, int);
+int __private_cond_signal(cnd_t*, int);
-int cnd_signal(cnd_t *c)
-{
- /* This internal function never fails, and always returns zero,
- * which matches the value thrd_success is defined with. */
- return __private_cond_signal(c, 1);
+int cnd_signal(cnd_t* c) {
+ /* This internal function never fails, and always returns zero,
+ * which matches the value thrd_success is defined with. */
+ return __private_cond_signal(c, 1);
}
diff --git a/fusl/src/thread/cnd_timedwait.c b/fusl/src/thread/cnd_timedwait.c
index 5997679..815dd2c 100644
--- a/fusl/src/thread/cnd_timedwait.c
+++ b/fusl/src/thread/cnd_timedwait.c
@@ -1,15 +1,21 @@
#include <threads.h>
#include <errno.h>
-int __pthread_cond_timedwait(cnd_t *restrict, mtx_t *restrict, const struct timespec *restrict);
+int __pthread_cond_timedwait(cnd_t* restrict,
+ mtx_t* restrict,
+ const struct timespec* restrict);
-int cnd_timedwait(cnd_t *restrict c, mtx_t *restrict m, const struct timespec *restrict ts)
-{
- int ret = __pthread_cond_timedwait(c, m, ts);
- switch (ret) {
- /* May also return EINVAL or EPERM. */
- default: return thrd_error;
- case 0: return thrd_success;
- case ETIMEDOUT: return thrd_timedout;
- }
+int cnd_timedwait(cnd_t* restrict c,
+ mtx_t* restrict m,
+ const struct timespec* restrict ts) {
+ int ret = __pthread_cond_timedwait(c, m, ts);
+ switch (ret) {
+ /* May also return EINVAL or EPERM. */
+ default:
+ return thrd_error;
+ case 0:
+ return thrd_success;
+ case ETIMEDOUT:
+ return thrd_timedout;
+ }
}
diff --git a/fusl/src/thread/cnd_wait.c b/fusl/src/thread/cnd_wait.c
index 602796f..694ad51 100644
--- a/fusl/src/thread/cnd_wait.c
+++ b/fusl/src/thread/cnd_wait.c
@@ -1,9 +1,8 @@
#include <threads.h>
-int cnd_wait(cnd_t *c, mtx_t *m)
-{
- /* Calling cnd_timedwait with a null pointer is an extension.
- * It is convenient here to avoid duplication of the logic
- * for return values. */
- return cnd_timedwait(c, m, 0);
+int cnd_wait(cnd_t* c, mtx_t* m) {
+ /* Calling cnd_timedwait with a null pointer is an extension.
+ * It is convenient here to avoid duplication of the logic
+ * for return values. */
+ return cnd_timedwait(c, m, 0);
}
diff --git a/fusl/src/thread/lock_ptc.c b/fusl/src/thread/lock_ptc.c
index 7adedab..3f905f1 100644
--- a/fusl/src/thread/lock_ptc.c
+++ b/fusl/src/thread/lock_ptc.c
@@ -2,17 +2,14 @@
static pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
-void __inhibit_ptc()
-{
- pthread_rwlock_wrlock(&lock);
+void __inhibit_ptc() {
+ pthread_rwlock_wrlock(&lock);
}
-void __acquire_ptc()
-{
- pthread_rwlock_rdlock(&lock);
+void __acquire_ptc() {
+ pthread_rwlock_rdlock(&lock);
}
-void __release_ptc()
-{
- pthread_rwlock_unlock(&lock);
+void __release_ptc() {
+ pthread_rwlock_unlock(&lock);
}
diff --git a/fusl/src/thread/mtx_destroy.c b/fusl/src/thread/mtx_destroy.c
index 40a0899..acd77ec 100644
--- a/fusl/src/thread/mtx_destroy.c
+++ b/fusl/src/thread/mtx_destroy.c
@@ -1,5 +1,3 @@
#include <threads.h>
-void mtx_destroy(mtx_t *mtx)
-{
-}
+void mtx_destroy(mtx_t* mtx) {}
diff --git a/fusl/src/thread/mtx_init.c b/fusl/src/thread/mtx_init.c
index 4826f76..808b056 100644
--- a/fusl/src/thread/mtx_init.c
+++ b/fusl/src/thread/mtx_init.c
@@ -1,10 +1,10 @@
#include "pthread_impl.h"
#include <threads.h>
-int mtx_init(mtx_t *m, int type)
-{
- *m = (mtx_t){
- ._m_type = ((type&mtx_recursive) ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL),
- };
- return thrd_success;
+int mtx_init(mtx_t* m, int type) {
+ *m = (mtx_t){
+ ._m_type = ((type & mtx_recursive) ? PTHREAD_MUTEX_RECURSIVE
+ : PTHREAD_MUTEX_NORMAL),
+ };
+ return thrd_success;
}
diff --git a/fusl/src/thread/mtx_lock.c b/fusl/src/thread/mtx_lock.c
index 5c2415c..a79464a 100644
--- a/fusl/src/thread/mtx_lock.c
+++ b/fusl/src/thread/mtx_lock.c
@@ -1,12 +1,11 @@
#include "pthread_impl.h"
#include <threads.h>
-int mtx_lock(mtx_t *m)
-{
- if (m->_m_type == PTHREAD_MUTEX_NORMAL && !a_cas(&m->_m_lock, 0, EBUSY))
- return thrd_success;
- /* Calling mtx_timedlock with a null pointer is an extension.
- * It is convenient, here to avoid duplication of the logic
- * for return values. */
- return mtx_timedlock(m, 0);
+int mtx_lock(mtx_t* m) {
+ if (m->_m_type == PTHREAD_MUTEX_NORMAL && !a_cas(&m->_m_lock, 0, EBUSY))
+ return thrd_success;
+ /* Calling mtx_timedlock with a null pointer is an extension.
+ * It is convenient, here to avoid duplication of the logic
+ * for return values. */
+ return mtx_timedlock(m, 0);
}
diff --git a/fusl/src/thread/mtx_timedlock.c b/fusl/src/thread/mtx_timedlock.c
index bcc152c..c8e448e 100644
--- a/fusl/src/thread/mtx_timedlock.c
+++ b/fusl/src/thread/mtx_timedlock.c
@@ -1,14 +1,16 @@
#include <threads.h>
#include <errno.h>
-int __pthread_mutex_timedlock(mtx_t *restrict, const struct timespec *restrict);
+int __pthread_mutex_timedlock(mtx_t* restrict, const struct timespec* restrict);
-int mtx_timedlock(mtx_t *restrict m, const struct timespec *restrict ts)
-{
- int ret = __pthread_mutex_timedlock(m, ts);
- switch (ret) {
- default: return thrd_error;
- case 0: return thrd_success;
- case ETIMEDOUT: return thrd_timedout;
- }
+int mtx_timedlock(mtx_t* restrict m, const struct timespec* restrict ts) {
+ int ret = __pthread_mutex_timedlock(m, ts);
+ switch (ret) {
+ default:
+ return thrd_error;
+ case 0:
+ return thrd_success;
+ case ETIMEDOUT:
+ return thrd_timedout;
+ }
}
diff --git a/fusl/src/thread/mtx_trylock.c b/fusl/src/thread/mtx_trylock.c
index 61e7694..b7c0ca2 100644
--- a/fusl/src/thread/mtx_trylock.c
+++ b/fusl/src/thread/mtx_trylock.c
@@ -1,17 +1,19 @@
#include "pthread_impl.h"
#include <threads.h>
-int __pthread_mutex_trylock(mtx_t *);
+int __pthread_mutex_trylock(mtx_t*);
-int mtx_trylock(mtx_t *m)
-{
- if (m->_m_type == PTHREAD_MUTEX_NORMAL)
- return (a_cas(&m->_m_lock, 0, EBUSY) & EBUSY) ? thrd_busy : thrd_success;
+int mtx_trylock(mtx_t* m) {
+ if (m->_m_type == PTHREAD_MUTEX_NORMAL)
+ return (a_cas(&m->_m_lock, 0, EBUSY) & EBUSY) ? thrd_busy : thrd_success;
- int ret = __pthread_mutex_trylock(m);
- switch (ret) {
- default: return thrd_error;
- case 0: return thrd_success;
- case EBUSY: return thrd_busy;
- }
+ int ret = __pthread_mutex_trylock(m);
+ switch (ret) {
+ default:
+ return thrd_error;
+ case 0:
+ return thrd_success;
+ case EBUSY:
+ return thrd_busy;
+ }
}
diff --git a/fusl/src/thread/mtx_unlock.c b/fusl/src/thread/mtx_unlock.c
index 5033ace..edc7241 100644
--- a/fusl/src/thread/mtx_unlock.c
+++ b/fusl/src/thread/mtx_unlock.c
@@ -1,11 +1,10 @@
#include <threads.h>
-int __pthread_mutex_unlock(mtx_t *);
+int __pthread_mutex_unlock(mtx_t*);
-int mtx_unlock(mtx_t *mtx)
-{
- /* The only cases where pthread_mutex_unlock can return an
- * error are undefined behavior for C11 mtx_unlock, so we can
- * assume it does not return an error and simply tail call. */
- return __pthread_mutex_unlock(mtx);
+int mtx_unlock(mtx_t* mtx) {
+ /* The only cases where pthread_mutex_unlock can return an
+ * error are undefined behavior for C11 mtx_unlock, so we can
+ * assume it does not return an error and simply tail call. */
+ return __pthread_mutex_unlock(mtx);
}
diff --git a/fusl/src/thread/pthread_atfork.c b/fusl/src/thread/pthread_atfork.c
index a40d7f6..1479a95 100644
--- a/fusl/src/thread/pthread_atfork.c
+++ b/fusl/src/thread/pthread_atfork.c
@@ -2,47 +2,53 @@
#include "libc.h"
static struct atfork_funcs {
- void (*prepare)(void);
- void (*parent)(void);
- void (*child)(void);
- struct atfork_funcs *prev, *next;
-} *funcs;
+ void (*prepare)(void);
+ void (*parent)(void);
+ void (*child)(void);
+ struct atfork_funcs *prev, *next;
+} * funcs;
static volatile int lock[2];
-void __fork_handler(int who)
-{
- struct atfork_funcs *p;
- if (!funcs) return;
- if (who < 0) {
- LOCK(lock);
- for (p=funcs; p; p = p->next) {
- if (p->prepare) p->prepare();
- funcs = p;
- }
- } else {
- for (p=funcs; p; p = p->prev) {
- if (!who && p->parent) p->parent();
- else if (who && p->child) p->child();
- funcs = p;
- }
- UNLOCK(lock);
- }
+void __fork_handler(int who) {
+ struct atfork_funcs* p;
+ if (!funcs)
+ return;
+ if (who < 0) {
+ LOCK(lock);
+ for (p = funcs; p; p = p->next) {
+ if (p->prepare)
+ p->prepare();
+ funcs = p;
+ }
+ } else {
+ for (p = funcs; p; p = p->prev) {
+ if (!who && p->parent)
+ p->parent();
+ else if (who && p->child)
+ p->child();
+ funcs = p;
+ }
+ UNLOCK(lock);
+ }
}
-int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
-{
- struct atfork_funcs *new = malloc(sizeof *new);
- if (!new) return -1;
+int pthread_atfork(void (*prepare)(void),
+ void (*parent)(void),
+ void (*child)(void)) {
+ struct atfork_funcs* new = malloc(sizeof *new);
+ if (!new)
+ return -1;
- LOCK(lock);
- new->next = funcs;
- new->prev = 0;
- new->prepare = prepare;
- new->parent = parent;
- new->child = child;
- if (funcs) funcs->prev = new;
- funcs = new;
- UNLOCK(lock);
- return 0;
+ LOCK(lock);
+ new->next = funcs;
+ new->prev = 0;
+ new->prepare = prepare;
+ new->parent = parent;
+ new->child = child;
+ if (funcs)
+ funcs->prev = new;
+ funcs = new;
+ UNLOCK(lock);
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_destroy.c b/fusl/src/thread/pthread_attr_destroy.c
index b5845dd..8c764f6 100644
--- a/fusl/src/thread/pthread_attr_destroy.c
+++ b/fusl/src/thread/pthread_attr_destroy.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_attr_destroy(pthread_attr_t *a)
-{
- return 0;
+int pthread_attr_destroy(pthread_attr_t* a) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_get.c b/fusl/src/thread/pthread_attr_get.c
index 3d296bf..295e107 100644
--- a/fusl/src/thread/pthread_attr_get.c
+++ b/fusl/src/thread/pthread_attr_get.c
@@ -1,98 +1,98 @@
#include "pthread_impl.h"
-int pthread_attr_getdetachstate(const pthread_attr_t *a, int *state)
-{
- *state = a->_a_detach;
- return 0;
+int pthread_attr_getdetachstate(const pthread_attr_t* a, int* state) {
+ *state = a->_a_detach;
+ return 0;
}
-int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size)
-{
- *size = a->_a_guardsize + DEFAULT_GUARD_SIZE;
- return 0;
+int pthread_attr_getguardsize(const pthread_attr_t* restrict a,
+ size_t* restrict size) {
+ *size = a->_a_guardsize + DEFAULT_GUARD_SIZE;
+ return 0;
}
-int pthread_attr_getinheritsched(const pthread_attr_t *restrict a, int *restrict inherit)
-{
- *inherit = a->_a_sched;
- return 0;
+int pthread_attr_getinheritsched(const pthread_attr_t* restrict a,
+ int* restrict inherit) {
+ *inherit = a->_a_sched;
+ return 0;
}
-int pthread_attr_getschedparam(const pthread_attr_t *restrict a, struct sched_param *restrict param)
-{
- param->sched_priority = a->_a_prio;
- return 0;
+int pthread_attr_getschedparam(const pthread_attr_t* restrict a,
+ struct sched_param* restrict param) {
+ param->sched_priority = a->_a_prio;
+ return 0;
}
-int pthread_attr_getschedpolicy(const pthread_attr_t *restrict a, int *restrict policy)
-{
- *policy = a->_a_policy;
- return 0;
+int pthread_attr_getschedpolicy(const pthread_attr_t* restrict a,
+ int* restrict policy) {
+ *policy = a->_a_policy;
+ return 0;
}
-int pthread_attr_getscope(const pthread_attr_t *restrict a, int *restrict scope)
-{
- *scope = PTHREAD_SCOPE_SYSTEM;
- return 0;
+int pthread_attr_getscope(const pthread_attr_t* restrict a,
+ int* restrict scope) {
+ *scope = PTHREAD_SCOPE_SYSTEM;
+ return 0;
}
-int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size)
-{
- if (!a->_a_stackaddr)
- return EINVAL;
- *size = a->_a_stacksize + DEFAULT_STACK_SIZE;
- *addr = (void *)(a->_a_stackaddr - *size);
- return 0;
+int pthread_attr_getstack(const pthread_attr_t* restrict a,
+ void** restrict addr,
+ size_t* restrict size) {
+ if (!a->_a_stackaddr)
+ return EINVAL;
+ *size = a->_a_stacksize + DEFAULT_STACK_SIZE;
+ *addr = (void*)(a->_a_stackaddr - *size);
+ return 0;
}
-int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size)
-{
- *size = a->_a_stacksize + DEFAULT_STACK_SIZE;
- return 0;
+int pthread_attr_getstacksize(const pthread_attr_t* restrict a,
+ size_t* restrict size) {
+ *size = a->_a_stacksize + DEFAULT_STACK_SIZE;
+ return 0;
}
-int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict a, int *restrict pshared)
-{
- *pshared = !!a->__attr;
- return 0;
+int pthread_barrierattr_getpshared(const pthread_barrierattr_t* restrict a,
+ int* restrict pshared) {
+ *pshared = !!a->__attr;
+ return 0;
}
-int pthread_condattr_getclock(const pthread_condattr_t *restrict a, clockid_t *restrict clk)
-{
- *clk = a->__attr & 0x7fffffff;
- return 0;
+int pthread_condattr_getclock(const pthread_condattr_t* restrict a,
+ clockid_t* restrict clk) {
+ *clk = a->__attr & 0x7fffffff;
+ return 0;
}
-int pthread_condattr_getpshared(const pthread_condattr_t *restrict a, int *restrict pshared)
-{
- *pshared = a->__attr>>31;
- return 0;
+int pthread_condattr_getpshared(const pthread_condattr_t* restrict a,
+ int* restrict pshared) {
+ *pshared = a->__attr >> 31;
+ return 0;
}
-int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict a, int *restrict protocol)
-{
- *protocol = PTHREAD_PRIO_NONE;
- return 0;
+int pthread_mutexattr_getprotocol(const pthread_mutexattr_t* restrict a,
+ int* restrict protocol) {
+ *protocol = PTHREAD_PRIO_NONE;
+ return 0;
}
-int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict a, int *restrict pshared)
-{
- *pshared = a->__attr / 128U % 2;
- return 0;
+int pthread_mutexattr_getpshared(const pthread_mutexattr_t* restrict a,
+ int* restrict pshared) {
+ *pshared = a->__attr / 128U % 2;
+ return 0;
}
-int pthread_mutexattr_getrobust(const pthread_mutexattr_t *restrict a, int *restrict robust)
-{
- *robust = a->__attr / 4U % 2;
- return 0;
+int pthread_mutexattr_getrobust(const pthread_mutexattr_t* restrict a,
+ int* restrict robust) {
+ *robust = a->__attr / 4U % 2;
+ return 0;
}
-int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict a, int *restrict type)
-{
- *type = a->__attr & 3;
- return 0;
+int pthread_mutexattr_gettype(const pthread_mutexattr_t* restrict a,
+ int* restrict type) {
+ *type = a->__attr & 3;
+ return 0;
}
-int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict a, int *restrict pshared)
-{
- *pshared = a->__attr[0];
- return 0;
+int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* restrict a,
+ int* restrict pshared) {
+ *pshared = a->__attr[0];
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_init.c b/fusl/src/thread/pthread_attr_init.c
index 969e0a3..b956ee5 100644
--- a/fusl/src/thread/pthread_attr_init.c
+++ b/fusl/src/thread/pthread_attr_init.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_attr_init(pthread_attr_t *a)
-{
- *a = (pthread_attr_t){0};
- return 0;
+int pthread_attr_init(pthread_attr_t* a) {
+ *a = (pthread_attr_t){0};
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_setdetachstate.c b/fusl/src/thread/pthread_attr_setdetachstate.c
index 1b71278..36f6705 100644
--- a/fusl/src/thread/pthread_attr_setdetachstate.c
+++ b/fusl/src/thread/pthread_attr_setdetachstate.c
@@ -1,8 +1,8 @@
#include "pthread_impl.h"
-int pthread_attr_setdetachstate(pthread_attr_t *a, int state)
-{
- if (state > 1U) return EINVAL;
- a->_a_detach = state;
- return 0;
+int pthread_attr_setdetachstate(pthread_attr_t* a, int state) {
+ if (state > 1U)
+ return EINVAL;
+ a->_a_detach = state;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_setguardsize.c b/fusl/src/thread/pthread_attr_setguardsize.c
index 9f21d24..6db7957 100644
--- a/fusl/src/thread/pthread_attr_setguardsize.c
+++ b/fusl/src/thread/pthread_attr_setguardsize.c
@@ -1,8 +1,8 @@
#include "pthread_impl.h"
-int pthread_attr_setguardsize(pthread_attr_t *a, size_t size)
-{
- if (size > SIZE_MAX/8) return EINVAL;
- a->_a_guardsize = size - DEFAULT_GUARD_SIZE;
- return 0;
+int pthread_attr_setguardsize(pthread_attr_t* a, size_t size) {
+ if (size > SIZE_MAX / 8)
+ return EINVAL;
+ a->_a_guardsize = size - DEFAULT_GUARD_SIZE;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_setinheritsched.c b/fusl/src/thread/pthread_attr_setinheritsched.c
index c91d8f8..c067de0 100644
--- a/fusl/src/thread/pthread_attr_setinheritsched.c
+++ b/fusl/src/thread/pthread_attr_setinheritsched.c
@@ -1,8 +1,8 @@
#include "pthread_impl.h"
-int pthread_attr_setinheritsched(pthread_attr_t *a, int inherit)
-{
- if (inherit > 1U) return EINVAL;
- a->_a_sched = inherit;
- return 0;
+int pthread_attr_setinheritsched(pthread_attr_t* a, int inherit) {
+ if (inherit > 1U)
+ return EINVAL;
+ a->_a_sched = inherit;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_setschedparam.c b/fusl/src/thread/pthread_attr_setschedparam.c
index d4c1204..6a8bd34 100644
--- a/fusl/src/thread/pthread_attr_setschedparam.c
+++ b/fusl/src/thread/pthread_attr_setschedparam.c
@@ -1,7 +1,7 @@
#include "pthread_impl.h"
-int pthread_attr_setschedparam(pthread_attr_t *restrict a, const struct sched_param *restrict param)
-{
- a->_a_prio = param->sched_priority;
- return 0;
+int pthread_attr_setschedparam(pthread_attr_t* restrict a,
+ const struct sched_param* restrict param) {
+ a->_a_prio = param->sched_priority;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_setschedpolicy.c b/fusl/src/thread/pthread_attr_setschedpolicy.c
index bb71f39..2687433 100644
--- a/fusl/src/thread/pthread_attr_setschedpolicy.c
+++ b/fusl/src/thread/pthread_attr_setschedpolicy.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_attr_setschedpolicy(pthread_attr_t *a, int policy)
-{
- a->_a_policy = policy;
- return 0;
+int pthread_attr_setschedpolicy(pthread_attr_t* a, int policy) {
+ a->_a_policy = policy;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_setscope.c b/fusl/src/thread/pthread_attr_setscope.c
index 46b520c..3ea46d88 100644
--- a/fusl/src/thread/pthread_attr_setscope.c
+++ b/fusl/src/thread/pthread_attr_setscope.c
@@ -1,13 +1,12 @@
#include "pthread_impl.h"
-int pthread_attr_setscope(pthread_attr_t *a, int scope)
-{
- switch (scope) {
- case PTHREAD_SCOPE_SYSTEM:
- return 0;
- case PTHREAD_SCOPE_PROCESS:
- return ENOTSUP;
- default:
- return EINVAL;
- }
+int pthread_attr_setscope(pthread_attr_t* a, int scope) {
+ switch (scope) {
+ case PTHREAD_SCOPE_SYSTEM:
+ return 0;
+ case PTHREAD_SCOPE_PROCESS:
+ return ENOTSUP;
+ default:
+ return EINVAL;
+ }
}
diff --git a/fusl/src/thread/pthread_attr_setstack.c b/fusl/src/thread/pthread_attr_setstack.c
index 61707a3..85c4549 100644
--- a/fusl/src/thread/pthread_attr_setstack.c
+++ b/fusl/src/thread/pthread_attr_setstack.c
@@ -1,9 +1,9 @@
#include "pthread_impl.h"
-int pthread_attr_setstack(pthread_attr_t *a, void *addr, size_t size)
-{
- if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL;
- a->_a_stackaddr = (size_t)addr + size;
- a->_a_stacksize = size - DEFAULT_STACK_SIZE;
- return 0;
+int pthread_attr_setstack(pthread_attr_t* a, void* addr, size_t size) {
+ if (size - PTHREAD_STACK_MIN > SIZE_MAX / 4)
+ return EINVAL;
+ a->_a_stackaddr = (size_t)addr + size;
+ a->_a_stacksize = size - DEFAULT_STACK_SIZE;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_attr_setstacksize.c b/fusl/src/thread/pthread_attr_setstacksize.c
index 09d3fda..b75fada 100644
--- a/fusl/src/thread/pthread_attr_setstacksize.c
+++ b/fusl/src/thread/pthread_attr_setstacksize.c
@@ -1,9 +1,9 @@
#include "pthread_impl.h"
-int pthread_attr_setstacksize(pthread_attr_t *a, size_t size)
-{
- if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL;
- a->_a_stackaddr = 0;
- a->_a_stacksize = size - DEFAULT_STACK_SIZE;
- return 0;
+int pthread_attr_setstacksize(pthread_attr_t* a, size_t size) {
+ if (size - PTHREAD_STACK_MIN > SIZE_MAX / 4)
+ return EINVAL;
+ a->_a_stackaddr = 0;
+ a->_a_stacksize = size - DEFAULT_STACK_SIZE;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_barrier_destroy.c b/fusl/src/thread/pthread_barrier_destroy.c
index 4ce0b2e..cbc585e 100644
--- a/fusl/src/thread/pthread_barrier_destroy.c
+++ b/fusl/src/thread/pthread_barrier_destroy.c
@@ -1,15 +1,14 @@
#include "pthread_impl.h"
-int pthread_barrier_destroy(pthread_barrier_t *b)
-{
- if (b->_b_limit < 0) {
- if (b->_b_lock) {
- int v;
- a_or(&b->_b_lock, INT_MIN);
- while ((v = b->_b_lock) & INT_MAX)
- __wait(&b->_b_lock, 0, v, 0);
- }
- __vm_wait();
- }
- return 0;
+int pthread_barrier_destroy(pthread_barrier_t* b) {
+ if (b->_b_limit < 0) {
+ if (b->_b_lock) {
+ int v;
+ a_or(&b->_b_lock, INT_MIN);
+ while ((v = b->_b_lock) & INT_MAX)
+ __wait(&b->_b_lock, 0, v, 0);
+ }
+ __vm_wait();
+ }
+ return 0;
}
diff --git a/fusl/src/thread/pthread_barrier_init.c b/fusl/src/thread/pthread_barrier_init.c
index 4c3cb28..5cc416a 100644
--- a/fusl/src/thread/pthread_barrier_init.c
+++ b/fusl/src/thread/pthread_barrier_init.c
@@ -1,8 +1,10 @@
#include "pthread_impl.h"
-int pthread_barrier_init(pthread_barrier_t *restrict b, const pthread_barrierattr_t *restrict a, unsigned count)
-{
- if (count-1 > INT_MAX-1) return EINVAL;
- *b = (pthread_barrier_t){ ._b_limit = count-1 | (a?a->__attr:0) };
- return 0;
+int pthread_barrier_init(pthread_barrier_t* restrict b,
+ const pthread_barrierattr_t* restrict a,
+ unsigned count) {
+ if (count - 1 > INT_MAX - 1)
+ return EINVAL;
+ *b = (pthread_barrier_t){._b_limit = count - 1 | (a ? a->__attr : 0)};
+ return 0;
}
diff --git a/fusl/src/thread/pthread_barrier_wait.c b/fusl/src/thread/pthread_barrier_wait.c
index 06b83db..5d5f68b 100644
--- a/fusl/src/thread/pthread_barrier_wait.c
+++ b/fusl/src/thread/pthread_barrier_wait.c
@@ -1,111 +1,118 @@
#include "pthread_impl.h"
-static int pshared_barrier_wait(pthread_barrier_t *b)
-{
- int limit = (b->_b_limit & INT_MAX) + 1;
- int ret = 0;
- int v, w;
+static int pshared_barrier_wait(pthread_barrier_t* b) {
+ int limit = (b->_b_limit & INT_MAX) + 1;
+ int ret = 0;
+ int v, w;
- if (limit==1) return PTHREAD_BARRIER_SERIAL_THREAD;
+ if (limit == 1)
+ return PTHREAD_BARRIER_SERIAL_THREAD;
- while ((v=a_cas(&b->_b_lock, 0, limit)))
- __wait(&b->_b_lock, &b->_b_waiters, v, 0);
+ while ((v = a_cas(&b->_b_lock, 0, limit)))
+ __wait(&b->_b_lock, &b->_b_waiters, v, 0);
- /* Wait for <limit> threads to get to the barrier */
- if (++b->_b_count == limit) {
- a_store(&b->_b_count, 0);
- ret = PTHREAD_BARRIER_SERIAL_THREAD;
- if (b->_b_waiters2) __wake(&b->_b_count, -1, 0);
- } else {
- a_store(&b->_b_lock, 0);
- if (b->_b_waiters) __wake(&b->_b_lock, 1, 0);
- while ((v=b->_b_count)>0)
- __wait(&b->_b_count, &b->_b_waiters2, v, 0);
- }
+ /* Wait for <limit> threads to get to the barrier */
+ if (++b->_b_count == limit) {
+ a_store(&b->_b_count, 0);
+ ret = PTHREAD_BARRIER_SERIAL_THREAD;
+ if (b->_b_waiters2)
+ __wake(&b->_b_count, -1, 0);
+ } else {
+ a_store(&b->_b_lock, 0);
+ if (b->_b_waiters)
+ __wake(&b->_b_lock, 1, 0);
+ while ((v = b->_b_count) > 0)
+ __wait(&b->_b_count, &b->_b_waiters2, v, 0);
+ }
- __vm_lock();
+ __vm_lock();
- /* Ensure all threads have a vm lock before proceeding */
- if (a_fetch_add(&b->_b_count, -1)==1-limit) {
- a_store(&b->_b_count, 0);
- if (b->_b_waiters2) __wake(&b->_b_count, -1, 0);
- } else {
- while ((v=b->_b_count))
- __wait(&b->_b_count, &b->_b_waiters2, v, 0);
- }
-
- /* Perform a recursive unlock suitable for self-sync'd destruction */
- do {
- v = b->_b_lock;
- w = b->_b_waiters;
- } while (a_cas(&b->_b_lock, v, v==INT_MIN+1 ? 0 : v-1) != v);
+ /* Ensure all threads have a vm lock before proceeding */
+ if (a_fetch_add(&b->_b_count, -1) == 1 - limit) {
+ a_store(&b->_b_count, 0);
+ if (b->_b_waiters2)
+ __wake(&b->_b_count, -1, 0);
+ } else {
+ while ((v = b->_b_count))
+ __wait(&b->_b_count, &b->_b_waiters2, v, 0);
+ }
- /* Wake a thread waiting to reuse or destroy the barrier */
- if (v==INT_MIN+1 || (v==1 && w))
- __wake(&b->_b_lock, 1, 0);
+ /* Perform a recursive unlock suitable for self-sync'd destruction */
+ do {
+ v = b->_b_lock;
+ w = b->_b_waiters;
+ } while (a_cas(&b->_b_lock, v, v == INT_MIN + 1 ? 0 : v - 1) != v);
- __vm_unlock();
+ /* Wake a thread waiting to reuse or destroy the barrier */
+ if (v == INT_MIN + 1 || (v == 1 && w))
+ __wake(&b->_b_lock, 1, 0);
- return ret;
+ __vm_unlock();
+
+ return ret;
}
-struct instance
-{
- volatile int count;
- volatile int last;
- volatile int waiters;
- volatile int finished;
+struct instance {
+ volatile int count;
+ volatile int last;
+ volatile int waiters;
+ volatile int finished;
};
-int pthread_barrier_wait(pthread_barrier_t *b)
-{
- int limit = b->_b_limit;
- struct instance *inst;
+int pthread_barrier_wait(pthread_barrier_t* b) {
+ int limit = b->_b_limit;
+ struct instance* inst;
- /* Trivial case: count was set at 1 */
- if (!limit) return PTHREAD_BARRIER_SERIAL_THREAD;
+ /* Trivial case: count was set at 1 */
+ if (!limit)
+ return PTHREAD_BARRIER_SERIAL_THREAD;
- /* Process-shared barriers require a separate, inefficient wait */
- if (limit < 0) return pshared_barrier_wait(b);
+ /* Process-shared barriers require a separate, inefficient wait */
+ if (limit < 0)
+ return pshared_barrier_wait(b);
- /* Otherwise we need a lock on the barrier object */
- while (a_swap(&b->_b_lock, 1))
- __wait(&b->_b_lock, &b->_b_waiters, 1, 1);
- inst = b->_b_inst;
+ /* Otherwise we need a lock on the barrier object */
+ while (a_swap(&b->_b_lock, 1))
+ __wait(&b->_b_lock, &b->_b_waiters, 1, 1);
+ inst = b->_b_inst;
- /* First thread to enter the barrier becomes the "instance owner" */
- if (!inst) {
- struct instance new_inst = { 0 };
- int spins = 200;
- b->_b_inst = inst = &new_inst;
- a_store(&b->_b_lock, 0);
- if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
- while (spins-- && !inst->finished)
- a_spin();
- a_inc(&inst->finished);
- while (inst->finished == 1)
- __syscall(SYS_futex,&inst->finished,FUTEX_WAIT|128,1,0) != -ENOSYS
- || __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0);
- return PTHREAD_BARRIER_SERIAL_THREAD;
- }
+ /* First thread to enter the barrier becomes the "instance owner" */
+ if (!inst) {
+ struct instance new_inst = {0};
+ int spins = 200;
+ b->_b_inst = inst = &new_inst;
+ a_store(&b->_b_lock, 0);
+ if (b->_b_waiters)
+ __wake(&b->_b_lock, 1, 1);
+ while (spins-- && !inst->finished)
+ a_spin();
+ a_inc(&inst->finished);
+ while (inst->finished == 1)
+ __syscall(SYS_futex, &inst->finished, FUTEX_WAIT | 128, 1, 0) !=
+ -ENOSYS ||
+ __syscall(SYS_futex, &inst->finished, FUTEX_WAIT, 1, 0);
+ return PTHREAD_BARRIER_SERIAL_THREAD;
+ }
- /* Last thread to enter the barrier wakes all non-instance-owners */
- if (++inst->count == limit) {
- b->_b_inst = 0;
- a_store(&b->_b_lock, 0);
- if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
- a_store(&inst->last, 1);
- if (inst->waiters)
- __wake(&inst->last, -1, 1);
- } else {
- a_store(&b->_b_lock, 0);
- if (b->_b_waiters) __wake(&b->_b_lock, 1, 1);
- __wait(&inst->last, &inst->waiters, 0, 1);
- }
+ /* Last thread to enter the barrier wakes all non-instance-owners */
+ if (++inst->count == limit) {
+ b->_b_inst = 0;
+ a_store(&b->_b_lock, 0);
+ if (b->_b_waiters)
+ __wake(&b->_b_lock, 1, 1);
+ a_store(&inst->last, 1);
+ if (inst->waiters)
+ __wake(&inst->last, -1, 1);
+ } else {
+ a_store(&b->_b_lock, 0);
+ if (b->_b_waiters)
+ __wake(&b->_b_lock, 1, 1);
+ __wait(&inst->last, &inst->waiters, 0, 1);
+ }
- /* Last thread to exit the barrier wakes the instance owner */
- if (a_fetch_add(&inst->count,-1)==1 && a_fetch_add(&inst->finished,1))
- __wake(&inst->finished, 1, 1);
+ /* Last thread to exit the barrier wakes the instance owner */
+ if (a_fetch_add(&inst->count, -1) == 1 && a_fetch_add(&inst->finished, 1))
+ __wake(&inst->finished, 1, 1);
- return 0;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_barrierattr_destroy.c b/fusl/src/thread/pthread_barrierattr_destroy.c
index adec738..04fc0fd 100644
--- a/fusl/src/thread/pthread_barrierattr_destroy.c
+++ b/fusl/src/thread/pthread_barrierattr_destroy.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_barrierattr_destroy(pthread_barrierattr_t *a)
-{
- return 0;
+int pthread_barrierattr_destroy(pthread_barrierattr_t* a) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_barrierattr_init.c b/fusl/src/thread/pthread_barrierattr_init.c
index fa742bb..b02b806 100644
--- a/fusl/src/thread/pthread_barrierattr_init.c
+++ b/fusl/src/thread/pthread_barrierattr_init.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_barrierattr_init(pthread_barrierattr_t *a)
-{
- *a = (pthread_barrierattr_t){0};
- return 0;
+int pthread_barrierattr_init(pthread_barrierattr_t* a) {
+ *a = (pthread_barrierattr_t){0};
+ return 0;
}
diff --git a/fusl/src/thread/pthread_barrierattr_setpshared.c b/fusl/src/thread/pthread_barrierattr_setpshared.c
index b391461..dd0589f 100644
--- a/fusl/src/thread/pthread_barrierattr_setpshared.c
+++ b/fusl/src/thread/pthread_barrierattr_setpshared.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_barrierattr_setpshared(pthread_barrierattr_t *a, int pshared)
-{
- a->__attr = pshared ? INT_MIN : 0;
- return 0;
+int pthread_barrierattr_setpshared(pthread_barrierattr_t* a, int pshared) {
+ a->__attr = pshared ? INT_MIN : 0;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_cancel.c b/fusl/src/thread/pthread_cancel.c
index 63e24c1..43b5a2c 100644
--- a/fusl/src/thread/pthread_cancel.c
+++ b/fusl/src/thread/pthread_cancel.c
@@ -4,94 +4,98 @@
#include "syscall.h"
#include "libc.h"
-__attribute__((__visibility__("hidden")))
-long __cancel(), __syscall_cp_asm(), __syscall_cp_c();
+__attribute__((__visibility__("hidden"))) long __cancel(), __syscall_cp_asm(),
+ __syscall_cp_c();
-long __cancel()
-{
- pthread_t self = __pthread_self();
- if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
- pthread_exit(PTHREAD_CANCELED);
- self->canceldisable = PTHREAD_CANCEL_DISABLE;
- return -ECANCELED;
+long __cancel() {
+ pthread_t self = __pthread_self();
+ if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
+ pthread_exit(PTHREAD_CANCELED);
+ self->canceldisable = PTHREAD_CANCEL_DISABLE;
+ return -ECANCELED;
}
-long __syscall_cp_asm(volatile void *, syscall_arg_t,
- syscall_arg_t, syscall_arg_t, syscall_arg_t,
- syscall_arg_t, syscall_arg_t, syscall_arg_t);
+long __syscall_cp_asm(volatile void*,
+ syscall_arg_t,
+ syscall_arg_t,
+ syscall_arg_t,
+ syscall_arg_t,
+ syscall_arg_t,
+ syscall_arg_t,
+ syscall_arg_t);
long __syscall_cp_c(syscall_arg_t nr,
- syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
- syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
-{
- pthread_t self;
- long r;
- int st;
+ syscall_arg_t u,
+ syscall_arg_t v,
+ syscall_arg_t w,
+ syscall_arg_t x,
+ syscall_arg_t y,
+ syscall_arg_t z) {
+ pthread_t self;
+ long r;
+ int st;
- if ((st=(self=__pthread_self())->canceldisable)
- && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
- return __syscall(nr, u, v, w, x, y, z);
+ if ((st = (self = __pthread_self())->canceldisable) &&
+ (st == PTHREAD_CANCEL_DISABLE || nr == SYS_close))
+ return __syscall(nr, u, v, w, x, y, z);
- r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
- if (r==-EINTR && nr!=SYS_close && self->cancel &&
- self->canceldisable != PTHREAD_CANCEL_DISABLE)
- r = __cancel();
- return r;
+ r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
+ if (r == -EINTR && nr != SYS_close && self->cancel &&
+ self->canceldisable != PTHREAD_CANCEL_DISABLE)
+ r = __cancel();
+ return r;
}
-static void _sigaddset(sigset_t *set, int sig)
-{
- unsigned s = sig-1;
- set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
+static void _sigaddset(sigset_t* set, int sig) {
+ unsigned s = sig - 1;
+ set->__bits[s / 8 / sizeof *set->__bits] |=
+ 1UL << (s & 8 * sizeof *set->__bits - 1);
}
-__attribute__((__visibility__("hidden")))
-extern const char __cp_begin[1], __cp_end[1], __cp_cancel[1];
+__attribute__((__visibility__("hidden"))) extern const char __cp_begin[1],
+ __cp_end[1], __cp_cancel[1];
-static void cancel_handler(int sig, siginfo_t *si, void *ctx)
-{
- pthread_t self = __pthread_self();
- ucontext_t *uc = ctx;
- uintptr_t pc = uc->uc_mcontext.MC_PC;
+static void cancel_handler(int sig, siginfo_t* si, void* ctx) {
+ pthread_t self = __pthread_self();
+ ucontext_t* uc = ctx;
+ uintptr_t pc = uc->uc_mcontext.MC_PC;
- a_barrier();
- if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
+ a_barrier();
+ if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE)
+ return;
- _sigaddset(&uc->uc_sigmask, SIGCANCEL);
+ _sigaddset(&uc->uc_sigmask, SIGCANCEL);
- if (self->cancelasync || (pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end)) {
- uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel;
- return;
- }
+ if (self->cancelasync ||
+ (pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end)) {
+ uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel;
+ return;
+ }
- __syscall(SYS_tkill, self->tid, SIGCANCEL);
+ __syscall(SYS_tkill, self->tid, SIGCANCEL);
}
-void __testcancel()
-{
- pthread_t self = __pthread_self();
- if (self->cancel && !self->canceldisable)
- __cancel();
+void __testcancel() {
+ pthread_t self = __pthread_self();
+ if (self->cancel && !self->canceldisable)
+ __cancel();
}
-static void init_cancellation()
-{
- struct sigaction sa = {
- .sa_flags = SA_SIGINFO | SA_RESTART,
- .sa_sigaction = cancel_handler
- };
- memset(&sa.sa_mask, -1, _NSIG/8);
- __libc_sigaction(SIGCANCEL, &sa, 0);
+static void init_cancellation() {
+ struct sigaction sa = {.sa_flags = SA_SIGINFO | SA_RESTART,
+ .sa_sigaction = cancel_handler};
+ memset(&sa.sa_mask, -1, _NSIG / 8);
+ __libc_sigaction(SIGCANCEL, &sa, 0);
}
-int pthread_cancel(pthread_t t)
-{
- static int init;
- if (!init) {
- init_cancellation();
- init = 1;
- }
- a_store(&t->cancel, 1);
- if (t == pthread_self() && !t->cancelasync) return 0;
- return pthread_kill(t, SIGCANCEL);
+int pthread_cancel(pthread_t t) {
+ static int init;
+ if (!init) {
+ init_cancellation();
+ init = 1;
+ }
+ a_store(&t->cancel, 1);
+ if (t == pthread_self() && !t->cancelasync)
+ return 0;
+ return pthread_kill(t, SIGCANCEL);
}
diff --git a/fusl/src/thread/pthread_cleanup_push.c b/fusl/src/thread/pthread_cleanup_push.c
index 9b21764..f42a8f8 100644
--- a/fusl/src/thread/pthread_cleanup_push.c
+++ b/fusl/src/thread/pthread_cleanup_push.c
@@ -1,20 +1,17 @@
#include "pthread_impl.h"
-static void dummy(struct __ptcb *cb)
-{
-}
+static void dummy(struct __ptcb* cb) {}
weak_alias(dummy, __do_cleanup_push);
weak_alias(dummy, __do_cleanup_pop);
-void _pthread_cleanup_push(struct __ptcb *cb, void (*f)(void *), void *x)
-{
- cb->__f = f;
- cb->__x = x;
- __do_cleanup_push(cb);
+void _pthread_cleanup_push(struct __ptcb* cb, void (*f)(void*), void* x) {
+ cb->__f = f;
+ cb->__x = x;
+ __do_cleanup_push(cb);
}
-void _pthread_cleanup_pop(struct __ptcb *cb, int run)
-{
- __do_cleanup_pop(cb);
- if (run) cb->__f(cb->__x);
+void _pthread_cleanup_pop(struct __ptcb* cb, int run) {
+ __do_cleanup_pop(cb);
+ if (run)
+ cb->__f(cb->__x);
}
diff --git a/fusl/src/thread/pthread_cond_broadcast.c b/fusl/src/thread/pthread_cond_broadcast.c
index 69f840f..acaa4ec 100644
--- a/fusl/src/thread/pthread_cond_broadcast.c
+++ b/fusl/src/thread/pthread_cond_broadcast.c
@@ -1,12 +1,13 @@
#include "pthread_impl.h"
-int __private_cond_signal(pthread_cond_t *, int);
+int __private_cond_signal(pthread_cond_t*, int);
-int pthread_cond_broadcast(pthread_cond_t *c)
-{
- if (!c->_c_shared) return __private_cond_signal(c, -1);
- if (!c->_c_waiters) return 0;
- a_inc(&c->_c_seq);
- __wake(&c->_c_seq, -1, 0);
- return 0;
+int pthread_cond_broadcast(pthread_cond_t* c) {
+ if (!c->_c_shared)
+ return __private_cond_signal(c, -1);
+ if (!c->_c_waiters)
+ return 0;
+ a_inc(&c->_c_seq);
+ __wake(&c->_c_seq, -1, 0);
+ return 0;
}
diff --git a/fusl/src/thread/pthread_cond_destroy.c b/fusl/src/thread/pthread_cond_destroy.c
index 8c55516..fad746e 100644
--- a/fusl/src/thread/pthread_cond_destroy.c
+++ b/fusl/src/thread/pthread_cond_destroy.c
@@ -1,14 +1,13 @@
#include "pthread_impl.h"
-int pthread_cond_destroy(pthread_cond_t *c)
-{
- if (c->_c_shared && c->_c_waiters) {
- int cnt;
- a_or(&c->_c_waiters, 0x80000000);
- a_inc(&c->_c_seq);
- __wake(&c->_c_seq, -1, 0);
- while ((cnt = c->_c_waiters) & 0x7fffffff)
- __wait(&c->_c_waiters, 0, cnt, 0);
- }
- return 0;
+int pthread_cond_destroy(pthread_cond_t* c) {
+ if (c->_c_shared && c->_c_waiters) {
+ int cnt;
+ a_or(&c->_c_waiters, 0x80000000);
+ a_inc(&c->_c_seq);
+ __wake(&c->_c_seq, -1, 0);
+ while ((cnt = c->_c_waiters) & 0x7fffffff)
+ __wait(&c->_c_waiters, 0, cnt, 0);
+ }
+ return 0;
}
diff --git a/fusl/src/thread/pthread_cond_init.c b/fusl/src/thread/pthread_cond_init.c
index 8c484dd..b97af17 100644
--- a/fusl/src/thread/pthread_cond_init.c
+++ b/fusl/src/thread/pthread_cond_init.c
@@ -1,11 +1,12 @@
#include "pthread_impl.h"
-int pthread_cond_init(pthread_cond_t *restrict c, const pthread_condattr_t *restrict a)
-{
- *c = (pthread_cond_t){0};
- if (a) {
- c->_c_clock = a->__attr & 0x7fffffff;
- if (a->__attr>>31) c->_c_shared = (void *)-1;
- }
- return 0;
+int pthread_cond_init(pthread_cond_t* restrict c,
+ const pthread_condattr_t* restrict a) {
+ *c = (pthread_cond_t){0};
+ if (a) {
+ c->_c_clock = a->__attr & 0x7fffffff;
+ if (a->__attr >> 31)
+ c->_c_shared = (void*)-1;
+ }
+ return 0;
}
diff --git a/fusl/src/thread/pthread_cond_signal.c b/fusl/src/thread/pthread_cond_signal.c
index 119c00a..1176cc5 100644
--- a/fusl/src/thread/pthread_cond_signal.c
+++ b/fusl/src/thread/pthread_cond_signal.c
@@ -1,12 +1,13 @@
#include "pthread_impl.h"
-int __private_cond_signal(pthread_cond_t *, int);
+int __private_cond_signal(pthread_cond_t*, int);
-int pthread_cond_signal(pthread_cond_t *c)
-{
- if (!c->_c_shared) return __private_cond_signal(c, 1);
- if (!c->_c_waiters) return 0;
- a_inc(&c->_c_seq);
- __wake(&c->_c_seq, 1, 0);
- return 0;
+int pthread_cond_signal(pthread_cond_t* c) {
+ if (!c->_c_shared)
+ return __private_cond_signal(c, 1);
+ if (!c->_c_waiters)
+ return 0;
+ a_inc(&c->_c_seq);
+ __wake(&c->_c_seq, 1, 0);
+ return 0;
}
diff --git a/fusl/src/thread/pthread_cond_timedwait.c b/fusl/src/thread/pthread_cond_timedwait.c
index 3526ecf..ec44234 100644
--- a/fusl/src/thread/pthread_cond_timedwait.c
+++ b/fusl/src/thread/pthread_cond_timedwait.c
@@ -1,9 +1,9 @@
#include "pthread_impl.h"
void __pthread_testcancel(void);
-int __pthread_mutex_lock(pthread_mutex_t *);
-int __pthread_mutex_unlock(pthread_mutex_t *);
-int __pthread_setcancelstate(int, int *);
+int __pthread_mutex_lock(pthread_mutex_t*);
+int __pthread_mutex_unlock(pthread_mutex_t*);
+int __pthread_setcancelstate(int, int*);
/*
* struct waiter
@@ -28,187 +28,205 @@
*/
struct waiter {
- struct waiter *prev, *next;
- volatile int state, barrier;
- volatile int *notify;
+ struct waiter *prev, *next;
+ volatile int state, barrier;
+ volatile int* notify;
};
/* Self-synchronized-destruction-safe lock functions */
-static inline void lock(volatile int *l)
-{
- if (a_cas(l, 0, 1)) {
- a_cas(l, 1, 2);
- do __wait(l, 0, 2, 1);
- while (a_cas(l, 0, 2));
- }
+static inline void lock(volatile int* l) {
+ if (a_cas(l, 0, 1)) {
+ a_cas(l, 1, 2);
+ do
+ __wait(l, 0, 2, 1);
+ while (a_cas(l, 0, 2));
+ }
}
-static inline void unlock(volatile int *l)
-{
- if (a_swap(l, 0)==2)
- __wake(l, 1, 1);
+static inline void unlock(volatile int* l) {
+ if (a_swap(l, 0) == 2)
+ __wake(l, 1, 1);
}
-static inline void unlock_requeue(volatile int *l, volatile int *r, int w)
-{
- a_store(l, 0);
- if (w) __wake(l, 1, 1);
- else __syscall(SYS_futex, l, FUTEX_REQUEUE|128, 0, 1, r) != -ENOSYS
- || __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r);
+static inline void unlock_requeue(volatile int* l, volatile int* r, int w) {
+ a_store(l, 0);
+ if (w)
+ __wake(l, 1, 1);
+ else
+ __syscall(SYS_futex, l, FUTEX_REQUEUE | 128, 0, 1, r) != -ENOSYS ||
+ __syscall(SYS_futex, l, FUTEX_REQUEUE, 0, 1, r);
}
enum {
- WAITING,
- SIGNALED,
- LEAVING,
+ WAITING,
+ SIGNALED,
+ LEAVING,
};
-int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
-{
- struct waiter node = { 0 };
- int e, seq, clock = c->_c_clock, cs, shared=0, oldstate, tmp;
- volatile int *fut;
+int __pthread_cond_timedwait(pthread_cond_t* restrict c,
+ pthread_mutex_t* restrict m,
+ const struct timespec* restrict ts) {
+ struct waiter node = {0};
+ int e, seq, clock = c->_c_clock, cs, shared = 0, oldstate, tmp;
+ volatile int* fut;
- if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
- return EPERM;
+ if ((m->_m_type & 15) && (m->_m_lock & INT_MAX) != __pthread_self()->tid)
+ return EPERM;
- if (ts && ts->tv_nsec >= 1000000000UL)
- return EINVAL;
+ if (ts && ts->tv_nsec >= 1000000000UL)
+ return EINVAL;
- __pthread_testcancel();
+ __pthread_testcancel();
- if (c->_c_shared) {
- shared = 1;
- fut = &c->_c_seq;
- seq = c->_c_seq;
- a_inc(&c->_c_waiters);
- } else {
- lock(&c->_c_lock);
+ if (c->_c_shared) {
+ shared = 1;
+ fut = &c->_c_seq;
+ seq = c->_c_seq;
+ a_inc(&c->_c_waiters);
+ } else {
+ lock(&c->_c_lock);
- seq = node.barrier = 2;
- fut = &node.barrier;
- node.state = WAITING;
- node.next = c->_c_head;
- c->_c_head = &node;
- if (!c->_c_tail) c->_c_tail = &node;
- else node.next->prev = &node;
+ seq = node.barrier = 2;
+ fut = &node.barrier;
+ node.state = WAITING;
+ node.next = c->_c_head;
+ c->_c_head = &node;
+ if (!c->_c_tail)
+ c->_c_tail = &node;
+ else
+ node.next->prev = &node;
- unlock(&c->_c_lock);
- }
+ unlock(&c->_c_lock);
+ }
- __pthread_mutex_unlock(m);
+ __pthread_mutex_unlock(m);
- __pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs);
- if (cs == PTHREAD_CANCEL_DISABLE) __pthread_setcancelstate(cs, 0);
+ __pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs);
+ if (cs == PTHREAD_CANCEL_DISABLE)
+ __pthread_setcancelstate(cs, 0);
- do e = __timedwait_cp(fut, seq, clock, ts, !shared);
- while (*fut==seq && (!e || e==EINTR));
- if (e == EINTR) e = 0;
+ do
+ e = __timedwait_cp(fut, seq, clock, ts, !shared);
+ while (*fut == seq && (!e || e == EINTR));
+ if (e == EINTR)
+ e = 0;
- if (shared) {
- /* Suppress cancellation if a signal was potentially
- * consumed; this is a legitimate form of spurious
- * wake even if not. */
- if (e == ECANCELED && c->_c_seq != seq) e = 0;
- if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff)
- __wake(&c->_c_waiters, 1, 0);
- oldstate = WAITING;
- goto relock;
- }
+ if (shared) {
+ /* Suppress cancellation if a signal was potentially
+ * consumed; this is a legitimate form of spurious
+ * wake even if not. */
+ if (e == ECANCELED && c->_c_seq != seq)
+ e = 0;
+ if (a_fetch_add(&c->_c_waiters, -1) == -0x7fffffff)
+ __wake(&c->_c_waiters, 1, 0);
+ oldstate = WAITING;
+ goto relock;
+ }
- oldstate = a_cas(&node.state, WAITING, LEAVING);
+ oldstate = a_cas(&node.state, WAITING, LEAVING);
- if (oldstate == WAITING) {
- /* Access to cv object is valid because this waiter was not
- * yet signaled and a new signal/broadcast cannot return
- * after seeing a LEAVING waiter without getting notified
- * via the futex notify below. */
+ if (oldstate == WAITING) {
+ /* Access to cv object is valid because this waiter was not
+ * yet signaled and a new signal/broadcast cannot return
+ * after seeing a LEAVING waiter without getting notified
+ * via the futex notify below. */
- lock(&c->_c_lock);
-
- if (c->_c_head == &node) c->_c_head = node.next;
- else if (node.prev) node.prev->next = node.next;
- if (c->_c_tail == &node) c->_c_tail = node.prev;
- else if (node.next) node.next->prev = node.prev;
-
- unlock(&c->_c_lock);
+ lock(&c->_c_lock);
- if (node.notify) {
- if (a_fetch_add(node.notify, -1)==1)
- __wake(node.notify, 1, 1);
- }
- } else {
- /* Lock barrier first to control wake order. */
- lock(&node.barrier);
- }
+ if (c->_c_head == &node)
+ c->_c_head = node.next;
+ else if (node.prev)
+ node.prev->next = node.next;
+ if (c->_c_tail == &node)
+ c->_c_tail = node.prev;
+ else if (node.next)
+ node.next->prev = node.prev;
+
+ unlock(&c->_c_lock);
+
+ if (node.notify) {
+ if (a_fetch_add(node.notify, -1) == 1)
+ __wake(node.notify, 1, 1);
+ }
+ } else {
+ /* Lock barrier first to control wake order. */
+ lock(&node.barrier);
+ }
relock:
- /* Errors locking the mutex override any existing error or
- * cancellation, since the caller must see them to know the
- * state of the mutex. */
- if ((tmp = pthread_mutex_lock(m))) e = tmp;
+ /* Errors locking the mutex override any existing error or
+ * cancellation, since the caller must see them to know the
+ * state of the mutex. */
+ if ((tmp = pthread_mutex_lock(m)))
+ e = tmp;
- if (oldstate == WAITING) goto done;
+ if (oldstate == WAITING)
+ goto done;
- if (!node.next) a_inc(&m->_m_waiters);
+ if (!node.next)
+ a_inc(&m->_m_waiters);
- /* Unlock the barrier that's holding back the next waiter, and
- * either wake it or requeue it to the mutex. */
- if (node.prev)
- unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & 128);
- else
- a_dec(&m->_m_waiters);
+ /* Unlock the barrier that's holding back the next waiter, and
+ * either wake it or requeue it to the mutex. */
+ if (node.prev)
+ unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & 128);
+ else
+ a_dec(&m->_m_waiters);
- /* Since a signal was consumed, cancellation is not permitted. */
- if (e == ECANCELED) e = 0;
+ /* Since a signal was consumed, cancellation is not permitted. */
+ if (e == ECANCELED)
+ e = 0;
done:
- __pthread_setcancelstate(cs, 0);
+ __pthread_setcancelstate(cs, 0);
- if (e == ECANCELED) {
- __pthread_testcancel();
- __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
- }
+ if (e == ECANCELED) {
+ __pthread_testcancel();
+ __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
+ }
- return e;
+ return e;
}
-int __private_cond_signal(pthread_cond_t *c, int n)
-{
- struct waiter *p, *first=0;
- volatile int ref = 0;
- int cur;
+int __private_cond_signal(pthread_cond_t* c, int n) {
+ struct waiter *p, *first = 0;
+ volatile int ref = 0;
+ int cur;
- lock(&c->_c_lock);
- for (p=c->_c_tail; n && p; p=p->prev) {
- if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) {
- ref++;
- p->notify = &ref;
- } else {
- n--;
- if (!first) first=p;
- }
- }
- /* Split the list, leaving any remainder on the cv. */
- if (p) {
- if (p->next) p->next->prev = 0;
- p->next = 0;
- } else {
- c->_c_head = 0;
- }
- c->_c_tail = p;
- unlock(&c->_c_lock);
+ lock(&c->_c_lock);
+ for (p = c->_c_tail; n && p; p = p->prev) {
+ if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) {
+ ref++;
+ p->notify = &ref;
+ } else {
+ n--;
+ if (!first)
+ first = p;
+ }
+ }
+ /* Split the list, leaving any remainder on the cv. */
+ if (p) {
+ if (p->next)
+ p->next->prev = 0;
+ p->next = 0;
+ } else {
+ c->_c_head = 0;
+ }
+ c->_c_tail = p;
+ unlock(&c->_c_lock);
- /* Wait for any waiters in the LEAVING state to remove
- * themselves from the list before returning or allowing
- * signaled threads to proceed. */
- while ((cur = ref)) __wait(&ref, 0, cur, 1);
+ /* Wait for any waiters in the LEAVING state to remove
+ * themselves from the list before returning or allowing
+ * signaled threads to proceed. */
+ while ((cur = ref))
+ __wait(&ref, 0, cur, 1);
- /* Allow first signaled waiter, if any, to proceed. */
- if (first) unlock(&first->barrier);
+ /* Allow first signaled waiter, if any, to proceed. */
+ if (first)
+ unlock(&first->barrier);
- return 0;
+ return 0;
}
weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
diff --git a/fusl/src/thread/pthread_cond_wait.c b/fusl/src/thread/pthread_cond_wait.c
index 8735bf1..85149c5 100644
--- a/fusl/src/thread/pthread_cond_wait.c
+++ b/fusl/src/thread/pthread_cond_wait.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_cond_wait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m)
-{
- return pthread_cond_timedwait(c, m, 0);
+int pthread_cond_wait(pthread_cond_t* restrict c, pthread_mutex_t* restrict m) {
+ return pthread_cond_timedwait(c, m, 0);
}
diff --git a/fusl/src/thread/pthread_condattr_destroy.c b/fusl/src/thread/pthread_condattr_destroy.c
index c54ec41..4efca98 100644
--- a/fusl/src/thread/pthread_condattr_destroy.c
+++ b/fusl/src/thread/pthread_condattr_destroy.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_condattr_destroy(pthread_condattr_t *a)
-{
- return 0;
+int pthread_condattr_destroy(pthread_condattr_t* a) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_condattr_init.c b/fusl/src/thread/pthread_condattr_init.c
index a41741b..1375a16 100644
--- a/fusl/src/thread/pthread_condattr_init.c
+++ b/fusl/src/thread/pthread_condattr_init.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_condattr_init(pthread_condattr_t *a)
-{
- *a = (pthread_condattr_t){0};
- return 0;
+int pthread_condattr_init(pthread_condattr_t* a) {
+ *a = (pthread_condattr_t){0};
+ return 0;
}
diff --git a/fusl/src/thread/pthread_condattr_setclock.c b/fusl/src/thread/pthread_condattr_setclock.c
index 7112594..98c7be7 100644
--- a/fusl/src/thread/pthread_condattr_setclock.c
+++ b/fusl/src/thread/pthread_condattr_setclock.c
@@ -1,9 +1,9 @@
#include "pthread_impl.h"
-int pthread_condattr_setclock(pthread_condattr_t *a, clockid_t clk)
-{
- if (clk < 0 || clk-2U < 2) return EINVAL;
- a->__attr &= 0x80000000;
- a->__attr |= clk;
- return 0;
+int pthread_condattr_setclock(pthread_condattr_t* a, clockid_t clk) {
+ if (clk < 0 || clk - 2U < 2)
+ return EINVAL;
+ a->__attr &= 0x80000000;
+ a->__attr |= clk;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_condattr_setpshared.c b/fusl/src/thread/pthread_condattr_setpshared.c
index 51453e0..7a7b623 100644
--- a/fusl/src/thread/pthread_condattr_setpshared.c
+++ b/fusl/src/thread/pthread_condattr_setpshared.c
@@ -1,9 +1,9 @@
#include "pthread_impl.h"
-int pthread_condattr_setpshared(pthread_condattr_t *a, int pshared)
-{
- if (pshared > 1U) return EINVAL;
- a->__attr &= 0x7fffffff;
- a->__attr |= (unsigned)pshared<<31;
- return 0;
+int pthread_condattr_setpshared(pthread_condattr_t* a, int pshared) {
+ if (pshared > 1U)
+ return EINVAL;
+ a->__attr &= 0x7fffffff;
+ a->__attr |= (unsigned)pshared << 31;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_create.c b/fusl/src/thread/pthread_create.c
index e7df34a..2b1b5fd 100644
--- a/fusl/src/thread/pthread_create.c
+++ b/fusl/src/thread/pthread_create.c
@@ -6,298 +6,303 @@
#include <string.h>
#include <stddef.h>
-void *__mmap(void *, size_t, int, int, int, off_t);
-int __munmap(void *, size_t);
-int __mprotect(void *, size_t, int);
+void* __mmap(void*, size_t, int, int, int, off_t);
+int __munmap(void*, size_t);
+int __mprotect(void*, size_t, int);
-static void dummy_0()
-{
-}
+static void dummy_0() {}
weak_alias(dummy_0, __acquire_ptc);
weak_alias(dummy_0, __release_ptc);
weak_alias(dummy_0, __pthread_tsd_run_dtors);
weak_alias(dummy_0, __do_orphaned_stdio_locks);
weak_alias(dummy_0, __dl_thread_cleanup);
-_Noreturn void __pthread_exit(void *result)
-{
- pthread_t self = __pthread_self();
- sigset_t set;
+_Noreturn void __pthread_exit(void* result) {
+ pthread_t self = __pthread_self();
+ sigset_t set;
- self->canceldisable = 1;
- self->cancelasync = 0;
- self->result = result;
+ self->canceldisable = 1;
+ self->cancelasync = 0;
+ self->result = result;
- while (self->cancelbuf) {
- void (*f)(void *) = self->cancelbuf->__f;
- void *x = self->cancelbuf->__x;
- self->cancelbuf = self->cancelbuf->__next;
- f(x);
- }
+ while (self->cancelbuf) {
+ void (*f)(void*) = self->cancelbuf->__f;
+ void* x = self->cancelbuf->__x;
+ self->cancelbuf = self->cancelbuf->__next;
+ f(x);
+ }
- __pthread_tsd_run_dtors();
+ __pthread_tsd_run_dtors();
- __lock(self->exitlock);
+ __lock(self->exitlock);
- /* Mark this thread dead before decrementing count */
- __lock(self->killlock);
- self->dead = 1;
+ /* Mark this thread dead before decrementing count */
+ __lock(self->killlock);
+ self->dead = 1;
- /* Block all signals before decrementing the live thread count.
- * This is important to ensure that dynamically allocated TLS
- * is not under-allocated/over-committed, and possibly for other
- * reasons as well. */
- __block_all_sigs(&set);
+ /* Block all signals before decrementing the live thread count.
+ * This is important to ensure that dynamically allocated TLS
+ * is not under-allocated/over-committed, and possibly for other
+ * reasons as well. */
+ __block_all_sigs(&set);
- /* Wait to unlock the kill lock, which governs functions like
- * pthread_kill which target a thread id, until signals have
- * been blocked. This precludes observation of the thread id
- * as a live thread (with application code running in it) after
- * the thread was reported dead by ESRCH being returned. */
- __unlock(self->killlock);
+ /* Wait to unlock the kill lock, which governs functions like
+ * pthread_kill which target a thread id, until signals have
+ * been blocked. This precludes observation of the thread id
+ * as a live thread (with application code running in it) after
+ * the thread was reported dead by ESRCH being returned. */
+ __unlock(self->killlock);
- /* It's impossible to determine whether this is "the last thread"
- * until performing the atomic decrement, since multiple threads
- * could exit at the same time. For the last thread, revert the
- * decrement and unblock signals to give the atexit handlers and
- * stdio cleanup code a consistent state. */
- if (a_fetch_add(&libc.threads_minus_1, -1)==0) {
- libc.threads_minus_1 = 0;
- __restore_sigs(&set);
- exit(0);
- }
+ /* It's impossible to determine whether this is "the last thread"
+ * until performing the atomic decrement, since multiple threads
+ * could exit at the same time. For the last thread, revert the
+ * decrement and unblock signals to give the atexit handlers and
+ * stdio cleanup code a consistent state. */
+ if (a_fetch_add(&libc.threads_minus_1, -1) == 0) {
+ libc.threads_minus_1 = 0;
+ __restore_sigs(&set);
+ exit(0);
+ }
- /* Process robust list in userspace to handle non-pshared mutexes
- * and the detached thread case where the robust list head will
- * be invalid when the kernel would process it. */
- __vm_lock();
- volatile void *volatile *rp;
- while ((rp=self->robust_list.head) && rp != &self->robust_list.head) {
- pthread_mutex_t *m = (void *)((char *)rp
- - offsetof(pthread_mutex_t, _m_next));
- int waiters = m->_m_waiters;
- int priv = (m->_m_type & 128) ^ 128;
- self->robust_list.pending = rp;
- self->robust_list.head = *rp;
- int cont = a_swap(&m->_m_lock, self->tid|0x40000000);
- self->robust_list.pending = 0;
- if (cont < 0 || waiters)
- __wake(&m->_m_lock, 1, priv);
- }
- __vm_unlock();
+ /* Process robust list in userspace to handle non-pshared mutexes
+ * and the detached thread case where the robust list head will
+ * be invalid when the kernel would process it. */
+ __vm_lock();
+ volatile void* volatile* rp;
+ while ((rp = self->robust_list.head) && rp != &self->robust_list.head) {
+ pthread_mutex_t* m =
+ (void*)((char*)rp - offsetof(pthread_mutex_t, _m_next));
+ int waiters = m->_m_waiters;
+ int priv = (m->_m_type & 128) ^ 128;
+ self->robust_list.pending = rp;
+ self->robust_list.head = *rp;
+ int cont = a_swap(&m->_m_lock, self->tid | 0x40000000);
+ self->robust_list.pending = 0;
+ if (cont < 0 || waiters)
+ __wake(&m->_m_lock, 1, priv);
+ }
+ __vm_unlock();
- __do_orphaned_stdio_locks();
- __dl_thread_cleanup();
+ __do_orphaned_stdio_locks();
+ __dl_thread_cleanup();
- if (self->detached && self->map_base) {
- /* Detached threads must avoid the kernel clear_child_tid
- * feature, since the virtual address will have been
- * unmapped and possibly already reused by a new mapping
- * at the time the kernel would perform the write. In
- * the case of threads that started out detached, the
- * initial clone flags are correct, but if the thread was
- * detached later (== 2), we need to clear it here. */
- if (self->detached == 2) __syscall(SYS_set_tid_address, 0);
+ if (self->detached && self->map_base) {
+ /* Detached threads must avoid the kernel clear_child_tid
+ * feature, since the virtual address will have been
+ * unmapped and possibly already reused by a new mapping
+ * at the time the kernel would perform the write. In
+ * the case of threads that started out detached, the
+ * initial clone flags are correct, but if the thread was
+ * detached later (== 2), we need to clear it here. */
+ if (self->detached == 2)
+ __syscall(SYS_set_tid_address, 0);
- /* Robust list will no longer be valid, and was already
- * processed above, so unregister it with the kernel. */
- if (self->robust_list.off)
- __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
+ /* Robust list will no longer be valid, and was already
+ * processed above, so unregister it with the kernel. */
+ if (self->robust_list.off)
+ __syscall(SYS_set_robust_list, 0, 3 * sizeof(long));
- /* Since __unmapself bypasses the normal munmap code path,
- * explicitly wait for vmlock holders first. */
- __vm_wait();
+ /* Since __unmapself bypasses the normal munmap code path,
+ * explicitly wait for vmlock holders first. */
+ __vm_wait();
- /* The following call unmaps the thread's stack mapping
- * and then exits without touching the stack. */
- __unmapself(self->map_base, self->map_size);
- }
+ /* The following call unmaps the thread's stack mapping
+ * and then exits without touching the stack. */
+ __unmapself(self->map_base, self->map_size);
+ }
- for (;;) __syscall(SYS_exit, 0);
+ for (;;)
+ __syscall(SYS_exit, 0);
}
-void __do_cleanup_push(struct __ptcb *cb)
-{
- struct pthread *self = __pthread_self();
- cb->__next = self->cancelbuf;
- self->cancelbuf = cb;
+void __do_cleanup_push(struct __ptcb* cb) {
+ struct pthread* self = __pthread_self();
+ cb->__next = self->cancelbuf;
+ self->cancelbuf = cb;
}
-void __do_cleanup_pop(struct __ptcb *cb)
-{
- __pthread_self()->cancelbuf = cb->__next;
+void __do_cleanup_pop(struct __ptcb* cb) {
+ __pthread_self()->cancelbuf = cb->__next;
}
-static int start(void *p)
-{
- pthread_t self = p;
- if (self->startlock[0]) {
- __wait(self->startlock, 0, 1, 1);
- if (self->startlock[0]) {
- self->detached = 2;
- pthread_exit(0);
- }
- __restore_sigs(self->sigmask);
- }
- if (self->unblock_cancel)
- __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
- SIGPT_SET, 0, _NSIG/8);
- __pthread_exit(self->start(self->start_arg));
- return 0;
+static int start(void* p) {
+ pthread_t self = p;
+ if (self->startlock[0]) {
+ __wait(self->startlock, 0, 1, 1);
+ if (self->startlock[0]) {
+ self->detached = 2;
+ pthread_exit(0);
+ }
+ __restore_sigs(self->sigmask);
+ }
+ if (self->unblock_cancel)
+ __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG / 8);
+ __pthread_exit(self->start(self->start_arg));
+ return 0;
}
-static int start_c11(void *p)
-{
- pthread_t self = p;
- int (*start)(void*) = (int(*)(void*)) self->start;
- __pthread_exit((void *)(uintptr_t)start(self->start_arg));
- return 0;
+static int start_c11(void* p) {
+ pthread_t self = p;
+ int (*start)(void*) = (int (*)(void*))self->start;
+ __pthread_exit((void*)(uintptr_t)start(self->start_arg));
+ return 0;
}
-#define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
+#define ROUND(x) (((x) + PAGE_SIZE - 1) & -PAGE_SIZE)
/* pthread_key_create.c overrides this */
static volatile size_t dummy = 0;
weak_alias(dummy, __pthread_tsd_size);
-static void *dummy_tsd[1] = { 0 };
+static void* dummy_tsd[1] = {0};
weak_alias(dummy_tsd, __pthread_tsd_main);
volatile int __block_new_threads = 0;
-static FILE *volatile dummy_file = 0;
+static FILE* volatile dummy_file = 0;
weak_alias(dummy_file, __stdin_used);
weak_alias(dummy_file, __stdout_used);
weak_alias(dummy_file, __stderr_used);
-static void init_file_lock(FILE *f)
-{
- if (f && f->lock<0) f->lock = 0;
+static void init_file_lock(FILE* f) {
+ if (f && f->lock < 0)
+ f->lock = 0;
}
-void *__copy_tls(unsigned char *);
+void* __copy_tls(unsigned char*);
-int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg)
-{
- int ret, c11 = (attrp == __ATTRP_C11_THREAD);
- size_t size, guard;
- struct pthread *self, *new;
- unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
- unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
- | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS
- | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
- int do_sched = 0;
- pthread_attr_t attr = {0};
+int __pthread_create(pthread_t* restrict res,
+ const pthread_attr_t* restrict attrp,
+ void* (*entry)(void*),
+ void* restrict arg) {
+ int ret, c11 = (attrp == __ATTRP_C11_THREAD);
+ size_t size, guard;
+ struct pthread *self, *new;
+ unsigned char *map = 0, *stack = 0, *tsd = 0, *stack_limit;
+ unsigned flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
+ CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS |
+ CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | CLONE_DETACHED;
+ int do_sched = 0;
+ pthread_attr_t attr = {0};
- if (!libc.can_do_threads) return ENOSYS;
- self = __pthread_self();
- if (!libc.threaded) {
- for (FILE *f=*__ofl_lock(); f; f=f->next)
- init_file_lock(f);
- __ofl_unlock();
- init_file_lock(__stdin_used);
- init_file_lock(__stdout_used);
- init_file_lock(__stderr_used);
- __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG/8);
- self->tsd = (void **)__pthread_tsd_main;
- libc.threaded = 1;
- }
- if (attrp && !c11) attr = *attrp;
+ if (!libc.can_do_threads)
+ return ENOSYS;
+ self = __pthread_self();
+ if (!libc.threaded) {
+ for (FILE* f = *__ofl_lock(); f; f = f->next)
+ init_file_lock(f);
+ __ofl_unlock();
+ init_file_lock(__stdin_used);
+ init_file_lock(__stdout_used);
+ init_file_lock(__stderr_used);
+ __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK, SIGPT_SET, 0, _NSIG / 8);
+ self->tsd = (void**)__pthread_tsd_main;
+ libc.threaded = 1;
+ }
+ if (attrp && !c11)
+ attr = *attrp;
- __acquire_ptc();
- if (__block_new_threads) __wait(&__block_new_threads, 0, 1, 1);
+ __acquire_ptc();
+ if (__block_new_threads)
+ __wait(&__block_new_threads, 0, 1, 1);
- if (attr._a_stackaddr) {
- size_t need = libc.tls_size + __pthread_tsd_size;
- size = attr._a_stacksize + DEFAULT_STACK_SIZE;
- stack = (void *)(attr._a_stackaddr & -16);
- stack_limit = (void *)(attr._a_stackaddr - size);
- /* Use application-provided stack for TLS only when
- * it does not take more than ~12% or 2k of the
- * application's stack space. */
- if (need < size/8 && need < 2048) {
- tsd = stack - __pthread_tsd_size;
- stack = tsd - libc.tls_size;
- memset(stack, 0, need);
- } else {
- size = ROUND(need);
- guard = 0;
- }
- } else {
- guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
- size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize
- + libc.tls_size + __pthread_tsd_size);
- }
+ if (attr._a_stackaddr) {
+ size_t need = libc.tls_size + __pthread_tsd_size;
+ size = attr._a_stacksize + DEFAULT_STACK_SIZE;
+ stack = (void*)(attr._a_stackaddr & -16);
+ stack_limit = (void*)(attr._a_stackaddr - size);
+ /* Use application-provided stack for TLS only when
+ * it does not take more than ~12% or 2k of the
+ * application's stack space. */
+ if (need < size / 8 && need < 2048) {
+ tsd = stack - __pthread_tsd_size;
+ stack = tsd - libc.tls_size;
+ memset(stack, 0, need);
+ } else {
+ size = ROUND(need);
+ guard = 0;
+ }
+ } else {
+ guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
+ size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize +
+ libc.tls_size + __pthread_tsd_size);
+ }
- if (!tsd) {
- if (guard) {
- map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
- if (map == MAP_FAILED) goto fail;
- if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
- && errno != ENOSYS) {
- __munmap(map, size);
- goto fail;
- }
- } else {
- map = __mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
- if (map == MAP_FAILED) goto fail;
- }
- tsd = map + size - __pthread_tsd_size;
- if (!stack) {
- stack = tsd - libc.tls_size;
- stack_limit = map + guard;
- }
- }
+ if (!tsd) {
+ if (guard) {
+ map = __mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
+ if (map == MAP_FAILED)
+ goto fail;
+ if (__mprotect(map + guard, size - guard, PROT_READ | PROT_WRITE) &&
+ errno != ENOSYS) {
+ __munmap(map, size);
+ goto fail;
+ }
+ } else {
+ map = __mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1,
+ 0);
+ if (map == MAP_FAILED)
+ goto fail;
+ }
+ tsd = map + size - __pthread_tsd_size;
+ if (!stack) {
+ stack = tsd - libc.tls_size;
+ stack_limit = map + guard;
+ }
+ }
- new = __copy_tls(tsd - libc.tls_size);
- new->map_base = map;
- new->map_size = size;
- new->stack = stack;
- new->stack_size = stack - stack_limit;
- new->start = entry;
- new->start_arg = arg;
- new->self = new;
- new->tsd = (void *)tsd;
- new->locale = &libc.global_locale;
- if (attr._a_detach) {
- new->detached = 1;
- flags -= CLONE_CHILD_CLEARTID;
- }
- if (attr._a_sched) {
- do_sched = new->startlock[0] = 1;
- __block_app_sigs(new->sigmask);
- }
- new->robust_list.head = &new->robust_list.head;
- new->unblock_cancel = self->cancel;
- new->CANARY = self->CANARY;
+ new = __copy_tls(tsd - libc.tls_size);
+ new->map_base = map;
+ new->map_size = size;
+ new->stack = stack;
+ new->stack_size = stack - stack_limit;
+ new->start = entry;
+ new->start_arg = arg;
+ new->self = new;
+ new->tsd = (void*)tsd;
+ new->locale = &libc.global_locale;
+ if (attr._a_detach) {
+ new->detached = 1;
+ flags -= CLONE_CHILD_CLEARTID;
+ }
+ if (attr._a_sched) {
+ do_sched = new->startlock[0] = 1;
+ __block_app_sigs(new->sigmask);
+ }
+ new->robust_list.head = &new->robust_list.head;
+ new->unblock_cancel = self->cancel;
+ new->CANARY = self->CANARY;
- a_inc(&libc.threads_minus_1);
- ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);
+ a_inc(&libc.threads_minus_1);
+ ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid,
+ TP_ADJ(new), &new->tid);
- __release_ptc();
+ __release_ptc();
- if (do_sched) {
- __restore_sigs(new->sigmask);
- }
+ if (do_sched) {
+ __restore_sigs(new->sigmask);
+ }
- if (ret < 0) {
- a_dec(&libc.threads_minus_1);
- if (map) __munmap(map, size);
- return EAGAIN;
- }
+ if (ret < 0) {
+ a_dec(&libc.threads_minus_1);
+ if (map)
+ __munmap(map, size);
+ return EAGAIN;
+ }
- if (do_sched) {
- ret = __syscall(SYS_sched_setscheduler, new->tid,
- attr._a_policy, &attr._a_prio);
- a_store(new->startlock, ret<0 ? 2 : 0);
- __wake(new->startlock, 1, 1);
- if (ret < 0) return -ret;
- }
+ if (do_sched) {
+ ret = __syscall(SYS_sched_setscheduler, new->tid, attr._a_policy,
+ &attr._a_prio);
+ a_store(new->startlock, ret < 0 ? 2 : 0);
+ __wake(new->startlock, 1, 1);
+ if (ret < 0)
+ return -ret;
+ }
- *res = new;
- return 0;
+ *res = new;
+ return 0;
fail:
- __release_ptc();
- return EAGAIN;
+ __release_ptc();
+ return EAGAIN;
}
weak_alias(__pthread_exit, pthread_exit);
diff --git a/fusl/src/thread/pthread_detach.c b/fusl/src/thread/pthread_detach.c
index ed77f74..3a5f70d 100644
--- a/fusl/src/thread/pthread_detach.c
+++ b/fusl/src/thread/pthread_detach.c
@@ -1,16 +1,15 @@
#include "pthread_impl.h"
#include <threads.h>
-int __pthread_join(pthread_t, void **);
+int __pthread_join(pthread_t, void**);
-static int __pthread_detach(pthread_t t)
-{
- /* Cannot detach a thread that's already exiting */
- if (a_swap(t->exitlock, 1))
- return __pthread_join(t, 0);
- t->detached = 2;
- __unlock(t->exitlock);
- return 0;
+static int __pthread_detach(pthread_t t) {
+ /* Cannot detach a thread that's already exiting */
+ if (a_swap(t->exitlock, 1))
+ return __pthread_join(t, 0);
+ t->detached = 2;
+ __unlock(t->exitlock);
+ return 0;
}
weak_alias(__pthread_detach, pthread_detach);
diff --git a/fusl/src/thread/pthread_equal.c b/fusl/src/thread/pthread_equal.c
index 7c31482..7bd03d1 100644
--- a/fusl/src/thread/pthread_equal.c
+++ b/fusl/src/thread/pthread_equal.c
@@ -2,9 +2,8 @@
#include <threads.h>
#include "libc.h"
-static int __pthread_equal(pthread_t a, pthread_t b)
-{
- return a==b;
+static int __pthread_equal(pthread_t a, pthread_t b) {
+ return a == b;
}
weak_alias(__pthread_equal, pthread_equal);
diff --git a/fusl/src/thread/pthread_getattr_np.c b/fusl/src/thread/pthread_getattr_np.c
index 10ea512..bb9ec5f 100644
--- a/fusl/src/thread/pthread_getattr_np.c
+++ b/fusl/src/thread/pthread_getattr_np.c
@@ -3,21 +3,22 @@
#include "libc.h"
#include <sys/mman.h>
-int pthread_getattr_np(pthread_t t, pthread_attr_t *a)
-{
- *a = (pthread_attr_t){0};
- a->_a_detach = !!t->detached;
- if (t->stack) {
- a->_a_stackaddr = (uintptr_t)t->stack;
- a->_a_stacksize = t->stack_size - DEFAULT_STACK_SIZE;
- } else {
- char *p = (void *)libc.auxv;
- size_t l = PAGE_SIZE;
- p += -(uintptr_t)p & PAGE_SIZE-1;
- a->_a_stackaddr = (uintptr_t)p;
- while (mremap(p-l-PAGE_SIZE, PAGE_SIZE, 2*PAGE_SIZE, 0)==MAP_FAILED && errno==ENOMEM)
- l += PAGE_SIZE;
- a->_a_stacksize = l - DEFAULT_STACK_SIZE;
- }
- return 0;
+int pthread_getattr_np(pthread_t t, pthread_attr_t* a) {
+ *a = (pthread_attr_t){0};
+ a->_a_detach = !!t->detached;
+ if (t->stack) {
+ a->_a_stackaddr = (uintptr_t)t->stack;
+ a->_a_stacksize = t->stack_size - DEFAULT_STACK_SIZE;
+ } else {
+ char* p = (void*)libc.auxv;
+ size_t l = PAGE_SIZE;
+ p += -(uintptr_t)p & PAGE_SIZE - 1;
+ a->_a_stackaddr = (uintptr_t)p;
+ while (mremap(p - l - PAGE_SIZE, PAGE_SIZE, 2 * PAGE_SIZE, 0) ==
+ MAP_FAILED &&
+ errno == ENOMEM)
+ l += PAGE_SIZE;
+ a->_a_stacksize = l - DEFAULT_STACK_SIZE;
+ }
+ return 0;
}
diff --git a/fusl/src/thread/pthread_getconcurrency.c b/fusl/src/thread/pthread_getconcurrency.c
index 269429a..d03d1a8 100644
--- a/fusl/src/thread/pthread_getconcurrency.c
+++ b/fusl/src/thread/pthread_getconcurrency.c
@@ -1,6 +1,5 @@
#include <pthread.h>
-int pthread_getconcurrency()
-{
- return 0;
+int pthread_getconcurrency() {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_getcpuclockid.c b/fusl/src/thread/pthread_getcpuclockid.c
index 9df14fb..75fcc58 100644
--- a/fusl/src/thread/pthread_getcpuclockid.c
+++ b/fusl/src/thread/pthread_getcpuclockid.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_getcpuclockid(pthread_t t, clockid_t *clockid)
-{
- *clockid = (-t->tid-1)*8U + 6;
- return 0;
+int pthread_getcpuclockid(pthread_t t, clockid_t* clockid) {
+ *clockid = (-t->tid - 1) * 8U + 6;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_getschedparam.c b/fusl/src/thread/pthread_getschedparam.c
index 3053c18..3ef2659 100644
--- a/fusl/src/thread/pthread_getschedparam.c
+++ b/fusl/src/thread/pthread_getschedparam.c
@@ -1,17 +1,18 @@
#include "pthread_impl.h"
-int pthread_getschedparam(pthread_t t, int *restrict policy, struct sched_param *restrict param)
-{
- int r;
- __lock(t->killlock);
- if (t->dead) {
- r = ESRCH;
- } else {
- r = -__syscall(SYS_sched_getparam, t->tid, param);
- if (!r) {
- *policy = __syscall(SYS_sched_getscheduler, t->tid);
- }
- }
- __unlock(t->killlock);
- return r;
+int pthread_getschedparam(pthread_t t,
+ int* restrict policy,
+ struct sched_param* restrict param) {
+ int r;
+ __lock(t->killlock);
+ if (t->dead) {
+ r = ESRCH;
+ } else {
+ r = -__syscall(SYS_sched_getparam, t->tid, param);
+ if (!r) {
+ *policy = __syscall(SYS_sched_getscheduler, t->tid);
+ }
+ }
+ __unlock(t->killlock);
+ return r;
}
diff --git a/fusl/src/thread/pthread_getspecific.c b/fusl/src/thread/pthread_getspecific.c
index d9342a5..08f49c5 100644
--- a/fusl/src/thread/pthread_getspecific.c
+++ b/fusl/src/thread/pthread_getspecific.c
@@ -1,10 +1,9 @@
#include "pthread_impl.h"
#include <threads.h>
-static void *__pthread_getspecific(pthread_key_t k)
-{
- struct pthread *self = __pthread_self();
- return self->tsd[k];
+static void* __pthread_getspecific(pthread_key_t k) {
+ struct pthread* self = __pthread_self();
+ return self->tsd[k];
}
weak_alias(__pthread_getspecific, pthread_getspecific);
diff --git a/fusl/src/thread/pthread_join.c b/fusl/src/thread/pthread_join.c
index 694d377..952bf90 100644
--- a/fusl/src/thread/pthread_join.c
+++ b/fusl/src/thread/pthread_join.c
@@ -1,22 +1,25 @@
#include "pthread_impl.h"
#include <sys/mman.h>
-int __munmap(void *, size_t);
+int __munmap(void*, size_t);
void __pthread_testcancel(void);
-int __pthread_setcancelstate(int, int *);
+int __pthread_setcancelstate(int, int*);
-int __pthread_join(pthread_t t, void **res)
-{
- int tmp, cs;
- __pthread_testcancel();
- __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0);
- while ((tmp = t->tid)) __timedwait_cp(&t->tid, tmp, 0, 0, 0);
- __pthread_setcancelstate(cs, 0);
- a_barrier();
- if (res) *res = t->result;
- if (t->map_base) __munmap(t->map_base, t->map_size);
- return 0;
+int __pthread_join(pthread_t t, void** res) {
+ int tmp, cs;
+ __pthread_testcancel();
+ __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ if (cs == PTHREAD_CANCEL_ENABLE)
+ __pthread_setcancelstate(cs, 0);
+ while ((tmp = t->tid))
+ __timedwait_cp(&t->tid, tmp, 0, 0, 0);
+ __pthread_setcancelstate(cs, 0);
+ a_barrier();
+ if (res)
+ *res = t->result;
+ if (t->map_base)
+ __munmap(t->map_base, t->map_size);
+ return 0;
}
weak_alias(__pthread_join, pthread_join);
diff --git a/fusl/src/thread/pthread_key_create.c b/fusl/src/thread/pthread_key_create.c
index a78e507..bacb4a6 100644
--- a/fusl/src/thread/pthread_key_create.c
+++ b/fusl/src/thread/pthread_key_create.c
@@ -1,55 +1,52 @@
#include "pthread_impl.h"
-volatile size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
-void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
+volatile size_t __pthread_tsd_size = sizeof(void*) * PTHREAD_KEYS_MAX;
+void* __pthread_tsd_main[PTHREAD_KEYS_MAX] = {0};
-static void (*volatile keys[PTHREAD_KEYS_MAX])(void *);
+static void (*volatile keys[PTHREAD_KEYS_MAX])(void*);
-static void nodtor(void *dummy)
-{
+static void nodtor(void* dummy) {}
+
+int __pthread_key_create(pthread_key_t* k, void (*dtor)(void*)) {
+ unsigned i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
+ unsigned j = i;
+ pthread_t self = __pthread_self();
+
+ /* This can only happen in the main thread before
+ * pthread_create has been called. */
+ if (!self->tsd)
+ self->tsd = __pthread_tsd_main;
+
+ if (!dtor)
+ dtor = nodtor;
+ do {
+ if (!a_cas_p(keys + j, 0, (void*)dtor)) {
+ *k = j;
+ return 0;
+ }
+ } while ((j = (j + 1) % PTHREAD_KEYS_MAX) != i);
+ return EAGAIN;
}
-int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
-{
- unsigned i = (uintptr_t)&k / 16 % PTHREAD_KEYS_MAX;
- unsigned j = i;
- pthread_t self = __pthread_self();
-
- /* This can only happen in the main thread before
- * pthread_create has been called. */
- if (!self->tsd) self->tsd = __pthread_tsd_main;
-
- if (!dtor) dtor = nodtor;
- do {
- if (!a_cas_p(keys+j, 0, (void *)dtor)) {
- *k = j;
- return 0;
- }
- } while ((j=(j+1)%PTHREAD_KEYS_MAX) != i);
- return EAGAIN;
+int __pthread_key_delete(pthread_key_t k) {
+ keys[k] = 0;
+ return 0;
}
-int __pthread_key_delete(pthread_key_t k)
-{
- keys[k] = 0;
- return 0;
-}
-
-void __pthread_tsd_run_dtors()
-{
- pthread_t self = __pthread_self();
- int i, j, not_finished = self->tsd_used;
- for (j=0; not_finished && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
- not_finished = 0;
- for (i=0; i<PTHREAD_KEYS_MAX; i++) {
- if (self->tsd[i] && keys[i]) {
- void *tmp = self->tsd[i];
- self->tsd[i] = 0;
- keys[i](tmp);
- not_finished = 1;
- }
- }
- }
+void __pthread_tsd_run_dtors() {
+ pthread_t self = __pthread_self();
+ int i, j, not_finished = self->tsd_used;
+ for (j = 0; not_finished && j < PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
+ not_finished = 0;
+ for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
+ if (self->tsd[i] && keys[i]) {
+ void* tmp = self->tsd[i];
+ self->tsd[i] = 0;
+ keys[i](tmp);
+ not_finished = 1;
+ }
+ }
+ }
}
weak_alias(__pthread_key_delete, pthread_key_delete);
diff --git a/fusl/src/thread/pthread_kill.c b/fusl/src/thread/pthread_kill.c
index acdb1ea..e891eb6 100644
--- a/fusl/src/thread/pthread_kill.c
+++ b/fusl/src/thread/pthread_kill.c
@@ -1,10 +1,9 @@
#include "pthread_impl.h"
-int pthread_kill(pthread_t t, int sig)
-{
- int r;
- __lock(t->killlock);
- r = t->dead ? ESRCH : -__syscall(SYS_tkill, t->tid, sig);
- __unlock(t->killlock);
- return r;
+int pthread_kill(pthread_t t, int sig) {
+ int r;
+ __lock(t->killlock);
+ r = t->dead ? ESRCH : -__syscall(SYS_tkill, t->tid, sig);
+ __unlock(t->killlock);
+ return r;
}
diff --git a/fusl/src/thread/pthread_mutex_consistent.c b/fusl/src/thread/pthread_mutex_consistent.c
index 96b83b5..9370ea5 100644
--- a/fusl/src/thread/pthread_mutex_consistent.c
+++ b/fusl/src/thread/pthread_mutex_consistent.c
@@ -1,10 +1,10 @@
#include "pthread_impl.h"
-int pthread_mutex_consistent(pthread_mutex_t *m)
-{
- if (!(m->_m_type & 8)) return EINVAL;
- if ((m->_m_lock & 0x7fffffff) != __pthread_self()->tid)
- return EPERM;
- m->_m_type &= ~8U;
- return 0;
+int pthread_mutex_consistent(pthread_mutex_t* m) {
+ if (!(m->_m_type & 8))
+ return EINVAL;
+ if ((m->_m_lock & 0x7fffffff) != __pthread_self()->tid)
+ return EPERM;
+ m->_m_type &= ~8U;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutex_destroy.c b/fusl/src/thread/pthread_mutex_destroy.c
index 6d49e68..1f60996 100644
--- a/fusl/src/thread/pthread_mutex_destroy.c
+++ b/fusl/src/thread/pthread_mutex_destroy.c
@@ -1,6 +1,5 @@
#include <pthread.h>
-int pthread_mutex_destroy(pthread_mutex_t *mutex)
-{
- return 0;
+int pthread_mutex_destroy(pthread_mutex_t* mutex) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutex_getprioceiling.c b/fusl/src/thread/pthread_mutex_getprioceiling.c
index 8c75a66..99e98ef 100644
--- a/fusl/src/thread/pthread_mutex_getprioceiling.c
+++ b/fusl/src/thread/pthread_mutex_getprioceiling.c
@@ -1,6 +1,6 @@
#include "pthread_impl.h"
-int pthread_mutex_getprioceiling(const pthread_mutex_t *restrict m, int *restrict ceiling)
-{
- return EINVAL;
+int pthread_mutex_getprioceiling(const pthread_mutex_t* restrict m,
+ int* restrict ceiling) {
+ return EINVAL;
}
diff --git a/fusl/src/thread/pthread_mutex_init.c b/fusl/src/thread/pthread_mutex_init.c
index acf45a7..dad438c 100644
--- a/fusl/src/thread/pthread_mutex_init.c
+++ b/fusl/src/thread/pthread_mutex_init.c
@@ -1,8 +1,9 @@
#include "pthread_impl.h"
-int pthread_mutex_init(pthread_mutex_t *restrict m, const pthread_mutexattr_t *restrict a)
-{
- *m = (pthread_mutex_t){0};
- if (a) m->_m_type = a->__attr;
- return 0;
+int pthread_mutex_init(pthread_mutex_t* restrict m,
+ const pthread_mutexattr_t* restrict a) {
+ *m = (pthread_mutex_t){0};
+ if (a)
+ m->_m_type = a->__attr;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutex_lock.c b/fusl/src/thread/pthread_mutex_lock.c
index d0c93ca..8135971 100644
--- a/fusl/src/thread/pthread_mutex_lock.c
+++ b/fusl/src/thread/pthread_mutex_lock.c
@@ -1,14 +1,14 @@
#include "pthread_impl.h"
-int __pthread_mutex_timedlock(pthread_mutex_t *restrict, const struct timespec *restrict);
+int __pthread_mutex_timedlock(pthread_mutex_t* restrict,
+ const struct timespec* restrict);
-int __pthread_mutex_lock(pthread_mutex_t *m)
-{
- if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
- && !a_cas(&m->_m_lock, 0, EBUSY))
- return 0;
+int __pthread_mutex_lock(pthread_mutex_t* m) {
+ if ((m->_m_type & 15) == PTHREAD_MUTEX_NORMAL &&
+ !a_cas(&m->_m_lock, 0, EBUSY))
+ return 0;
- return __pthread_mutex_timedlock(m, 0);
+ return __pthread_mutex_timedlock(m, 0);
}
weak_alias(__pthread_mutex_lock, pthread_mutex_lock);
diff --git a/fusl/src/thread/pthread_mutex_setprioceiling.c b/fusl/src/thread/pthread_mutex_setprioceiling.c
index 681f07c..6b411df 100644
--- a/fusl/src/thread/pthread_mutex_setprioceiling.c
+++ b/fusl/src/thread/pthread_mutex_setprioceiling.c
@@ -1,6 +1,7 @@
#include "pthread_impl.h"
-int pthread_mutex_setprioceiling(pthread_mutex_t *restrict m, int ceiling, int *restrict old)
-{
- return EINVAL;
+int pthread_mutex_setprioceiling(pthread_mutex_t* restrict m,
+ int ceiling,
+ int* restrict old) {
+ return EINVAL;
}
diff --git a/fusl/src/thread/pthread_mutex_timedlock.c b/fusl/src/thread/pthread_mutex_timedlock.c
index 0a240e7..dcca44b 100644
--- a/fusl/src/thread/pthread_mutex_timedlock.c
+++ b/fusl/src/thread/pthread_mutex_timedlock.c
@@ -1,34 +1,37 @@
#include "pthread_impl.h"
-int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
-{
- if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
- && !a_cas(&m->_m_lock, 0, EBUSY))
- return 0;
+int __pthread_mutex_timedlock(pthread_mutex_t* restrict m,
+ const struct timespec* restrict at) {
+ if ((m->_m_type & 15) == PTHREAD_MUTEX_NORMAL &&
+ !a_cas(&m->_m_lock, 0, EBUSY))
+ return 0;
- int r, t, priv = (m->_m_type & 128) ^ 128;
+ int r, t, priv = (m->_m_type & 128) ^ 128;
- r = pthread_mutex_trylock(m);
- if (r != EBUSY) return r;
-
- int spins = 100;
- while (spins-- && m->_m_lock && !m->_m_waiters) a_spin();
+ r = pthread_mutex_trylock(m);
+ if (r != EBUSY)
+ return r;
- while ((r=pthread_mutex_trylock(m)) == EBUSY) {
- if (!(r=m->_m_lock) || ((r&0x40000000) && (m->_m_type&4)))
- continue;
- if ((m->_m_type&3) == PTHREAD_MUTEX_ERRORCHECK
- && (r&0x7fffffff) == __pthread_self()->tid)
- return EDEADLK;
+ int spins = 100;
+ while (spins-- && m->_m_lock && !m->_m_waiters)
+ a_spin();
- a_inc(&m->_m_waiters);
- t = r | 0x80000000;
- a_cas(&m->_m_lock, r, t);
- r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, priv);
- a_dec(&m->_m_waiters);
- if (r && r != EINTR) break;
- }
- return r;
+ while ((r = pthread_mutex_trylock(m)) == EBUSY) {
+ if (!(r = m->_m_lock) || ((r & 0x40000000) && (m->_m_type & 4)))
+ continue;
+ if ((m->_m_type & 3) == PTHREAD_MUTEX_ERRORCHECK &&
+ (r & 0x7fffffff) == __pthread_self()->tid)
+ return EDEADLK;
+
+ a_inc(&m->_m_waiters);
+ t = r | 0x80000000;
+ a_cas(&m->_m_lock, r, t);
+ r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, priv);
+ a_dec(&m->_m_waiters);
+ if (r && r != EINTR)
+ break;
+ }
+ return r;
}
weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);
diff --git a/fusl/src/thread/pthread_mutex_trylock.c b/fusl/src/thread/pthread_mutex_trylock.c
index 0df3ce2..bcb4913 100644
--- a/fusl/src/thread/pthread_mutex_trylock.c
+++ b/fusl/src/thread/pthread_mutex_trylock.c
@@ -1,58 +1,59 @@
#include "pthread_impl.h"
-int __pthread_mutex_trylock_owner(pthread_mutex_t *m)
-{
- int old, own;
- int type = m->_m_type & 15;
- pthread_t self = __pthread_self();
- int tid = self->tid;
+int __pthread_mutex_trylock_owner(pthread_mutex_t* m) {
+ int old, own;
+ int type = m->_m_type & 15;
+ pthread_t self = __pthread_self();
+ int tid = self->tid;
- old = m->_m_lock;
- own = old & 0x7fffffff;
- if (own == tid && (type&3) == PTHREAD_MUTEX_RECURSIVE) {
- if ((unsigned)m->_m_count >= INT_MAX) return EAGAIN;
- m->_m_count++;
- return 0;
- }
- if (own == 0x40000000) return ENOTRECOVERABLE;
+ old = m->_m_lock;
+ own = old & 0x7fffffff;
+ if (own == tid && (type & 3) == PTHREAD_MUTEX_RECURSIVE) {
+ if ((unsigned)m->_m_count >= INT_MAX)
+ return EAGAIN;
+ m->_m_count++;
+ return 0;
+ }
+ if (own == 0x40000000)
+ return ENOTRECOVERABLE;
- if (m->_m_type & 128) {
- if (!self->robust_list.off) {
- self->robust_list.off = (char*)&m->_m_lock-(char *)&m->_m_next;
- __syscall(SYS_set_robust_list, &self->robust_list, 3*sizeof(long));
- }
- if (m->_m_waiters) tid |= 0x80000000;
- self->robust_list.pending = &m->_m_next;
- }
+ if (m->_m_type & 128) {
+ if (!self->robust_list.off) {
+ self->robust_list.off = (char*)&m->_m_lock - (char*)&m->_m_next;
+ __syscall(SYS_set_robust_list, &self->robust_list, 3 * sizeof(long));
+ }
+ if (m->_m_waiters)
+ tid |= 0x80000000;
+ self->robust_list.pending = &m->_m_next;
+ }
- if ((own && (!(own & 0x40000000) || !(type & 4)))
- || a_cas(&m->_m_lock, old, tid) != old) {
- self->robust_list.pending = 0;
- return EBUSY;
- }
+ if ((own && (!(own & 0x40000000) || !(type & 4))) ||
+ a_cas(&m->_m_lock, old, tid) != old) {
+ self->robust_list.pending = 0;
+ return EBUSY;
+ }
- volatile void *next = self->robust_list.head;
- m->_m_next = next;
- m->_m_prev = &self->robust_list.head;
- if (next != &self->robust_list.head) *(volatile void *volatile *)
- ((char *)next - sizeof(void *)) = &m->_m_next;
- self->robust_list.head = &m->_m_next;
- self->robust_list.pending = 0;
+ volatile void* next = self->robust_list.head;
+ m->_m_next = next;
+ m->_m_prev = &self->robust_list.head;
+ if (next != &self->robust_list.head)
+ *(volatile void* volatile*)((char*)next - sizeof(void*)) = &m->_m_next;
+ self->robust_list.head = &m->_m_next;
+ self->robust_list.pending = 0;
- if (own) {
- m->_m_count = 0;
- m->_m_type |= 8;
- return EOWNERDEAD;
- }
+ if (own) {
+ m->_m_count = 0;
+ m->_m_type |= 8;
+ return EOWNERDEAD;
+ }
- return 0;
+ return 0;
}
-int __pthread_mutex_trylock(pthread_mutex_t *m)
-{
- if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL)
- return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY;
- return __pthread_mutex_trylock_owner(m);
+int __pthread_mutex_trylock(pthread_mutex_t* m) {
+ if ((m->_m_type & 15) == PTHREAD_MUTEX_NORMAL)
+ return a_cas(&m->_m_lock, 0, EBUSY) & EBUSY;
+ return __pthread_mutex_trylock_owner(m);
}
weak_alias(__pthread_mutex_trylock, pthread_mutex_trylock);
diff --git a/fusl/src/thread/pthread_mutex_unlock.c b/fusl/src/thread/pthread_mutex_unlock.c
index 02da92a..d80fde0 100644
--- a/fusl/src/thread/pthread_mutex_unlock.c
+++ b/fusl/src/thread/pthread_mutex_unlock.c
@@ -1,37 +1,36 @@
#include "pthread_impl.h"
-int __pthread_mutex_unlock(pthread_mutex_t *m)
-{
- pthread_t self;
- int waiters = m->_m_waiters;
- int cont;
- int type = m->_m_type & 15;
- int priv = (m->_m_type & 128) ^ 128;
+int __pthread_mutex_unlock(pthread_mutex_t* m) {
+ pthread_t self;
+ int waiters = m->_m_waiters;
+ int cont;
+ int type = m->_m_type & 15;
+ int priv = (m->_m_type & 128) ^ 128;
- if (type != PTHREAD_MUTEX_NORMAL) {
- self = __pthread_self();
- if ((m->_m_lock&0x7fffffff) != self->tid)
- return EPERM;
- if ((type&3) == PTHREAD_MUTEX_RECURSIVE && m->_m_count)
- return m->_m_count--, 0;
- if (!priv) {
- self->robust_list.pending = &m->_m_next;
- __vm_lock();
- }
- volatile void *prev = m->_m_prev;
- volatile void *next = m->_m_next;
- *(volatile void *volatile *)prev = next;
- if (next != &self->robust_list.head) *(volatile void *volatile *)
- ((char *)next - sizeof(void *)) = prev;
- }
- cont = a_swap(&m->_m_lock, (type & 8) ? 0x40000000 : 0);
- if (type != PTHREAD_MUTEX_NORMAL && !priv) {
- self->robust_list.pending = 0;
- __vm_unlock();
- }
- if (waiters || cont<0)
- __wake(&m->_m_lock, 1, priv);
- return 0;
+ if (type != PTHREAD_MUTEX_NORMAL) {
+ self = __pthread_self();
+ if ((m->_m_lock & 0x7fffffff) != self->tid)
+ return EPERM;
+ if ((type & 3) == PTHREAD_MUTEX_RECURSIVE && m->_m_count)
+ return m->_m_count--, 0;
+ if (!priv) {
+ self->robust_list.pending = &m->_m_next;
+ __vm_lock();
+ }
+ volatile void* prev = m->_m_prev;
+ volatile void* next = m->_m_next;
+ *(volatile void* volatile*)prev = next;
+ if (next != &self->robust_list.head)
+ *(volatile void* volatile*)((char*)next - sizeof(void*)) = prev;
+ }
+ cont = a_swap(&m->_m_lock, (type & 8) ? 0x40000000 : 0);
+ if (type != PTHREAD_MUTEX_NORMAL && !priv) {
+ self->robust_list.pending = 0;
+ __vm_unlock();
+ }
+ if (waiters || cont < 0)
+ __wake(&m->_m_lock, 1, priv);
+ return 0;
}
weak_alias(__pthread_mutex_unlock, pthread_mutex_unlock);
diff --git a/fusl/src/thread/pthread_mutexattr_destroy.c b/fusl/src/thread/pthread_mutexattr_destroy.c
index 9fd6974..8f30e21 100644
--- a/fusl/src/thread/pthread_mutexattr_destroy.c
+++ b/fusl/src/thread/pthread_mutexattr_destroy.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_mutexattr_destroy(pthread_mutexattr_t *a)
-{
- return 0;
+int pthread_mutexattr_destroy(pthread_mutexattr_t* a) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutexattr_init.c b/fusl/src/thread/pthread_mutexattr_init.c
index 0b72c1b..658f26a 100644
--- a/fusl/src/thread/pthread_mutexattr_init.c
+++ b/fusl/src/thread/pthread_mutexattr_init.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_mutexattr_init(pthread_mutexattr_t *a)
-{
- *a = (pthread_mutexattr_t){0};
- return 0;
+int pthread_mutexattr_init(pthread_mutexattr_t* a) {
+ *a = (pthread_mutexattr_t){0};
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutexattr_setprotocol.c b/fusl/src/thread/pthread_mutexattr_setprotocol.c
index c92a31c..05d7de4 100644
--- a/fusl/src/thread/pthread_mutexattr_setprotocol.c
+++ b/fusl/src/thread/pthread_mutexattr_setprotocol.c
@@ -1,7 +1,7 @@
#include "pthread_impl.h"
-int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int protocol)
-{
- if (protocol) return ENOTSUP;
- return 0;
+int pthread_mutexattr_setprotocol(pthread_mutexattr_t* a, int protocol) {
+ if (protocol)
+ return ENOTSUP;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutexattr_setpshared.c b/fusl/src/thread/pthread_mutexattr_setpshared.c
index 100f6ff..5e16ab9 100644
--- a/fusl/src/thread/pthread_mutexattr_setpshared.c
+++ b/fusl/src/thread/pthread_mutexattr_setpshared.c
@@ -1,9 +1,9 @@
#include "pthread_impl.h"
-int pthread_mutexattr_setpshared(pthread_mutexattr_t *a, int pshared)
-{
- if (pshared > 1U) return EINVAL;
- a->__attr &= ~128U;
- a->__attr |= pshared<<7;
- return 0;
+int pthread_mutexattr_setpshared(pthread_mutexattr_t* a, int pshared) {
+ if (pshared > 1U)
+ return EINVAL;
+ a->__attr &= ~128U;
+ a->__attr |= pshared << 7;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutexattr_setrobust.c b/fusl/src/thread/pthread_mutexattr_setrobust.c
index dcfa4cf..2deef1d 100644
--- a/fusl/src/thread/pthread_mutexattr_setrobust.c
+++ b/fusl/src/thread/pthread_mutexattr_setrobust.c
@@ -1,9 +1,9 @@
#include "pthread_impl.h"
-int pthread_mutexattr_setrobust(pthread_mutexattr_t *a, int robust)
-{
- if (robust > 1U) return EINVAL;
- a->__attr &= ~4;
- a->__attr |= robust*4;
- return 0;
+int pthread_mutexattr_setrobust(pthread_mutexattr_t* a, int robust) {
+ if (robust > 1U)
+ return EINVAL;
+ a->__attr &= ~4;
+ a->__attr |= robust * 4;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_mutexattr_settype.c b/fusl/src/thread/pthread_mutexattr_settype.c
index cd7a80e..1b95c4a 100644
--- a/fusl/src/thread/pthread_mutexattr_settype.c
+++ b/fusl/src/thread/pthread_mutexattr_settype.c
@@ -1,8 +1,8 @@
#include "pthread_impl.h"
-int pthread_mutexattr_settype(pthread_mutexattr_t *a, int type)
-{
- if ((unsigned)type > 2) return EINVAL;
- a->__attr = (a->__attr & ~3) | type;
- return 0;
+int pthread_mutexattr_settype(pthread_mutexattr_t* a, int type) {
+ if ((unsigned)type > 2)
+ return EINVAL;
+ a->__attr = (a->__attr & ~3) | type;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_once.c b/fusl/src/thread/pthread_once.c
index a8f8aeb..0c17d6d 100644
--- a/fusl/src/thread/pthread_once.c
+++ b/fusl/src/thread/pthread_once.c
@@ -1,50 +1,48 @@
#include "pthread_impl.h"
-static void undo(void *control)
-{
- /* Wake all waiters, since the waiter status is lost when
- * resetting control to the initial state. */
- if (a_swap(control, 0) == 3)
- __wake(control, -1, 1);
+static void undo(void* control) {
+ /* Wake all waiters, since the waiter status is lost when
+ * resetting control to the initial state. */
+ if (a_swap(control, 0) == 3)
+ __wake(control, -1, 1);
}
-int __pthread_once_full(pthread_once_t *control, void (*init)(void))
-{
- /* Try to enter initializing state. Four possibilities:
- * 0 - we're the first or the other cancelled; run init
- * 1 - another thread is running init; wait
- * 2 - another thread finished running init; just return
- * 3 - another thread is running init, waiters present; wait */
+int __pthread_once_full(pthread_once_t* control, void (*init)(void)) {
+ /* Try to enter initializing state. Four possibilities:
+ * 0 - we're the first or the other cancelled; run init
+ * 1 - another thread is running init; wait
+ * 2 - another thread finished running init; just return
+ * 3 - another thread is running init, waiters present; wait */
- for (;;) switch (a_cas(control, 0, 1)) {
- case 0:
- pthread_cleanup_push(undo, control);
- init();
- pthread_cleanup_pop(0);
+ for (;;)
+ switch (a_cas(control, 0, 1)) {
+ case 0:
+ pthread_cleanup_push(undo, control);
+ init();
+ pthread_cleanup_pop(0);
- if (a_swap(control, 2) == 3)
- __wake(control, -1, 1);
- return 0;
- case 1:
- /* If this fails, so will __wait. */
- a_cas(control, 1, 3);
- case 3:
- __wait(control, 0, 3, 1);
- continue;
- case 2:
- return 0;
- }
+ if (a_swap(control, 2) == 3)
+ __wake(control, -1, 1);
+ return 0;
+ case 1:
+ /* If this fails, so will __wait. */
+ a_cas(control, 1, 3);
+ case 3:
+ __wait(control, 0, 3, 1);
+ continue;
+ case 2:
+ return 0;
+ }
}
-int __pthread_once(pthread_once_t *control, void (*init)(void))
-{
- /* Return immediately if init finished before, but ensure that
- * effects of the init routine are visible to the caller. */
- if (*(volatile int *)control == 2) {
- a_barrier();
- return 0;
- }
- return __pthread_once_full(control, init);
+int __pthread_once(pthread_once_t* control, void (*init)(void)) {
+ /* Return immediately if init finished before, but ensure that
+ * effects of the init routine are visible to the caller. */
+ if (*(volatile int*)control == 2) {
+ a_barrier();
+ return 0;
+ }
+ return __pthread_once_full(control, init);
}
weak_alias(__pthread_once, pthread_once);
diff --git a/fusl/src/thread/pthread_rwlock_destroy.c b/fusl/src/thread/pthread_rwlock_destroy.c
index 49ecfbd..173148d 100644
--- a/fusl/src/thread/pthread_rwlock_destroy.c
+++ b/fusl/src/thread/pthread_rwlock_destroy.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_rwlock_destroy(pthread_rwlock_t *rw)
-{
- return 0;
+int pthread_rwlock_destroy(pthread_rwlock_t* rw) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_rwlock_init.c b/fusl/src/thread/pthread_rwlock_init.c
index a2c0b47..4dd931f 100644
--- a/fusl/src/thread/pthread_rwlock_init.c
+++ b/fusl/src/thread/pthread_rwlock_init.c
@@ -1,8 +1,9 @@
#include "pthread_impl.h"
-int pthread_rwlock_init(pthread_rwlock_t *restrict rw, const pthread_rwlockattr_t *restrict a)
-{
- *rw = (pthread_rwlock_t){0};
- if (a) rw->_rw_shared = a->__attr[0]*128;
- return 0;
+int pthread_rwlock_init(pthread_rwlock_t* restrict rw,
+ const pthread_rwlockattr_t* restrict a) {
+ *rw = (pthread_rwlock_t){0};
+ if (a)
+ rw->_rw_shared = a->__attr[0] * 128;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_rwlock_rdlock.c b/fusl/src/thread/pthread_rwlock_rdlock.c
index 0800d21..5869421 100644
--- a/fusl/src/thread/pthread_rwlock_rdlock.c
+++ b/fusl/src/thread/pthread_rwlock_rdlock.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_rwlock_rdlock(pthread_rwlock_t *rw)
-{
- return pthread_rwlock_timedrdlock(rw, 0);
+int pthread_rwlock_rdlock(pthread_rwlock_t* rw) {
+ return pthread_rwlock_timedrdlock(rw, 0);
}
diff --git a/fusl/src/thread/pthread_rwlock_timedrdlock.c b/fusl/src/thread/pthread_rwlock_timedrdlock.c
index 0d5d0d6..942eea8 100644
--- a/fusl/src/thread/pthread_rwlock_timedrdlock.c
+++ b/fusl/src/thread/pthread_rwlock_timedrdlock.c
@@ -1,23 +1,27 @@
#include "pthread_impl.h"
-int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
-{
- int r, t;
+int pthread_rwlock_timedrdlock(pthread_rwlock_t* restrict rw,
+ const struct timespec* restrict at) {
+ int r, t;
- r = pthread_rwlock_tryrdlock(rw);
- if (r != EBUSY) return r;
-
- int spins = 100;
- while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
+ r = pthread_rwlock_tryrdlock(rw);
+ if (r != EBUSY)
+ return r;
- while ((r=pthread_rwlock_tryrdlock(rw))==EBUSY) {
- if (!(r=rw->_rw_lock) || (r&0x7fffffff)!=0x7fffffff) continue;
- t = r | 0x80000000;
- a_inc(&rw->_rw_waiters);
- a_cas(&rw->_rw_lock, r, t);
- r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, rw->_rw_shared^128);
- a_dec(&rw->_rw_waiters);
- if (r && r != EINTR) return r;
- }
- return r;
+ int spins = 100;
+ while (spins-- && rw->_rw_lock && !rw->_rw_waiters)
+ a_spin();
+
+ while ((r = pthread_rwlock_tryrdlock(rw)) == EBUSY) {
+ if (!(r = rw->_rw_lock) || (r & 0x7fffffff) != 0x7fffffff)
+ continue;
+ t = r | 0x80000000;
+ a_inc(&rw->_rw_waiters);
+ a_cas(&rw->_rw_lock, r, t);
+ r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, rw->_rw_shared ^ 128);
+ a_dec(&rw->_rw_waiters);
+ if (r && r != EINTR)
+ return r;
+ }
+ return r;
}
diff --git a/fusl/src/thread/pthread_rwlock_timedwrlock.c b/fusl/src/thread/pthread_rwlock_timedwrlock.c
index 7f26dad..cf4d432 100644
--- a/fusl/src/thread/pthread_rwlock_timedwrlock.c
+++ b/fusl/src/thread/pthread_rwlock_timedwrlock.c
@@ -1,23 +1,27 @@
#include "pthread_impl.h"
-int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const struct timespec *restrict at)
-{
- int r, t;
-
- r = pthread_rwlock_trywrlock(rw);
- if (r != EBUSY) return r;
-
- int spins = 100;
- while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();
+int pthread_rwlock_timedwrlock(pthread_rwlock_t* restrict rw,
+ const struct timespec* restrict at) {
+ int r, t;
- while ((r=pthread_rwlock_trywrlock(rw))==EBUSY) {
- if (!(r=rw->_rw_lock)) continue;
- t = r | 0x80000000;
- a_inc(&rw->_rw_waiters);
- a_cas(&rw->_rw_lock, r, t);
- r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, rw->_rw_shared^128);
- a_dec(&rw->_rw_waiters);
- if (r && r != EINTR) return r;
- }
- return r;
+ r = pthread_rwlock_trywrlock(rw);
+ if (r != EBUSY)
+ return r;
+
+ int spins = 100;
+ while (spins-- && rw->_rw_lock && !rw->_rw_waiters)
+ a_spin();
+
+ while ((r = pthread_rwlock_trywrlock(rw)) == EBUSY) {
+ if (!(r = rw->_rw_lock))
+ continue;
+ t = r | 0x80000000;
+ a_inc(&rw->_rw_waiters);
+ a_cas(&rw->_rw_lock, r, t);
+ r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, rw->_rw_shared ^ 128);
+ a_dec(&rw->_rw_waiters);
+ if (r && r != EINTR)
+ return r;
+ }
+ return r;
}
diff --git a/fusl/src/thread/pthread_rwlock_tryrdlock.c b/fusl/src/thread/pthread_rwlock_tryrdlock.c
index fa271fc..0246fc5 100644
--- a/fusl/src/thread/pthread_rwlock_tryrdlock.c
+++ b/fusl/src/thread/pthread_rwlock_tryrdlock.c
@@ -1,13 +1,14 @@
#include "pthread_impl.h"
-int pthread_rwlock_tryrdlock(pthread_rwlock_t *rw)
-{
- int val, cnt;
- do {
- val = rw->_rw_lock;
- cnt = val & 0x7fffffff;
- if (cnt == 0x7fffffff) return EBUSY;
- if (cnt == 0x7ffffffe) return EAGAIN;
- } while (a_cas(&rw->_rw_lock, val, val+1) != val);
- return 0;
+int pthread_rwlock_tryrdlock(pthread_rwlock_t* rw) {
+ int val, cnt;
+ do {
+ val = rw->_rw_lock;
+ cnt = val & 0x7fffffff;
+ if (cnt == 0x7fffffff)
+ return EBUSY;
+ if (cnt == 0x7ffffffe)
+ return EAGAIN;
+ } while (a_cas(&rw->_rw_lock, val, val + 1) != val);
+ return 0;
}
diff --git a/fusl/src/thread/pthread_rwlock_trywrlock.c b/fusl/src/thread/pthread_rwlock_trywrlock.c
index bb3d3a9..b68e10c 100644
--- a/fusl/src/thread/pthread_rwlock_trywrlock.c
+++ b/fusl/src/thread/pthread_rwlock_trywrlock.c
@@ -1,7 +1,7 @@
#include "pthread_impl.h"
-int pthread_rwlock_trywrlock(pthread_rwlock_t *rw)
-{
- if (a_cas(&rw->_rw_lock, 0, 0x7fffffff)) return EBUSY;
- return 0;
+int pthread_rwlock_trywrlock(pthread_rwlock_t* rw) {
+ if (a_cas(&rw->_rw_lock, 0, 0x7fffffff))
+ return EBUSY;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_rwlock_unlock.c b/fusl/src/thread/pthread_rwlock_unlock.c
index 7b5eec8..033fbe1 100644
--- a/fusl/src/thread/pthread_rwlock_unlock.c
+++ b/fusl/src/thread/pthread_rwlock_unlock.c
@@ -1,18 +1,17 @@
#include "pthread_impl.h"
-int pthread_rwlock_unlock(pthread_rwlock_t *rw)
-{
- int val, cnt, waiters, new, priv = rw->_rw_shared^128;
+int pthread_rwlock_unlock(pthread_rwlock_t* rw) {
+ int val, cnt, waiters, new, priv = rw->_rw_shared ^ 128;
- do {
- val = rw->_rw_lock;
- cnt = val & 0x7fffffff;
- waiters = rw->_rw_waiters;
- new = (cnt == 0x7fffffff || cnt == 1) ? 0 : val-1;
- } while (a_cas(&rw->_rw_lock, val, new) != val);
+ do {
+ val = rw->_rw_lock;
+ cnt = val & 0x7fffffff;
+ waiters = rw->_rw_waiters;
+ new = (cnt == 0x7fffffff || cnt == 1) ? 0 : val - 1;
+ } while (a_cas(&rw->_rw_lock, val, new) != val);
- if (!new && (waiters || val<0))
- __wake(&rw->_rw_lock, cnt, priv);
+ if (!new && (waiters || val < 0))
+ __wake(&rw->_rw_lock, cnt, priv);
- return 0;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_rwlock_wrlock.c b/fusl/src/thread/pthread_rwlock_wrlock.c
index 7f33535..c13e870 100644
--- a/fusl/src/thread/pthread_rwlock_wrlock.c
+++ b/fusl/src/thread/pthread_rwlock_wrlock.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_rwlock_wrlock(pthread_rwlock_t *rw)
-{
- return pthread_rwlock_timedwrlock(rw, 0);
+int pthread_rwlock_wrlock(pthread_rwlock_t* rw) {
+ return pthread_rwlock_timedwrlock(rw, 0);
}
diff --git a/fusl/src/thread/pthread_rwlockattr_destroy.c b/fusl/src/thread/pthread_rwlockattr_destroy.c
index fc8d611..8652405 100644
--- a/fusl/src/thread/pthread_rwlockattr_destroy.c
+++ b/fusl/src/thread/pthread_rwlockattr_destroy.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_rwlockattr_destroy(pthread_rwlockattr_t *a)
-{
- return 0;
+int pthread_rwlockattr_destroy(pthread_rwlockattr_t* a) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_rwlockattr_init.c b/fusl/src/thread/pthread_rwlockattr_init.c
index e742069..980f86b 100644
--- a/fusl/src/thread/pthread_rwlockattr_init.c
+++ b/fusl/src/thread/pthread_rwlockattr_init.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_rwlockattr_init(pthread_rwlockattr_t *a)
-{
- *a = (pthread_rwlockattr_t){0};
- return 0;
+int pthread_rwlockattr_init(pthread_rwlockattr_t* a) {
+ *a = (pthread_rwlockattr_t){0};
+ return 0;
}
diff --git a/fusl/src/thread/pthread_rwlockattr_setpshared.c b/fusl/src/thread/pthread_rwlockattr_setpshared.c
index e706197..a7249de 100644
--- a/fusl/src/thread/pthread_rwlockattr_setpshared.c
+++ b/fusl/src/thread/pthread_rwlockattr_setpshared.c
@@ -1,8 +1,8 @@
#include "pthread_impl.h"
-int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *a, int pshared)
-{
- if (pshared > 1U) return EINVAL;
- a->__attr[0] = pshared;
- return 0;
+int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* a, int pshared) {
+ if (pshared > 1U)
+ return EINVAL;
+ a->__attr[0] = pshared;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_self.c b/fusl/src/thread/pthread_self.c
index 241a620..9c05904 100644
--- a/fusl/src/thread/pthread_self.c
+++ b/fusl/src/thread/pthread_self.c
@@ -2,9 +2,8 @@
#include <threads.h>
#include "libc.h"
-static pthread_t __pthread_self_internal()
-{
- return __pthread_self();
+static pthread_t __pthread_self_internal() {
+ return __pthread_self();
}
weak_alias(__pthread_self_internal, pthread_self);
diff --git a/fusl/src/thread/pthread_setcancelstate.c b/fusl/src/thread/pthread_setcancelstate.c
index 5ab8c33..826ebad 100644
--- a/fusl/src/thread/pthread_setcancelstate.c
+++ b/fusl/src/thread/pthread_setcancelstate.c
@@ -1,12 +1,13 @@
#include "pthread_impl.h"
-int __pthread_setcancelstate(int new, int *old)
-{
- if (new > 2U) return EINVAL;
- struct pthread *self = __pthread_self();
- if (old) *old = self->canceldisable;
- self->canceldisable = new;
- return 0;
+int __pthread_setcancelstate(int new, int* old) {
+ if (new > 2U)
+ return EINVAL;
+ struct pthread* self = __pthread_self();
+ if (old)
+ *old = self->canceldisable;
+ self->canceldisable = new;
+ return 0;
}
weak_alias(__pthread_setcancelstate, pthread_setcancelstate);
diff --git a/fusl/src/thread/pthread_setcanceltype.c b/fusl/src/thread/pthread_setcanceltype.c
index bf0a3f3..d3fb8bd 100644
--- a/fusl/src/thread/pthread_setcanceltype.c
+++ b/fusl/src/thread/pthread_setcanceltype.c
@@ -1,11 +1,13 @@
#include "pthread_impl.h"
-int pthread_setcanceltype(int new, int *old)
-{
- struct pthread *self = __pthread_self();
- if (new > 1U) return EINVAL;
- if (old) *old = self->cancelasync;
- self->cancelasync = new;
- if (new) pthread_testcancel();
- return 0;
+int pthread_setcanceltype(int new, int* old) {
+ struct pthread* self = __pthread_self();
+ if (new > 1U)
+ return EINVAL;
+ if (old)
+ *old = self->cancelasync;
+ self->cancelasync = new;
+ if (new)
+ pthread_testcancel();
+ return 0;
}
diff --git a/fusl/src/thread/pthread_setconcurrency.c b/fusl/src/thread/pthread_setconcurrency.c
index 091abf9..de329f9 100644
--- a/fusl/src/thread/pthread_setconcurrency.c
+++ b/fusl/src/thread/pthread_setconcurrency.c
@@ -1,9 +1,10 @@
#include <pthread.h>
#include <errno.h>
-int pthread_setconcurrency(int val)
-{
- if (val < 0) return EINVAL;
- if (val > 0) return EAGAIN;
- return 0;
+int pthread_setconcurrency(int val) {
+ if (val < 0)
+ return EINVAL;
+ if (val > 0)
+ return EAGAIN;
+ return 0;
}
diff --git a/fusl/src/thread/pthread_setschedparam.c b/fusl/src/thread/pthread_setschedparam.c
index c4738d6..bc3702b 100644
--- a/fusl/src/thread/pthread_setschedparam.c
+++ b/fusl/src/thread/pthread_setschedparam.c
@@ -1,10 +1,12 @@
#include "pthread_impl.h"
-int pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param)
-{
- int r;
- __lock(t->killlock);
- r = t->dead ? ESRCH : -__syscall(SYS_sched_setscheduler, t->tid, policy, param);
- __unlock(t->killlock);
- return r;
+int pthread_setschedparam(pthread_t t,
+ int policy,
+ const struct sched_param* param) {
+ int r;
+ __lock(t->killlock);
+ r = t->dead ? ESRCH
+ : -__syscall(SYS_sched_setscheduler, t->tid, policy, param);
+ __unlock(t->killlock);
+ return r;
}
diff --git a/fusl/src/thread/pthread_setschedprio.c b/fusl/src/thread/pthread_setschedprio.c
index e0bdc03..4f9f9f7 100644
--- a/fusl/src/thread/pthread_setschedprio.c
+++ b/fusl/src/thread/pthread_setschedprio.c
@@ -1,10 +1,9 @@
#include "pthread_impl.h"
-int pthread_setschedprio(pthread_t t, int prio)
-{
- int r;
- __lock(t->killlock);
- r = t->dead ? ESRCH : -__syscall(SYS_sched_setparam, t->tid, &prio);
- __unlock(t->killlock);
- return r;
+int pthread_setschedprio(pthread_t t, int prio) {
+ int r;
+ __lock(t->killlock);
+ r = t->dead ? ESRCH : -__syscall(SYS_sched_setparam, t->tid, &prio);
+ __unlock(t->killlock);
+ return r;
}
diff --git a/fusl/src/thread/pthread_setspecific.c b/fusl/src/thread/pthread_setspecific.c
index 55e46a8..720495d 100644
--- a/fusl/src/thread/pthread_setspecific.c
+++ b/fusl/src/thread/pthread_setspecific.c
@@ -1,12 +1,11 @@
#include "pthread_impl.h"
-int pthread_setspecific(pthread_key_t k, const void *x)
-{
- struct pthread *self = __pthread_self();
- /* Avoid unnecessary COW */
- if (self->tsd[k] != x) {
- self->tsd[k] = (void *)x;
- self->tsd_used = 1;
- }
- return 0;
+int pthread_setspecific(pthread_key_t k, const void* x) {
+ struct pthread* self = __pthread_self();
+ /* Avoid unnecessary COW */
+ if (self->tsd[k] != x) {
+ self->tsd[k] = (void*)x;
+ self->tsd_used = 1;
+ }
+ return 0;
}
diff --git a/fusl/src/thread/pthread_sigmask.c b/fusl/src/thread/pthread_sigmask.c
index 88c333f..eb82c60 100644
--- a/fusl/src/thread/pthread_sigmask.c
+++ b/fusl/src/thread/pthread_sigmask.c
@@ -2,18 +2,20 @@
#include <errno.h>
#include "syscall.h"
-int pthread_sigmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
-{
- int ret;
- if ((unsigned)how - SIG_BLOCK > 2U) return EINVAL;
- ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG/8);
- if (!ret && old) {
- if (sizeof old->__bits[0] == 8) {
- old->__bits[0] &= ~0x380000000ULL;
- } else {
- old->__bits[0] &= ~0x80000000UL;
- old->__bits[1] &= ~0x3UL;
- }
- }
- return ret;
+int pthread_sigmask(int how,
+ const sigset_t* restrict set,
+ sigset_t* restrict old) {
+ int ret;
+ if ((unsigned)how - SIG_BLOCK > 2U)
+ return EINVAL;
+ ret = -__syscall(SYS_rt_sigprocmask, how, set, old, _NSIG / 8);
+ if (!ret && old) {
+ if (sizeof old->__bits[0] == 8) {
+ old->__bits[0] &= ~0x380000000ULL;
+ } else {
+ old->__bits[0] &= ~0x80000000UL;
+ old->__bits[1] &= ~0x3UL;
+ }
+ }
+ return ret;
}
diff --git a/fusl/src/thread/pthread_spin_destroy.c b/fusl/src/thread/pthread_spin_destroy.c
index e65a820..fef004f 100644
--- a/fusl/src/thread/pthread_spin_destroy.c
+++ b/fusl/src/thread/pthread_spin_destroy.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_spin_destroy(pthread_spinlock_t *s)
-{
- return 0;
+int pthread_spin_destroy(pthread_spinlock_t* s) {
+ return 0;
}
diff --git a/fusl/src/thread/pthread_spin_init.c b/fusl/src/thread/pthread_spin_init.c
index 681881c..fc0c9aa 100644
--- a/fusl/src/thread/pthread_spin_init.c
+++ b/fusl/src/thread/pthread_spin_init.c
@@ -1,6 +1,5 @@
#include "pthread_impl.h"
-int pthread_spin_init(pthread_spinlock_t *s, int shared)
-{
- return *s = 0;
+int pthread_spin_init(pthread_spinlock_t* s, int shared) {
+ return *s = 0;
}
diff --git a/fusl/src/thread/pthread_spin_lock.c b/fusl/src/thread/pthread_spin_lock.c
index ded2b65..761941d 100644
--- a/fusl/src/thread/pthread_spin_lock.c
+++ b/fusl/src/thread/pthread_spin_lock.c
@@ -1,8 +1,8 @@
#include "pthread_impl.h"
#include <errno.h>
-int pthread_spin_lock(pthread_spinlock_t *s)
-{
- while (*(volatile int *)s || a_cas(s, 0, EBUSY)) a_spin();
- return 0;
+int pthread_spin_lock(pthread_spinlock_t* s) {
+ while (*(volatile int*)s || a_cas(s, 0, EBUSY))
+ a_spin();
+ return 0;
}
diff --git a/fusl/src/thread/pthread_spin_trylock.c b/fusl/src/thread/pthread_spin_trylock.c
index 5284fda..c521e80 100644
--- a/fusl/src/thread/pthread_spin_trylock.c
+++ b/fusl/src/thread/pthread_spin_trylock.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
#include <errno.h>
-int pthread_spin_trylock(pthread_spinlock_t *s)
-{
- return a_cas(s, 0, EBUSY);
+int pthread_spin_trylock(pthread_spinlock_t* s) {
+ return a_cas(s, 0, EBUSY);
}
diff --git a/fusl/src/thread/pthread_spin_unlock.c b/fusl/src/thread/pthread_spin_unlock.c
index 724d9e0..3d34efd 100644
--- a/fusl/src/thread/pthread_spin_unlock.c
+++ b/fusl/src/thread/pthread_spin_unlock.c
@@ -1,7 +1,6 @@
#include "pthread_impl.h"
-int pthread_spin_unlock(pthread_spinlock_t *s)
-{
- a_store(s, 0);
- return 0;
+int pthread_spin_unlock(pthread_spinlock_t* s) {
+ a_store(s, 0);
+ return 0;
}
diff --git a/fusl/src/thread/pthread_testcancel.c b/fusl/src/thread/pthread_testcancel.c
index ee48e6d..cc92b28 100644
--- a/fusl/src/thread/pthread_testcancel.c
+++ b/fusl/src/thread/pthread_testcancel.c
@@ -1,15 +1,12 @@
#include "pthread_impl.h"
#include "libc.h"
-static void dummy()
-{
-}
+static void dummy() {}
weak_alias(dummy, __testcancel);
-void __pthread_testcancel()
-{
- __testcancel();
+void __pthread_testcancel() {
+ __testcancel();
}
weak_alias(__pthread_testcancel, pthread_testcancel);
diff --git a/fusl/src/thread/sem_destroy.c b/fusl/src/thread/sem_destroy.c
index f4aced5..9cf8fcd 100644
--- a/fusl/src/thread/sem_destroy.c
+++ b/fusl/src/thread/sem_destroy.c
@@ -1,6 +1,5 @@
#include <semaphore.h>
-int sem_destroy(sem_t *sem)
-{
- return 0;
+int sem_destroy(sem_t* sem) {
+ return 0;
}
diff --git a/fusl/src/thread/sem_getvalue.c b/fusl/src/thread/sem_getvalue.c
index d9d8307..94e0e2b 100644
--- a/fusl/src/thread/sem_getvalue.c
+++ b/fusl/src/thread/sem_getvalue.c
@@ -1,8 +1,7 @@
#include <semaphore.h>
-int sem_getvalue(sem_t *restrict sem, int *restrict valp)
-{
- int val = sem->__val[0];
- *valp = val < 0 ? 0 : val;
- return 0;
+int sem_getvalue(sem_t* restrict sem, int* restrict valp) {
+ int val = sem->__val[0];
+ *valp = val < 0 ? 0 : val;
+ return 0;
}
diff --git a/fusl/src/thread/sem_init.c b/fusl/src/thread/sem_init.c
index 5509243..45b2ed7 100644
--- a/fusl/src/thread/sem_init.c
+++ b/fusl/src/thread/sem_init.c
@@ -2,14 +2,13 @@
#include <limits.h>
#include <errno.h>
-int sem_init(sem_t *sem, int pshared, unsigned value)
-{
- if (value > SEM_VALUE_MAX) {
- errno = EINVAL;
- return -1;
- }
- sem->__val[0] = value;
- sem->__val[1] = 0;
- sem->__val[2] = pshared ? 0 : 128;
- return 0;
+int sem_init(sem_t* sem, int pshared, unsigned value) {
+ if (value > SEM_VALUE_MAX) {
+ errno = EINVAL;
+ return -1;
+ }
+ sem->__val[0] = value;
+ sem->__val[1] = 0;
+ sem->__val[2] = pshared ? 0 : 128;
+ return 0;
}
diff --git a/fusl/src/thread/sem_open.c b/fusl/src/thread/sem_open.c
index fda0acd..f8ce96a 100644
--- a/fusl/src/thread/sem_open.c
+++ b/fusl/src/thread/sem_open.c
@@ -13,163 +13,169 @@
#include <pthread.h>
#include "libc.h"
-char *__shm_mapname(const char *, char *);
+char* __shm_mapname(const char*, char*);
static struct {
- ino_t ino;
- sem_t *sem;
- int refcnt;
-} *semtab;
+ ino_t ino;
+ sem_t* sem;
+ int refcnt;
+} * semtab;
static volatile int lock[2];
-#define FLAGS (O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK)
+#define FLAGS (O_RDWR | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK)
-sem_t *sem_open(const char *name, int flags, ...)
-{
- va_list ap;
- mode_t mode;
- unsigned value;
- int fd, i, e, slot, first=1, cnt, cs;
- sem_t newsem;
- void *map;
- char tmp[64];
- struct timespec ts;
- struct stat st;
- char buf[NAME_MAX+10];
+sem_t* sem_open(const char* name, int flags, ...) {
+ va_list ap;
+ mode_t mode;
+ unsigned value;
+ int fd, i, e, slot, first = 1, cnt, cs;
+ sem_t newsem;
+ void* map;
+ char tmp[64];
+ struct timespec ts;
+ struct stat st;
+ char buf[NAME_MAX + 10];
- if (!(name = __shm_mapname(name, buf)))
- return SEM_FAILED;
+ if (!(name = __shm_mapname(name, buf)))
+ return SEM_FAILED;
- LOCK(lock);
- /* Allocate table if we don't have one yet */
- if (!semtab && !(semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX))) {
- UNLOCK(lock);
- return SEM_FAILED;
- }
+ LOCK(lock);
+ /* Allocate table if we don't have one yet */
+ if (!semtab && !(semtab = calloc(sizeof *semtab, SEM_NSEMS_MAX))) {
+ UNLOCK(lock);
+ return SEM_FAILED;
+ }
- /* Reserve a slot in case this semaphore is not mapped yet;
- * this is necessary because there is no way to handle
- * failures after creation of the file. */
- slot = -1;
- for (cnt=i=0; i<SEM_NSEMS_MAX; i++) {
- cnt += semtab[i].refcnt;
- if (!semtab[i].sem && slot < 0) slot = i;
- }
- /* Avoid possibility of overflow later */
- if (cnt == INT_MAX || slot < 0) {
- errno = EMFILE;
- UNLOCK(lock);
- return SEM_FAILED;
- }
- /* Dummy pointer to make a reservation */
- semtab[slot].sem = (sem_t *)-1;
- UNLOCK(lock);
+ /* Reserve a slot in case this semaphore is not mapped yet;
+ * this is necessary because there is no way to handle
+ * failures after creation of the file. */
+ slot = -1;
+ for (cnt = i = 0; i < SEM_NSEMS_MAX; i++) {
+ cnt += semtab[i].refcnt;
+ if (!semtab[i].sem && slot < 0)
+ slot = i;
+ }
+ /* Avoid possibility of overflow later */
+ if (cnt == INT_MAX || slot < 0) {
+ errno = EMFILE;
+ UNLOCK(lock);
+ return SEM_FAILED;
+ }
+ /* Dummy pointer to make a reservation */
+ semtab[slot].sem = (sem_t*)-1;
+ UNLOCK(lock);
- flags &= (O_CREAT|O_EXCL);
+ flags &= (O_CREAT | O_EXCL);
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- /* Early failure check for exclusive open; otherwise the case
- * where the semaphore already exists is expensive. */
- if (flags == (O_CREAT|O_EXCL) && access(name, F_OK) == 0) {
- errno = EEXIST;
- goto fail;
- }
+ /* Early failure check for exclusive open; otherwise the case
+ * where the semaphore already exists is expensive. */
+ if (flags == (O_CREAT | O_EXCL) && access(name, F_OK) == 0) {
+ errno = EEXIST;
+ goto fail;
+ }
- for (;;) {
- /* If exclusive mode is not requested, try opening an
- * existing file first and fall back to creation. */
- if (flags != (O_CREAT|O_EXCL)) {
- fd = open(name, FLAGS);
- if (fd >= 0) {
- if (fstat(fd, &st) < 0 ||
- (map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
- close(fd);
- goto fail;
- }
- close(fd);
- break;
- }
- if (errno != ENOENT)
- goto fail;
- }
- if (!(flags & O_CREAT))
- goto fail;
- if (first) {
- first = 0;
- va_start(ap, flags);
- mode = va_arg(ap, mode_t) & 0666;
- value = va_arg(ap, unsigned);
- va_end(ap);
- if (value > SEM_VALUE_MAX) {
- errno = EINVAL;
- goto fail;
- }
- sem_init(&newsem, 1, value);
- }
- /* Create a temp file with the new semaphore contents
- * and attempt to atomically link it as the new name */
- clock_gettime(CLOCK_REALTIME, &ts);
- snprintf(tmp, sizeof(tmp), "/dev/shm/tmp-%d", (int)ts.tv_nsec);
- fd = open(tmp, O_CREAT|O_EXCL|FLAGS, mode);
- if (fd < 0) {
- if (errno == EEXIST) continue;
- goto fail;
- }
- if (write(fd, &newsem, sizeof newsem) != sizeof newsem || fstat(fd, &st) < 0 ||
- (map = mmap(0, sizeof(sem_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
- close(fd);
- unlink(tmp);
- goto fail;
- }
- close(fd);
- e = link(tmp, name) ? errno : 0;
- unlink(tmp);
- if (!e) break;
- munmap(map, sizeof(sem_t));
- /* Failure is only fatal when doing an exclusive open;
- * otherwise, next iteration will try to open the
- * existing file. */
- if (e != EEXIST || flags == (O_CREAT|O_EXCL))
- goto fail;
- }
+ for (;;) {
+ /* If exclusive mode is not requested, try opening an
+ * existing file first and fall back to creation. */
+ if (flags != (O_CREAT | O_EXCL)) {
+ fd = open(name, FLAGS);
+ if (fd >= 0) {
+ if (fstat(fd, &st) < 0 ||
+ (map = mmap(0, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED,
+ fd, 0)) == MAP_FAILED) {
+ close(fd);
+ goto fail;
+ }
+ close(fd);
+ break;
+ }
+ if (errno != ENOENT)
+ goto fail;
+ }
+ if (!(flags & O_CREAT))
+ goto fail;
+ if (first) {
+ first = 0;
+ va_start(ap, flags);
+ mode = va_arg(ap, mode_t) & 0666;
+ value = va_arg(ap, unsigned);
+ va_end(ap);
+ if (value > SEM_VALUE_MAX) {
+ errno = EINVAL;
+ goto fail;
+ }
+ sem_init(&newsem, 1, value);
+ }
+ /* Create a temp file with the new semaphore contents
+ * and attempt to atomically link it as the new name */
+ clock_gettime(CLOCK_REALTIME, &ts);
+ snprintf(tmp, sizeof(tmp), "/dev/shm/tmp-%d", (int)ts.tv_nsec);
+ fd = open(tmp, O_CREAT | O_EXCL | FLAGS, mode);
+ if (fd < 0) {
+ if (errno == EEXIST)
+ continue;
+ goto fail;
+ }
+ if (write(fd, &newsem, sizeof newsem) != sizeof newsem ||
+ fstat(fd, &st) < 0 ||
+ (map = mmap(0, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+ 0)) == MAP_FAILED) {
+ close(fd);
+ unlink(tmp);
+ goto fail;
+ }
+ close(fd);
+ e = link(tmp, name) ? errno : 0;
+ unlink(tmp);
+ if (!e)
+ break;
+ munmap(map, sizeof(sem_t));
+ /* Failure is only fatal when doing an exclusive open;
+ * otherwise, next iteration will try to open the
+ * existing file. */
+ if (e != EEXIST || flags == (O_CREAT | O_EXCL))
+ goto fail;
+ }
- /* See if the newly mapped semaphore is already mapped. If
- * so, unmap the new mapping and use the existing one. Otherwise,
- * add it to the table of mapped semaphores. */
- LOCK(lock);
- for (i=0; i<SEM_NSEMS_MAX && semtab[i].ino != st.st_ino; i++);
- if (i<SEM_NSEMS_MAX) {
- munmap(map, sizeof(sem_t));
- semtab[slot].sem = 0;
- slot = i;
- map = semtab[i].sem;
- }
- semtab[slot].refcnt++;
- semtab[slot].sem = map;
- semtab[slot].ino = st.st_ino;
- UNLOCK(lock);
- pthread_setcancelstate(cs, 0);
- return map;
+ /* See if the newly mapped semaphore is already mapped. If
+ * so, unmap the new mapping and use the existing one. Otherwise,
+ * add it to the table of mapped semaphores. */
+ LOCK(lock);
+ for (i = 0; i < SEM_NSEMS_MAX && semtab[i].ino != st.st_ino; i++)
+ ;
+ if (i < SEM_NSEMS_MAX) {
+ munmap(map, sizeof(sem_t));
+ semtab[slot].sem = 0;
+ slot = i;
+ map = semtab[i].sem;
+ }
+ semtab[slot].refcnt++;
+ semtab[slot].sem = map;
+ semtab[slot].ino = st.st_ino;
+ UNLOCK(lock);
+ pthread_setcancelstate(cs, 0);
+ return map;
fail:
- pthread_setcancelstate(cs, 0);
- LOCK(lock);
- semtab[slot].sem = 0;
- UNLOCK(lock);
- return SEM_FAILED;
+ pthread_setcancelstate(cs, 0);
+ LOCK(lock);
+ semtab[slot].sem = 0;
+ UNLOCK(lock);
+ return SEM_FAILED;
}
-int sem_close(sem_t *sem)
-{
- int i;
- LOCK(lock);
- for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
- if (!--semtab[i].refcnt) {
- semtab[i].sem = 0;
- semtab[i].ino = 0;
- }
- UNLOCK(lock);
- munmap(sem, sizeof *sem);
- return 0;
+int sem_close(sem_t* sem) {
+ int i;
+ LOCK(lock);
+ for (i = 0; i < SEM_NSEMS_MAX && semtab[i].sem != sem; i++)
+ ;
+ if (!--semtab[i].refcnt) {
+ semtab[i].sem = 0;
+ semtab[i].ino = 0;
+ }
+ UNLOCK(lock);
+ munmap(sem, sizeof *sem);
+ return 0;
}
diff --git a/fusl/src/thread/sem_post.c b/fusl/src/thread/sem_post.c
index 31e3293..b374c51 100644
--- a/fusl/src/thread/sem_post.c
+++ b/fusl/src/thread/sem_post.c
@@ -1,17 +1,17 @@
#include <semaphore.h>
#include "pthread_impl.h"
-int sem_post(sem_t *sem)
-{
- int val, waiters, priv = sem->__val[2];
- do {
- val = sem->__val[0];
- waiters = sem->__val[1];
- if (val == SEM_VALUE_MAX) {
- errno = EOVERFLOW;
- return -1;
- }
- } while (a_cas(sem->__val, val, val+1+(val<0)) != val);
- if (val<0 || waiters) __wake(sem->__val, 1, priv);
- return 0;
+int sem_post(sem_t* sem) {
+ int val, waiters, priv = sem->__val[2];
+ do {
+ val = sem->__val[0];
+ waiters = sem->__val[1];
+ if (val == SEM_VALUE_MAX) {
+ errno = EOVERFLOW;
+ return -1;
+ }
+ } while (a_cas(sem->__val, val, val + 1 + (val < 0)) != val);
+ if (val < 0 || waiters)
+ __wake(sem->__val, 1, priv);
+ return 0;
}
diff --git a/fusl/src/thread/sem_timedwait.c b/fusl/src/thread/sem_timedwait.c
index 8132eb1..da98b5d 100644
--- a/fusl/src/thread/sem_timedwait.c
+++ b/fusl/src/thread/sem_timedwait.c
@@ -1,31 +1,31 @@
#include <semaphore.h>
#include "pthread_impl.h"
-static void cleanup(void *p)
-{
- a_dec(p);
+static void cleanup(void* p) {
+ a_dec(p);
}
-int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at)
-{
- pthread_testcancel();
+int sem_timedwait(sem_t* restrict sem, const struct timespec* restrict at) {
+ pthread_testcancel();
- if (!sem_trywait(sem)) return 0;
+ if (!sem_trywait(sem))
+ return 0;
- int spins = 100;
- while (spins-- && sem->__val[0] <= 0 && !sem->__val[1]) a_spin();
+ int spins = 100;
+ while (spins-- && sem->__val[0] <= 0 && !sem->__val[1])
+ a_spin();
- while (sem_trywait(sem)) {
- int r;
- a_inc(sem->__val+1);
- a_cas(sem->__val, 0, -1);
- pthread_cleanup_push(cleanup, (void *)(sem->__val+1));
- r = __timedwait_cp(sem->__val, -1, CLOCK_REALTIME, at, sem->__val[2]);
- pthread_cleanup_pop(1);
- if (r && r != EINTR) {
- errno = r;
- return -1;
- }
- }
- return 0;
+ while (sem_trywait(sem)) {
+ int r;
+ a_inc(sem->__val + 1);
+ a_cas(sem->__val, 0, -1);
+ pthread_cleanup_push(cleanup, (void*)(sem->__val + 1));
+ r = __timedwait_cp(sem->__val, -1, CLOCK_REALTIME, at, sem->__val[2]);
+ pthread_cleanup_pop(1);
+ if (r && r != EINTR) {
+ errno = r;
+ return -1;
+ }
+ }
+ return 0;
}
diff --git a/fusl/src/thread/sem_trywait.c b/fusl/src/thread/sem_trywait.c
index 04edf46..98998a6 100644
--- a/fusl/src/thread/sem_trywait.c
+++ b/fusl/src/thread/sem_trywait.c
@@ -1,13 +1,13 @@
#include <semaphore.h>
#include "pthread_impl.h"
-int sem_trywait(sem_t *sem)
-{
- int val;
- while ((val=sem->__val[0]) > 0) {
- int new = val-1-(val==1 && sem->__val[1]);
- if (a_cas(sem->__val, val, new)==val) return 0;
- }
- errno = EAGAIN;
- return -1;
+int sem_trywait(sem_t* sem) {
+ int val;
+ while ((val = sem->__val[0]) > 0) {
+ int new = val - 1 - (val == 1 && sem->__val[1]);
+ if (a_cas(sem->__val, val, new) == val)
+ return 0;
+ }
+ errno = EAGAIN;
+ return -1;
}
diff --git a/fusl/src/thread/sem_unlink.c b/fusl/src/thread/sem_unlink.c
index c06134b..758a7b4 100644
--- a/fusl/src/thread/sem_unlink.c
+++ b/fusl/src/thread/sem_unlink.c
@@ -1,7 +1,6 @@
#include <semaphore.h>
#include <sys/mman.h>
-int sem_unlink(const char *name)
-{
- return shm_unlink(name);
+int sem_unlink(const char* name) {
+ return shm_unlink(name);
}
diff --git a/fusl/src/thread/sem_wait.c b/fusl/src/thread/sem_wait.c
index 264194f..e2ae6a0 100644
--- a/fusl/src/thread/sem_wait.c
+++ b/fusl/src/thread/sem_wait.c
@@ -1,6 +1,5 @@
#include <semaphore.h>
-int sem_wait(sem_t *sem)
-{
- return sem_timedwait(sem, 0);
+int sem_wait(sem_t* sem) {
+ return sem_timedwait(sem, 0);
}
diff --git a/fusl/src/thread/synccall.c b/fusl/src/thread/synccall.c
index 000ec4e..1f29b3a 100644
--- a/fusl/src/thread/synccall.c
+++ b/fusl/src/thread/synccall.c
@@ -9,170 +9,179 @@
#include "../dirent/__dirent.h"
static struct chain {
- struct chain *next;
- int tid;
- sem_t target_sem, caller_sem;
-} *volatile head;
+ struct chain* next;
+ int tid;
+ sem_t target_sem, caller_sem;
+} * volatile head;
static volatile int synccall_lock[2];
static volatile int target_tid;
-static void (*callback)(void *), *context;
+static void (*callback)(void*), *context;
static volatile int dummy = 0;
weak_alias(dummy, __block_new_threads);
-static void handler(int sig)
-{
- struct chain ch;
- int old_errno = errno;
+static void handler(int sig) {
+ struct chain ch;
+ int old_errno = errno;
- sem_init(&ch.target_sem, 0, 0);
- sem_init(&ch.caller_sem, 0, 0);
+ sem_init(&ch.target_sem, 0, 0);
+ sem_init(&ch.caller_sem, 0, 0);
- ch.tid = __syscall(SYS_gettid);
+ ch.tid = __syscall(SYS_gettid);
- do ch.next = head;
- while (a_cas_p(&head, ch.next, &ch) != ch.next);
+ do
+ ch.next = head;
+ while (a_cas_p(&head, ch.next, &ch) != ch.next);
- if (a_cas(&target_tid, ch.tid, 0) == (ch.tid | 0x80000000))
- __syscall(SYS_futex, &target_tid, FUTEX_UNLOCK_PI|FUTEX_PRIVATE);
+ if (a_cas(&target_tid, ch.tid, 0) == (ch.tid | 0x80000000))
+ __syscall(SYS_futex, &target_tid, FUTEX_UNLOCK_PI | FUTEX_PRIVATE);
- sem_wait(&ch.target_sem);
- callback(context);
- sem_post(&ch.caller_sem);
- sem_wait(&ch.target_sem);
+ sem_wait(&ch.target_sem);
+ callback(context);
+ sem_post(&ch.caller_sem);
+ sem_wait(&ch.target_sem);
- errno = old_errno;
+ errno = old_errno;
}
-void __synccall(void (*func)(void *), void *ctx)
-{
- sigset_t oldmask;
- int cs, i, r, pid, self;;
- DIR dir = {0};
- struct dirent *de;
- struct sigaction sa = { .sa_flags = 0, .sa_handler = handler };
- struct chain *cp, *next;
- struct timespec ts;
+void __synccall(void (*func)(void*), void* ctx) {
+ sigset_t oldmask;
+ int cs, i, r, pid, self;
+ ;
+ DIR dir = {0};
+ struct dirent* de;
+ struct sigaction sa = {.sa_flags = 0, .sa_handler = handler};
+ struct chain *cp, *next;
+ struct timespec ts;
- /* Blocking signals in two steps, first only app-level signals
- * before taking the lock, then all signals after taking the lock,
- * is necessary to achieve AS-safety. Blocking them all first would
- * deadlock if multiple threads called __synccall. Waiting to block
- * any until after the lock would allow re-entry in the same thread
- * with the lock already held. */
- __block_app_sigs(&oldmask);
- LOCK(synccall_lock);
- __block_all_sigs(0);
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ /* Blocking signals in two steps, first only app-level signals
+ * before taking the lock, then all signals after taking the lock,
+ * is necessary to achieve AS-safety. Blocking them all first would
+ * deadlock if multiple threads called __synccall. Waiting to block
+ * any until after the lock would allow re-entry in the same thread
+ * with the lock already held. */
+ __block_app_sigs(&oldmask);
+ LOCK(synccall_lock);
+ __block_all_sigs(0);
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- head = 0;
+ head = 0;
- if (!libc.threaded) goto single_threaded;
+ if (!libc.threaded)
+ goto single_threaded;
- callback = func;
- context = ctx;
+ callback = func;
+ context = ctx;
- /* This atomic store ensures that any signaled threads will see the
- * above stores, and prevents more than a bounded number of threads,
- * those already in pthread_create, from creating new threads until
- * the value is cleared to zero again. */
- a_store(&__block_new_threads, 1);
+ /* This atomic store ensures that any signaled threads will see the
+ * above stores, and prevents more than a bounded number of threads,
+ * those already in pthread_create, from creating new threads until
+ * the value is cleared to zero again. */
+ a_store(&__block_new_threads, 1);
- /* Block even implementation-internal signals, so that nothing
- * interrupts the SIGSYNCCALL handlers. The main possible source
- * of trouble is asynchronous cancellation. */
- memset(&sa.sa_mask, -1, sizeof sa.sa_mask);
- __libc_sigaction(SIGSYNCCALL, &sa, 0);
+ /* Block even implementation-internal signals, so that nothing
+ * interrupts the SIGSYNCCALL handlers. The main possible source
+ * of trouble is asynchronous cancellation. */
+ memset(&sa.sa_mask, -1, sizeof sa.sa_mask);
+ __libc_sigaction(SIGSYNCCALL, &sa, 0);
- pid = __syscall(SYS_getpid);
- self = __syscall(SYS_gettid);
+ pid = __syscall(SYS_getpid);
+ self = __syscall(SYS_gettid);
- /* Since opendir is not AS-safe, the DIR needs to be setup manually
- * in automatic storage. Thankfully this is easy. */
- dir.fd = open("/proc/self/task", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
- if (dir.fd < 0) goto out;
+ /* Since opendir is not AS-safe, the DIR needs to be setup manually
+ * in automatic storage. Thankfully this is easy. */
+ dir.fd = open("/proc/self/task", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (dir.fd < 0)
+ goto out;
- /* Initially send one signal per counted thread. But since we can't
- * synchronize with thread creation/exit here, there could be too
- * few signals. This initial signaling is just an optimization, not
- * part of the logic. */
- for (i=libc.threads_minus_1; i; i--)
- __syscall(SYS_kill, pid, SIGSYNCCALL);
+ /* Initially send one signal per counted thread. But since we can't
+ * synchronize with thread creation/exit here, there could be too
+ * few signals. This initial signaling is just an optimization, not
+ * part of the logic. */
+ for (i = libc.threads_minus_1; i; i--)
+ __syscall(SYS_kill, pid, SIGSYNCCALL);
- /* Loop scanning the kernel-provided thread list until it shows no
- * threads that have not already replied to the signal. */
- for (;;) {
- int miss_cnt = 0;
- while ((de = readdir(&dir))) {
- if (!isdigit(de->d_name[0])) continue;
- int tid = atoi(de->d_name);
- if (tid == self || !tid) continue;
+ /* Loop scanning the kernel-provided thread list until it shows no
+ * threads that have not already replied to the signal. */
+ for (;;) {
+ int miss_cnt = 0;
+ while ((de = readdir(&dir))) {
+ if (!isdigit(de->d_name[0]))
+ continue;
+ int tid = atoi(de->d_name);
+ if (tid == self || !tid)
+ continue;
- /* Set the target thread as the PI futex owner before
- * checking if it's in the list of caught threads. If it
- * adds itself to the list after we check for it, then
- * it will see its own tid in the PI futex and perform
- * the unlock operation. */
- a_store(&target_tid, tid);
+ /* Set the target thread as the PI futex owner before
+ * checking if it's in the list of caught threads. If it
+ * adds itself to the list after we check for it, then
+ * it will see its own tid in the PI futex and perform
+ * the unlock operation. */
+ a_store(&target_tid, tid);
- /* Thread-already-caught is a success condition. */
- for (cp = head; cp && cp->tid != tid; cp=cp->next);
- if (cp) continue;
+ /* Thread-already-caught is a success condition. */
+ for (cp = head; cp && cp->tid != tid; cp = cp->next)
+ ;
+ if (cp)
+ continue;
- r = -__syscall(SYS_tgkill, pid, tid, SIGSYNCCALL);
+ r = -__syscall(SYS_tgkill, pid, tid, SIGSYNCCALL);
- /* Target thread exit is a success condition. */
- if (r == ESRCH) continue;
+ /* Target thread exit is a success condition. */
+ if (r == ESRCH)
+ continue;
- /* The FUTEX_LOCK_PI operation is used to loan priority
- * to the target thread, which otherwise may be unable
- * to run. Timeout is necessary because there is a race
- * condition where the tid may be reused by a different
- * process. */
- clock_gettime(CLOCK_REALTIME, &ts);
- ts.tv_nsec += 10000000;
- if (ts.tv_nsec >= 1000000000) {
- ts.tv_sec++;
- ts.tv_nsec -= 1000000000;
- }
- r = -__syscall(SYS_futex, &target_tid,
- FUTEX_LOCK_PI|FUTEX_PRIVATE, 0, &ts);
+ /* The FUTEX_LOCK_PI operation is used to loan priority
+ * to the target thread, which otherwise may be unable
+ * to run. Timeout is necessary because there is a race
+ * condition where the tid may be reused by a different
+ * process. */
+ clock_gettime(CLOCK_REALTIME, &ts);
+ ts.tv_nsec += 10000000;
+ if (ts.tv_nsec >= 1000000000) {
+ ts.tv_sec++;
+ ts.tv_nsec -= 1000000000;
+ }
+ r = -__syscall(SYS_futex, &target_tid, FUTEX_LOCK_PI | FUTEX_PRIVATE, 0,
+ &ts);
- /* Obtaining the lock means the thread responded. ESRCH
- * means the target thread exited, which is okay too. */
- if (!r || r == ESRCH) continue;
+ /* Obtaining the lock means the thread responded. ESRCH
+ * means the target thread exited, which is okay too. */
+ if (!r || r == ESRCH)
+ continue;
- miss_cnt++;
- }
- if (!miss_cnt) break;
- rewinddir(&dir);
- }
- close(dir.fd);
+ miss_cnt++;
+ }
+ if (!miss_cnt)
+ break;
+ rewinddir(&dir);
+ }
+ close(dir.fd);
- /* Serialize execution of callback in caught threads. */
- for (cp=head; cp; cp=cp->next) {
- sem_post(&cp->target_sem);
- sem_wait(&cp->caller_sem);
- }
+ /* Serialize execution of callback in caught threads. */
+ for (cp = head; cp; cp = cp->next) {
+ sem_post(&cp->target_sem);
+ sem_wait(&cp->caller_sem);
+ }
- sa.sa_handler = SIG_IGN;
- __libc_sigaction(SIGSYNCCALL, &sa, 0);
+ sa.sa_handler = SIG_IGN;
+ __libc_sigaction(SIGSYNCCALL, &sa, 0);
single_threaded:
- func(ctx);
+ func(ctx);
- /* Only release the caught threads once all threads, including the
- * caller, have returned from the callback function. */
- for (cp=head; cp; cp=next) {
- next = cp->next;
- sem_post(&cp->target_sem);
- }
+ /* Only release the caught threads once all threads, including the
+ * caller, have returned from the callback function. */
+ for (cp = head; cp; cp = next) {
+ next = cp->next;
+ sem_post(&cp->target_sem);
+ }
out:
- a_store(&__block_new_threads, 0);
- __wake(&__block_new_threads, -1, 1);
+ a_store(&__block_new_threads, 0);
+ __wake(&__block_new_threads, -1, 1);
- pthread_setcancelstate(cs, 0);
- UNLOCK(synccall_lock);
- __restore_sigs(&oldmask);
+ pthread_setcancelstate(cs, 0);
+ UNLOCK(synccall_lock);
+ __restore_sigs(&oldmask);
}
diff --git a/fusl/src/thread/thrd_create.c b/fusl/src/thread/thrd_create.c
index e033669..b8f604d 100644
--- a/fusl/src/thread/thrd_create.c
+++ b/fusl/src/thread/thrd_create.c
@@ -1,14 +1,20 @@
#include "pthread_impl.h"
#include <threads.h>
-int __pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict);
+int __pthread_create(pthread_t* restrict,
+ const pthread_attr_t* restrict,
+ void* (*)(void*),
+ void* restrict);
-int thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
-{
- int ret = __pthread_create(thr, __ATTRP_C11_THREAD, (void *(*)(void *))func, arg);
- switch (ret) {
- case 0: return thrd_success;
- case EAGAIN: return thrd_nomem;
- default: return thrd_error;
- }
+int thrd_create(thrd_t* thr, thrd_start_t func, void* arg) {
+ int ret =
+ __pthread_create(thr, __ATTRP_C11_THREAD, (void* (*)(void*))func, arg);
+ switch (ret) {
+ case 0:
+ return thrd_success;
+ case EAGAIN:
+ return thrd_nomem;
+ default:
+ return thrd_error;
+ }
}
diff --git a/fusl/src/thread/thrd_exit.c b/fusl/src/thread/thrd_exit.c
index b66bd99..e254a5f 100644
--- a/fusl/src/thread/thrd_exit.c
+++ b/fusl/src/thread/thrd_exit.c
@@ -1,9 +1,8 @@
#include "pthread_impl.h"
#include <threads.h>
-_Noreturn void __pthread_exit(void *);
+_Noreturn void __pthread_exit(void*);
-_Noreturn void thrd_exit(int result)
-{
- __pthread_exit((void*)(intptr_t)result);
+_Noreturn void thrd_exit(int result) {
+ __pthread_exit((void*)(intptr_t)result);
}
diff --git a/fusl/src/thread/thrd_join.c b/fusl/src/thread/thrd_join.c
index ac66789..d8270ea 100644
--- a/fusl/src/thread/thrd_join.c
+++ b/fusl/src/thread/thrd_join.c
@@ -3,10 +3,10 @@
int __pthread_join(thrd_t, void**);
-int thrd_join(thrd_t t, int *res)
-{
- void *pthread_res;
- __pthread_join(t, &pthread_res);
- if (res) *res = (int)(intptr_t)pthread_res;
- return thrd_success;
+int thrd_join(thrd_t t, int* res) {
+ void* pthread_res;
+ __pthread_join(t, &pthread_res);
+ if (res)
+ *res = (int)(intptr_t)pthread_res;
+ return thrd_success;
}
diff --git a/fusl/src/thread/thrd_sleep.c b/fusl/src/thread/thrd_sleep.c
index e8dfe40..f8eedfe 100644
--- a/fusl/src/thread/thrd_sleep.c
+++ b/fusl/src/thread/thrd_sleep.c
@@ -2,12 +2,14 @@
#include <errno.h>
#include "syscall.h"
-int thrd_sleep(const struct timespec *req, struct timespec *rem)
-{
- int ret = __syscall(SYS_nanosleep, req, rem);
- switch (ret) {
- case 0: return 0;
- case -EINTR: return -1; /* value specified by C11 */
- default: return -2;
- }
+int thrd_sleep(const struct timespec* req, struct timespec* rem) {
+ int ret = __syscall(SYS_nanosleep, req, rem);
+ switch (ret) {
+ case 0:
+ return 0;
+ case -EINTR:
+ return -1; /* value specified by C11 */
+ default:
+ return -2;
+ }
}
diff --git a/fusl/src/thread/thrd_yield.c b/fusl/src/thread/thrd_yield.c
index f7ad132..a4ecf43 100644
--- a/fusl/src/thread/thrd_yield.c
+++ b/fusl/src/thread/thrd_yield.c
@@ -1,7 +1,6 @@
#include <threads.h>
#include "syscall.h"
-void thrd_yield()
-{
- __syscall(SYS_sched_yield);
+void thrd_yield() {
+ __syscall(SYS_sched_yield);
}
diff --git a/fusl/src/thread/tss_create.c b/fusl/src/thread/tss_create.c
index 251d22b..8b4199f 100644
--- a/fusl/src/thread/tss_create.c
+++ b/fusl/src/thread/tss_create.c
@@ -1,11 +1,10 @@
#include <threads.h>
-int __pthread_key_create(tss_t *, void (*)(void *));
+int __pthread_key_create(tss_t*, void (*)(void*));
-int tss_create(tss_t *tss, tss_dtor_t dtor)
-{
- /* Different error returns are possible. C glues them together into
- * just failure notification. Can't be optimized to a tail call,
- * unless thrd_error equals EAGAIN. */
- return __pthread_key_create(tss, dtor) ? thrd_error : thrd_success;
+int tss_create(tss_t* tss, tss_dtor_t dtor) {
+ /* Different error returns are possible. C glues them together into
+ * just failure notification. Can't be optimized to a tail call,
+ * unless thrd_error equals EAGAIN. */
+ return __pthread_key_create(tss, dtor) ? thrd_error : thrd_success;
}
diff --git a/fusl/src/thread/tss_delete.c b/fusl/src/thread/tss_delete.c
index 35db103..9d5a928 100644
--- a/fusl/src/thread/tss_delete.c
+++ b/fusl/src/thread/tss_delete.c
@@ -2,7 +2,6 @@
int __pthread_key_delete(tss_t k);
-void tss_delete(tss_t key)
-{
- __pthread_key_delete(key);
+void tss_delete(tss_t key) {
+ __pthread_key_delete(key);
}
diff --git a/fusl/src/thread/tss_set.c b/fusl/src/thread/tss_set.c
index 70c4fb7..5a9532a 100644
--- a/fusl/src/thread/tss_set.c
+++ b/fusl/src/thread/tss_set.c
@@ -1,13 +1,12 @@
#include "pthread_impl.h"
#include <threads.h>
-int tss_set(tss_t k, void *x)
-{
- struct pthread *self = __pthread_self();
- /* Avoid unnecessary COW */
- if (self->tsd[k] != x) {
- self->tsd[k] = x;
- self->tsd_used = 1;
- }
- return thrd_success;
+int tss_set(tss_t k, void* x) {
+ struct pthread* self = __pthread_self();
+ /* Avoid unnecessary COW */
+ if (self->tsd[k] != x) {
+ self->tsd[k] = x;
+ self->tsd_used = 1;
+ }
+ return thrd_success;
}
diff --git a/fusl/src/thread/vmlock.c b/fusl/src/thread/vmlock.c
index 75f3cb7..f05fbe0 100644
--- a/fusl/src/thread/vmlock.c
+++ b/fusl/src/thread/vmlock.c
@@ -2,20 +2,17 @@
static volatile int vmlock[2];
-void __vm_wait()
-{
- int tmp;
- while ((tmp=vmlock[0]))
- __wait(vmlock, vmlock+1, tmp, 1);
+void __vm_wait() {
+ int tmp;
+ while ((tmp = vmlock[0]))
+ __wait(vmlock, vmlock + 1, tmp, 1);
}
-void __vm_lock()
-{
- a_inc(vmlock);
+void __vm_lock() {
+ a_inc(vmlock);
}
-void __vm_unlock()
-{
- if (a_fetch_add(vmlock, -1)==1 && vmlock[1])
- __wake(vmlock, -1, 1);
+void __vm_unlock() {
+ if (a_fetch_add(vmlock, -1) == 1 && vmlock[1])
+ __wake(vmlock, -1, 1);
}
diff --git a/fusl/src/thread/x32/syscall_cp_fixup.c b/fusl/src/thread/x32/syscall_cp_fixup.c
index b1f3a38..2688856 100644
--- a/fusl/src/thread/x32/syscall_cp_fixup.c
+++ b/fusl/src/thread/x32/syscall_cp_fixup.c
@@ -1,38 +1,61 @@
#include <sys/syscall.h>
-__attribute__((__visibility__("hidden")))
-long __syscall_cp_internal(volatile void*, long long, long long, long long, long long,
- long long, long long, long long);
+__attribute__((__visibility__("hidden"))) long __syscall_cp_internal(
+ volatile void*,
+ long long,
+ long long,
+ long long,
+ long long,
+ long long,
+ long long,
+ long long);
-struct __timespec { long long tv_sec; long tv_nsec; };
-struct __timespec_kernel { long long tv_sec; long long tv_nsec; };
+struct __timespec {
+ long long tv_sec;
+ long tv_nsec;
+};
+struct __timespec_kernel {
+ long long tv_sec;
+ long long tv_nsec;
+};
#define __tsc(X) ((struct __timespec*)(unsigned long)(X))
-#define __fixup(X) do { if(X) { \
- ts->tv_sec = __tsc(X)->tv_sec; \
- ts->tv_nsec = __tsc(X)->tv_nsec; \
- (X) = (unsigned long)ts; } } while(0)
+#define __fixup(X) \
+ do { \
+ if (X) { \
+ ts->tv_sec = __tsc(X)->tv_sec; \
+ ts->tv_nsec = __tsc(X)->tv_nsec; \
+ (X) = (unsigned long)ts; \
+ } \
+ } while (0)
-__attribute__((__visibility__("hidden")))
-long __syscall_cp_asm (volatile void * foo, long long n, long long a1, long long a2, long long a3,
- long long a4, long long a5, long long a6)
-{
- struct __timespec_kernel ts[1];
- switch (n) {
- case SYS_mq_timedsend: case SYS_mq_timedreceive: case SYS_pselect6:
- __fixup(a5);
- break;
- case SYS_futex:
- if((a2 & (~128 /* FUTEX_PRIVATE_FLAG */)) == 0 /* FUTEX_WAIT */)
- __fixup(a4);
- break;
- case SYS_clock_nanosleep:
- case SYS_rt_sigtimedwait: case SYS_ppoll:
- __fixup(a3);
- break;
- case SYS_nanosleep:
- __fixup(a1);
- break;
- }
- return __syscall_cp_internal(foo, n, a1, a2, a3, a4, a5, a6);
+__attribute__((__visibility__("hidden"))) long __syscall_cp_asm(
+ volatile void* foo,
+ long long n,
+ long long a1,
+ long long a2,
+ long long a3,
+ long long a4,
+ long long a5,
+ long long a6) {
+ struct __timespec_kernel ts[1];
+ switch (n) {
+ case SYS_mq_timedsend:
+ case SYS_mq_timedreceive:
+ case SYS_pselect6:
+ __fixup(a5);
+ break;
+ case SYS_futex:
+ if ((a2 & (~128 /* FUTEX_PRIVATE_FLAG */)) == 0 /* FUTEX_WAIT */)
+ __fixup(a4);
+ break;
+ case SYS_clock_nanosleep:
+ case SYS_rt_sigtimedwait:
+ case SYS_ppoll:
+ __fixup(a3);
+ break;
+ case SYS_nanosleep:
+ __fixup(a1);
+ break;
+ }
+ return __syscall_cp_internal(foo, n, a1, a2, a3, a4, a5, a6);
}
-