Implement poll() on top of select()
[akaros.git] / user / pthread / pthread.c
index 508cf8f..9a6691e 100644 (file)
@@ -1,27 +1,30 @@
 #include <ros/trapframe.h>
-#include <pthread.h>
-#include <vcore.h>
-#include <mcs.h>
+#include "pthread.h"
+#include <parlib/vcore.h>
+#include <parlib/mcs.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
+#include <parlib/assert.h>
 #include <stdio.h>
 #include <errno.h>
-#include <parlib.h>
+#include <parlib/parlib.h>
 #include <ros/event.h>
-#include <arch/atomic.h>
-#include <arch/arch.h>
+#include <parlib/arch/atomic.h>
+#include <parlib/arch/arch.h>
 #include <sys/queue.h>
 #include <sys/mman.h>
-#include <event.h>
-#include <ucq.h>
+#include <parlib/event.h>
+#include <parlib/ucq.h>
+#include <parlib/signal.h>
+#include <parlib/arch/trap.h>
 
 struct pthread_queue ready_queue = TAILQ_HEAD_INITIALIZER(ready_queue);
 struct pthread_queue active_queue = TAILQ_HEAD_INITIALIZER(active_queue);
 struct mcs_pdr_lock queue_lock;
 int threads_ready = 0;
 int threads_active = 0;
-bool can_adjust_vcores = TRUE;
+atomic_t threads_total;
+bool need_tls = TRUE;
 
 /* Array of per-vcore structs to manage waiting on syscalls and handling
  * overflow.  Init'd in pth_init(). */
@@ -30,43 +33,46 @@ struct sysc_mgmt *sysc_mgmt = 0;
 /* Helper / local functions */
 static int get_next_pid(void);
 static inline void spin_to_sleep(unsigned int spins, unsigned int *spun);
+static inline void pthread_exit_no_cleanup(void *ret);
 
 /* Pthread 2LS operations */
-void pth_sched_entry(void);
-void pth_thread_runnable(struct uthread *uthread);
-void pth_thread_paused(struct uthread *uthread);
-void pth_thread_blockon_sysc(struct uthread *uthread, void *sysc);
-void pth_thread_has_blocked(struct uthread *uthread, int flags);
-void pth_preempt_pending(void);
-void pth_spawn_thread(uintptr_t pc_start, void *data);
+static void pth_sched_entry(void);
+static void pth_thread_runnable(struct uthread *uthread);
+static void pth_thread_paused(struct uthread *uthread);
+static void pth_thread_blockon_sysc(struct uthread *uthread, void *sysc);
+static void pth_thread_has_blocked(struct uthread *uthread, int flags);
+static void pth_thread_refl_fault(struct uthread *uth,
+                                  struct user_context *ctx);
 
 /* Event Handlers */
-static void pth_handle_syscall(struct event_msg *ev_msg, unsigned int ev_type);
+static void pth_handle_syscall(struct event_msg *ev_msg, unsigned int ev_type,
+                               void *data);
 
 struct schedule_ops pthread_sched_ops = {
-       pth_sched_entry,
-       pth_thread_runnable,
-       pth_thread_paused,
-       pth_thread_blockon_sysc,
-       pth_thread_has_blocked,
-       0, /* pth_preempt_pending, */
-       0, /* pth_spawn_thread, */
+       .sched_entry = pth_sched_entry,
+       .thread_runnable = pth_thread_runnable,
+       .thread_paused = pth_thread_paused,
+       .thread_blockon_sysc = pth_thread_blockon_sysc,
+       .thread_has_blocked = pth_thread_has_blocked,
+       .thread_refl_fault = pth_thread_refl_fault,
 };
 
-/* Publish our sched_ops, overriding the weak defaults */
-struct schedule_ops *sched_ops = &pthread_sched_ops;
-
 /* Static helpers */
 static void __pthread_free_stack(struct pthread_tcb *pt);
 static int __pthread_allocate_stack(struct pthread_tcb *pt);
+static void __pth_yield_cb(struct uthread *uthread, void *junk);
 
 /* Called from vcore entry.  Options usually include restarting whoever was
  * running there before or running a new thread.  Events are handled out of
  * event.c (table of function pointers, stuff like that). */
