2015-10-01 23:12 GMT+02:00 Alexander Ilin <ajs...@yandex.ru>: > Hello! > > 01.10.2015, 22:33, "HP wei" <hpwe...@gmail.com>: >> I try to hit Control-C but it continues to run. >> *** How to exit a running words ? > > HP wei raises a very good point. Is there a way to interrupt an > infinite loop or a long-running word?
Yep. Ctrl+C is supposed to always interrupt the currently running thread. > If such a mechanism is not there in Factor UI, I could share a way > to implement it (Windows-specific). > > It involves running a native background thread with the Ctrl+Break > hotkey globally hooked. When the hotkey is triggered, the main > thread is interrupted by setting its exception flag. All words > running from the Scratchpad should expect this special kind of > exception. I'm not too good with Factor yet, so I could not > implement this without some serious help/pointers, but I can share > the details of the same mechanism implemented elsewhere (an > open-source run-time environment). It works already, mostly. Roughly in the same way as you are describing it. On windows, there is code in vm/os-windows.cpp that sets the consoles ctrl key handler. So when ctrl+c is pressed, it enqueues a safepoint. Meaning that it sets the write protection on a memory segment. Threads periodically (like once per loop iteration) write to that segment which causes segfaults which is how they are stopped. The details are in vm/safepoints.cpp. Then Factor brings up a very simple CLI in which the VM state can be inspected and the threads continued. It is implemented in factor_vm::factorbug() in vm/debug.cpp. But the Console Ctrl Handler is only for the console. So when ctrl+c is pressed in the GUI it is not even heard by the system. So no safepoint is enqueued and no threads are ever stopped. I'd be happy to hear if you know of a way to send ctrl+c to a GUI program but I don't think there is one. The situation is the same on Linux. On the console, ctrl+c generates a SIGINT signal but not when the GUI is run. Running a Factor thread in the background that listens for ctrl+c won't do much good unfortunately because Factor's threading is cooperative and not preemptive. So, if you run a busy loop such as [ t ] [ ] while, then that thread will starve out all others. When we get preemptive multitasking, aka kernel threads, into Factor this problem would be trivial to fix but right now it is very hard. -- mvh/best regards Björn Lindqvist ------------------------------------------------------------------------------ _______________________________________________ Factor-talk mailing list Factor-talk@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/factor-talk