Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / ThreadManagement / Signals.cpp
blob157274c1e6678a485d1623e01dd00e15b641e681
1 #include "ace/config-lite.h"
2 #if defined (ACE_HAS_THREADS)
4 #include "ace/OS_NS_time.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/Task.h"
7 #include "ace/Log_Msg.h"
8 #include "ace/Signal.h"
9 #include "ace/Sig_Handler.h"
11 // Listing 1 code/ch13
12 class SignalableTask : public ACE_Task<ACE_MT_SYNCH>
14 public:
15 virtual int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0)
17 if (signum == SIGUSR1)
19 ACE_DEBUG ((LM_DEBUG,
20 ACE_TEXT ("(%t) received a %S signal\n"),
21 signum));
22 handle_alert ();
24 return 0;
27 virtual int svc ()
29 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Starting thread\n")));
31 while (1)
33 ACE_Message_Block* mb = 0;
34 ACE_Time_Value tv (0, 1000);
35 tv += ACE_OS::time (0);
36 int result = this->getq (mb, &tv);
37 if (result == -1 && errno == EWOULDBLOCK)
38 continue;
39 else
40 process_message (mb);
43 ACE_NOTREACHED (return 0);
46 void handle_alert ();
47 void process_message (ACE_Message_Block *mb);
49 // Listing 1
51 void
52 SignalableTask::process_message (ACE_Message_Block *)
56 void
57 SignalableTask::handle_alert ()
61 // Listing 2 code/ch13
62 int ACE_TMAIN (int, ACE_TCHAR *[])
64 SignalableTask handler;
65 handler.activate (THR_NEW_LWP | THR_JOINABLE , 5);
67 ACE_Sig_Handler sh;
68 sh.register_handler (SIGUSR1, &handler);
70 ACE_OS::sleep (1);
72 ACE_Thread_Manager::instance () ->
73 kill_grp (handler.grp_id (), SIGUSR1);
74 handler.wait ();
75 return 0;
77 // Listing 2
79 #else
80 #include "ace/OS_main.h"
81 #include "ace/OS_NS_stdio.h"
83 int ACE_TMAIN (int, ACE_TCHAR *[])
85 ACE_OS::puts (ACE_TEXT ("This example requires threads."));
86 return 0;
89 #endif /* ACE_HAS_THREADS */