-void __attribute__((noreturn)) pth_sched_entry(void)
+static void __attribute__((noreturn)) pth_sched_entry(void)
 {
        uint32_t vcoreid = vcore_id();
        if (current_uthread) {
+               /* Prep the pthread to run any pending posix signal handlers registered
+         * via pthread_kill once it is restored. */
+               uthread_prep_pending_signals(current_uthread);
+               /* Run the thread itself */
                run_current_uthread();
                assert(0);
        }
@@ -81,8 +87,10 @@ void __attribute__((noreturn)) pth_sched_entry(void)
                mcs_pdr_lock(&queue_lock);
                new_thread = TAILQ_FIRST(&ready_queue);
                if (new_thread) {
-                       TAILQ_REMOVE(&ready_queue, new_thread, next);
-                       TAILQ_INSERT_TAIL(&active_queue, new_thread, next);
+                       TAILQ_REMOVE(&ready_queue, new_thread, tq_next);
+                       assert(new_thread->state == PTH_RUNNABLE);
+                       new_thread->state = PTH_RUNNING;
+                       TAILQ_INSERT_TAIL(&active_queue, new_thread, tq_next);
                        threads_active++;
                        threads_ready--;
                        mcs_pdr_unlock(&queue_lock);
@@ -98,11 +106,13 @@ void __attribute__((noreturn)) pth_sched_entry(void)
                /* no new thread, try to yield */
                printd("[P] No threads, vcore %d is yielding\n", vcore_id());
                /* TODO: you can imagine having something smarter here, like spin for a
-                * bit before yielding (or not at all if you want to be greedy). */
-               if (can_adjust_vcores)
-                       vcore_yield(FALSE);
+                * bit before yielding. */
+               vcore_yield(FALSE);
        } while (1);
-       assert(new_thread->state == PTH_RUNNABLE);
+       /* Prep the pthread to run any pending posix signal handlers registered
+     * via pthread_kill once it is restored. */
+       uthread_prep_pending_signals((struct uthread*)new_thread);
+       /* Run the thread itself */
        run_uthread((struct uthread*)new_thread);
        assert(0);
 }
@@ -111,12 +121,12 @@ void __attribute__((noreturn)) pth_sched_entry(void)
 static void __pthread_run(void)
 {
        struct pthread_tcb *me = pthread_self();
-       pthread_exit(me->start_routine(me->arg));
+       pthread_exit_no_cleanup(me->start_routine(me->arg));
 }
 
 /* GIANT WARNING: if you make any changes to this, also change the broadcast
  * wakeups (cond var, barrier, etc) */
-void pth_thread_runnable(struct uthread *uthread)
+static void pth_thread_runnable(struct uthread *uthread)
 {
        struct pthread_tcb *pthread = (struct pthread_tcb*)uthread;
        /* At this point, the 2LS can see why the thread blocked and was woken up in
@@ -134,20 +144,19 @@ void pth_thread_runnable(struct uthread *uthread)
                        /* can do whatever for each of these cases */
                        break;
                default:
-                       printf("Odd state %d for pthread %08p\n", pthread->state, pthread);
+                       panic("Odd state %d for pthread %08p\n", pthread->state, pthread);
        }
        pthread->state = PTH_RUNNABLE;
        /* Insert the newly created thread into the ready queue of threads.
         * It will be removed from this queue later when vcore_entry() comes up */
        mcs_pdr_lock(&queue_lock);
        /* Again, GIANT WARNING: if you change this, change batch wakeup code */
-       TAILQ_INSERT_TAIL(&ready_queue, pthread, next);
+       TAILQ_INSERT_TAIL(&ready_queue, pthread, tq_next);
        threads_ready++;
        mcs_pdr_unlock(&queue_lock);
        /* Smarter schedulers should look at the num_vcores() and how much work is
         * going on to make a decision about how many vcores to request. */
-       if (can_adjust_vcores)
-               vcore_request(threads_ready);
+       vcore_request_more(threads_ready);
 }
 
 /* For some reason not under its control, the uthread stopped running (compared
@@ -160,16 +169,11 @@ void pth_thread_runnable(struct uthread *uthread)
  * vcore that is losing the uthread.  If that vcore is running, it'll be in a
  * preempt-event handling loop (not in your 2LS code).  If this is a big
  * problem, I'll change it. */
