|We don't bite newbies here... much|
XS replacing my c library, not compatible with OS threadsby patcat88 (Deacon)
|on Nov 30, 2010 at 18:49 UTC||Need Help??|
patcat88 has asked for the
wisdom of the Perl Monks concerning the following question:
I'm writing an XS module and Perl seems to be rewriting my C standard library from Visual Studio CRT on win32. malloc() free() and exit() are being redefined to perl specific versions, which can not work inside the thread. Why is Perl rewriting my C standard library functions? I know Perl has its internal C library from perlclib, so why are my C library's functions being redefined? Perl functions never work outside the Perl thread (no "context"), so the moment I try using a C library function in the other thread, they crash. The free() will always crash. The code sample is not compilable. Its shortened from something much bigger.
"exit(1);" becomes "(*(*Perl_IProc_ptr(((PerlInterpreter *)Perl_get_context())))->pExit)((*Perl_IProc_ptr(((PerlInterpreter *)Perl_get_context()))), (1));" everywhere
printf stays the same everywhere
"free(eventPtr);" becomes "(*(*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context())))->pFree)((*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context()))), (eventPtr));"
"evtHandlePtr = malloc(sizeof(HANDLE));" becomes "evtHandlePtr = (*(*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context())))->pMalloc)((*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context()))), (sizeof(HANDLE)));"
I need to be able to allocate memory inside the XS func, then eventually free it from the thread. The ultimate goal is a timeout feature that creates a thread to cancel a blocking function that is called in the XS func. The full timeout code inside the thread crashes on the free() always. The blocking function sometimes might never return hanging forever due to bugs in the that closed source library. The developer of the library suggests making another thread to end the job if a timeout period elapses without the blocking function returning (and then my XS function telling the timeout thread not to kill the job through a Win32 Event object).
But how can I use my native OS (windows)'s C library when Perl is redefining it?
The exit(1)s are supposed to be basically graceful crashes, but they would crash really hard if they ran inside the thread since perl doesn't exist inside that thread. I'm not sure if Perl's exit replacement will continue to execute destroyers and more perl code or not.
Also this closed source library I'm trying to wrap into XS, has errata that it will overrun the buffer you gave it in rare cases, and it returns an error code that it overran its buffer and you need to exit now since your stack and/or heap are corrupted.
How can I get a hard exit that is guaranteed not to run any more Perl code, or is basically a kill on the process?