Paweł Wieczorek avatar Paweł Wieczorek committed ba80eee

* dodano makra od ABI i API
* dodano unix_stack

Comments (0)

Files changed (7)

 CC= gcc
-CFLAGS= -Wall -g -fPIC -Iinclude
+CFLAGS= -Wall -g -fPIC -Iinclude 
 LD= ${CC} -shared
 LDFLAGS= 
 LIBNAME=libpwuthread.so
-LIBVER=1
-LIBRARY=${LIBNAME}.${LIBVER}
+LIBABIVER=1
+LIBAPIVER=1.0
+LIBRARY=${LIBNAME}.${LIBABIVER}
 SRCS= ${LIB_SRC} ${SUPPORT_SRC}
 LIB_SRC=\
     lib/main.c\
 SUPPORT_SRC=\
     support/unix_ctx_setjmp.c\
     support/unix_sched.c\
+    support/unix_stack.c\
 
 OBJS= ${SRCS:%.c=%.o}
 EXAMPLES= ${EXAMPLE_SRC:%.c=%}

include/pwuthread/mainapi.h

 #ifndef PWUTHREAD_MAINAPI_H
 #define PWUTHREAD_MAINAPI_H
 
+#define PWUTHREAD_ABI 1
+#define PWUTHREAD_API_MINOR 1
+#define PWUTHREAD_API_MAJOR 1
+#define PWUTHREAD_API "1.0-devel"
+
 enum PWUTHREAD_CANCEL_STATE {
     PWUTHREAD_CANCEL_ENABLE,
     PWUTHREAD_CANCEL_DISABLE

include/pwuthread/private.h

 #include "types.h"
 #include "mainapi.h"
 #include "ctx.h"
+#include "support.h"
 #include <sys/types.h>
 #include <errno.h>
 #include <stdlib.h>
     void *main_arg;
     void *main_ret;
     pwuthread_main_f *main;
+    void *stack_addr;
+    size_t stack_len;
 };
 
 void pwuthread_log(const char *fmt, ...);
 
 #define PWUTHREAD_MALLOC(type) (type*)malloc(sizeof(type))
 #define PWUTHREAD_ICALL(c) ( (err = c) ) 
+
 #endif

include/pwuthread/support.h

 #ifndef PWUTHREAD_SUPPORT_H
 #define PWUTHREAD_SUPPORT_H
 
+int pwuthread_stack_alloc(void **addr, size_t len);
+int pwuthread_stack_free(void *addr, size_t len);
 #endif
 
 
 #include "pwuthread/private.h"
+#include <stdarg.h>
+#include <string.h>
+#include <stdio.h>
 
 static int initialized = 0;
 int pwuthread_sched_type = PWUTHREAD_SCHED_PREEMPTIVE;
 int pwuthread_cancel_type = PWUTHREAD_CANCEL_DEFERED;
 int pwuthread_sched_quantuum = 50000;
 
+#define strchsn(a, b, y, n) (a == b)? y : n
+
+#define sched_type_str(s)\
+    strchsn(pwuthread_sched_type, PWUTHREAD_SCHED_PREEMPTIVE,\
+        "PREEMPTIVE",\
+        "COOPERATIVE")
+
+#define cancel_state_str(s)\
+    strchsn(pwuthread_cancel_state, PWUTHREAD_CANCEL_ENABLE,\
+        "ENABLE",\
+        "DISABLE")
+
+#define cancel_type_str(s)\
+    strchsn(pwuthread_cancel_type, PWUTHREAD_CANCEL_DEFERED,\
+        "DEFERED",\
+        "ASYNCHRONOUS")
 int
 pwuthread_init(void)
 {
         return err;
     }
     initialized = 1;
+    pwuthread_log("Pawel Wieczorek User Thread Library initialized");
+    pwuthread_log("libpwuthread: ABI %u API %s", 
+        PWUTHREAD_ABI, PWUTHREAD_API);
+    pwuthread_log(" * sched_type PWUTHREAD_SCHED_%s",
+        sched_type_str(pwuthread_sched_type));
+    pwuthread_log(" * cancel_state PWUTHREAD_CANCEL_%s",
+        cancel_state_str(pwuthread_cancel_state));
+    pwuthread_log(" * cancel_type PWUTHREAD_CANCEL_%s",
+        cancel_type_str(pwuthread_cancel_type));
+    pwuthread_log(" * quantuum %u", pwuthread_sched_quantuum);
     return 0;
 }
 
     return r;
 }
 
+void
+pwuthread_log(const char *fmt, ...)
+{
+    char buf[512];
+    va_list ap;
+    va_start(ap, fmt);
+    vsnprintf(buf, sizeof(buf), fmt, ap);
+    fprintf(stderr, "\033[1m[PWUTHREAD] %s\033[0m\n", buf);
+    va_end(ap);
+}
+ 
 pwuthread_create(pwuthread_t *t, pwuthread_attr_t *a,
     pwuthread_main_f *func, void *arg)
 {
-    pwuthread_t thr = PWUTHREAD_MALLOC(struct pwuthread);
-    return ENOTSUP;
+    int err;
+    pwuthread_t thr;
+
+    pwuthread_init();
+    thr = PWUTHREAD_MALLOC(struct pwuthread);
+    thr->stack_len = 2*1024*1024;
+    if (PWUTHREAD_ICALL(pwuthread_stack_alloc(&thr->stack_addr, thr->stack_len))) {
+        return err;
+    }
+    return 0;
 }
 
 int
 void
 pwuthread_yield(void)
 {
+    pwuthread_init();
 }
 

support/unix_stack.c

+#include "pwuthread/private.h"
+#include <sys/mman.h>
+
+int
+pwuthread_stack_alloc(void **addr, size_t len)
+{
+    *addr = mmap(NULL, len, MAP_ANON, PROT_READ|PROT_WRITE, -1, 0);
+    if (*addr == MAP_FAILED) {
+        return errno;
+    }
+    return 0;
+}
+
+int
+pwuthread_stack_free(void *addr, size_t len)
+{
+    if (munmap(addr, len) == -1) {
+        return errno;
+    }
+    return 0;
+}
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.