-void pth_thread_paused(struct uthread *uthread)
+static void pth_thread_paused(struct uthread *uthread)
 {
        struct pthread_tcb *pthread = (struct pthread_tcb*)uthread;
-       /* Remove from the active list.  Note that I don't particularly care about
-        * the active list.  We keep it around because it causes bugs and keeps us
-        * honest.  After all, some 2LS may want an active list */
-       mcs_pdr_lock(&queue_lock);
-       threads_active--;
-       TAILQ_REMOVE(&active_queue, pthread, next);
-       mcs_pdr_unlock(&queue_lock);
+
+       __pthread_generic_yield(pthread);
        /* communicate to pth_thread_runnable */
        pthread->state = PTH_BLK_PAUSED;
        /* At this point, you could do something clever, like put it at the front of
@@ -193,7 +197,8 @@ static void restart_thread(struct syscall *sysc)
 
 /* This handler is usually run in vcore context, though I can imagine it being
  * called by a uthread in some other threading library. */
-static void pth_handle_syscall(struct event_msg *ev_msg, unsigned int ev_type)
+static void pth_handle_syscall(struct event_msg *ev_msg, unsigned int ev_type,
+                               void *data)
 {
        struct syscall *sysc;
        assert(in_vcore_context());
@@ -217,19 +222,15 @@ static void pth_handle_syscall(struct event_msg *ev_msg, unsigned int ev_type)
  * and is trying to block on sysc.  Need to put it somewhere were we can wake it
  * up when the sysc is done.  For now, we'll have the kernel send us an event
  * when the syscall is done. */
-void pth_thread_blockon_sysc(struct uthread *uthread, void *syscall)
+static void pth_thread_blockon_sysc(struct uthread *uthread, void *syscall)
 {
        struct syscall *sysc = (struct syscall*)syscall;
        int old_flags;
-       bool need_to_restart = FALSE;
        uint32_t vcoreid = vcore_id();
-       /* rip from the active queue */
        struct pthread_tcb *pthread = (struct pthread_tcb*)uthread;
+
+       __pthread_generic_yield(pthread);
        pthread->state = PTH_BLK_SYSC;
-       mcs_pdr_lock(&queue_lock);
-       threads_active--;
-       TAILQ_REMOVE(&active_queue, pthread, next);
-       mcs_pdr_unlock(&queue_lock);
        /* Set things up so we can wake this thread up later */
        sysc->u_data = uthread;
        /* Register our vcore's syscall ev_q to hear about this syscall. */
@@ -241,9 +242,11 @@ void pth_thread_blockon_sysc(struct uthread *uthread, void *syscall)
        /* GIANT WARNING: do not touch the thread after this point. */
 }
 
-void pth_thread_has_blocked(struct uthread *uthread, int flags)
+static void pth_thread_has_blocked(struct uthread *uthread, int flags)
 {
        struct pthread_tcb *pthread = (struct pthread_tcb*)uthread;
+
+       __pthread_generic_yield(pthread);
        /* could imagine doing something with the flags.  For now, we just treat all
         * externally blocked reasons as 'MUTEX'.  Whatever we do here, we are
         * mostly communicating to our future selves in pth_thread_runnable(), which
@@ -254,30 +257,99 @@ void pth_thread_has_blocked(struct uthread *uthread, int flags)
                printf("For great justice!\n");
 }
 
-void pth_preempt_pending(void)
+static void __signal_and_restart(struct uthread *uthread,
+                                 int signo, int code, void *addr)
+{
+       uthread_prep_signal_from_fault(uthread, signo, code, addr);
+       pth_thread_runnable(uthread);
+}
+
+static void handle_div_by_zero(struct uthread *uthread, unsigned int err,
+                               unsigned long aux)
+{
+       __signal_and_restart(uthread, SIGFPE, FPE_INTDIV, (void*)aux);
+}
+
+static void handle_gp_fault(struct uthread *uthread, unsigned int err,
+                            unsigned long aux)
+{
+       __signal_and_restart(uthread, SIGSEGV, SEGV_ACCERR, (void*)aux);
+}
+
+static void handle_page_fault(struct uthread *uthread, unsigned int err,
+                              unsigned long aux)
 {
+       struct pthread_tcb *pthread = (struct pthread_tcb*)uthread;
+       if (!(err & PF_VMR_BACKED)) {
+               __signal_and_restart(uthread, SIGSEGV, SEGV_MAPERR, (void*)aux);
+       } else {
+               syscall_async(&uthread->local_sysc, SYS_populate_va, aux, 1);
+               __block_uthread_on_async_sysc(uthread);
+       }
+}
+
+static void pth_thread_refl_hw_fault(struct uthread *uthread,
+                                     unsigned int trap_nr,
+                                     unsigned int err, unsigned long aux)
+{
+       struct pthread_tcb *pthread = (struct pthread_tcb*)uthread;
+
+       __pthread_generic_yield(pthread);
+       pthread->state = PTH_BLK_SYSC;
+
+       switch (trap_nr) {
+       case HW_TRAP_DIV_ZERO:
+               handle_div_by_zero(uthread, err, aux);
+               break;
+       case HW_TRAP_GP_FAULT:
+               handle_gp_fault(uthread, err, aux);
+               break;
+       case HW_TRAP_PAGE_FAULT:
+               handle_page_fault(uthread, err, aux);
+               break;
+       default:
+               printf("Pthread has unhandled fault: %d, err: %d, aux: %p\n",
+                      trap_nr, err, aux);
+               /* Note that uthread.c already copied out our ctx into the uth
+                * struct */
+               print_user_context(&uthread->u_ctx);
+               printf("Turn on printx to spew unhandled, malignant trap info\n");
+               exit(-1);
+       }
 }
 
-void pth_spawn_thread(uintptr_t pc_start, void *data)
+static void pth_thread_refl_fault(struct uthread *uth,
+                                  struct user_context *ctx)
 {
+       switch (ctx->type) {
+       case ROS_HW_CTX:
+               pth_thread_refl_hw_fault(uth, __arch_refl_get_nr(ctx),
+                                        __arch_refl_get_err(ctx),
+                                        __arch_refl_get_aux(ctx));
+               break;
+       default:
+               assert(0);
+       }
 }
 
 /* Akaros pthread extensions / hacks */
 
-/* Tells the pthread 2LS to not change the number of vcores.  This means it will
- * neither request vcores nor yield vcores.  Only used for testing. */
-void pthread_can_vcore_request(bool can)
+void pthread_need_tls(bool need)
 {
-       /* checked when we would request or yield */
-       can_adjust_vcores = can;
+       need_tls = need;
 }
 
 /* Pthread interface stuff and helpers */
 
 int pthread_attr_init(pthread_attr_t *a)
 {
+       a->stackaddr = 0;
        a->stacksize = PTHREAD_STACK_SIZE;
        a->detachstate = PTHREAD_CREATE_JOINABLE;
+       /* priority and policy should be set by anyone changing inherit. */
+       a->sched_priority = 0;
+       a->sched_policy = 0;
+       a->sched_inherit = PTHREAD_INHERIT_SCHED;
        return 0;
 }
 
@@ -294,13 +366,17 @@ static void __pthread_free_stack(struct pthread_tcb *pt)
 
 static int __pthread_allocate_stack(struct pthread_tcb *pt)
 {
+       int force_a_page_fault;
        assert(pt->stacksize);
        void* stackbot = mmap(0, pt->stacksize,
                              PROT_READ|PROT_WRITE|PROT_EXEC,
-                             MAP_POPULATE|MAP_ANONYMOUS, -1, 0);
+                             MAP_ANONYMOUS, -1, 0);
        if (stackbot == MAP_FAILED)
                return -1; // errno set by mmap
        pt->stacktop = stackbot + pt->stacksize;
+       /* Want the top of the stack populated, but not the rest of the stack;
+        * that'll grow on demand (up to pt->stacksize) */
+       force_a_page_fault = ACCESS_ONCE(*(int*)(pt->stacktop - sizeof(int)));
        return 0;
 }
 
