uth: add got_posix_signal() to the 2LS ops
[akaros.git] / user / parlib / thread0_sched.c
index 4367d9e..62a7f6f 100644 (file)
 #include <parlib/uthread.h>
 #include <parlib/event.h>
 #include <parlib/arch/trap.h>
+#include <parlib/ros_debug.h>
 #include <stdlib.h>
+#include <sys/fork_cb.h>
 
+static void thread0_sched_init(void);
 static void thread0_sched_entry(void);
 static void thread0_thread_blockon_sysc(struct uthread *uthread, void *sysc);
 static void thread0_thread_refl_fault(struct uthread *uth,
                                       struct user_context *ctx);
 static void thread0_thread_runnable(struct uthread *uth);
 static void thread0_thread_has_blocked(struct uthread *uth, int flags);
-static uth_mutex_t thread0_mtx_alloc(void);
-static void thread0_mtx_free(uth_mutex_t m);
-static void thread0_mtx_lock(uth_mutex_t m);
-static bool thread0_mtx_trylock(uth_mutex_t m);
-static void thread0_mtx_unlock(uth_mutex_t m);
+static void thread0_thread_exited(struct uthread *uth);
+static struct uthread *thread0_thread_create(void *(*func)(void *), void *arg);
+static void thread0_got_posix_signal(int sig_nr, struct siginfo *info);
+static void thread0_sync_init(uth_sync_t *s);
+static void thread0_sync_destroy(uth_sync_t *s);
+static void thread0_sync_enqueue(struct uthread *uth, uth_sync_t *s);
+static struct uthread *thread0_sync_get_next(uth_sync_t *s);
+static bool thread0_sync_get_uth(uth_sync_t *s, struct uthread *uth);
+static void thread0_sync_swap(uth_sync_t *a, uth_sync_t *b);
+static bool thread0_sync_is_empty(uth_sync_t *s);
 
 /* externed into uthread.c */
 struct schedule_ops thread0_2ls_ops = {
+       .sched_init = thread0_sched_init,
        .sched_entry = thread0_sched_entry,
        .thread_blockon_sysc = thread0_thread_blockon_sysc,
        .thread_refl_fault = thread0_thread_refl_fault,
        .thread_runnable = thread0_thread_runnable,
        .thread_paused = thread0_thread_runnable,
        .thread_has_blocked = thread0_thread_has_blocked,
-       .mutex_alloc = thread0_mtx_alloc,
-       .mutex_free = thread0_mtx_free,
-       .mutex_lock = thread0_mtx_lock,
-       .mutex_trylock = thread0_mtx_trylock,
-       .mutex_unlock = thread0_mtx_unlock,
+       .thread_exited = thread0_thread_exited,
+       .thread_create = thread0_thread_create,
+       .got_posix_signal = thread0_got_posix_signal,
+       .sync_init = thread0_sync_init,
+       .sync_destroy = thread0_sync_destroy,
+       .sync_enqueue = thread0_sync_enqueue,
+       .sync_get_next = thread0_sync_get_next,
+       .sync_get_uth = thread0_sync_get_uth,
+       .sync_swap = thread0_sync_swap,
+       .sync_is_empty = thread0_sync_is_empty,
 };
 
+struct schedule_ops *sched_ops __attribute__((weak)) = &thread0_2ls_ops;
+
 /* externed into uthread.c */
 struct uthread *thread0_uth;
 
@@ -50,7 +66,7 @@ struct uthread *thread0_uth;
  * don't actually attach this mgmt info to it.  But since we just have one
  * thread, it doesn't matter. */
 struct thread0_info {
-       bool                                            is_blocked;
+       bool                            is_blocked;
 };
 static struct thread0_info thread0_info;
 static struct event_queue *sysc_evq;
@@ -61,12 +77,30 @@ void thread0_handle_syscall(struct event_msg *ev_msg,
        thread0_info.is_blocked = FALSE;
 }
 
-void thread0_lib_init(void)
+static void thread0_pre_fork(void)
+{
+}
+
+static void thread0_post_fork(pid_t ret)
+{
+}
+
+void thread0_sched_init(void)
 {
+       int ret;
+
+       ret = posix_memalign((void**)&thread0_uth, __alignof__(struct uthread),
+                            sizeof(struct uthread));
+       assert(!ret);
+       /* aggressively 0 for bugs*/
+       memset(thread0_uth, 0, sizeof(struct uthread));
        memset(&thread0_info, 0, sizeof(thread0_info));
        /* we don't care about the message, so don't bother with a UCQ */
        sysc_evq = get_eventq(EV_MBOX_BITMAP);
        sysc_evq->ev_flags = EVENT_INDIR | EVENT_WAKEUP;
+       uthread_2ls_init(thread0_uth, thread0_handle_syscall, NULL);
+       pre_fork_2ls = thread0_pre_fork;
+       post_fork_2ls = thread0_post_fork;
 }
 
 /* Thread0 scheduler ops (for processes that haven't linked in a full 2LS) */
