Paweł Wieczorek avatar Paweł Wieczorek committed 13022bb

* dodano typy strukturalne
* dodano PTHREAD_DEFAULT_STACK_SIZE
* zmieniono stack_len na stack_size

Comments (0)

Files changed (8)

     lib/main.c\
     lib/sched.c\
     lib/thread.c\
+    lib/thread_attr.c\
 
 EXAMPLE_SRC=\
     examples/ex01.c\

include/pwuthread/mainapi.h

     PWUTHREAD_SCHED_COOPERATIVE
 };
 
+enum PWUTHREAD_DEFAULT {
+    PWUTHREAD_DEFAULT_STACK_SIZE = 2*1024*1024
+};
+
 int pwuthread_create(pwuthread_t *t, pwuthread_attr_t *a,
         pwuthread_main_f *func, void  *arg);
 int pwuthread_detach(pwuthread_t t);
 int pwuthread_attr_destroy(pwuthread_attr_t *a);
 int pwuthread_attr_setstack(pwuthread_attr_t *a, void *, size_t );
 int pwuthread_attr_getstack(pwuthread_attr_t *a, void **, size_t *);
-
+int pwuthread_attr_setstacksize(pwuthread_attr_t *a, size_t);
+int pwutherad_attr_getstacksize(pwuthread_attr_t *a, size_t *);
+int pwuthread_attr_setstackaddr(pwuthread_attr_t *a, void *);
+int pwuthread_attr_getstackaddr(pwuthread_attr_t *a, void **);
 int pwuthread_key_create(pwuthread_key_t *k);
 int pwuthread_key_destroy(pwuthread_key_t k);
 int pwuthread_setspecific(pwuthread_key_t k, const void *);

include/pwuthread/private.h

 #include <sys/types.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <string.h>
+
+struct pwuthread_attr {
+    void                *stack_addr;
+    size_t              stack_size;
+};
 
 struct pwuthread {
-    pwuthread_t next;
-    pwuthread_t prev;
-    pwuthread_ctx_t context;
-    void *main_arg;
-    void *main_ret;
-    pwuthread_main_f *main;
-    void *stack_addr;
-    size_t stack_len;
-    int saved_error;
-    int flags;
+    pwuthread_t         next;
+    pwuthread_t         prev;
+    pwuthread_t         joined;
+    pwuthread_ctx_t     context;
+    pwuthread_attr_s    attr;
+    struct {
+        void                *arg;
+        void                *retval;
+        pwuthread_main_f    *func;
+    } entry;
+    void                *stack_addr;
+    size_t              stack_size;
+    int                 saved_error;
+    int                 flags;
 };
 
 enum PWUTHREAD_FLAGS {
 void pwuthread_log(const char *fmt, ...);
 void pwuthread_perror(const char *fmt, ...);
 int pwuthread_init(void);
+void pwuthread_attr_default_init(void);
 
 extern int pwuthread_sched_quantuum;
 extern pwuthread_t pwuthread_current;
 
-#define PWUTHREAD_MALLOC(type) (type*)malloc(sizeof(type))
+#define PWUTHREAD_MALLOC(type) (type*)calloc(sizeof(type), 1)
 #define PWUTHREAD_ICALL(c) ( (err = c) ) 
 
 #define PWUTHREAD_ISSET(x,b) ((x) & (b))

include/pwuthread/types.h

 struct pwuthread_ctx;
 
 typedef struct pwuthread *pwuthread_t;
+typedef struct pwuthread pwuthread_s;
 typedef struct pwuthread_attr *pwuthread_attr_t;
+typedef struct pwuthread_attr pwuthread_attr_s;
 typedef struct pwuthread_exun *pwuthread_exun_t;
 typedef struct pwuthread_exun_attr *pwuthread_exun_attr_t;
 typedef struct pwuthread_ctx *pwuthread_ctx_t;
+typedef struct pwuthread_ctx pwuthread_ctx_s;
 typedef int pwuthread_key_t;
 
 typedef void *pwuthread_main_f(void *);
 int pwuthread_cancel_type = PWUTHREAD_CANCEL_DEFERED;
 int pwuthread_sched_quantuum = 50000;
 pwuthread_t pwuthread_current;
+static pwuthread_t pwuthread_thread0;
 
-static pwuthread_t pwuthread_thread0;
+static void default_init(void);
 
 #define strchsn(a, b, y, n) (a == b)? y : n
 
     pwuthread_log(" * quantuum %u", pwuthread_sched_quantuum);
 
     pwuthread_thread0 = PWUTHREAD_MALLOC(struct pwuthread);
-    pwuthread_thread0->main = 0;
-    pwuthread_thread0->main_arg = 0;
+    pwuthread_thread0->entry.func = 0;
+    pwuthread_thread0->entry.arg = 0;
     pwuthread_thread0->next = pwuthread_thread0;
     pwuthread_thread0->prev = pwuthread_thread0;
     pwuthread_thread0->flags = 1;
     pwuthread_current = pwuthread_thread0;
     pwuthread_ctx_init();
+    default_init();
     pwuthread_sched_install();
     initialized = 1;
     
     return 0;
 }
 