@@ -316,24 +392,56 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
        attr->stacksize = stacksize;
        return 0;
 }
+
 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
 {
        *stacksize = attr->stacksize;
        return 0;
 }
 
-/* Do whatever init you want.  At some point call uthread_lib_init() and pass it
+int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
+{
+       attr->guardsize = guardsize;
+       return 0;
+}
+
+int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize)
+{
+       *guardsize = attr->guardsize;
+       return 0;
+}
+
+int pthread_attr_getstack(const pthread_attr_t *__restrict __attr,
+                                                  void **__stackaddr, size_t *__stacksize)
+{
+       *__stackaddr = __attr->stackaddr;
+       *__stacksize = __attr->stacksize;
+       return 0;
+}
+
+int pthread_getattr_np(pthread_t __th, pthread_attr_t *__attr)
+{
+       __attr->stackaddr = __th->stacktop - __th->stacksize;
+       __attr->stacksize = __th->stacksize;
+       if (__th->detached)
+               __attr->detachstate = PTHREAD_CREATE_DETACHED;
+       else
+               __attr->detachstate = PTHREAD_CREATE_JOINABLE;
+       return 0;
+}
+
+/* Do whatever init you want.  At some point call uthread_2ls_init() and pass it
  * a uthread representing thread0 (int main()) */
