Skip to content

Commit

Permalink
lazy LibGit2 initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jul 13, 2018
1 parent a5a4e99 commit 9fa52ad
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ precompile(Tuple{typeof(Core.Compiler.length), Tuple{typeof(Base.:(*)), Int64}})
precompile(Tuple{typeof(Core.Compiler.length), Tuple{typeof(Base.open_flags)}})
precompile(Tuple{typeof(Core.Compiler.vect), Type{typeof(typeassert)}})
precompile(Tuple{typeof(Distributed.terminate_all_workers)})
precompile(Tuple{typeof(LibGit2.__init__)})
precompile(Tuple{typeof(LibGit2.initialize)})
precompile(Tuple{typeof(Logging.__init__)})
precompile(Tuple{typeof(Logging.default_metafmt), Base.CoreLogging.LogLevel, Module, Symbol, Symbol, String, Int64})
precompile(Tuple{typeof(Logging.default_metafmt), Base.CoreLogging.LogLevel, Nothing, Symbol, Symbol, String, Int64})
Expand Down
8 changes: 4 additions & 4 deletions stdlib/LibGit2/src/LibGit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export with, GitRepo, GitConfig
const GITHUB_REGEX =
r"^(?:git@|git://|https://(?:[\w\.\+\-]+@)?)github.com[:/](([^/].+)/(.+?))(?:\.git)?$"i

const REFCOUNT = Threads.Atomic{UInt}()
const REFCOUNT = Threads.Atomic{UInt}(0)

include("utils.jl")
include("consts.jl")
Expand Down Expand Up @@ -972,12 +972,12 @@ function set_ssl_cert_locations(cert_loc)
Cint(Consts.SET_SSL_CERT_LOCATIONS), cert_file, cert_dir)
end

function __init__()
@check ccall((:git_libgit2_init, :libgit2), Cint, ())
function initialize()
REFCOUNT[] = 1
@check ccall((:git_libgit2_init, :libgit2), Cint, ())

atexit() do
if Threads.atomic_sub!(REFCOUNT, UInt(1)) == 1
if Threads.atomic_sub!(REFCOUNT, UInt(1)) >= 1
# refcount zero, no objects to be finalized
ccall((:git_libgit2_shutdown, :libgit2), Cint, ())
end
Expand Down
1 change: 1 addition & 0 deletions stdlib/LibGit2/src/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ end # Error module

macro check(git_func)
quote
REFCOUNT[] == 0 && initialize()
err = Cint($(esc(git_func::Expr)))
if err < 0
throw(Error.GitError(err))
Expand Down

0 comments on commit 9fa52ad

Please sign in to comment.