+void
+default_init(void)
+{
+    pwuthread_attr_default_init();
+}
+
+
 int
 pwuthread_setschedtype(int st, int *ost)
 {
 
     pwuthread_init();
     thr = PWUTHREAD_MALLOC(struct pwuthread);
-    thr->stack_len = 2*1024*1024;
-    thr->main = func;
-    thr->main_arg = arg;
+    thr->stack_size = 2*1024*1024;
+    thr->entry.func = func;
+    thr->entry.arg = arg;
     thr->flags = 0;
-    if (PWUTHREAD_ICALL(pwuthread_stack_alloc(&thr->stack_addr, thr->stack_len))) {
+    if (PWUTHREAD_ICALL(pwuthread_stack_alloc(&thr->stack_addr, thr->stack_size))) {
         return err;
     }
-    pwuthread_ctx_create(thr, thr->main, thr->stack_addr, thr->stack_len);
+    pwuthread_ctx_create(thr, thr->entry.func, thr->stack_addr, thr->stack_size);
     pwuthread_log("new uthread %p entry=%p arg=%p ctx=%p stack=%p+%x",
-        thr, thr->main, thr->main_arg, thr->context, thr->stack_addr,
-        thr->stack_len);
+        thr, thr->entry.func, thr->entry.arg, thr->context, thr->stack_addr,
+        thr->stack_size);
     *t = thr;
     thr->flags = 1;
     return 0;
 int
 pwuthread_join(pwuthread_t t, void **arg)
 {
-    if (arg) *arg = t->main_ret;
+    if (arg) *arg = t->entry.retval;
     return ENOTSUP;
 }
 

lib/thread_attr.c

+#include "pwuthread/private.h"
+
+static pwuthread_attr_t pwuthread_default_attr;
+
+void
+pwuthread_attr_default_init(void)
+{
+    pwuthread_default_attr = PWUTHREAD_MALLOC(pwuthread_attr_s);
+    pwuthread_default_attr->stack_size = PWUTHREAD_DEFAULT_STACK_SIZE;
+}
+
+int
+pwuthread_attr_init(pwuthread_attr_t *attr)
+{
+    *attr = PWUTHREAD_MALLOC(struct pwuthread_attr);
+    memcpy(*attr, pwuthread_default_attr, sizeof(pwuthread_attr_s));
+    return 0;
+}
+
+int
+pwuthread_attr_get(pwuthread_t t, pwuthread_attr_t *dst)
+{
+    memcpy(*dst, &t->attr, sizeof(pwuthread_attr_s));
+    return 0;
+}
+
+int
+pwuthread_attr_setstackaddr(pwuthread_attr_t *attr, void *addr)
+{
+    return 0;
+}
+
+int
+pwuthread_attr_setstacksize(pwuthread_attr_t *attr, size_t s)
+{
+    return 0;
+}
+
+int
+pwuthread_attr_setstack(pwuthread_attr_t *attr, void *sa, size_t sl)
+{
+    pwuthread_attr_setstackaddr(attr, sa);
+    pwuthread_attr_setstacksize(attr, sl);
+    return 0;
+}
+

support/unix_ctx_setjmp.c

     pwuthread_t thread = shortjump_thread;
     pwuthread_log("shortjump done for %p", thread);
     if (setjmp(thread->context->jb)) {
-        thread->main_ret = thread->main(thread->main_arg);
+        thread->entry.retval = thread->entry.func(thread->entry.arg);
         while(1);
     }
     shortjump_thread = 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.