[fpc-pascal] Stack problem in externally called callback function

Jonas Maebe jonas.maebe at elis.ugent.be
Mon Aug 3 10:25:35 CEST 2009

On 03 Aug 2009, at 08:49, Wimpie Nortje wrote:

> The FPC thread setup code (CAllocateThreadVars, InitThread) happens  
> inside the newly created thread before the user's thread function  
> starts.
> The only viable way I see to get support from the C library is for  
> it to inform FPC about a new thread after creation, but this means  
> that the C user's thread function has already started executing when  
> FPC gets the new ID. Is it possible to do the FPC thread setup from  
> outside the new thread?

No, because those functions need the thread ID. And e.g.  
CAllocateThreadVars calls pthread_setspecific(), which is defined as  
associating a value with the current thread.

I think the "easiest" way for you to do this would be to do something  
similar to what the FPC rtl does: write your own function that does  
all of this initialisation, and pass that one to pthread_create as the  
function to run in the new thread. Then from this function perform all  
initialisation, and finally call the original function.

> Suppose this setup can be completed before the library starts  
> calling FPC callbacks, would all the current memory problems then be  
> solved?

Only if you can also guarantee that all thread exits can be hooked by  
your library (so no direct pthread_exit() calls from the user  
function) and that you then call FPC's DoneThread.


More information about the fpc-pascal mailing list