Skip to content

Commit b358204

Browse files
committed
Lazy initalize persistent engine on Windows
1 parent e128c0e commit b358204

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/MATLAB.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ include("matfile.jl")
4646
include("engine.jl")
4747
include("matstr.jl")
4848

49+
if iswindows()
50+
# workaround "primary message table for module 77" error
51+
# creates a dummy Engine session and keeps it open so the libraries used by all other
52+
# Engine clients are not loaded and unloaded repeatedly
53+
# see: https://www.mathworks.com/matlabcentral/answers/305877-what-is-the-primary-message-table-for-module-77
54+
55+
# initialization is delayed untill first call to MSession
56+
const persistent_msession_ref = Ref{MSession}()
57+
const persistent_msession_assigned = Ref(false)
58+
59+
function assign_persistent_msession()
60+
if persistent_msession_assigned[] == false
61+
persistent_msession_assigned[] = true
62+
persistent_msession_ref[] = MSession(0)
63+
end
64+
end
65+
end
66+
4967
function __init__()
5068

5169
# initialize library paths
@@ -151,14 +169,6 @@ function __init__()
151169
mat_put_variable[] = matfunc(:matPutVariable)
152170
mat_get_dir[] = matfunc(:matGetDir)
153171

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

164174

src/engine.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ mutable struct MSession
1616
bufptr::Ptr{UInt8}
1717

1818
function MSession(bufsize::Integer = default_output_buffer_size)
19+
if iswindows()
20+
assign_persistent_msession()
21+
end
22+
1923
ep = ccall(eng_open[], Ptr{Cvoid}, (Ptr{UInt8},), default_startcmd)
2024
if ep == C_NULL
2125
@warn "Confirm MATLAB is installed and discoverable."

0 commit comments

Comments
 (0)