Commits

Paweł Wieczorek committed 6b89c7a

* zliwkdiwono duplikacje stack_* w pwuthread

Comments (0)

Files changed (5)

 int
 main()
 {
+    void *saddr;
     pwuthread_t t;
+    pwuthread_attr_t attr;
     pwuthread_create(&t, NULL, tmain, NULL);
+    pwuthread_attr_init(&attr);
+    pwuthread_attr_get(t, &attr);
+    pwuthread_attr_getstackaddr(&attr, &saddr);
+    printf("thread has stack %p\n", saddr);
     while (1);
     return 0;
 }

include/pwuthread/private.h

     pwuthread_t         prev;
     pwuthread_t         joined;
     pwuthread_ctx_t     context;
-    pwuthread_attr_s    attr;
+    pwuthread_attr_t    attr;
     struct {
         void                *arg;
         void                *retval;
         pwuthread_main_f    *func;
     } entry;
-    void                *stack_addr;
-    size_t              stack_size;
     int                 saved_error;
     int                 flags;
 };
 {
     int err;
     pwuthread_t thr;
+    pwuthread_attr_t attr;
+    pwuthread_init();
 
-    pwuthread_init();
-    thr = PWUTHREAD_MALLOC(struct pwuthread);
-    thr->stack_size = 2*1024*1024;
+    thr = PWUTHREAD_MALLOC(pwuthread_s);
+    pwuthread_attr_init(&thr->attr);
+    if (a != NULL) {
+        memcpy(thr->attr, *a, sizeof(pwuthread_attr_s));
+    }
+    attr = thr->attr;
     thr->entry.func = func;
     thr->entry.arg = arg;
     thr->flags = 0;
-    if (PWUTHREAD_ICALL(pwuthread_stack_alloc(&thr->stack_addr, thr->stack_size))) {
-        return err;
+
+    if (attr->stack_addr == 0) {
+        err = pwuthread_stack_alloc(&attr->stack_addr, attr->stack_size);
+        if (err) return err;
     }
-    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->entry.func, thr->entry.arg, thr->context, thr->stack_addr,
-        thr->stack_size);
+
+    pwuthread_ctx_create(thr, thr->entry.func, thr->attr->stack_addr,
+        thr->attr->stack_size);
+
+    pwuthread_log("thread %p entry=%p arg=%p stack=%p+%x",
+        thr, thr->entry.func, thr->entry.arg, thr->attr->stack_addr,
+        thr->attr->stack_size);
     *t = thr;
-    thr->flags = 1;
     return 0;
 }
 

lib/thread_attr.c

 int
 pwuthread_attr_get(pwuthread_t t, pwuthread_attr_t *dst)
 {
-    memcpy(*dst, &t->attr, sizeof(pwuthread_attr_s));
+    memcpy(*dst, t->attr, sizeof(pwuthread_attr_s));
     return 0;
 }
 
 }
 
 int
+pwuthread_attr_getstackaddr(pwuthread_attr_t *attr, void **addr)
+{
+    *addr = (*attr)->stack_addr;
+}
+
+int
 pwuthread_attr_setstacksize(pwuthread_attr_t *attr, size_t s)
 {
     return 0;

support/unix_ctx_setjmp.c

     struct sigaction oldsa;
     sigset_t oldmask;
 
-    pwuthread_log("setjmp-ctx create %p entry=%p sa=%p+%x",
-        t, func, sa, sl);
     curstack.ss_sp = sa;
     curstack.ss_size = sl;
     curstack.ss_flags = 0;
     }
 
     t->context = PWUTHREAD_MALLOC(struct pwuthread_ctx);
-    t->prev = pwuthread_current;
-    t->next = pwuthread_current->next;
-    pwuthread_current->next = t;
-    t->next->prev = t;
     shortjump_raised = 0;
     shortjump_thread = t;
     kill(getpid(), SIGUSR2);