calling EvalExpr from a posix thread

Are you developing an External using the LiveCode Externals SDK?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SoapDog
Posts: 84
Joined: Sun Apr 09, 2006 10:03 pm
Location: Niterói, RJ
Contact:

calling EvalExpr from a posix thread

Post by SoapDog » Thu Mar 05, 2009 12:58 am

Hello Folks,

I am building an external that makes use of pthreads. It is a web server external and it uses threads to multiplex connections. I am able to call External SDK functions from the main thread but not from the child threads. Can someone shed a light? I though posix threads shared data and environment so they should be able to call the external functions right?

Not all threads need to call revolution sdk calls, only when certain urls are called, in those cases, a callback should be dispatched. The calls from that thread fail with EXTERNAL_FAILURE.

I am lost here, can someone shed some light?

Andre
http://www.andregarzia.com

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Thu Mar 05, 2009 6:41 am

Unlike Java, the Revolutioon engine is inherently single-threaded. Even 'send in time' messages are queued up for handling at the first opportunity after their initial start timestamp.
I think the best soluton, even though it's for the long run, is asking the RunRev team to introduce a thread-safe 'EnqueueMessage' function which does something similar to 'send in time' - and if that mauy have to callback into your external, pass some sort of id so that you can park the pthread and revive it when needed.
Please keep me posted if you make any headway in this - it's an area that's caught my interest.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Post by Janschenkel » Thu Mar 05, 2009 7:01 am

Hmm, about five minutes after clicking 'Submit' I realized I had the correct conclusion, but a false premise :-)

The Revolution engine is multi-threaded when it comes to network I/O (socket commands with callback messages) so the following possible reasons for this limitation come to mind:
  • while network I/O is handled on a separate thread, the rest of the engine isn't thread-safe
  • whenever the network I/O thread has something ready to be handled in our scripts, it uses a synchronization mechanism to put the callbacks onto a thread-safe event queue, where the main thread will pick it up
  • externals are a special kind of problem in this regard, as the whole external call context is gone as soon as the main thread control returns to the Revolution engine
  • even the revXML external with its callback mechanism for an event-driven SAX parser, doesn't do this on a separate thread and hogs the main thread until it has parsed the entire XML tree or failed
So, happily speculating about the internal workings of the engine, if all the above is true, we would like for the team to make an 'EnqueueMessage' function available for those externals that want to spawn their own threads and call back into the main thread to trigger their own set of events.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply

Return to “Building Externals”