Issue #109 - Copy parent thread's params when spinning up a new thread.

This commit is contained in:
Justin Ethier 2017-03-13 22:55:18 +00:00
parent a964bb17ae
commit 6742142b9d
2 changed files with 47 additions and 10 deletions

View file

@ -1,4 +1,8 @@
;; A simple program demonstrating how parameter objects interact with threads ;; A simple program demonstrating how parameter objects interact with threads
;;
;; Note this is poor code as it uses timing via sleeps instead of proper
;; thread synchronization!!!
;;
(import (scheme base) (import (scheme base)
(scheme read) (scheme read)
(scheme write) (scheme write)
@ -8,18 +12,20 @@
(thread-start! (thread-start!
(make-thread (make-thread
(lambda () (lambda ()
(thread-sleep! 1000) (thread-sleep! 2000)
(display "started thread, this should be written to console") (display "started thread, this should be written to console")
(newline) (newline)
(display "thread done") (display "thread done")
(newline)))) (newline)
(flush-output-port (current-output-port)))))
(thread-sleep! 1000) ;; Prevent race condition replacing stdout before thread is spawned
(write `(1 2 3)) (write `(1 2 3))
(define fp (open-output-file "tmp.txt")) (define fp (open-output-file "tmp.txt"))
(parameterize (parameterize
((current-output-port fp)) ((current-output-port fp))
(write `(4 5 6)) (write `(4 5 6))
(thread-sleep! 5000) (thread-sleep! 3000)
) )
(close-port fp) (close-port fp)
(write `(7 8 9)) (write `(7 8 9))

View file

@ -5326,6 +5326,21 @@ const object primitive_Cyc_91write = &Cyc_91write_primitive;
const object primitive_Cyc_91display = &Cyc_91display_primitive; const object primitive_Cyc_91display = &Cyc_91display_primitive;
const object primitive_call_95cc = &call_95cc_primitive; const object primitive_call_95cc = &call_95cc_primitive;
void *gc_alloc_pair(gc_thread_data *data, object head, object tail)
{
int heap_grown;
pair_type *p;
pair_type tmp;
tmp.hdr.mark = gc_color_red;
tmp.hdr.grayed = 0;
tmp.tag = pair_tag;
tmp.pair_car = head;
tmp.pair_cdr = tail;
p = gc_alloc(((gc_thread_data *)data)->heap, sizeof(pair_type), (char *)(&tmp), (gc_thread_data *)data, &heap_grown);
return p;
}
/** /**
* Thread initialization function only called from within the runtime * Thread initialization function only called from within the runtime
*/ */
@ -5341,18 +5356,34 @@ void *Cyc_init_thread(object thread_and_thunk)
thd->gc_args[0] = &Cyc_91end_91thread_67_primitive; thd->gc_args[0] = &Cyc_91end_91thread_67_primitive;
thd->thread_id = pthread_self(); thd->thread_id = pthread_self();
TODO: want to get thread params from calling thread, and probably // TODO: want to get thread params from calling thread, and probably
allocate a new set of cells instead of just assigning this thread's // allocate a new set of cells instead of just assigning this thread's
params to the parent's params. // params to the parent's params.
vector_type *t = (vector_type *)thd->scm_thread_obj; vector_type *t = (vector_type *)thd->scm_thread_obj;
object op = Cyc_vector_ref(thd, t, obj_int2obj(2)); object op = Cyc_vector_ref(thd, t, obj_int2obj(2));
c_opaque_type *o = (c_opaque_type *)op; c_opaque_type *o = (c_opaque_type *)op;
// thd->param_objs = ?? object par = ((gc_thread_data *)o->ptr)->param_objs;
object obj = ((gc_thread_data *)o->ptr)->param_objs; object child = NULL;
while (obj) { thd->param_objs = NULL;
while (par) {
if (thd->param_objs == NULL) {
thd->param_objs = gc_alloc_pair(thd, NULL, NULL);
child = thd->param_objs;
} else {
pair_type *p = gc_alloc_pair(thd, NULL, NULL);
cdr(child) = p;
child = p;
} }
car(child) = gc_alloc_pair(thd, car(car(par)), cdr(car(par)));
par = cdr(par);
}
// fprintf(stdout, "old: ");
// Cyc_display(thd, ((gc_thread_data *)o->ptr)->param_objs, stdout);
// fprintf(stdout, "\n");
// fprintf(stdout, "new: ");
// Cyc_display(thd, thd->param_objs, stdout);
// fprintf(stdout, "\n");
// END TODO // END TODO
gc_add_mutator(thd); gc_add_mutator(thd);