-void pthread_lib_init(void)
+void __attribute__((constructor)) pthread_lib_init(void)
 {
        uintptr_t mmap_block;
        struct pthread_tcb *t;
        int ret;
-       /* Some testing code might call this more than once (once for a slimmed down
-        * pth 2LS, and another from pthread_create().  Also, this is racy, but the
-        * first time through we are an SCP. */
+
+       /* Only run once, but make sure that uthread_lib_init() has run already. */
        init_once_racy(return);
-       assert(!in_multi_mode());
+       uthread_lib_init();
+
        mcs_pdr_init(&queue_lock);
        /* Create a pthread_tcb for the main thread */
        ret = posix_memalign((void**)&t, __alignof__(struct pthread_tcb),
@@ -346,11 +454,15 @@ void pthread_lib_init(void)
        t->detached = TRUE;
        t->state = PTH_RUNNING;
        t->joiner = 0;
+       /* implies that sigmasks are longs, which they are. */
        assert(t->id == 0);
+       t->sched_policy = SCHED_FIFO;
+       t->sched_priority = 0;
+       SLIST_INIT(&t->cr_stack);
        /* Put the new pthread (thread0) on the active queue */
        mcs_pdr_lock(&queue_lock);
        threads_active++;
-       TAILQ_INSERT_TAIL(&active_queue, t, next);
+       TAILQ_INSERT_TAIL(&active_queue, t, tq_next);
        mcs_pdr_unlock(&queue_lock);
        /* Tell the kernel where and how we want to receive events.  This is just an
         * example of what to do to have a notification turned on.  We're turning on
@@ -361,7 +473,7 @@ void pthread_lib_init(void)
        enable_kevent(EV_USER_IPI, 0, EVENT_IPI | EVENT_VCORE_PRIVATE);
 
        /* Handle syscall events. */
-       ev_handlers[EV_SYSCALL] = pth_handle_syscall;
+       register_ev_handler(EV_SYSCALL, pth_handle_syscall, 0);
        /* Set up the per-vcore structs to track outstanding syscalls */
        sysc_mgmt = malloc(sizeof(struct sysc_mgmt) * max_vcores());
        assert(sysc_mgmt);
@@ -375,10 +487,12 @@ void pthread_lib_init(void)
         * max_vcores()). */
        for (int i = 0; i < max_vcores(); i++) {
                /* Each vcore needs to point to a non-VCPD ev_q */
-               sysc_mgmt[i].ev_q = get_big_event_q_raw();
-               sysc_mgmt[i].ev_q->ev_flags = EVENT_IPI | EVENT_INDIR | EVENT_FALLBACK;
+               sysc_mgmt[i].ev_q = get_eventq_raw();
+               sysc_mgmt[i].ev_q->ev_flags = EVENT_IPI | EVENT_INDIR |
+                                             EVENT_SPAM_INDIR | EVENT_WAKEUP;
                sysc_mgmt[i].ev_q->ev_vcore = i;
-               ucq_init_raw(&sysc_mgmt[i].ev_q->ev_mbox->ev_msgs, 
+               sysc_mgmt[i].ev_q->ev_mbox->type = EV_MBOX_UCQ;
+               ucq_init_raw(&sysc_mgmt[i].ev_q->ev_mbox->ucq,
                             mmap_block + (2 * i    ) * PGSIZE, 
                             mmap_block + (2 * i + 1) * PGSIZE); 
        }
@@ -393,31 +507,49 @@ void pthread_lib_init(void)
        assert(sysc_mbox);
        assert(two_pages);
        memset(sysc_mbox, 0, sizeof(struct event_mbox));
-       ucq_init_raw(&sysc_mbox->ev_msgs, two_pages, two_pages + PGSIZE);
+       sysc_mbox->type = EV_MBOX_UCQ;
+       ucq_init_raw(&sysc_mbox->ucq, two_pages, two_pages + PGSIZE);
        for (int i = 0; i < max_vcores(); i++) {
-               sysc_mgmt[i].ev_q = get_event_q();
-               sysc_mgmt[i].ev_q->ev_flags = EVENT_IPI | EVENT_INDIR | EVENT_FALLBACK;
+               sysc_mgmt[i].ev_q = get_eventq_slim();
+               sysc_mgmt[i].ev_q->ev_flags = EVENT_IPI | EVENT_INDIR |
+                                             EVENT_SPAM_INDIR | EVENT_WAKEUP;
                sysc_mgmt[i].ev_q->ev_vcore = i;
                sysc_mgmt[i].ev_q->ev_mbox = sysc_mbox;
        }
 #endif
-       /* Initialize the uthread code (we're in _M mode after this).  Doing this
-        * last so that all the event stuff is ready when we're in _M mode.  Not a
-        * big deal one way or the other.  Note that vcore_init() probably has
-        * happened, but don't rely on this.  Careful if your 2LS somehow wants to
-        * have its init stuff use things like vcore stacks or TLSs, we'll need to
-        * change this. */
-       uthread_lib_init((struct uthread*)t);
+       /* Sched ops is set by 2ls_init */
+       uthread_2ls_init((struct uthread*)t, &pthread_sched_ops);
+       atomic_init(&threads_total, 1);                 /* one for thread0 */
 }
 
-int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
-                   void *(*start_routine)(void *), void *arg)
+/* Make sure our scheduler runs inside an MCP rather than an SCP. */
+void pthread_mcp_init()
 {
-       run_once(pthread_lib_init());
-       /* Create the actual thread */
+       /* Prevent this from happening more than once. */
+       init_once_racy(return);
+
+       uthread_mcp_init();
+       /* From here forward we are an MCP running on vcore 0. Could consider doing
+        * other pthread specific initialization based on knowing we are an mcp
+        * after this point. */
+}
+
+int __pthread_create(pthread_t *thread, const pthread_attr_t *attr,
+                     void *(*start_routine)(void *), void *arg)
+{
+       struct uth_thread_attr uth_attr = {0};
+       struct pthread_tcb *parent;
        struct pthread_tcb *pthread;
-       int ret = posix_memalign((void**)&pthread, __alignof__(struct pthread_tcb),
-                                sizeof(struct pthread_tcb));
+       int ret;
+
+       /* For now, unconditionally become an mcp when creating a pthread (if not
+        * one already). This may change in the future once we support 2LSs in an
+        * SCP. */
+       pthread_mcp_init();
+
+       parent = (struct pthread_tcb*)current_uthread;
+       ret = posix_memalign((void**)&pthread, __alignof__(struct pthread_tcb),
+                            sizeof(struct pthread_tcb));
        assert(!ret);
        memset(pthread, 0, sizeof(struct pthread_tcb)); /* aggressively 0 for bugs*/
        pthread->stacksize = PTHREAD_STACK_SIZE;        /* default */
@@ -425,12 +557,20 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
        pthread->id = get_next_pid();
        pthread->detached = FALSE;                              /* default */
        pthread->joiner = 0;
+       /* Might override these later, based on attr && EXPLICIT_SCHED */
+       pthread->sched_policy = parent->sched_policy;
+       pthread->sched_priority = parent->sched_priority;
+       SLIST_INIT(&pthread->cr_stack);
        /* Respect the attributes */
        if (attr) {
                if (attr->stacksize)                                    /* don't set a 0 stacksize */
                        pthread->stacksize = attr->stacksize;
                if (attr->detachstate == PTHREAD_CREATE_DETACHED)
                        pthread->detached = TRUE;
+               if (attr->sched_inherit == PTHREAD_EXPLICIT_SCHED) {
+                       pthread->sched_policy = attr->sched_policy;
+                       pthread->sched_priority = attr->sched_priority;
+               }
        }
        /* allocate a stack */
        if (__pthread_allocate_stack(pthread))
@@ -443,9 +583,19 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
        pthread->start_routine = start_routine;
        pthread->arg = arg;
        /* Initialize the uthread */
-       uthread_init((struct uthread*)pthread);
-       pth_thread_runnable((struct uthread*)pthread);
+       if (need_tls)
+               uth_attr.want_tls = TRUE;
+       uthread_init((struct uthread*)pthread, &uth_attr);
        *thread = pthread;
+       atomic_inc(&threads_total);
+       return 0;
+}
+
+int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
+                   void *(*start_routine)(void *), void *arg)
+{
+       if (!__pthread_create(thread, attr, start_routine, arg))
+               pth_thread_runnable((struct uthread*)*thread);
        return 0;
 }
 
