Skip to content

Commit

Permalink
return previous GC state for gc_enable/gc_disable
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolewski committed Feb 27, 2015
1 parent d397abe commit 23a41de
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Library improvements

* Other improvements

* `gc_enable`, `gc_disable` returns previous GC state.

* `assert`, `@assert` now throws an `AssertionError` exception type ([#9734]).

* `convert` now checks for overflow when truncating integers or converting between
Expand Down
6 changes: 3 additions & 3 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ end

finalize(o::ANY) = ccall(:jl_finalize, Void, (Any,), o)

gc(full = true) = ccall(:jl_gc_collect, Void, (Int,), full ? 1 : 0)
gc_enable() = ccall(:jl_gc_enable, Void, ())
gc_disable() = ccall(:jl_gc_disable, Void, ())
gc(full::Bool=true) = ccall(:jl_gc_collect, Void, (Cint,), full)
gc_enable() = bool(ccall(:jl_gc_enable, Cint, ()))
gc_disable() = bool(ccall(:jl_gc_disable, Cint, ()))

bytestring(str::ByteString) = str

Expand Down
3 changes: 2 additions & 1 deletion doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -955,10 +955,11 @@ Internals

Disable garbage collection. This should be used only with extreme
caution, as it can cause memory use to grow without bound.
Returns previous GC state.

.. function:: gc_enable()

Re-enable garbage collection after calling :func:`gc_disable`.
Re-enable garbage collection after calling :func:`gc_disable`. Returns previous GC state.

.. function:: macroexpand(x)

Expand Down
14 changes: 12 additions & 2 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,18 @@ static void gc_verify(void)
// collector entry point and control

static int is_gc_enabled = 1;
DLLEXPORT void jl_gc_enable(void) { is_gc_enabled = 1; }
DLLEXPORT void jl_gc_disable(void) { is_gc_enabled = 0; }
DLLEXPORT int jl_gc_enable(void)
{
int prev = is_gc_enabled;
is_gc_enabled = 1;
return prev;
}
DLLEXPORT int jl_gc_disable(void)
{
int prev = is_gc_enabled;
is_gc_enabled = 0;
return prev;
}
DLLEXPORT int jl_gc_is_enabled(void) { return is_gc_enabled; }

DLLEXPORT int64_t jl_gc_total_bytes(void) { return total_allocd_bytes + allocd_bytes + collect_interval; }
Expand Down
4 changes: 2 additions & 2 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1132,8 +1132,8 @@ extern DLLEXPORT jl_gcframe_t *jl_pgcstack;

void jl_gc_init(void);
void jl_gc_setmark(jl_value_t *v);
DLLEXPORT void jl_gc_enable(void);
DLLEXPORT void jl_gc_disable(void);
DLLEXPORT int jl_gc_enable(void);
DLLEXPORT int jl_gc_disable(void);
DLLEXPORT int jl_gc_is_enabled(void);
DLLEXPORT int64_t jl_gc_total_bytes(void);
DLLEXPORT uint64_t jl_gc_total_hrtime(void);
Expand Down
8 changes: 8 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ let # test the process title functions, issue #9957
Sys.set_process_title(oldtitle)
@test Sys.get_process_title() == oldtitle
end


# test gc_enable/disable
@test gc_enable()
@test gc_disable()
@test gc_disable() == false
@test gc_enable() == false
@test gc_enable()

0 comments on commit 23a41de

Please sign in to comment.