External Callback Question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

External Callback Question

Andre Schnoor
Hi all,

does anyone know how to call Smalltalk from an /arbitrary/ native thread
(already existing and running) in C code via a CCallback thunk? It looks
like this only works when the thread was previously created by ST for
the purpose of a non-blocking external call.

A workaround could be to create a native "worker" thread with THAPI by
sending an external call with <C: _threaded ... >. The worker runs
infinitely until the VM quits (assuming there's no timeout). Meanwhile
it accepts "jobs" from arbitrary other native threads, calling back into
the VI on their behalf and returning the result to the asking thread.

This however seems rather oversized to me. I assume there must be a
simpler solution.

Any suggestions?

(I want arbitrary native threads to call Smalltalk code for services and
receive the results)

Andre

Reply | Threaded
Open this post in threaded view
|

Fwd: External Callback Question

Eliot Miranda-2


---------- Forwarded message ----------
From: Eliot Miranda <[hidden email]>
Date: Oct 1, 2007 2:39 PM
Subject: Re: External Callback Question
To: Andre Schnoor <[hidden email]>

THAPI maintains a (set of) foreign thread calback worker process(es) for handling these callbacks.  The processes get created as needed, meaning at least one exists at all times and subsequent ones will be created as needed when and if there are multiple concurrent callbacks from foreign threads.  So u don't ave to do anything other than get a calback thunk to a thread and make the callback.

See NBAPIInvokeCallback and IsForeign in ext/exThapi.c


On 10/1/07, Andre Schnoor <[hidden email]> wrote:
Hi all,

does anyone know how to call Smalltalk from an /arbitrary/ native thread
(already existing and running) in C code via a CCallback thunk? It looks
like this only works when the thread was previously created by ST for
the purpose of a non-blocking external call.

A workaround could be to create a native "worker" thread with THAPI by
sending an external call with <C: _threaded ... >. The worker runs
infinitely until the VM quits (assuming there's no timeout). Meanwhile
it accepts "jobs" from arbitrary other native threads, calling back into
the VI on their behalf and returning the result to the asking thread.

This however seems rather oversized to me. I assume there must be a
simpler solution.

Any suggestions?

(I want arbitrary native threads to call Smalltalk code for services and
receive the results)

Andre


Reply | Threaded
Open this post in threaded view
|

Re: Fwd: External Callback Question

Andre Schnoor
Eliot,

    thanks for your quick reply. Yep, I exactly did what you described: Create a thunk (Smalltalk) and use it from a foreign thread in the VM as a C function. Works great 99% of the time (BTW: THAPI is a masterpiece), but occasionally it fails on a slow PPC Mac. Upon NBAPICallbackReturn

    assert( SameThreads(smalltalkThread, NBAPIThisThread()) )

fails, leading to havoc. This looks to me as if the VM's execution thread ("smalltalkThread"), which is spun off the Cocoa main thread at boot time ("startSupervisor() ... startKernel() ... initNBAPI() ), gets swapped with some other thread early in a session ;-)  Weird.

I now coded my own worker thread, which is started by <C: int _threaded runWorkerThread()> and thus attached to a Smalltalk process from the beginning. That one is guaranteed to never change. Although there's a small overhead of two semaphores for thread sychronization, it works fine now ;-)

Cheers
Andre



Eliot Miranda wrote:


---------- Forwarded message ----------
From: Eliot Miranda <[hidden email]>
Date: Oct 1, 2007 2:39 PM
Subject: Re: External Callback Question
To: Andre Schnoor <[hidden email]>

THAPI maintains a (set of) foreign thread calback worker process(es) for handling these callbacks.  The processes get created as needed, meaning at least one exists at all times and subsequent ones will be created as needed when and if there are multiple concurrent callbacks from foreign threads.  So u don't ave to do anything other than get a calback thunk to a thread and make the callback.

See NBAPIInvokeCallback and IsForeign in ext/exThapi.c


On 10/1/07, Andre Schnoor <[hidden email]> wrote:
Hi all,

does anyone know how to call Smalltalk from an /arbitrary/ native thread
(already existing and running) in C code via a CCallback thunk? It looks
like this only works when the thread was previously created by ST for
the purpose of a non-blocking external call.

A workaround could be to create a native "worker" thread with THAPI by
sending an external call with <C: _threaded ... >. The worker runs
infinitely until the VM quits (assuming there's no timeout). Meanwhile
it accepts "jobs" from arbitrary other native threads, calling back into
the VI on their behalf and returning the result to the asking thread.

This however seems rather oversized to me. I assume there must be a
simpler solution.

Any suggestions?

(I want arbitrary native threads to call Smalltalk code for services and
receive the results)

Andre



-- 
Andre Schnoor
Cognitone GmbH
www.cognitone.com