Threads not created with BeginThread can't make use of FPC's RTL
Original Reporter info from Mantis: PascalDragon @PascalDragon
-
Reporter name: Sven Barth
Original Reporter info from Mantis: PascalDragon @PascalDragon
- Reporter name: Sven Barth
Description:
This issue is Windows only, but might apply to other platforms as well.
If a thread, which is not created by BeginThread/TThread, executes Pascal code (common case: a thread created in a library calling a Pascal callback), this code can not make use of FPC's RTL (like exceptions and I/O), because its subsystems have to be initialized per thread.
Calling InitThread at the beginning of such Pascal code solves this issue.
A possible solution without calling InitThread manually:
The problem might be solved if a call to InitThread is placed inside SysRelocateThreadVar (inside the "if dataindex=nil" branch) in win/systhrd.inc, because the broken subsystems rely on threadvars and those are accessed by calling FPC_THREADVAR_RELOCATE (to which the value of SysRelocateThreadVar is assigned). Thus the thread should be initialised once such a "subsystem threadvar" is accessed.
But I haven't tested this and don't know if it would solve all subsystem problems.
Steps to reproduce:
Attached is a example based on Alexander Grau's code on the Lazarus mailing list (link to thread see below).
There are two constants at the top of the code which influence the behavior:
USE_EXT_THREADS:
- false: Threads are created with a TThread class. The Execute method works as expected.
- true: Threads are created using a direct call to CreateThread. The "result" depends on the setting of USE_FPC_INIT
USE_FPC_INIT:
- false: all threads terminate uncatchable at the Writeln in ExternalThread. Wine debug output tells me that an unhandled exception has occured.
- true: same result as with USE_EXT_THREADS=false. All threads run as expected.
Additional information:
See this thread on the Lazarus mailing list:
http://lists.lazarus.freepascal.org/pipermail/lazarus/2010-August/054727.html
Mantis conversion info:
- Mantis ID: 17300
- OS: Win32
- OS Build: NT 6.1 (7)
- Build: 15917
- Platform: i386
- Version: 2.5.1
- Monitored by: » Giel (Giel), » @joshyfun (José Mejuto), » AntonK (Anton Kavalenka)