Skip to content

Commit 5e520f4

Browse files
committed
Refactoring write barrier code
1 parent 56aed62 commit 5e520f4

File tree

4 files changed

+91
-33
lines changed

4 files changed

+91
-33
lines changed

src/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ UV_HEADERS += uv/*.h
123123
endif
124124
PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,work-stealing-queue.h gc-interface.h gc-tls-common.h julia.h julia_assert.h julia_threads.h julia_fasttls.h julia_locks.h julia_atomics.h jloptions.h)
125125
ifneq (${MMTK_PLAN},None)
126-
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,gc-tls-mmtk.h)
126+
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,gc-tls-mmtk.h gc-wb-mmtk.h)
127127
else
128-
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,gc-tls-stock.h)
128+
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,gc-tls-stock.h gc-wb-stock.h)
129129
endif
130130
ifeq ($(OS),WINNT)
131131
PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,win32_ucontext.h)

src/gc-wb-mmtk.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
/*
4+
write barriers which should be inlined by the compiler
5+
*/
6+
7+
#ifndef JL_GC_WB_H
8+
#define JL_GC_WB_H
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
// GC write barriers
15+
16+
// TODO: implement these functions for MMTk
17+
STATIC_INLINE void jl_gc_wb(const void *parent, const void *ptr) JL_NOTSAFEPOINT
18+
{
19+
}
20+
21+
STATIC_INLINE void jl_gc_wb_back(const void *ptr) JL_NOTSAFEPOINT // ptr isa jl_value_t*
22+
{
23+
}
24+
25+
STATIC_INLINE void jl_gc_multi_wb(const void *parent, const jl_value_t *ptr) JL_NOTSAFEPOINT
26+
{
27+
}
28+
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif
33+
34+
#endif

src/gc-wb-stock.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
/*
4+
write barriers which should be inlined by the compiler
5+
*/
6+
7+
#ifndef JL_GC_WB_H
8+
#define JL_GC_WB_H
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
// GC write barriers
15+
16+
STATIC_INLINE void jl_gc_wb(const void *parent, const void *ptr) JL_NOTSAFEPOINT
17+
{
18+
// parent and ptr isa jl_value_t*
19+
if (__unlikely(jl_astaggedvalue(parent)->bits.gc == 3 /* GC_OLD_MARKED */ && // parent is old and not in remset
20+
(jl_astaggedvalue(ptr)->bits.gc & 1 /* GC_MARKED */) == 0)) // ptr is young
21+
jl_gc_queue_root((jl_value_t*)parent);
22+
}
23+
24+
STATIC_INLINE void jl_gc_wb_back(const void *ptr) JL_NOTSAFEPOINT // ptr isa jl_value_t*
25+
{
26+
// if ptr is old
27+
if (__unlikely(jl_astaggedvalue(ptr)->bits.gc == 3 /* GC_OLD_MARKED */)) {
28+
jl_gc_queue_root((jl_value_t*)ptr);
29+
}
30+
}
31+
32+
STATIC_INLINE void jl_gc_multi_wb(const void *parent, const jl_value_t *ptr) JL_NOTSAFEPOINT
33+
{
34+
// 3 == GC_OLD_MARKED
35+
// ptr is an immutable object
36+
if (__likely(jl_astaggedvalue(parent)->bits.gc != 3))
37+
return; // parent is young or in remset
38+
if (__likely(jl_astaggedvalue(ptr)->bits.gc == 3))
39+
return; // ptr is old and not in remset (thus it does not point to young)
40+
jl_datatype_t *dt = (jl_datatype_t*)jl_typeof(ptr);
41+
const jl_datatype_layout_t *ly = dt->layout;
42+
if (ly->npointers)
43+
jl_gc_queue_multiroot((jl_value_t*)parent, ptr, dt);
44+
}
45+
46+
#ifdef __cplusplus
47+
}
48+
#endif
49+
50+
#endif

src/julia.h

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,37 +1181,11 @@ JL_DLLEXPORT void jl_free_stack(void *stkbuf, size_t bufsz);
11811181
// thread-local allocator of the current thread.
11821182
JL_DLLEXPORT jl_weakref_t *jl_gc_new_weakref(jl_value_t *value);
11831183

1184-
// GC write barriers
1185-
1186-
STATIC_INLINE void jl_gc_wb(const void *parent, const void *ptr) JL_NOTSAFEPOINT
1187-
{
1188-
// parent and ptr isa jl_value_t*
1189-
if (__unlikely(jl_astaggedvalue(parent)->bits.gc == 3 /* GC_OLD_MARKED */ && // parent is old and not in remset
1190-
(jl_astaggedvalue(ptr)->bits.gc & 1 /* GC_MARKED */) == 0)) // ptr is young
1191-
jl_gc_queue_root((jl_value_t*)parent);
1192-
}
1193-
1194-
STATIC_INLINE void jl_gc_wb_back(const void *ptr) JL_NOTSAFEPOINT // ptr isa jl_value_t*
1195-
{
1196-
// if ptr is old
1197-
if (__unlikely(jl_astaggedvalue(ptr)->bits.gc == 3 /* GC_OLD_MARKED */)) {
1198-
jl_gc_queue_root((jl_value_t*)ptr);
1199-
}
1200-
}
1201-
1202-
STATIC_INLINE void jl_gc_multi_wb(const void *parent, const jl_value_t *ptr) JL_NOTSAFEPOINT
1203-
{
1204-
// 3 == GC_OLD_MARKED
1205-
// ptr is an immutable object
1206-
if (__likely(jl_astaggedvalue(parent)->bits.gc != 3))
1207-
return; // parent is young or in remset
1208-
if (__likely(jl_astaggedvalue(ptr)->bits.gc == 3))
1209-
return; // ptr is old and not in remset (thus it does not point to young)
1210-
jl_datatype_t *dt = (jl_datatype_t*)jl_typeof(ptr);
1211-
const jl_datatype_layout_t *ly = dt->layout;
1212-
if (ly->npointers)
1213-
jl_gc_queue_multiroot((jl_value_t*)parent, ptr, dt);
1214-
}
1184+
#ifndef MMTK_GC
1185+
#include "gc-wb-stock.h"
1186+
#else
1187+
#include "gc-wb-mmtk.h"
1188+
#endif
12151189

12161190
JL_DLLEXPORT void jl_gc_safepoint(void);
12171191
JL_DLLEXPORT int jl_safepoint_suspend_thread(int tid, int waitstate);

0 commit comments

Comments
 (0)