@@ -456,7 +606,7 @@ void __pthread_generic_yield(struct pthread_tcb *pthread)
 {
        mcs_pdr_lock(&queue_lock);
        threads_active--;
-       TAILQ_REMOVE(&active_queue, pthread, next);
+       TAILQ_REMOVE(&active_queue, pthread, tq_next);
        mcs_pdr_unlock(&queue_lock);
 }
 
@@ -539,16 +689,31 @@ static void __pth_exit_cb(struct uthread *uthread, void *junk)
                        pth_thread_runnable((struct uthread*)temp_pth);
                }
        }
+       /* If we were the last pthread, we exit for the whole process.  Keep in mind
+        * that thread0 is counted in this, so this will only happen if that thread
+        * calls pthread_exit(). */
+       if ((atomic_fetch_and_add(&threads_total, -1) == 1))
+               exit(0);
 }
 
-void pthread_exit(void *ret)
+static inline void pthread_exit_no_cleanup(void *ret)
 {
        struct pthread_tcb *pthread = pthread_self();
        pthread->retval = ret;
        destroy_dtls();
+       while (SLIST_FIRST(&pthread->cr_stack))
+               pthread_cleanup_pop(FALSE);
        uthread_yield(FALSE, __pth_exit_cb, 0);
 }
 
+void pthread_exit(void *ret)
+{
+       struct pthread_tcb *pthread = pthread_self();
+       while (SLIST_FIRST(&pthread->cr_stack))
+               pthread_cleanup_pop(TRUE);
+       pthread_exit_no_cleanup(ret);
+}
+
 /* Callback/bottom half of yield.  For those writing these pth callbacks, the
  * minimum is call generic, set state (communicate with runnable), then do
  * something that causes it to be runnable in the future (or right now). */
@@ -568,6 +733,34 @@ int pthread_yield(void)
        return 0;
 }
 
