Skip to content

Commit

Permalink
Lazy initalize persistent engine on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
musm committed Aug 14, 2018
1 parent e128c0e commit b358204
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/MATLAB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ include("matfile.jl")
include("engine.jl")
include("matstr.jl")

if iswindows()
# workaround "primary message table for module 77" error
# creates a dummy Engine session and keeps it open so the libraries used by all other
# Engine clients are not loaded and unloaded repeatedly
# see: https://www.mathworks.com/matlabcentral/answers/305877-what-is-the-primary-message-table-for-module-77

# initialization is delayed untill first call to MSession
const persistent_msession_ref = Ref{MSession}()
const persistent_msession_assigned = Ref(false)

function assign_persistent_msession()
if persistent_msession_assigned[] == false
persistent_msession_assigned[] = true
persistent_msession_ref[] = MSession(0)
end
end
end

function __init__()

# initialize library paths
Expand Down Expand Up @@ -151,14 +169,6 @@ function __init__()
mat_put_variable[] = matfunc(:matPutVariable)
mat_get_dir[] = matfunc(:matGetDir)


if iswindows()
# workaround "primary message table for module 77" error
# creates a dummy Engine session and keeps it open so the libraries used by all other
# Engine clients are not loaded and unloaded repeatedly
# see: https://www.mathworks.com/matlabcentral/answers/305877-what-is-the-primary-message-table-for-module-77
global persistent_msession = MSession(0)
end
end


Expand Down
4 changes: 4 additions & 0 deletions src/engine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ mutable struct MSession
bufptr::Ptr{UInt8}

function MSession(bufsize::Integer = default_output_buffer_size)
if iswindows()
assign_persistent_msession()
end

ep = ccall(eng_open[], Ptr{Cvoid}, (Ptr{UInt8},), default_startcmd)
if ep == C_NULL
@warn "Confirm MATLAB is installed and discoverable."
Expand Down

0 comments on commit b358204

Please sign in to comment.