Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / LoadBalancer / Signal_Handler.cpp
blobbd4eca3296087a74f39ddb24c521855d90a9eb6d
1 #include "orbsvcs/Log_Macros.h"
2 #include "Signal_Handler.h"
3 #include "tao/ORB_Core.h"
4 #include "ace/Reactor.h"
6 TAO_LB_Signal_Handler::TAO_LB_Signal_Handler (CORBA::ORB_ptr orb,
7 PortableServer::POA_ptr poa)
9 #ifdef ACE_HAS_THREADS
10 signal_guard_ (),
11 #endif /* ACE_HAS_THREADS */
12 sigset_ (),
13 orb_ (CORBA::ORB::_duplicate (orb)),
14 poa_ (PortableServer::POA::_duplicate (poa))
16 // Register signal handlers.
17 this->sigset_.sig_add (SIGINT);
18 this->sigset_.sig_add (SIGTERM);
21 int
22 TAO_LB_Signal_Handler::svc (void)
24 // This method is only invoked when performing synchronous signal
25 // handling.
27 int signum = -1;
29 // Block waiting for the registered signals.
30 if (ACE_OS::sigwait (this->sigset_, &signum) == -1)
32 ORBSVCS_ERROR_RETURN ((LM_ERROR,
33 "(%P|%t) %p\n",
34 "ERROR waiting on signal"),
35 -1);
38 ACE_ASSERT (signum == SIGINT || signum == SIGTERM);
40 // ORBSVCS_DEBUG ((LM_DEBUG,
41 // ACE_TEXT ("(%P|%t) synchronous signal handler done\n")));
43 return this->perform_cleanup (signum);
46 int
47 TAO_LB_Signal_Handler::activate (long flags,
48 int n_threads,
49 int force_active,
50 long priority,
51 int grp_id,
52 ACE_Task_Base *task,
53 ACE_hthread_t thread_handles[],
54 void *stack[],
55 size_t stack_size[],
56 ACE_thread_t thread_ids[],
57 const char* thr_name[])
59 // sigwait() is not implemented on MS Windows. Handle signals
60 // asynchronously through the ORB's reactor in that case instead.
61 // Otherwise, handle signals synchronously in another thread.
63 #if defined (ACE_HAS_THREADS) && !defined (ACE_WIN32)
64 return this->ACE_Task_Base::activate (flags,
65 n_threads,
66 force_active,
67 priority,
68 grp_id,
69 task,
70 thread_handles,
71 stack,
72 stack_size,
73 thread_ids,
74 thr_name);
75 #else
76 ACE_UNUSED_ARG (flags);
77 ACE_UNUSED_ARG (n_threads);
78 ACE_UNUSED_ARG (force_active);
79 ACE_UNUSED_ARG (priority);
80 ACE_UNUSED_ARG (grp_id);
81 ACE_UNUSED_ARG (task);
82 ACE_UNUSED_ARG (thread_handles);
83 ACE_UNUSED_ARG (stack);
84 ACE_UNUSED_ARG (stack_size);
85 ACE_UNUSED_ARG (thread_ids);
86 ACE_UNUSED_ARG (thr_name);
88 return
89 this->orb_->orb_core ()->reactor ()->register_handler (this->sigset_,
90 this);
91 #endif /* ACE_HAS_THREADS */
94 int
95 TAO_LB_Signal_Handler::handle_signal (int signum, siginfo_t *, ucontext_t *)
97 // ORBSVCS_DEBUG ((LM_DEBUG,
98 // ACE_TEXT ("(%P|%t) ASYNCHRONOUS signal handler done\n")));
100 // This method is only used in the asynchronous signal handling case
101 // (i.e. when single-threaded build is used).
103 // @note Is it okay to perform ORB operations from this method? The
104 // issue here is whether or not
105 // ACE_Event_Handler::handle_signal() is called from within an
106 // OS signal handler, and if that did occur is it safe to
107 // perform ORB operations?
108 return this->perform_cleanup (signum);
112 TAO_LB_Signal_Handler::perform_cleanup (int signum)
116 // Shutdown the POA.
118 // Shutting down the POA will cause servants "owned" by the POA
119 // to be destroyed. Servants will then have the opportunity to
120 // clean up all resources they are responsible for.
121 this->poa_->destroy (1, 1);
123 // Now shutdown the ORB.
124 this->orb_->shutdown (1);
126 catch (const CORBA::Exception& ex)
128 ex._tao_print_exception ("Caught exception");
130 ORBSVCS_ERROR_RETURN ((LM_ERROR,
131 "Problem during cleanup initiated by signal %d.\n",
132 signum),
133 -1);
136 return 0;