Skip to content

Commit

Permalink
Forward any remaining definitions in Singular to Sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbehrends committed Aug 21, 2020
1 parent c0c6ea4 commit 2a1946f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Singular_jll = "43d676ae-4934-50ba-8046-7a96366d613b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
lib4ti2_jll = "1493ae25-0f90-5c0e-a06c-8c5077d6d66f"
libsingular_julia_jll = "ae4fbd8f-ecdb-54f8-bbce-35570499b30e"
SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"

[compat]
AbstractAlgebra = "^0.10, 0.11"
Expand Down
49 changes: 39 additions & 10 deletions src/Sync.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# This module provides an automatic wrapping mechanism for Singular
# that supports mutual exclusion in multi-threaded code.
#
# In order to use it, simply substitute calls to
#
# Singular.f(...)
#
# with calls to:
#
# Singular.Sync.f(...)
#
# Future versions may offer automatic locking for Singular by default
# in multi-threaded programs.

module Sync

# import the normal modules in use by Singular so that forwarded
# definitions have access to the necessary types defined by them.

using Singular
using AbstractAlgebra
using Nemo
Expand All @@ -13,6 +30,9 @@ module Sync
import Base: reduce

const _mutex = ReentrantLock()
const _reserved = Set([
:eval, :include, :__init__,
])

function _lock()
Base.lock(_mutex)
Expand Down Expand Up @@ -217,11 +237,17 @@ module Sync
end
end

function _forward_value(mod :: Module, name :: Symbol)
if !(Base.isbindingresolved(Sync, name))
code = :( const $(name) = $(mod).$(name) )
Sync.eval(code)
end
end

function _wrap_module(mod :: Module)
reserved = Set([:eval, :include, :__init__])

function find_def(name)
if '#' in string(name) || name in reserved
if '#' in string(name) || name in _reserved
return nothing
end
try
Expand All @@ -232,23 +258,27 @@ module Sync
end


# num_errors = 0
errors = []
unwrapped = Set{Symbol}()
for name in names(mod; all = true)
def = find_def(name)
if def isa Function
try
_wrap_methods(mod, def)
catch
_forward_value(mod, name)
push!(errors, name)
# debugging code
println("error wrapping $name[$method_num]")
for (exception, backtrace) in Base.catch_stack()
# showerror(stdout, exception, backtrace)
# println()
end
# println("error wrapping $name[$method_num]")
# for (exception, backtrace) in Base.catch_stack()
# showerror(stdout, exception, backtrace)
# println()
# end
end
elseif def !== nothing
_forward_value(mod, name)
end
end
# println("error count: $(num_errors)")
end

end
Expand All @@ -258,7 +288,6 @@ end
display(x::Int) = println("Int: $x")
display(x::String) = println("String: $x")
export display
# f(x::T, Y::U) where {T, U} = 0
display2(;alpha = 1, kw...) = println("kw: alpha = $(alpha); $(kw)")
display3(x::T) where {T <: Int} = println("varargs: $(x)")
display4(x...) where Q = println(x)
Expand Down

0 comments on commit 2a1946f

Please sign in to comment.