Commits

Paweł Wieczorek  committed 768d38c

* prosty planista

  • Participants
  • Parent commits ba80eee

Comments (0)

Files changed (6)

 SRCS= ${LIB_SRC} ${SUPPORT_SRC}
 LIB_SRC=\
     lib/main.c\
+    lib/sched.c\
     lib/thread.c\
 
 EXAMPLE_SRC=\

File include/pwuthread/private.h

 #include "mainapi.h"
 #include "ctx.h"
 #include "support.h"
+#include "sched.h"
 #include <sys/types.h>
 #include <errno.h>
 #include <stdlib.h>
 
 struct pwuthread {
+    pwuthread_t next;
+    pwuthread_t prev;
     pwuthread_ctx_t context;
     void *main_arg;
     void *main_ret;
 };
 
 void pwuthread_log(const char *fmt, ...);
+void pwuthread_perror(const char *fmt, ...);
 int pwuthread_init(void);
 
 extern int pwuthread_sched_quantuum;
+extern pwuthread_t pwuthread_current;
 
 #define PWUTHREAD_MALLOC(type) (type*)malloc(sizeof(type))
 #define PWUTHREAD_ICALL(c) ( (err = c) ) 

File include/pwuthread/sched.h

 #ifndef PWUTHREAD_SCHED_H
 #define PWUTHREAD_SCHED_H
 
-void pwuthread_sched_install(void);
-void *pwuthread_sched_clocktick(siginfo_t *si, void *u);
+int pwuthread_sched_install(void);
+void pwuthread_sched_clocktick(void *u);
+pwuthread_t pwuthread_sched_next();
 
 #endif
 int pwuthread_cancel_state = PWUTHREAD_CANCEL_ENABLE;
 int pwuthread_cancel_type = PWUTHREAD_CANCEL_DEFERED;
 int pwuthread_sched_quantuum = 50000;
+pwuthread_t pwuthread_current;
+
+static pwuthread_t pwuthread_thread0;
 
 #define strchsn(a, b, y, n) (a == b)? y : n
 
 int
 pwuthread_init(void)
 {
-    int err;
     if (initialized) return 0;
-    if (PWUTHREAD_ICALL(pwuthread_ctx_init())) {
-        return err;
-    }
-    initialized = 1;
-    pwuthread_log("Pawel Wieczorek User Thread Library initialized");
+    pwuthread_log("Pawel Wieczorek User Thread Library");
     pwuthread_log("libpwuthread: ABI %u API %s", 
         PWUTHREAD_ABI, PWUTHREAD_API);
     pwuthread_log(" * sched_type PWUTHREAD_SCHED_%s",
     pwuthread_log(" * cancel_type PWUTHREAD_CANCEL_%s",
         cancel_type_str(pwuthread_cancel_type));
     pwuthread_log(" * quantuum %u", pwuthread_sched_quantuum);
+
+    pwuthread_thread0 = PWUTHREAD_MALLOC(struct pwuthread);
+    pwuthread_thread0->main = 0;
+    pwuthread_thread0->main_arg = 0;
+    pwuthread_current = pwuthread_thread0;
+    pwuthread_ctx_init();
+    pwuthread_sched_install();
+    initialized = 1;
+    
     return 0;
 }
 
     fprintf(stderr, "\033[1m[PWUTHREAD] %s\033[0m\n", buf);
     va_end(ap);
 }
- 
+
+void
+pwuthread_perror(const char *fmt, ...)
+{
+    int e = errno;
+    char buf[512];
+    char ebuf[256];
+    va_list ap;
+    va_start(ap, fmt);
+    vsnprintf(buf, sizeof(buf), fmt, ap);
+    va_end(ap);
+    strerror_r(e, ebuf, sizeof(ebuf));
+    pwuthread_log("%s: %s (errno %i)", buf, ebuf, e);
+} 
+#include "pwuthread/private.h"
+
+pwuthread_t
+pwuthread_sched_next()
+{
+    pwuthread_t n = pwuthread_current->next;
+    return n;
+}
+

File support/unix_ctx_setjmp.c

 
 struct pwuthread_ctx {
     jmp_buf jb;
-    pwuthread_main_f *func;
 };
 
 static struct sigaction shortjump_sa;
 
     sigfillset(&shortjump_waitmask);
     sigdelset(&shortjump_waitmask, SIGUSR2);
+
+    pwuthread_current->context = PWUTHREAD_MALLOC(struct pwuthread_ctx);
     return 0;
 }
 
     curstack.ss_flags = 0;
 
     if (sigaltstack(&curstack, &oldstack) == -1) {
+        int e = errno;
+        pwuthread_perror("setting new sigaltstack");
+        return e;
+    }
+
+    if (sigaction(SIGUSR2, &shortjump_sa, &oldsa) == -1) {
+        pwuthread_perror("setting new signal handler");
         return errno;
     }
 
-    if (sigaction(SIGUSR2, &shortjump_sa, &oldsa) == -1) {
-        return errno;
-    }
-
+    t->context = PWUTHREAD_MALLOC(struct pwuthread_ctx);
     shortjump_raised = 0;
     kill(getpid(), SIGUSR2);
 
     while (!shortjump_raised) {
         if (sigsuspend(&shortjump_waitmask) != -1  && errno != EINTR) {
+            pwuthread_perror("suspend until signal occur");
             return errno;
         } 
     }
 
     if (sigaction(SIGUSR2, &oldsa, NULL) == -1) {
+        pwuthread_perror("setting old signal handler");
         return errno;
     }
 
-    if (sigaltstack(&oldstack, &curstack)) {
+    if (sigaltstack(&oldstack, &curstack) == -1) {
+        pwuthread_perror("setting old sigaltstack");
+        return errno;
     }
 
     return 0;