Skip to content

Commit

Permalink
src: use thread local variable for the GC instance
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao committed Nov 14, 2024
1 parent 22832f3 commit a8c6d29
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/infer.cora
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
(declare '- `(int -> (int -> int)))
(declare '* `(int -> (int -> int)))
(declare '= (let a (tvar) b (tvar) `(,a -> (,b -> bool))))
(declare 'set (let a (tvar) ['symbol '-> [a '-> a]]))
;; (declare 'set (let a (tvar) ['symbol '-> [a '-> a]]))
(declare 'null? [(tvar) '-> 'bool])
(declare 'cons? [(tvar) '-> 'bool])
(declare '*type-env* ['list ['tuple 'symbol (tvar)]])
Expand Down
38 changes: 19 additions & 19 deletions lib/toc.cora
Original file line number Diff line number Diff line change
Expand Up @@ -516,27 +516,27 @@

(set 'cora/lib/infer.*typecheck* false)

(defun .preprocess (sexp)
(let sexp1 (if (and (pair? sexp) (= 'begin (car sexp)))
(cdr sexp)
(cons sexp ()))
(begin
(.handle-import-eagerly sexp1)
(.split-type-and-code sexp1 () ()
(lambda (type code)
(if cora/lib/infer.*typecheck*
(cons 'begin (append type code))
(cons 'begin code)))))))
(defun .preprocess (file-path pkg-str)
(let sexp (read-file-as-sexp file-path pkg-str)
(let sexp1 (if (and (pair? sexp) (= 'begin (car sexp)))
(cdr sexp)
(cons sexp ()))
(begin
(.handle-import-eagerly sexp1)
(.split-type-and-code sexp1 () ()
(lambda (type code)
(if cora/lib/infer.*typecheck*
(cons 'begin (append type code))
(cons 'begin code))))))))

(defun .compile-to-c (from to pkg-str)
(let sexp (read-file-as-sexp from pkg-str)
(let sexp1 (.preprocess sexp)
(let input (macroexpand sexp1)
(let bc (.compile input)
(let stream (io.open-output-file to)
(begin
(.generate-c stream bc)
(io.close-output-file stream))))))))
(let sexp1 (.preprocess from pkg-str)
(let input (macroexpand sexp1)
(let bc (.compile input)
(let stream (io.open-output-file to)
(begin
(.generate-c stream bc)
(io.close-output-file stream)))))))


;; ============== utilities of eval0 ==========
Expand Down
20 changes: 14 additions & 6 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,11 @@ blockReset(struct Block *block) {
}
}

__thread struct GC* threadLocalGC;

void
gcInit(struct GC *gc, uintptr_t *baseStackAddr) {
gcInit(uintptr_t *baseStackAddr) {
struct GC *gc = malloc(sizeof(struct GC));
gc->heap = NULL;
gc->state = gcStateNone;
gc->baseStackAddr = baseStackAddr;
Expand All @@ -211,6 +214,13 @@ gcInit(struct GC *gc, uintptr_t *baseStackAddr) {
for (int i=0; i<sizeClassSZ; i++) {
gc->sizeClass[i] = NULL;
}

threadLocalGC = gc;
}

struct GC*
getGC() {
return threadLocalGC;
}

static struct heapArena*
Expand Down Expand Up @@ -324,8 +334,6 @@ gcRegistForType(uint8_t idx, gcFunc fn) {
return true;
}

struct GC gc;

static void
gcQueueInit(struct GC *gc) {
struct Block *b = blockNew(gc);
Expand Down Expand Up @@ -550,11 +558,11 @@ gcInuseSizeInc(struct GC *gc, int size) {
}

void
writeBarrier(uintptr_t *slot, uintptr_t val) {
writeBarrier(struct GC *gc, uintptr_t *slot, uintptr_t val) {
// Yuasa-style deletion barrier
if (gc.state == gcStateIncremental) {
if (gc->state == gcStateIncremental) {
uintptr_t old = *slot;
gcMark(&gc, old);
gcMark(gc, old);
}
*slot = val;
}
11 changes: 6 additions & 5 deletions src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ typedef struct {
#define ptr(x) ((void*)((x)&~TAG_PTR))
#define tag(x) ((x) & TAG_MASK)

extern struct GC gc;
void gcInit(struct GC* gc, uintptr_t* mark);
struct GC;

void gcInit(uintptr_t* mark);
struct GC *getGC();

void* gcAlloc(struct GC* gc, int size);

void writeBarrier(uintptr_t *slot, uintptr_t val);
void writeBarrier(struct GC *gc, uintptr_t *slot, uintptr_t val);
void gcMark(struct GC *gc, uintptr_t head);
void gcInuseSizeInc(struct GC *gc, int size);

typedef void (*gcFunc)(struct GC *gc, void* from);
bool gcRegistForType(uint8_t type, gcFunc fn);

extern struct GC gc;

#endif
2 changes: 1 addition & 1 deletion src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ registerAPI(struct registerModule* m, str pkg) {

void
coraInit(uintptr_t *mark) {
gcInit(&gc, mark);
gcInit(mark);
typesInit();
symQuote = intern("quote");
symBackQuote = intern("backquote");
Expand Down
4 changes: 2 additions & 2 deletions src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const char* typeNameX[8] = {
void*
newObj(scmHeadType tp, int sz) {
// scmHead* p = malloc(sz);
scmHead* p = gcAlloc(&gc, sz);
scmHead* p = gcAlloc(getGC(), sz);
assert(((Obj)p & TAG_PTR) == 0);
p->type = tp;
/* printf("alloc object -- %p %s\n", p, typeNameX[tp]); */
Expand Down Expand Up @@ -338,7 +338,7 @@ vectorSet(Obj vec, int idx, Obj val) {
struct scmVector* v = ptr(vec);
assert(v->head.type == scmHeadVector);
assert(idx >= 0 && idx < v->size);
writeBarrier(&v->data[idx], val);
writeBarrier(getGC(), &v->data[idx], val);
return vec;
}

Expand Down

0 comments on commit a8c6d29

Please sign in to comment.