@@ -92,6 +126,7 @@ static void thread0_sched_entry(void)
 static void thread0_thread_blockon_sysc(struct uthread *uthread, void *arg)
 {
        struct syscall *sysc = (struct syscall*)arg;
+
        thread0_thread_has_blocked(uthread, 0);
        if (!register_evq(sysc, sysc_evq))
                thread0_thread_runnable(uthread);
@@ -144,50 +179,77 @@ static void thread0_thread_runnable(struct uthread *uth)
 
 static void thread0_thread_has_blocked(struct uthread *uth, int flags)
 {
+       assert(!thread0_info.is_blocked);
        thread0_info.is_blocked = TRUE;
 }
 
-/* We only have one thread, so we don't *need* mutexes.  But we'll use a bool to
- * catch code that could deadlock itself. */
-static uth_mutex_t thread0_mtx_alloc(void)
+/* Actually, a 2LS only needs to implement this if it calls
+ * uth_2ls_thread_exit().  Keep it here to catch bugs. */
+static void thread0_thread_exited(struct uthread *uth)
 {
-       bool *mtx = malloc(sizeof(bool));
+       assert(0);
+}
 
-       assert(mtx);
-       *mtx = FALSE;
-       return (uth_mutex_t)mtx;
+static struct uthread *thread0_thread_create(void *(*func)(void *), void *arg)
+{
+       panic("Thread0 sched asked to create more threads!");
 }
 
-static void thread0_mtx_free(uth_mutex_t m)
+static void thread0_got_posix_signal(int sig_nr, struct siginfo *info)
 {
-       bool *mtx = (bool*)m;
+       if (current_uthread)
+               trigger_posix_signal(sig_nr, info, get_cur_uth_ctx());
+       else
+               trigger_posix_signal(sig_nr, info, &thread0_uth->u_ctx);
+       /* Legacy single-threaded programs, which often use thread0, expect
+        * signals to interrupt their syscall.  For most 2LSes, we can't match a
+        * process-wide signal to a particular thread; the kernel knows nothing
+        * of threads, we're just receiving an event.  However, thread0 has only
+        * one thread. */
+       if (thread0_uth->sysc)
+               sys_abort_sysc(thread0_uth->sysc);
+}
 
-       assert(*mtx == FALSE);
-       free((void*)m);
+static void thread0_sync_init(uth_sync_t *s)
+{
+       memset(s, 0x5a, sizeof(uth_sync_t));
 }
 
-static void thread0_mtx_lock(uth_mutex_t m)
+static void thread0_sync_destroy(uth_sync_t *s)
 {
-       bool *mtx = (bool*)m;
+}
 
-       assert(*mtx == FALSE);
-       *mtx = TRUE;
+static void thread0_sync_enqueue(struct uthread *uth, uth_sync_t *s)
+{
 }
 
-static bool thread0_mtx_trylock(uth_mutex_t m)
+static struct uthread *thread0_sync_get_next(uth_sync_t *s)
 {
-       bool *mtx = (bool*)m;
+       if (thread0_info.is_blocked) {
+               /* Note we don't clear is_blocked.  Runnable does that, which
+                * should be called before the next get_next (since we have only
+                * one thread). */
+               return thread0_uth;
+       } else {
+               return NULL;
+       }
+}
 
-       if (*mtx)
-               return FALSE;
-       *mtx = TRUE;
-       return TRUE;
+static bool thread0_sync_get_uth(uth_sync_t *s, struct uthread *uth)
+{
+       assert(uth == thread0_uth);
+       if (thread0_info.is_blocked) {
+               /* Note we don't clear is_blocked.  Runnable does that. */
+               return TRUE;
+       }
+       return FALSE;
 }
 
-static void thread0_mtx_unlock(uth_mutex_t m)
+static void thread0_sync_swap(uth_sync_t *a, uth_sync_t *b)
 {
-       bool *mtx = (bool*)m;
+}
 
-       assert(*mtx == TRUE);
-       *mtx = FALSE;
+static bool thread0_sync_is_empty(uth_sync_t *s)
+{
+       return !thread0_info.is_blocked;
 }