[fpc-pascal] SysSetCtrlBreakHandler

Tomas Hajny XHajT03 at hajny.biz
Tue Nov 29 23:13:16 CET 2016


On Tue, November 29, 2016 20:47, Dmitry Boyarintsev wrote:


Hello Dmitry,

> A question regarding your comment (
> http://bugs.freepascal.org/view.php?id=31023#c96393)
> Specifically ad 2)
>
> Is it fine to safe-guard access to SpecialKey / ScanCode through the whole
> crt unit? My only concern is that there're many places that has to be
> adjusted making the patch rather big, (I'm looking into using
> TRTLCriticalSection)

I don't know what's the best solution, but unit Crt isn't thread-safe (not
really supposed to be). If the handler runs in a different thread on MS
Windows, the risk of breaking the event processing is IMHO rather high
(well, probably partly depending on how badly the event loop is written -
it's extremely high if there are no delays between event availability
checks). Some alternative ideas coming to my mind:

1) Instead of using the special handler registered via
SetConsoleCtrlHandler (invoked in a special thread), isn't it possible to
process this event in the exception handler (running in the same thread
probably)?

2) Instead of directly updating the ScanCode and SpecialCode variables
(accessed directly during each and every ReadKey/KeyPressed call), maybe
you could add a new variable specifically for this purpose which would
signalize readiness of this special key event. ReadKey and KeyPressed
could then check this variable if and only if they are not in the middle
of combined key processing yet (before trying to read / poll additional
events from the keyboard driver) and update SpecialKey and ScanCode
themselves (inside the main thread already - then the update from the
other thread becomes an atomic operation and thus probably safe).

Maybe there are other options, I don't know.

Tomas





More information about the fpc-pascal mailing list