Signal.cpp

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile$
00010  * Date modified: $Date: 2005-01-17 22:34:01 -0600 (Mon, 17 Jan 2005) $
00011  * Version:       $Revision: 16635 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
00041 
00042 #include <vpr/vprConfig.h>
00043 
00044 #include <signal.h>
00045 #include <boost/concept_check.hpp>
00046 
00047 #include <vpr/vprTypes.h>
00048 #include <vpr/Util/Assert.h>
00049 #include <vpr/Thread/Signal.h>
00050 
00051 
00052 namespace vpr
00053 {
00054 
00055 // ============================================================================
00056 // vpr::SignalSet stuff.
00057 // ============================================================================
00058 
00059 vpr::ReturnStatus SignalSet::emptySet()
00060 {
00061    vpr::ReturnStatus status;
00062 
00063 #ifdef HAVE_SIGEMPTYSET
00064    if ( sigemptyset(&mSigSet) != 0 )
00065    {
00066       status.setCode(vpr::ReturnStatus::Fail);
00067    }
00068 #else
00069    mSigSet = 0;
00070 #endif
00071 
00072    return status;
00073 }
00074 
00075 vpr::ReturnStatus SignalSet::fillSet()
00076 {
00077    vpr::ReturnStatus status;
00078 
00079 #ifdef HAVE_SIGFILLSET
00080    if ( sigfillset(&mSigSet) != 0 ) {
00081       status.setCode(vpr::ReturnStatus::Fail);
00082    }
00083 #else
00084    mSigSet = ~(sigset_t) 0;
00085 #endif
00086 
00087    return status;
00088 }
00089 
00090 vpr::ReturnStatus SignalSet::addSignal(const int sigNum)
00091 {
00092    vpr::ReturnStatus status;
00093 
00094    vprASSERT(sigNum >= 1 && "Invalid signal number");
00095 
00096 #ifdef HAVE_SIGADDSET
00097    if ( sigaddset(&mSigSet, sigNum) != 0 )
00098    {
00099       status.setCode(vpr::ReturnStatus::Fail);
00100    }
00101 #else
00102    mSigSet |= (1 << (sigNum - 1));
00103 #endif
00104 
00105    return status;
00106 }
00107 
00108 vpr::ReturnStatus SignalSet::removeSignal(const int sigNum)
00109 {
00110    vpr::ReturnStatus status;
00111 
00112    vprASSERT(sigNum >= 1 && "Invalid signal number");
00113 
00114 #ifdef HAVE_SIGDELSET
00115    if ( sigdelset(&mSigSet, sigNum) != 0 )
00116    {
00117       status.setCode(vpr::ReturnStatus::Fail);
00118    }
00119 #else
00120    mSigSet &= ~(1 << (sigNum - 1)) ;
00121 #endif
00122 
00123    return status;
00124 }
00125 
00126 bool SignalSet::isMember(const int sigNum) const
00127 {
00128    bool is_member;
00129 
00130    vprASSERT(sigNum >= 1 && "Invalid signal number");
00131 
00132 #ifdef HAVE_SIGISMEMBER
00133    is_member = (sigismember(&mSigSet, sigNum) == 1);
00134 #else
00135    is_member = ((mSigSet & (1 << (sigNum - 1))) != 0);
00136 #endif
00137 
00138    return is_member;
00139 }
00140 
00141 // ============================================================================
00142 // vpr::SignalAction stuff.
00143 // ============================================================================
00144 
00145 const vpr::SignalHandler_t SignalAction::DefaultAction = SIG_DFL;
00146 const vpr::SignalHandler_t SignalAction::IgnoreAction  = SIG_IGN;
00147 
00148 SignalAction::SignalAction(vpr::SignalHandler_t handler,
00149                            const vpr::SignalSet* sig_set, const int flags)
00150 {
00151    if ( sig_set == NULL )
00152    {
00153       init(handler, NULL, flags);
00154    }
00155    else
00156    {
00157       init(handler, sig_set->getMask(), flags);
00158    }
00159 }
00160 
00161 // ============================================================================
00162 // vpr::SigHandler stuff.
00163 // ============================================================================
00164 
00165 vpr::ReturnStatus SigHandler::registerHandler(const int sigNum,
00166                                               vpr::SignalHandler_t handler,
00167                                               const bool restart)
00168 {
00169    boost::ignore_unused_variable_warning(restart);
00170    vpr::SignalAction sa(handler);
00171    return registerHandler(sigNum, sa);
00172 }
00173 
00174 vpr::ReturnStatus SigHandler::registerHandler(const int sigNum,
00175                                               vpr::SignalAction& action,
00176                                               const bool restart)
00177 {
00178    vpr::ReturnStatus status;
00179 
00180 #ifdef SA_RESTART
00181    if ( sigNum != SIGALRM && restart )
00182    {
00183       action.mSA.sa_flags |= SA_RESTART;
00184    }
00185 #endif
00186 
00187    if ( vpr::SigHandler::sigaction(sigNum, &action.mSA) != 0 )
00188    {
00189       status.setCode(vpr::ReturnStatus::Fail);
00190    }
00191 
00192    return status;
00193 }
00194 
00195 int SigHandler::sigaction(const int sigNum, const struct sigaction* action,
00196                           struct sigaction* oldAction)
00197 {
00198 #ifdef HAVE_SIGACTION
00199    return ::sigaction(sigNum, action, oldAction);
00200 #else
00201    // If sigaction(2) is not available, simulate its functionality using
00202    // signal(2).
00203    struct sigaction sa;
00204 
00205    if ( oldAction == NULL )
00206    {
00207       oldAction = &sa;
00208    }
00209 
00210    if ( action == NULL )
00211    {
00212       oldAction->sa_handler = ::signal(sigNum, SIG_IGN);
00213       ::signal(sigNum, oldAction->sa_handler);
00214    }
00215    else
00216    {
00217       oldAction->sa_handler = ::signal(sigNum, action->sa_handler);
00218    }
00219 
00220    return (oldAction->sa_handler == SIG_ERR ? -1 : 0);
00221 #endif
00222 }
00223 
00224 } // End of vpr namespace

Generated on Thu Jan 4 10:52:10 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1