+int pthread_cancel(pthread_t __th)
+{
+       fprintf(stderr, "Unsupported %s!", __FUNCTION__);
+       abort();
+       return -1;
+}
+
+void pthread_cleanup_push(void (*routine)(void *), void *arg)
+{
+       struct pthread_tcb *p = pthread_self();
+       struct pthread_cleanup_routine *r = malloc(sizeof(*r));
+       r->routine = routine;
+       r->arg = arg;
+       SLIST_INSERT_HEAD(&p->cr_stack, r, cr_next);
+}
+
+void pthread_cleanup_pop(int execute)
+{
+       struct pthread_tcb *p = pthread_self();
+       struct pthread_cleanup_routine *r = SLIST_FIRST(&p->cr_stack);
+       if (r) {
+               SLIST_REMOVE_HEAD(&p->cr_stack, cr_next);
+               if (execute)
+                       r->routine(r->arg);
+               free(r);
+       }
+}
+
 int pthread_mutexattr_init(pthread_mutexattr_t* attr)
 {
   attr->type = PTHREAD_MUTEX_DEFAULT;
@@ -669,7 +862,7 @@ int pthread_mutex_destroy(pthread_mutex_t* m)
 
 int pthread_cond_init(pthread_cond_t *c, const pthread_condattr_t *a)
 {
-       TAILQ_INIT(&c->waiters);
+       SLIST_INIT(&c->waiters);
        spin_pdr_init(&c->spdr_lock);
        if (a) {
                c->attr_pshared = a->pshared;
@@ -686,29 +879,40 @@ int pthread_cond_destroy(pthread_cond_t *c)
        return 0;
 }
 
-int pthread_cond_broadcast(pthread_cond_t *c)
+static void swap_slists(struct pthread_list *a, struct pthread_list *b)
+{
+       struct pthread_list temp;
+       temp = *a;
+       *a = *b;
+       *b = temp;
+}
+
+static void wake_slist(struct pthread_list *to_wake)
 {
        unsigned int nr_woken = 0;      /* assuming less than 4 bil threads */
-       struct pthread_queue restartees = TAILQ_HEAD_INITIALIZER(restartees);
-       struct pthread_tcb *pthread_i;
-       spin_pdr_lock(&c->spdr_lock);
-       /* moves all items from waiters onto the end of restartees */
-       TAILQ_CONCAT(&restartees, &c->waiters, next);
-       spin_pdr_unlock(&c->spdr_lock);
+       struct pthread_tcb *pthread_i, *pth_temp;
+       /* Amortize the lock grabbing over all restartees */
+       mcs_pdr_lock(&queue_lock);
        /* Do the work of pth_thread_runnable().  We're in uth context here, but I
         * think it's okay.  When we need to (when locking) we drop into VC ctx, as
         * far as the kernel and other cores are concerned. */
-       TAILQ_FOREACH(pthread_i, &restartees, next) {
+       SLIST_FOREACH_SAFE(pthread_i, to_wake, sl_next, pth_temp) {
                pthread_i->state = PTH_RUNNABLE;
                nr_woken++;
+               TAILQ_INSERT_TAIL(&ready_queue, pthread_i, tq_next);
        }
-       /* Amortize the lock grabbing over all restartees */
-       mcs_pdr_lock(&queue_lock);
        threads_ready += nr_woken;
-       TAILQ_CONCAT(&ready_queue, &restartees, next);
        mcs_pdr_unlock(&queue_lock);
-       if (can_adjust_vcores)
-               vcore_request(threads_ready);
+       vcore_request_more(threads_ready);
+}
+
+int pthread_cond_broadcast(pthread_cond_t *c)
+{
+       struct pthread_list restartees = SLIST_HEAD_INITIALIZER(restartees);
+       spin_pdr_lock(&c->spdr_lock);
+       swap_slists(&restartees, &c->waiters);
+       spin_pdr_unlock(&c->spdr_lock);
+       wake_slist(&restartees);
        return 0;
 }
 
@@ -718,12 +922,12 @@ int pthread_cond_signal(pthread_cond_t *c)
 {
        struct pthread_tcb *pthread;
        spin_pdr_lock(&c->spdr_lock);
-       pthread = TAILQ_FIRST(&c->waiters);
+       pthread = SLIST_FIRST(&c->waiters);
        if (!pthread) {
                spin_pdr_unlock(&c->spdr_lock);
                return 0;
        }
-       TAILQ_REMOVE(&c->waiters, pthread, next);
+       SLIST_REMOVE_HEAD(&c->waiters, sl_next);
        spin_pdr_unlock(&c->spdr_lock);
        pth_thread_runnable((struct uthread*)pthread);
        return 0;
@@ -747,7 +951,7 @@ static void __pth_wait_cb(struct uthread *uthread, void *junk)
        __pthread_generic_yield(pthread);
        pthread->state = PTH_BLK_MUTEX;
        spin_pdr_lock(&c->spdr_lock);
-       TAILQ_INSERT_TAIL(&c->waiters, pthread, next);
+       SLIST_INSERT_HEAD(&c->waiters, pthread, sl_next);
        spin_pdr_unlock(&c->spdr_lock);
        pthread_mutex_unlock(m);
 }
@@ -826,7 +1030,7 @@ int pthread_barrier_init(pthread_barrier_t *b,
        b->sense = 0;
        atomic_set(&b->count, count);
        spin_pdr_init(&b->lock);
-       TAILQ_INIT(&b->waiters);
+       SLIST_INIT(&b->waiters);
        b->nr_waiters = 0;
        return 0;
 }
@@ -856,7 +1060,7 @@ static void __pth_barrier_cb(struct uthread *uthread, void *junk)
        }
        /* otherwise, we sleep */
        pthread->state = PTH_BLK_MUTEX; /* TODO: consider ignoring this */
-       TAILQ_INSERT_TAIL(&b->waiters, pthread, next);
+       SLIST_INSERT_HEAD(&b->waiters, pthread, sl_next);
        b->nr_waiters++;
        spin_pdr_unlock(&b->lock);
 }
@@ -879,8 +1083,7 @@ int pthread_barrier_wait(pthread_barrier_t *b)
 {
        unsigned int spin_state = 0;
        int ls = !b->sense;     /* when b->sense is the value we read, then we're free*/
-       int nr_waiters;
-       struct pthread_queue restartees = TAILQ_HEAD_INITIALIZER(restartees);
+       struct pthread_list restartees = SLIST_HEAD_INITIALIZER(restartees);
        struct pthread_tcb *pthread_i;
        struct barrier_junk local_junk;
        
@@ -904,20 +1107,10 @@ int pthread_barrier_wait(pthread_barrier_t *b)
                        spin_pdr_unlock(&b->lock);
                        return PTHREAD_BARRIER_SERIAL_THREAD;
                }
-               TAILQ_CONCAT(&restartees, &b->waiters, next);
-               nr_waiters = b->nr_waiters;
+               swap_slists(&restartees, &b->waiters);
                b->nr_waiters = 0;
                spin_pdr_unlock(&b->lock);
-               /* TODO: do we really need this state tracking? */
-               TAILQ_FOREACH(pthread_i, &restartees, next)
-                       pthread_i->state = PTH_RUNNABLE;
-               /* bulk restart waiters (skipping pth_thread_runnable()) */
-               mcs_pdr_lock(&queue_lock);
-               threads_ready += nr_waiters;
-               TAILQ_CONCAT(&ready_queue, &restartees, next);
-               mcs_pdr_unlock(&queue_lock);
-               if (can_adjust_vcores)
-                       vcore_request(threads_ready);
+               wake_slist(&restartees);
                return PTHREAD_BARRIER_SERIAL_THREAD;
        } else {
                /* Spin if there are no other threads to run.  No sense sleeping */
@@ -938,7 +1131,7 @@ int pthread_barrier_wait(pthread_barrier_t *b)
 
 int pthread_barrier_destroy(pthread_barrier_t *b)
 {
-       assert(TAILQ_EMPTY(&b->waiters));
+       assert(SLIST_EMPTY(&b->waiters));
        assert(!b->nr_waiters);
        /* Free any locks (if we end up using an MCS) */
        return 0;
@@ -951,17 +1144,19 @@ int pthread_detach(pthread_t thread)
        return 0;
 }
 
-int pthread_kill (pthread_t __threadid, int __signo)
+int pthread_kill(pthread_t thread, int signo)
 {
-       printf("pthread_kill is not yet implemented!");
-       return -1;
+       return uthread_signal(&thread->uthread, signo);
 }
 
-
 int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
 {
-       printf("pthread_sigmask is not yet implemented!");
-       return -1;
+       int ret = sigprocmask(how, set, oset);
+
+       /* Ensures any pending signals we just unmasked get processed. */
+       if (set && ret == 0)
+               pthread_yield();
+       return ret;
 }
 
 int pthread_sigqueue(pthread_t *thread, int sig, const union sigval value)
@@ -994,3 +1189,125 @@ int pthread_setspecific(pthread_key_t key, const void *value)
        return 0;
 }
 
