Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interact with R event loops #49

Closed
simonbyrne opened this issue Apr 15, 2015 · 9 comments
Closed

interact with R event loops #49

simonbyrne opened this issue Apr 15, 2015 · 9 comments

Comments

@simonbyrne
Copy link
Member

In order to get features like graphics windows working correctly, we need to be able to interact with the R event loops. Some details are covered in R-ext §8.

From what I understand, we need to implement (some of) the functionality provided by run_Rmainloop.

@randy3k
Copy link
Member

randy3k commented Apr 15, 2015

Eventloop was implemented in RCalling.jl and Rif.jl. See for example this link. The code was written in C, however, it should be easy to port it to Julia. I can submit a PR this afternoon.

@randy3k randy3k mentioned this issue Apr 16, 2015
@randy3k
Copy link
Member

randy3k commented Apr 16, 2015

Please checkout the current master branch for the eventloop. Basically, there are two functions: RCall.rgui_start() and RCall.rgui_stop().

@simonbyrne
Copy link
Member Author

Once we have callbacks sorted out, we could automate this using setHook.

@randy3k
Copy link
Member

randy3k commented Oct 22, 2015

In the eventloop, there was a dirty fix related to sigint. The problem was when a sigint is signaled from Julia (pressing Control - C), the R_interrupts_pending is set to state 1. Without a proper R eventloop, R_interrupts_pending remains state 1. And it causes segfault when evaluating R_ProcessEvents. So R_interrupts_pending was forced to be 0 in there.

For a while, I only think that it is needed for R_ProcessEvents. Until just now, I discovered this

julia> using RCall

julia> reval("gc()")
RCall.RObject{RCall.RealSxp}
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 219236 11.8     460000 24.6   350000 18.7
Vcells 322397  2.5     786432  6.0   786432  6.0


julia> unsafe_load(cglobal((:R_interrupts_pending,RCall.libR),Cint))
0

julia> ^C

julia> unsafe_load(cglobal((:R_interrupts_pending,RCall.libR),Cint))
1

julia> reval("gc()")
ERROR: RCall.jl 

 in error at error.jl:22
 in reval_p at /Users/Randy/.julia/v0.4/RCall/src/iface.jl:11
 in reval_p at /Users/Randy/.julia/v0.4/RCall/src/iface.jl:19
 in reval at /Users/Randy/.julia/v0.4/RCall/src/iface.jl:29
 in reval at /Users/Randy/.julia/v0.4/RCall/src/iface.jl:31

julia> unsafe_load(cglobal((:R_interrupts_pending,RCall.libR),Cint))
0

julia> reval("gc()")

signal (11): Segmentation fault: 11
sexp at /Users/Randy/.julia/v0.4/RCall/src/types.jl:332
jlcall_sexp_21406 at  (unknown line)
reval_p at /Users/Randy/.julia/v0.4/RCall/src/iface.jl:18
reval at /Users/Randy/.julia/v0.4/RCall/src/iface.jl:29
jlcall_reval_21501 at  (unknown line)
reval at /Users/Randy/.julia/v0.4/RCall/src/iface.jl:31
jl_apply at /opt/local/julia/src/interpreter.c:55
eval at /opt/local/julia/src/interpreter.c:213
jl_toplevel_eval_flex at /opt/local/julia/src/toplevel.c:527
jl_toplevel_eval_in at /opt/local/julia/src/builtins.c:579
eval_user_input at REPL.jl:62
jlcall_eval_user_input_21271 at  (unknown line)
anonymous at REPL.jl:92
jl_apply at /opt/local/julia/src/task.c:241
Selection: 

But interestingly, if reval("gc()") is replaced by rcall(:gc), it causes no issues.

It means that we might need to check the value of R_interrupts_pending constantly.

@randy3k
Copy link
Member

randy3k commented Oct 22, 2015

One possible solution is to disable signal handling

    if !Rinited
        unsafe_store!(cglobal((:R_SignalHandlers,libR),Cint),0)
        argv = ["REmbeddedJulia","--silent","--no-save"]
        i = ccall((:Rf_initEmbeddedR,libR),Cint,(Cint,Ptr{Ptr{UInt8}}),length(argv),argv)
        i > 0 || error("initEmbeddedR failed. Try restarting and running Pkg.build(\"RCall\").")
        global const Rproc = Rinstance(i)
    end

@randy3k
Copy link
Member

randy3k commented Dec 1, 2015

Though the eventloop implementation is still primitive, I believe this thread could be closed (at least for now).

@randy3k randy3k closed this as completed Dec 1, 2015
@simonbyrne
Copy link
Member Author

One thought: in the timer loop we could check if there are any open graphics devices, and if not, close the timer?

@randy3k
Copy link
Member

randy3k commented Dec 2, 2015

If I am correct, the timer loop is also needed for some X11 applications and they are not necessary graphics.

@simonbyrne
Copy link
Member Author

We could create a graphic-specific one (started by a hook), and a general (manual) one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants