Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Asynch_Pseudo_Task.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Asynch_Pseudo_Task.cpp,v 1.1.1.1.2.1 2003/03/13 19:44:20 chad Exp $
00003 
00004 #include "ace/Asynch_Pseudo_Task.h"
00005 
00006 ACE_RCSID(ace, Asynch_Pseudo_Task, "$Id: Asynch_Pseudo_Task.cpp,v 1.1.1.1.2.1 2003/03/13 19:44:20 chad Exp $")
00007 
00008 ACE_Asynch_Pseudo_Task::ACE_Asynch_Pseudo_Task()
00009   : flg_active_ (0),
00010     select_reactor_ (),               // should be initialized before reactor_
00011     reactor_ (&select_reactor_, 0),   // don't delete implementation
00012     token_ (select_reactor_.lock ()), // we can use reactor token       
00013     finish_count_ (0)
00014 {
00015 }
00016 
00017 ACE_Asynch_Pseudo_Task::~ACE_Asynch_Pseudo_Task()
00018 {
00019   stop();
00020 }
00021 
00022 int 
00023 ACE_Asynch_Pseudo_Task::is_active (void)
00024 {
00025   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00026   return flg_active_;
00027 }
00028 
00029 int 
00030 ACE_Asynch_Pseudo_Task::start (void)
00031 {
00032   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00033 
00034   if (this->flg_active_)
00035     ACE_ERROR_RETURN ((LM_ERROR,
00036                        ACE_LIB_TEXT ("%N:%l:%p\n"),
00037                        ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::start already started")),
00038                        -1);
00039 
00040   if (this->reactor_.initialized () == 0)
00041     ACE_ERROR_RETURN ((LM_ERROR,
00042                        ACE_LIB_TEXT ("%N:%l:%p\n"),
00043                        ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::start reactor is not initialized")),
00044                        -1);
00045 
00046 
00047   if (this->activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
00048     ACE_ERROR_RETURN ((LM_ERROR,
00049                        ACE_LIB_TEXT ("%N:%l:%p\n"),
00050                        ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::start failed")),
00051                        -1);
00052 
00053   this->flg_active_ = 1;
00054   return 0;
00055 }
00056 
00057 int 
00058 ACE_Asynch_Pseudo_Task::stop (void)
00059 {
00060   {
00061     ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00062 
00063     if (this->flg_active_ == 0)  // already stopped 
00064       return 0;
00065 
00066     reactor_.end_reactor_event_loop ();
00067   }
00068 
00069   int rc = this->wait ();
00070 
00071   if  (rc != 0)
00072     ACE_ERROR_RETURN ((LM_ERROR,
00073                        ACE_LIB_TEXT ("%N:%l:%p\n"),
00074                        ACE_LIB_TEXT ("ACE_Asynch_Pseudo_Task::stop failed")),
00075                        -1);
00076 
00077   {
00078     ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00079     this->flg_active_ = 0;
00080 
00081     if (this->reactor_.initialized ())
00082       this->reactor_.close ();
00083 
00084     while (finish_count_ > 0)
00085       {
00086         ACE_MT (ace_mon.release ());
00087         finish_event_.wait ();
00088 
00089         ACE_MT (ace_mon.acquire ());
00090         finish_event_.reset ();
00091       }
00092   }
00093 
00094   return rc;
00095 }
00096 
00097 int
00098 ACE_Asynch_Pseudo_Task::lock_finish (void)
00099 {
00100   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00101   finish_count_ ++;
00102   return 0;
00103 }
00104 
00105 int
00106 ACE_Asynch_Pseudo_Task::unlock_finish (void)
00107 {
00108   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00109 
00110   --finish_count_;
00111   finish_event_.signal ();
00112 
00113   return 0;
00114 }
00115 
00116 int
00117 ACE_Asynch_Pseudo_Task::svc (void)
00118 {
00119 #if !defined (ACE_WIN32) 
00120 
00121   sigset_t RT_signals;
00122 
00123   if (sigemptyset (&RT_signals) == -1)
00124     ACE_ERROR ((LM_ERROR,
00125                 ACE_LIB_TEXT ("Error:(%P | %t):%p\n"),
00126                 ACE_LIB_TEXT ("sigemptyset failed")));
00127 
00128   int member = 0;
00129 
00130   for (int si = ACE_SIGRTMIN; si <= ACE_SIGRTMAX; si++)
00131     {
00132       member = sigismember (& RT_signals , si);
00133       if (member == 1)
00134         {
00135           sigaddset (&RT_signals, si);
00136         }
00137     }
00138 
00139   if (ACE_OS::pthread_sigmask (SIG_BLOCK, &RT_signals, 0) != 0)
00140     ACE_ERROR ((LM_ERROR,
00141                 ACE_LIB_TEXT ("Error:(%P | %t):%p\n"),
00142                 ACE_LIB_TEXT ("pthread_sigmask failed")));
00143 #endif
00144 
00145   reactor_.owner (ACE_Thread::self());
00146 
00147   reactor_.run_reactor_event_loop ();
00148 
00149   return 0;
00150 }
00151 
00152 
00153 
00154 int
00155 ACE_Asynch_Pseudo_Task::register_io_handler (ACE_HANDLE handle,
00156                                              ACE_Event_Handler *handler,
00157                                              ACE_Reactor_Mask mask,
00158                                              int flg_suspend)
00159 {
00160   //  Return codes : 
00161   //   0  success
00162   //  -1  reactor errors
00163   //  -2  task not active 
00164 
00165   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00166 
00167   if (this->flg_active_ == 0)
00168     ACE_ERROR_RETURN ((LM_ERROR,
00169                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::register_io_handler \n")
00170                        ACE_LIB_TEXT ("task not active \n")),
00171                       -2);
00172     
00173   // Register the handler with the reactor.
00174   int retval = this->reactor_.register_handler (handle, handler, mask);
00175                                                     
00176   if (retval == -1)
00177     ACE_ERROR_RETURN ((LM_ERROR,
00178                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::register_io_handler \n")
00179                        ACE_LIB_TEXT ("register_handler failed \n")),
00180                       -1);
00181 
00182   if (flg_suspend == 0 )
00183     return 0;
00184 
00185   // Suspend the <handle> now. Enable only when the <accept> is issued
00186   // by the application.
00187   retval = this->reactor_.suspend_handler (handle);
00188 
00189   if (retval == -1)
00190     {
00191       this->reactor_.remove_handler (handle,
00192                                      ACE_Event_Handler::ALL_EVENTS_MASK
00193                                      | ACE_Event_Handler::DONT_CALL); 
00194  
00195       ACE_ERROR_RETURN ((LM_ERROR,
00196                          ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::register_io_handler \n")
00197                          ACE_LIB_TEXT ("suspend_handler failed \n")),
00198                         -1);
00199     }
00200 
00201   return 0;
00202 }
00203 
00204 int
00205 ACE_Asynch_Pseudo_Task::remove_io_handler (ACE_HANDLE handle)
00206 {
00207   //  Return codes : 
00208   //   0  success
00209   //  -1  reactor errors
00210   //  -2  task not active 
00211 
00212   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00213 
00214   if (this->flg_active_ == 0)
00215     ACE_ERROR_RETURN ((LM_ERROR,
00216                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
00217                        ACE_LIB_TEXT ("task not active \n")),
00218                       -2);
00219     
00220   int retval =
00221     this->reactor_.remove_handler (handle ,
00222                                    ACE_Event_Handler::ALL_EVENTS_MASK
00223                                    | ACE_Event_Handler::DONT_CALL); 
00224   if (retval == -1)
00225     ACE_ERROR_RETURN ((LM_ERROR,
00226                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
00227                        ACE_LIB_TEXT ("remove_handler failed \n")),
00228                       -1);
00229 
00230   return 0;
00231 }
00232 
00233 int
00234 ACE_Asynch_Pseudo_Task::remove_io_handler (ACE_Handle_Set &set)
00235 {
00236   //  Return codes : 
00237   //   0  success
00238   //  -1  reactor errors
00239   //  -2  task not active 
00240 
00241   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00242 
00243   if (this->flg_active_ == 0)
00244     ACE_ERROR_RETURN ((LM_ERROR,
00245                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
00246                        ACE_LIB_TEXT ("task not active \n")),
00247                       -2);
00248     
00249   int retval =
00250     this->reactor_.remove_handler (set ,
00251                                    ACE_Event_Handler::ALL_EVENTS_MASK
00252                                    | ACE_Event_Handler::DONT_CALL); 
00253   if (retval == -1)
00254     ACE_ERROR_RETURN ((LM_ERROR,
00255                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::remove_io_handler \n")
00256                        ACE_LIB_TEXT ("remove_handler failed \n")),
00257                       -1);
00258 
00259   return 0;
00260 }
00261 
00262 int
00263 ACE_Asynch_Pseudo_Task::suspend_io_handler (ACE_HANDLE handle)
00264 {
00265   //  Return codes : 
00266   //   0  success
00267   //  -1  reactor errors
00268   //  -2  task not active 
00269 
00270   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00271 
00272   if (this->flg_active_ == 0)
00273     ACE_ERROR_RETURN ((LM_ERROR,
00274                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::suspend_io_handler \n")
00275                        ACE_LIB_TEXT ("task not active \n")),
00276                       -2);
00277     
00278   int retval = this->reactor_.suspend_handler (handle);
00279 
00280   if (retval == -1)
00281     ACE_ERROR_RETURN ((LM_ERROR,
00282                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::suspend_io_handler \n")
00283                        ACE_LIB_TEXT ("suspend_handler failed \n")),
00284                       -1);
00285 
00286   return 0;
00287 }
00288 
00289 int
00290 ACE_Asynch_Pseudo_Task::resume_io_handler (ACE_HANDLE handle)
00291 {
00292   //  Return codes : 
00293   //   0  success
00294   //  -1  reactor errors
00295   //  -2  task not active 
00296 
00297   ACE_MT (ACE_GUARD_RETURN (ACE_Lock, ace_mon, this->token_, -1));
00298 
00299   if (this->flg_active_ == 0)
00300     ACE_ERROR_RETURN ((LM_ERROR,
00301                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::resume_io_handler \n")
00302                        ACE_LIB_TEXT ("task not active \n")),
00303                       -2);
00304     
00305   int retval = this->reactor_.resume_handler (handle);
00306 
00307   if (retval == -1)
00308     ACE_ERROR_RETURN ((LM_ERROR,
00309                        ACE_LIB_TEXT ("%N:%l:ACE_Asynch_Pseudo_Task::resume_io_handler \n")
00310                        ACE_LIB_TEXT ("resume_handler failed \n")),
00311                       -1);
00312 
00313   return 0;
00314 }

Generated on Mon Jun 16 11:19:07 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002