Re: [Factor-talk] Ctrl+Break

2015-10-12 Thread Björn Lindqvist
2015-10-01 23:12 GMT+02:00 Alexander Ilin :
> Hello!
>
> 01.10.2015, 22:33, "HP wei" :
>> 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


Re: [Factor-talk] Ctrl+Break

2015-10-02 Thread Jon Harper
I don't use windows normally, but I did a quick test and it worked.
Double click factor.com or run it from a terminal, enter "[ t ] loop"
in the listener and then ctrl-c and t in the terminal and it
interrupts.

For example, if you do "[ 1 seconds sleep t ] loop" and do the same
factor will most likely die. Similarly, if you do this with code
that's doing a lot of IO, it probably won't work because the exception
will happen in the wrong thread.
Jon


On Fri, Oct 2, 2015 at 1:34 PM, Alexander Ilin  wrote:
> Are we still talking about Factor on Windows?
>
> 02.10.2015, 13:11, "Jon Harper" :
>> You can also hit CTRL-C on the terminal, then press 't' to throw an 
>> exception in factor. If the listener is doing a busy loop, it will get the 
>> exception and it should work. If the exception goes to another thread 
>> because the listener thread yields, it can kill the whole process, so you 
>> have to use with care..
>
> ---=---
> Александр
>
> --
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk

--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Ctrl+Break

2015-10-02 Thread Alexander Ilin
Are we still talking about Factor on Windows?

02.10.2015, 13:11, "Jon Harper" :
> You can also hit CTRL-C on the terminal, then press 't' to throw an exception 
> in factor. If the listener is doing a busy loop, it will get the exception 
> and it should work. If the exception goes to another thread because the 
> listener thread yields, it can kill the whole process, so you have to use 
> with care..

---=---
Александр

--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Ctrl+Break

2015-10-02 Thread Jon Harper
You can also hit CTRL-C on the terminal, then press 't' to throw an
exception in factor. If the listener is doing a busy loop, it will get the
exception and it should work. If the exception goes to another thread
because the listener thread yields, it can kill the whole process, so you
have to use with care..

Jon

On Thu, Oct 1, 2015 at 11:30 PM, Alexander Ilin  wrote:

> Hello!
>
>   You could have that as a library function, not necessarily written in
> Factor.
>
>   The Listener already catches and handles all exceptions raised by
> running user code, am I right?
>
> 02.10.2015, 00:19, "John Benediktsson" :
> > you can open another listener and suspend the thread of the first
> >
> > we don't have a way to kill threads:
> >
> > https://github.com/slavapestov/factor/issues/1387
> >
> > or wait/join threads:
> >
> > https://github.com/slavapestov/factor/issues/1407
> >
> > On Thu, Oct 1, 2015 at 2:12 PM, Alexander Ilin  wrote:
> >> Hello!
> >>
> >> 01.10.2015, 22:33, "HP wei" :
> >>> 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?
> >>
> >>   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).
> >>
> >> ---=---
> >> Александр
> >>
> >>
> --
> >> ___
> >> Factor-talk mailing list
> >> Factor-talk@lists.sourceforge.net
> >> https://lists.sourceforge.net/lists/listinfo/factor-talk
> > ,
> >
> >
> --
> > ,
> >
> > ___
> > Factor-talk mailing list
> > Factor-talk@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
> ---=---
> Александр
>
>
> --
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Ctrl+Break

2015-10-01 Thread Alexander Ilin
Hello!

01.10.2015, 22:33, "HP wei" :
> 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?

  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).

---=---
Александр

--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Ctrl+Break

2015-10-01 Thread Alexander Ilin
Hello!

  You could have that as a library function, not necessarily written in Factor.

  The Listener already catches and handles all exceptions raised by running 
user code, am I right?

02.10.2015, 00:19, "John Benediktsson" :
> you can open another listener and suspend the thread of the first
>
> we don't have a way to kill threads:
>
>     https://github.com/slavapestov/factor/issues/1387
>
> or wait/join threads:
>
>     https://github.com/slavapestov/factor/issues/1407
>
> On Thu, Oct 1, 2015 at 2:12 PM, Alexander Ilin  wrote:
>> Hello!
>>
>> 01.10.2015, 22:33, "HP wei" :
>>> 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?
>>
>>   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).
>>
>> ---=---
>> Александр
>>
>> --
>> ___
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
> ,
>
> --
> ,
>
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk


---=---
Александр

--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk