[gambit-list] Gambit and Thread safity

tcl super dude tcleval at gmail.com
Tue Mar 12 12:08:18 EDT 2013


I am trying to develop a gambit-c image processing app on android. My
approach is to develop all the GUI related code in java and all image
processing code in scheme. So far, I tested my scheme code on the PC and I
am about to migrate it to android. I use a remote repl to develop on
android. The java class run as an Android Service so it is not directly
connected to an Activity (GUI). This way I can still send code while using
different windows (Activities). There are 2 threads on the Service.

Thread1 - java
Thread2 - gambit (using a heartbeat to resume gambit threads)

from the remote repl side everything is fine, coz I am talking only with
gambit using TCP. Now I want Java to talk to gambit and back. I know JNI,
but my concern is with thread safety. what I want:

1 - I want to call a gambit eval function from java so I can execute
arbitrary scheme code from my service on the java side.

2 - I want to eval_scheme(String code) to not block the java thread

3 - I want it to be safe (I don't want the code to deadlock or crash)

Let me explain what I have so far:

1 - I can eval any scheme code. I just wrote a c-define of eval plus some
exception handling on scheme side. It works so far, and even show errors on
LOGCAT (android error, debug and info log facility)

2 - eval_scheme does not block if I create a scheme thread and yield, so it
will actually execute the scheme procedure on the next heartbeat (so
eval_scheme returns immediately)

3 - I don't know if what I am doing is thread safe. I mean, when I can
eval_scheme it uses a scheme procedure which I don't know if is safe. I
mean, I am executing two peaces of gambit code in two different system
threads here. (1) the gambit interpreter/repl on its own system thread, and
(2) eval_scheme() on the java thread (the second system thread).

is eval_scheme() thread safe? Can I execute it in two different system
(remember eval_scheme () is just scheme eval from the C/java/JNI side)

Just before wrote this e-mail I was driving back home while thinking about
a solution in case what I am coding is the wrong approach. I though I could
code eval_scheme to change an attribute of the Java Service class, a String
for example(or maybe a list of those), and that string to be the code
gambit has to execute next. In the gambit side I would have a scheme thread
checking this string once in while (every 200ms for example) and of course
making sure that gambit system thread is the only one accessing the java
string (MonitorEnter/MonitorExit JNI functions) and then execute the code
if the string is not null, and set the string to null after reading it and
before executing the scheme code (which could take a while).

This way I would not need to execute a gambit procedure from the java

I'd like to know what you guys thing about the second approach to the
problem too.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://webmail.iro.umontreal.ca/pipermail/gambit-list/attachments/20130312/645d045e/attachment.html 

More information about the Gambit-list mailing list