From 6742142b9d59fa8035ef60f0113c809a6899ccb7 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 13 Mar 2017 22:55:18 +0000 Subject: [PATCH] Issue #109 - Copy parent thread's params when spinning up a new thread. --- examples/threading/parameters.scm | 12 ++++++--- runtime.c | 45 ++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/examples/threading/parameters.scm b/examples/threading/parameters.scm index 7221cae8..7c5f83fa 100644 --- a/examples/threading/parameters.scm +++ b/examples/threading/parameters.scm @@ -1,4 +1,8 @@ ;; 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) (scheme read) (scheme write) @@ -8,18 +12,20 @@ (thread-start! (make-thread (lambda () - (thread-sleep! 1000) + (thread-sleep! 2000) (display "started thread, this should be written to console") (newline) (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)) (define fp (open-output-file "tmp.txt")) (parameterize ((current-output-port fp)) (write `(4 5 6)) - (thread-sleep! 5000) + (thread-sleep! 3000) ) (close-port fp) (write `(7 8 9)) diff --git a/runtime.c b/runtime.c index bee3855f..b3dd7abc 100644 --- a/runtime.c +++ b/runtime.c @@ -5326,6 +5326,21 @@ const object primitive_Cyc_91write = &Cyc_91write_primitive; const object primitive_Cyc_91display = &Cyc_91display_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 */ @@ -5341,18 +5356,34 @@ void *Cyc_init_thread(object thread_and_thunk) thd->gc_args[0] = &Cyc_91end_91thread_67_primitive; thd->thread_id = pthread_self(); - TODO: want to get thread params from calling thread, and probably - allocate a new set of cells instead of just assigning this thread's - params to the parent's params. +// TODO: want to get thread params from calling thread, and probably +// allocate a new set of cells instead of just assigning this thread's +// params to the parent's params. vector_type *t = (vector_type *)thd->scm_thread_obj; object op = Cyc_vector_ref(thd, t, obj_int2obj(2)); c_opaque_type *o = (c_opaque_type *)op; -// thd->param_objs = ?? - object obj = ((gc_thread_data *)o->ptr)->param_objs; - while (obj) { - + object par = ((gc_thread_data *)o->ptr)->param_objs; + object child = NULL; + 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 gc_add_mutator(thd);