This commit is contained in:
Justin Ethier 2016-11-19 05:35:25 +00:00
parent 9e21c855e2
commit 97fa0c2dbd
4 changed files with 16 additions and 10 deletions

6
gc.c
View file

@ -1043,6 +1043,10 @@ void gc_mut_cooperate(gc_thread_data * thd, int buf_len)
for (i = 0; i < thd->gc_num_args; i++) {
gc_mark_gray(thd, thd->gc_args[i]);
}
// Mark thread object, if applicable. Very likely this is its only ref
// if (thd->scm_thread_obj) {
// gc_mark_gray(thd, thd->scm_thread_obj);
// }
// Also, mark everything the collector moved to the heap
for (i = 0; i < buf_len; i++) {
gc_mark_gray(thd, thd->moveBuf[i]);
@ -1532,7 +1536,7 @@ void gc_thread_data_init(gc_thread_data * thd, int mut_num, char *stack_base,
thd->mutations =
vpbuffer_realloc(thd->mutations, &(thd->mutation_buflen));
thd->exception_handler_stack = NULL;
// thd->thread = NULL;
thd->scm_thread_obj = NULL;
thd->thread_state = CYC_THREAD_STATE_NEW;
//thd->mutator_num = mut_num;
thd->jmp_start = malloc(sizeof(jmp_buf));

View file

@ -111,8 +111,8 @@ typedef enum { CYC_THREAD_STATE_NEW, CYC_THREAD_STATE_RUNNABLE,
/* Thread data structures */
typedef struct gc_thread_data_t gc_thread_data;
struct gc_thread_data_t {
// TODO:
// pthread_t *thread;
// Thread object, if applicable
object scm_thread_obj;
cyc_thread_state_type thread_state;
// Data needed to initiate stack-based minor GC
char *stack_start;

View file

@ -4713,13 +4713,14 @@ const object primitive_call_95cc = &call_95cc_primitive;
/**
* Thread initialization function only called from within the runtime
*/
void *Cyc_init_thread(object thunk)
void *Cyc_init_thread(object thread_and_thunk)
{
long stack_start;
gc_thread_data *thd;
thd = malloc(sizeof(gc_thread_data));
gc_thread_data_init(thd, 0, (char *)&stack_start, global_stack_size);
thd->gc_cont = thunk;
thd->scm_thread_obj = car(thread_and_thunk);
thd->gc_cont = cdr(thread_and_thunk);
thd->gc_num_args = 1;
thd->gc_args[0] = &Cyc_91end_91thread_67_primitive;
// thd->thread = pthread_self(); // TODO: ptr vs instance
@ -4735,7 +4736,7 @@ void *Cyc_init_thread(object thunk)
/**
* Spawn a new thread to execute the given thunk
*/
object Cyc_spawn_thread(object thunk)
object Cyc_spawn_thread(object thread_and_thunk)
{
// TODO: if we want to return mutator number to the caller, we need
// to reserve a number here. need to figure out how we are going to
@ -4760,7 +4761,7 @@ to look at the lock-free structures provided by ck?
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&thread, NULL, Cyc_init_thread, thunk)) {
if (pthread_create(&thread, NULL, Cyc_init_thread, thread_and_thunk)) {
fprintf(stderr, "Error creating a new thread\n");
exit(1);
}

View file

@ -78,10 +78,11 @@
(define (thread-start! t)
;; Initiate a GC prior to running the thread, in case
;; t contains any closures on the "parent" thread's stack
(Cyc-minor-gc)
(let* ((thunk (vector-ref t 1))
(mutator-id (Cyc-spawn-thread! thunk)))
(vector-set! t 2 mutator-id)))
(thread-params (cons t thunk)))
(Cyc-minor-gc)
(let ((mutator-id (Cyc-spawn-thread! thread-params)))
(vector-set! t 2 mutator-id))))
(define (thread-yield!) (thread-sleep! 1))
(define-c thread-terminate!
"(void *data, int argc, closure _, object k)"