+
+/* Scheduling Stuff */
+
+static bool policy_is_supported(int policy)
+{
+       /* As our scheduler changes, we can add more policies here */
+       switch (policy) {
+               case SCHED_FIFO:
+                       return TRUE;
+               default:
+                       return FALSE;
+       }
+}
+
+int pthread_attr_setschedparam(pthread_attr_t *attr,
+                               const struct sched_param *param)
+{
+       /* The set of acceptable priorities are based on the scheduling policy.
+        * We'll just accept any old number, since we might not know the policy
+        * yet.  I didn't see anything in the man pages saying attr had to have a
+        * policy set before setting priority. */
+       attr->sched_priority = param->sched_priority;
+       return 0;
+}
+
+int pthread_attr_getschedparam(pthread_attr_t *attr,
+                               struct sched_param *param)
+{
+       param->sched_priority = attr->sched_priority;
+       return 0;
+}
+
+int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
+{
+       if (!policy_is_supported(policy))
+               return -EINVAL;
+       attr->sched_policy = policy;
+       return 0;
+}
+
+int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy)
+{
+       *policy = attr->sched_policy;
+       return 0;
+}
+
+/* We only support SCOPE_PROCESS, so we don't even use the attr. */
+int pthread_attr_setscope(pthread_attr_t *attr, int scope)
+{
+       if (scope != PTHREAD_SCOPE_PROCESS)
+               return -ENOTSUP;
+       return 0;
+}
+
+int pthread_attr_getscope(pthread_attr_t *attr, int *scope)
+{
+       *scope = PTHREAD_SCOPE_PROCESS;
+       return 0;
+}
+
+/* Inheritance refers to policy, priority, scope */
+int pthread_attr_setinheritsched(pthread_attr_t *attr,
+                                 int inheritsched)
+{
+       switch (inheritsched) {
+               case PTHREAD_INHERIT_SCHED:
+               case PTHREAD_EXPLICIT_SCHED:
+                       break;
+               default:
+                       return -EINVAL;
+       }
+       attr->sched_inherit = inheritsched;
+       return 0;
+}
+
+int pthread_attr_getinheritsched(const pthread_attr_t *attr,
+                                 int *inheritsched)
+{
+       *inheritsched = attr->sched_inherit;
+       return 0;
+}
+
+int pthread_setschedparam(pthread_t thread, int policy,
+                           const struct sched_param *param)
+{
+       if (!policy_is_supported(policy))
+               return -EINVAL;
+       thread->sched_policy = policy;
+       /* We actually could check if the priority falls in the range of the
+        * specified policy here, since we have both policy and priority. */
+       thread->sched_priority = param->sched_priority;
+       return 0;
+}
+
+int pthread_getschedparam(pthread_t thread, int *policy,
+                           struct sched_param *param)
+{
+       *policy = thread->sched_policy;
+       param->sched_priority = thread->sched_priority;
+       return 0;
+}
+
+
+/* Unsupported Stuff */
+
+int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
+                                       const struct timespec *__restrict
+                                       __abstime)
+{
+       fprintf(stderr, "Unsupported %s!", __FUNCTION__);
+       abort();
+       return -1;
+}
+
+int pthread_cond_timedwait (pthread_cond_t *__restrict __cond,
+                                  pthread_mutex_t *__restrict __mutex,
+                                  const struct timespec *__restrict __abstime)
+{
+       fprintf(stderr, "Unsupported %s!", __FUNCTION__);
+       abort();
+       return -1;
+}