comp.lang.ada
 help / color / mirror / Atom feed
* Catching Ctrl-C SIGINT
@ 2005-01-17 12:26 Christopher Broeg
  2005-01-17 14:19 ` Martin Dowie
  2005-01-17 21:37 ` Simon Wright
  0 siblings, 2 replies; 6+ messages in thread
From: Christopher Broeg @ 2005-01-17 12:26 UTC (permalink / raw)


Hi,

I am having some trouble with protected types. I am trying to do some 
cleanup work if the user presses Ctrl-C SIGINT.
For this I wrote a little test program. The problem is getting the 
information about the raised interrupt back to the main program. I tried 
using a little task that calls a entry in a protected type:

with Catch_Interrupt;

with Ada.Text_IO;use Ada.Text_IO;
procedure Test1 is
    Stop: exception;

    task Get_Info is
       entry Stop;
    end Get_info;
    task body Get_Info is
    begin
       loop
          Catch_Interrupt.Event.wait;
	 -- here comes the code that handles the interrupt received
          select
             accept Stop do
                Stop;
             end Stop;
          or
             terminate;
          end select;
       end loop;
    end Get_Info;


begin

    for i in 1 .. 5 loop
       Put(Integer'Image(i));
       delay 1.0;
    end loop;

end Test1;


the package actually doing the interrupt catching is:

with Ada.Interrupts;
with Ada.Interrupts.names;
package Catch_Interrupt is

    Sig_Int_Caught : exception;

    procedure Signal_Public;

    protected Catch_Interrupt is
       procedure Clean_up;
       pragma Unreserve_All_Interrupts;
       --pragma interrupt_Handler(Clean_Up);
       pragma Attach_Handler(Clean_Up,Ada.Interrupts.Names.SIGINT);
    end Catch_Interrupt;

    protected Event is
       entry Wait;
       procedure Signal;
    private
       Allowed : Boolean := False;
    end Event;
end Catch_Interrupt;


with Ada.Text_IO;
use Ada.Text_IO;
package body Catch_Interrupt is

    procedure Signal_Public is
    begin
       Event.Signal;
    end Signal_Public;


    protected body Catch_Interrupt is
       procedure Clean_up is
       begin
          Put_line("Interrupt SIGINT caught. sending signal ");
          Signal_public;
          Put_line("SIGNAL SENT. ");
       end Clean_Up;
    end Catch_Interrupt;

    protected body Event is
       entry Wait when allowed is
       begin
          Put_line("catch_interrupt msg: in entry wait");
       end Wait;
       procedure  Signal is
       begin
          Put_line("Signal received!!!");
          Allowed := True;
       end Signal;
    end Event;
end Catch_Interrupt;

Here the problem:
I do get the message
"Interrupt SIGINT caught. sending signal "
but the signal_public procedure never manages to send the signal. I 
don't know why. If I call the signal_public procedure from test1 it 
works, but is of course useless.

Can you give me any hints on what I am doing wrong? I am lost.

Maybe there is a better way to react upon the interrupt? But still I am 
wondering why my solution is not working.

Thanx a lot,

Chris



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Catching Ctrl-C SIGINT
@ 2005-01-17 14:07 Christopher Broeg
  0 siblings, 0 replies; 6+ messages in thread
From: Christopher Broeg @ 2005-01-17 14:07 UTC (permalink / raw)


Hi,

I am having some trouble with protected types. I am trying to do some 
cleanup work if the user presses Ctrl-C SIGINT.
For this I wrote a little test program. The problem is getting the 
information about the raised interrupt back to the main program. I tried 
using a little task that calls a entry in a protected type:

with Catch_Interrupt;

with Ada.Text_IO;use Ada.Text_IO;
procedure Test1 is
    Stop: exception;

    task Get_Info is
       entry Stop;
    end Get_info;
    task body Get_Info is
    begin
       loop
          Catch_Interrupt.Event.wait;
      -- here comes the code that handles the interrupt received
          select
             accept Stop do
                Stop;
             end Stop;
          or
             terminate;
          end select;
       end loop;
    end Get_Info;


begin

    for i in 1 .. 5 loop
       Put(Integer'Image(i));
       delay 1.0;
    end loop;

end Test1;


the package actually doing the interrupt catching is:

with Ada.Interrupts;
with Ada.Interrupts.names;
package Catch_Interrupt is

    Sig_Int_Caught : exception;

    procedure Signal_Public;

    protected Catch_Interrupt is
       procedure Clean_up;
       pragma Unreserve_All_Interrupts;
       --pragma interrupt_Handler(Clean_Up);
       pragma Attach_Handler(Clean_Up,Ada.Interrupts.Names.SIGINT);
    end Catch_Interrupt;

    protected Event is
       entry Wait;
       procedure Signal;
    private
       Allowed : Boolean := False;
    end Event;
end Catch_Interrupt;


with Ada.Text_IO;
use Ada.Text_IO;
package body Catch_Interrupt is

    procedure Signal_Public is
    begin
       Event.Signal;
    end Signal_Public;


    protected body Catch_Interrupt is
       procedure Clean_up is
       begin
          Put_line("Interrupt SIGINT caught. sending signal ");
          Signal_public;
          Put_line("SIGNAL SENT. ");
       end Clean_Up;
    end Catch_Interrupt;

    protected body Event is
       entry Wait when allowed is
       begin
          Put_line("catch_interrupt msg: in entry wait");
       end Wait;
       procedure  Signal is
       begin
          Put_line("Signal received!!!");
          Allowed := True;
       end Signal;
    end Event;
end Catch_Interrupt;

Here the problem:
I do get the message
"Interrupt SIGINT caught. sending signal "
but the signal_public procedure never manages to send the signal. I 
don't know why. If I call the signal_public procedure from test1 it 
works, but is of course useless.

Can you give me any hints on what I am doing wrong? I am lost.

Maybe there is a better way to react upon the interrupt? But still I am 
wondering why my solution is not working.

Thanx a lot,

Chris



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Catching Ctrl-C SIGINT
@ 2005-01-17 14:13 Christopher Broeg
  0 siblings, 0 replies; 6+ messages in thread
From: Christopher Broeg @ 2005-01-17 14:13 UTC (permalink / raw)


Hi,

I am having some trouble with protected types. I am trying to do some 
cleanup work if the user presses Ctrl-C SIGINT.
For this I wrote a little test program. The problem is getting the 
information about the raised interrupt back to the main program. I tried 
using a little task that calls a entry in a protected type:

with Catch_Interrupt;

with Ada.Text_IO;use Ada.Text_IO;
procedure Test1 is
    Stop: exception;

    task Get_Info is
       entry Stop;
    end Get_info;
    task body Get_Info is
    begin
       loop
          Catch_Interrupt.Event.wait;
      -- here comes the code that handles the interrupt received
          select
             accept Stop do
                Stop;
             end Stop;
          or
             terminate;
          end select;
       end loop;
    end Get_Info;


begin

    for i in 1 .. 5 loop
       Put(Integer'Image(i));
       delay 1.0;
    end loop;

end Test1;


the package actually doing the interrupt catching is:

with Ada.Interrupts;
with Ada.Interrupts.names;
package Catch_Interrupt is

    Sig_Int_Caught : exception;

    procedure Signal_Public;

    protected Catch_Interrupt is
       procedure Clean_up;
       pragma Unreserve_All_Interrupts;
       --pragma interrupt_Handler(Clean_Up);
       pragma Attach_Handler(Clean_Up,Ada.Interrupts.Names.SIGINT);
    end Catch_Interrupt;

    protected Event is
       entry Wait;
       procedure Signal;
    private
       Allowed : Boolean := False;
    end Event;
end Catch_Interrupt;


with Ada.Text_IO;
use Ada.Text_IO;
package body Catch_Interrupt is

    procedure Signal_Public is
    begin
       Event.Signal;
    end Signal_Public;


    protected body Catch_Interrupt is
       procedure Clean_up is
       begin
          Put_line("Interrupt SIGINT caught. sending signal ");
          Signal_public;
          Put_line("SIGNAL SENT. ");
       end Clean_Up;
    end Catch_Interrupt;

    protected body Event is
       entry Wait when allowed is
       begin
          Put_line("catch_interrupt msg: in entry wait");
       end Wait;
       procedure  Signal is
       begin
          Put_line("Signal received!!!");
          Allowed := True;
       end Signal;
    end Event;
end Catch_Interrupt;

Here the problem:
I do get the message
"Interrupt SIGINT caught. sending signal "
but the signal_public procedure never manages to send the signal. I 
don't know why. If I call the signal_public procedure from test1 it 
works, but is of course useless.

Can you give me any hints on what I am doing wrong? I am lost.

Maybe there is a better way to react upon the interrupt? But still I am 
wondering why my solution is not working.

Thanx a lot,

Chris



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching Ctrl-C SIGINT
  2005-01-17 12:26 Christopher Broeg
@ 2005-01-17 14:19 ` Martin Dowie
  2005-01-17 14:35   ` Christopher Broeg
  2005-01-17 21:37 ` Simon Wright
  1 sibling, 1 reply; 6+ messages in thread
From: Martin Dowie @ 2005-01-17 14:19 UTC (permalink / raw)


Christopher Broeg wrote:
[snip]
> Here the problem:
> I do get the message
> "Interrupt SIGINT caught. sending signal "
> but the signal_public procedure never manages to send the signal. I
> don't know why. If I call the signal_public procedure from test1 it
> works, but is of course useless.
>
> Can you give me any hints on what I am doing wrong? I am lost.
>
> Maybe there is a better way to react upon the interrupt? But still I
> am wondering why my solution is not working.

Not a good idea to call "Put_Line" from a protected object as it is a
(potentially blocking call).

Take out the "Put_Lines" and increment an unprotected unsigned integer
instead and from within a task poll say @1Hz and look for the integer
changing - i.e. try an look for a side-effect of the protected object doing
its job. You can always remove this task once you're convinced all is ok.

Cheers

-- Martin






^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching Ctrl-C SIGINT
  2005-01-17 14:19 ` Martin Dowie
@ 2005-01-17 14:35   ` Christopher Broeg
  0 siblings, 0 replies; 6+ messages in thread
From: Christopher Broeg @ 2005-01-17 14:35 UTC (permalink / raw)


Martin Dowie wrote:
> Christopher Broeg wrote:
> [snip]
> 
>>Here the problem:
>>I do get the message
>>"Interrupt SIGINT caught. sending signal "
>>but the signal_public procedure never manages to send the signal. I
>>don't know why. If I call the signal_public procedure from test1 it
>>works, but is of course useless.
>>
>>Can you give me any hints on what I am doing wrong? I am lost.
>>
>>Maybe there is a better way to react upon the interrupt? But still I
>>am wondering why my solution is not working.
> 
> 
> Not a good idea to call "Put_Line" from a protected object as it is a
> (potentially blocking call).
> 
> Take out the "Put_Lines" and increment an unprotected unsigned integer
> instead and from within a task poll say @1Hz and look for the integer
> changing - i.e. try an look for a side-effect of the protected object doing
> its job. You can always remove this task once you're convinced all is ok.
> 
> Cheers
> 
> -- Martin
> 
> 
> 

Thanx for the answer. I'll give it a try.

-- Chris



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching Ctrl-C SIGINT
  2005-01-17 12:26 Christopher Broeg
  2005-01-17 14:19 ` Martin Dowie
@ 2005-01-17 21:37 ` Simon Wright
  1 sibling, 0 replies; 6+ messages in thread
From: Simon Wright @ 2005-01-17 21:37 UTC (permalink / raw)


Christopher Broeg <broeg@astro.uni-jena.de~> writes:

>     protected body Catch_Interrupt is
>        procedure Clean_up is
>        begin
>           Put_line("Interrupt SIGINT caught. sending signal ");
>           Signal_public;
>           Put_line("SIGNAL SENT. ");
>        end Clean_Up;
>     end Catch_Interrupt;

I suspect that an interrupt handler isn't allowed to call an entry of
a PO?

(gdb) b exception
Breakpoint 1 at 0x8059012: file a-except.adb, line 1306.
(gdb) handle SIGINT pass
SIGINT is used by the debugger.
Are you sure you want to change it? (y or n) y

Signal        Stop      Print   Pass to program Description
SIGINT        Yes       Yes     Yes             Interrupt
(gdb) run
Starting program: /home/simon/tmp/broeg/test1 
[New Thread 1024 (LWP 4260)]
[New Thread 2049 (LWP 4261)]
[New Thread 1026 (LWP 4262)]
[New Thread 2051 (LWP 4263)]
[New Thread 3076 (LWP 4264)]
 1 2 3
Program received signal SIGINT, Interrupt.
[Switching to Thread 2051 (LWP 4263)]
0x400656b2 in sigsuspend () from /lib/libc.so.6
Current language:  auto; currently c
(gdb) c
Continuing.
 4Interrupt SIGINT caught. sending signal 

Breakpoint 1, PROGRAM_ERROR at 0x0804ab66 in <catch_interrupt__eventPT__signalP> (<_object>=@0x806f38c) at catch_interrupt.adb:27
27             procedure  Signal is
Current language:  auto; currently ada
(gdb) bt
#0  <__gnat_raise_nodefer_with_msg> (e=0x806b494) at a-except.adb:1306
#1  0x0805934b in ada.exceptions.raise_with_msg (e=0x806b494, setup=1)
    at a-except.adb:1480
#2  0x080591bb in <__gnat_raise_exception> (e=0x806b494, message=0x806871c)
    at a-except.adb:1348
#3  0x08055b2e in system__tasking__protected_objects__entries__lock_entries ()
#4  0x0804ab66 in <catch_interrupt__eventPT__signalP> (<_object>=@0x806f38c)
    at catch_interrupt.adb:27
#5  0x0804a7e1 in catch_interrupt.signal_public () at catch_interrupt.adb:9



   protected type Handling is
      entry Wait;
      procedure Trigger;
      pragma Interrupt_Handler (Trigger);
   private
      Ready : Boolean := False;
   end Handling;

   protected body Handling is

      entry Wait when Ready is
      begin
         Ready := False;
      end Wait;

      procedure Trigger is
      begin
         Ready := True;
      end Trigger;

   end Handling;


-- 
Simon Wright                               100% Ada, no bugs.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-01-17 21:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-17 14:13 Catching Ctrl-C SIGINT Christopher Broeg
  -- strict thread matches above, loose matches on Subject: below --
2005-01-17 14:07 Christopher Broeg
2005-01-17 12:26 Christopher Broeg
2005-01-17 14:19 ` Martin Dowie
2005-01-17 14:35   ` Christopher Broeg
2005-01-17 21:37 ` Simon Wright

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox