moss-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Moss-devel] CVS: moss/family/include Daemonizer.h,NONE,1.1 Makefile.am,


From: Alexander Feder <address@hidden>
Subject: [Moss-devel] CVS: moss/family/include Daemonizer.h,NONE,1.1 Makefile.am,NONE,1.1 Mutex.h,NONE,1.1 MutexLock.h,NONE,1.1 PluginHandler.h,NONE,1.1 SignalHandler.h,NONE,1.1 Threaded.h,NONE,1.1 family.h,NONE,1.1 generic.h,NONE,1.1
Date: Sat, 22 Jun 2002 16:06:04 -0400

Update of /cvsroot/moss/moss/family/include
In directory subversions:/tmp/cvs-serv21079/family/include

Added Files:
        Daemonizer.h Makefile.am Mutex.h MutexLock.h PluginHandler.h 
        SignalHandler.h Threaded.h family.h generic.h 
Log Message:
added family


--- NEW FILE ---
/***************************************************************************
                              Daemonizer.h
                           -------------------
    begin                : Thu June 28 2001
    copyright            : (C) 2001 Alexander Feder
    email                : address@hidden
 
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

  2001-12-14 Manfred Morgner

    * changed to "all inline" - no cxx file anymore

 ***************************************************************************/

#ifndef __DAEMONIZER_H
#define __DAEMONIZER_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#if __GNU__ >= 2
#  pragma interface
#endif // __GNU__ >= 2

#include "generic.h"
#include <iostream>


#ifdef POSIX
    #include <sys/wait.h>
#endif


namespace family
  {
  class CDaemonizer
    {
    protected:
      int m_nPid;

    public:
      CDaemonizer()
        {
        Initialize();
        } // CDaemonizer()

      bool Detach()
        {
#ifdef WIN32

        TRACE("E: there is no 'fork()' support in Win32 derivates\n");
        TRACE("E: \tthe application can't be daemonized\n");
        return true;

#endif // WIN32


#ifdef POSIX
        int nStatus;
        if ( ( m_nPid = fork () ) < 0 )
          {
          TRACE("fork() failed\n")
          return false;
          }
        else
          {
          if ( m_nPid != 0 )
            {
#ifndef quiet
            TRACE("parent says: bye bye\n")
#endif //ifndef quiet
            return false;
            }
          } // if ( ( m_nPid = fork () ) < 0 )
        wait ( &nStatus );
#else // POSIX
        TRACE("E: no POSIX support ->\n");
        TRACE("E: \tno fork()->\n");
        TRACE("E: \t\tno daemonizing\n");
#endif // else POSIX
    
#ifndef quiet
        TRACE("Daemonized\n")
#endif // #ifndef quiet

        return true;
        } // bool CDaemonizer::Detach()

    protected:
      void Initialize()
        {
        m_nPid = 0;
        } //void Initialize()

      virtual bool CheckUniqueness()
        {
        //+ insert uniqueness check here :)
        return true;
        } // bool CDaemonizer::CheckUniqueness()

    }; //class CDaemonizer
  } // namespace family

#endif //#ifndef __DAEMONIZER_H

--- NEW FILE ---
EXTRA_DIST = Mutex.h MutexLock.h Daemonizer.h PluginHandler.h SignalHandler.h 
family.h Threaded.h

install-data-local:
        $(mkinstalldirs) $(prefix)/include/family
        $(INSTALL_DATA) Mutex.h $(prefix)/include/family/Mutex.h
        $(INSTALL_DATA) MutexLock.h $(prefix)/include/family/MutexLock.h
        $(INSTALL_DATA) Daemonizer.h $(prefix)/include/family/Daemonizer.h
        $(INSTALL_DATA) PluginHandler.h $(prefix)/include/family/PluginHandler.h
        $(INSTALL_DATA) SignalHandler.h $(prefix)/include/family/SignalHandler.h
        $(INSTALL_DATA) family.h $(prefix)/include/family/family.h
        $(INSTALL_DATA) Threaded.h $(prefix)/include/family/Threaded.h



--- NEW FILE ---
/***************************************************************************
                                Mutex.h
                           -------------------
    begin                : Sun Mar 04 2001
    copyright            : (C) 2001 by Manfred Morgner
    email                : address@hidden

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

  2001-12-14 Manfred Morgner

    * changed to "all inline" - no cxx file anymore

 ***************************************************************************/

#ifndef __FAMILY_MUTEX_H
#define __FAMILY_MUTEX_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#if __GNU__ >= 2
#  pragma interface
#endif // #if __GNU__ >= 2


namespace family
  {
#ifdef WIN32

  class CMutex
    {
    protected:
      HANDLE m_hMutex;

    public:
      CMutex()
        {
        m_hMutex = ::CreateMutex(NULL, true, NULL);
        } // CMutex()

      ~CMutex()
        {
        ::CloseHandle(m_hMutex);
        } // ~CMutex()

      operator HANDLE&()
        {
        return m_hMutex;
        } // operator HANDLE&()

    }; // class CMutex

#else // WIN32

#include <pthread.h>

  class CMutex : public pthread_mutex_t
    {
    public:
      CMutex()
        {
        TRACE("CMutex Contructor\n");
        pthread_mutex_init(this, 0);
        TRACE("\tCMutex Constructor finished\n");
        } // CMutex()

      ~CMutex()
        {
        TRACE("CMutex Destructor\n");
        pthread_mutex_destroy(this);
        TRACE("\tCMutex Destructor finished\n");
        } // ~CMutex()

    }; // class CMutex

#endif // WIN32

  } // namespace family

#endif // __FAMILY_MUTEX_H

--- NEW FILE ---
/***************************************************************************
                               MutexLock.h
                           -------------------
    begin                : Sun Mar 04 2001
    copyright            : (C) 2001 by Manfred Morgner
    email                : address@hidden

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

  2001-12-14 Manfred Morgner

    * changed to "all inline" - no cxx file anymore

 ***************************************************************************/

#ifndef __FAMILY_MUTEX_LOCK_H
#define __FAMILY_MUTEX_LOCK_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#if __GNU__ >= 2
#  pragma interface
#endif // #if __GNU__ >= 2


#ifdef WIN32
#include <process.h>
#else // WIN32
#include <pthread.h>
#endif  // WIN32


namespace family
  {
  class CMutexLock
    {
    protected:
#ifdef WIN32
      HANDLE m_hMutex;
#else  // WIN32
      pthread_mutex_t *m_ptMutex;
#endif  // WIN32

    protected:
      CMutexLock() {}; // forbidden
    public:
#ifdef WIN32
      CMutexLock(HANDLE &rhMutex)
        {
        m_hMutex = ::OpenMutex( SYNCHRONIZE, false, NULL );
        ::WaitForSingleObject(m_hMutex, INFINITE);  // == WAIT_OBJECT_0 
        } // CMutexLock()

      ~CMutexLock()
        {
        ::ReleaseMutex(m_hMutex);
        } // ~CMutexLock()

#else  // WIN32

      CMutexLock(pthread_mutex_t &rtMutex)
        {
        pthread_mutex_lock(&rtMutex);
        m_ptMutex = &rtMutex;
        } // CMutexLock()

      ~CMutexLock()
        {
        pthread_mutex_unlock(m_ptMutex);
        } // ~CMutexLock()

#endif  // else WIN32
    
    }; // class CMutexLock

  } // namespace family

#endif // __FAMILY_MUTEX_LOCK_H

--- NEW FILE ---
/***************************************************************************
                             PluginHandler.h
                           -------------------
    begin                : Sat Mar 17 2001
    copyright            : (C) 2001 by Alexander Feder
    email                : address@hidden

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

  2001-12-14 Manfred Morgner

    * changed to "all inline" - no cxx file anymore

 ***************************************************************************/

#ifndef __PLUGIN_HANDLER_H
#define __PLUGIN_HANDLER_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#if __GNU__ >= 2
#  pragma interface
#endif // #if __GNU__ >= 2

#include "family.h"

#ifdef WIN32
  // nothings to include for LoadLibrary
#else // WIN32
  #include <dlfcn.h>
  #include <stdio.h>
#endif // WIN32

//STL
#include <string>

namespace family 
  {
  class CPluginHandler
    {
    protected:
      std::string m_sFilename;

#ifdef WIN32
      HINSTANCE m_hPlugin;
#else // WIN32
      void*  m_hPlugin;
#endif // else WIN32

      bool m_bIsOwner;
          
    public:
      CPluginHandler ( const std::string &rsFilename = _T("") )
        {
        Initialize();
        FilenameSet( rsFilename );
        } // CPluginHandler ( const std::string& rsFilename )

      virtual ~CPluginHandler ()
        {
        Unload();
        } // ~CPluginHandler ( )


      void FilenameSet ( const std::string& rsFilename )
        {
        TRACE("set plugin file name: ")
        TRACE(rsFilename) TRACE("\n")

        Unload();

        m_sFilename = rsFilename;

        if ( m_sFilename.length() <= 0 )
          {
          return;
          } // if ( m_sFilename.length() <= 0 )

        Load();
  
        if ( m_hPlugin == 0 )
          {
          m_sFilename = _T("");
          } // if ( m_hPlugin == 0 )
        } // void FilenameSet( const std::string& rsFilename )

      void* FunctionGet ( const std::string& rsName )
        {
        TRACE("get plugin function\n");
        if ( IsValid() == false )
          {
          TRACE("\tE: plugin not loaded.\n");
          return 0;
          } // if ( m_hPlugin == 0 )

#ifdef WIN32
        return GetProcAddress( m_hPlugin, rsName.c_str() );
#else // WIN32
        return dlsym( m_hPlugin, rsName.c_str() );
#endif // else WIN32
        } // void* CPluginHandler::FunctionGet ( const std::string& rsName )

      bool IsValid ()
        {
        return m_hPlugin != 0;
        } // bool IsValid ()

    protected:
      void Initialize ()
        {
        m_hPlugin  = 0;
        m_bIsOwner = true;
        } // void Initialize()

      int Load ()
        {
        TRACE("load plugin\n");

        if ( IsValid() == true )
          {
          return FAMILY_FAILURE;
          } // if ( IsValid() == true )

        if ( m_sFilename == "" ) 
          {
          return FAMILY_FAILURE;
          }

#ifdef WIN32
        m_hPlugin = LoadLibrary( m_sFilename.c_str() );
#else // WIN32
        m_hPlugin = dlopen( m_sFilename.c_str(), RTLD_LAZY );
#endif // WIN32
        if ( m_hPlugin != 0 )
          {
          return FAMILY_SUCCESS;
          } // if ( m_hPlugin != 0 )
        perror ( "dlopen" );
#ifdef WIN32
  // 
#else // WIN32
        cout << endl << dlerror() << endl;
#endif // WIN32
        TRACE("\tE: can't load plugin\n");
        return FAMILY_FAILURE;
        } // int Load()

      int Unload ()
        {
        if ( m_bIsOwner == false )
          {
          return FAMILY_SUCCESS;
          } // if ( m_bIsOwner == false )

        if ( m_hPlugin == 0 )
          {
          return FAMILY_SUCCESS;
          } // if ( m_hPlugin != 0 )
  
        TRACE("unload plugin\n");
  
#ifdef WIN32
        FreeLibrary( m_hPlugin );
#else // WIN32
        dlclose( m_hPlugin );
#endif // else WIN32

        m_hPlugin = 0;
        return FAMILY_SUCCESS;
        } // int CPluginHandler::Unload()

    }; // class CPluginHandler

  } // namespace family 

#endif // __PLUGIN_HANDLER_H

--- NEW FILE ---
/***************************************************************************
                             SignalHandler.h
                           -------------------
    begin                : Tue June 26 2001
    copyright            : (C) 2001 Alexander Feder
    email                : address@hidden
 
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

  2001-12-14 Manfred Morgner

    * changed to "all inline" - no cxx file anymore

 ***************************************************************************/
 
#ifndef __SIGNAL_HANDLER_H
#define __SIGNAL_HANDLER_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#if __GNU__ >= 2
#  pragma interface
#endif // #if __GNU__ >= 2

#ifdef WIN32
#  pragma warning(disable : 4786)
#endif

// system header
#include <signal.h>

// STL
#include <map>
#include <string>

////////////////////////////////////////////
//
//  type definitions
//


namespace family
  {

  extern "C"
    {
    // to ensure "C"-linkage of  "void(*fnHandler)(int)"
    typedef void(*FHandler)(int);
    
    // signal handler description
    typedef
      struct tagSSignalHandler
      {
      int       nSignal;
      char*     sName;
      FHandler  fnHandler;
      } SSignalHandler;

    } // extern "C"

  ////////////////////////////////////////////
  //
  //  class
  //

  class CSignalManager
    {
    private:      // map to find names for signal values
      typedef std::map<int, std::string> CMapSignal2Name;

    public:
      static CSignalManager *s_poManager;

    protected:
      // NUMBER -> NAME translation map for the signals
      // CMapSignal2Name family::g_mnsSignal2Name;
      CMapSignal2Name   m_mnsSignal2Name;
      SSignalHandler   *m_ptSignalHandler;
      bool              m_bExitApplication;

    public:
      CSignalManager()
        {
        m_bExitApplication = false;
        // signal handler description list for signals
        // we will handle (sooner or later)
        static SSignalHandler sptSignalHandler[] =
            {
              { SIGINT,   "CTRL+C Interrupt",         Signal },
              { SIGILL,   "Illegal Instruction",      Signal },
              { SIGFPE,   "Floating Point Error",     Signal },
              { SIGTERM,  "Termination Request",      Signal },
              { SIGABRT,  "Abnormal Termination",     Signal },
#ifdef WIN32
              { SIGBREAK, "CTRL+Break Interrupt",     Signal },
              { SIGSEGV,  "General Protection Fault", Signal },
              // all previouse signals are handled under WinNT
              // the following are not handled under WinNT ;-)
#else // WIN32
              { SIGSEGV,  "Segmentation Fault",       Signal },
              { SIGPIPE,  "Broken Pipe",              Signal },
              { SIGHUP,   "Hangup",                   Signal },
              { SIGTSTP,  "Keyboard Stop",            Signal },
              { SIGKILL,  "Killed",                   Signal },
#endif // else WIN32
              // the finalizing entry for the signal handler list
              { 0,0,0 }
            }; // SSignalHandler g_atSignalHandler[]

        m_ptSignalHandler = sptSignalHandler;

        InitializeSignalHandling();
        } // CSignalManager()

     virtual ~CSignalManager()
       {
       // we have to have an virtual destructor because we have virtual methods
       } // virtual ~CSignalManager()

      virtual bool Exit()
        {
        return m_bExitApplication;
        } // bool Exit()

      virtual void Unhandled(int nSignal)
        {
#ifdef _DEBUG
        CMapSignal2Name::iterator it = m_mnsSignal2Name.find(nSignal);
        std::cerr << "unhandled signal: "
          << nSignal << ", "
          << ( ( it == m_mnsSignal2Name.end() ) ? "unknown" : 
it->second.c_str() )
          << std::endl;
#endif // _DEBUG
        } // void Unhandled(int nSignal)

      virtual void Handled(int nSignal)
        {
#ifdef _DEBUG
        CMapSignal2Name::iterator it = m_mnsSignal2Name.find(nSignal);
        std::cout << "signal: "
          << nSignal << ", "
          << ( ( it == m_mnsSignal2Name.end() ) ? "unknown" : 
it->second.c_str() )
          << std::endl;
#endif // _DEBUG

        switch (nSignal)
          {
          case SIGINT:  Interrupt();                   break;
          case SIGILL:  IllegalInstruction();          break;
          case SIGFPE:  FloatingPointError();          break;
          case SIGTERM: Terminated();                  break;
          case SIGABRT: AbnormalTermination();         break;
#ifdef WIN32
          case SIGBREAK:Win32Interrupt();              break;
          case SIGSEGV: Win32GeneralProtectionFault(); break;
#else // WIN32
          case SIGSEGV: SegmentationFault();           break;
          case SIGPIPE: BrokenPipe();                  break;
          case SIGHUP:  Hangup();                      break;
          case SIGTSTP: KeyboardStop();                break;
          case SIGKILL: Killed();                      break;
#endif // else WIN32
          default:      Unhandled(nSignal);
          } // switch (nSignal)
        } // void Handled(int nSignal)


    protected:
      void Interrupt()
        {
        m_bExitApplication = true;
        } // void Interrupt()

      void IllegalInstruction()
        {
        exit(1);
        } // void IllegalInstruction()

      void FloatingPointError()
        {
        exit(1);
        } // void FloatingPointError()

      void Terminated()
        {
        m_bExitApplication = true;
        } // void Terminated()

      void AbnormalTermination()
        {
        exit(1);
        } // void AbnormalTermination()

#ifdef WIN32

      void Win32Interrupt()
        {
        m_bExitApplication = true;
        } // void Win32Interrupt()

      void Win32GeneralProtectionFault()
        {
        exit(1);
        } // void Win32GeneralProtectionFault()

#else // WIN32

      void SegmentationFault()
        {
        exit(1);
        } // SegmentationFault()

      void BrokenPipe()
        {
        // continue
        } // void BrokenPipe()

      void Hangup()
        {
        exit(0);
        } // void Hangup()

      void KeyboardStop()
        {
        exit(0);
        } // void KeyboardStop()

      void Killed()
        {
        exit(0);
        } // void Killed()

#endif // else WIN32


    protected:
      // initialisation for our signal handlers
      // registers all handlers in our list
      void InitializeSignalHandling()
        {
        for ( int n = 0; m_ptSignalHandler[n].nSignal != 0; n++ )
          {
          HandlerSet(m_ptSignalHandler[n]);
          } // for ( int n = 0; g_atSignalHandler[n].nSignal != 0; n++ )
        } // void InitializeSignalHandling()

      void HandlerSet(const SSignalHandler& stHandlerRecord)
        {
        HandlerSet(stHandlerRecord.nSignal,
                   stHandlerRecord.fnHandler,
                   stHandlerRecord.sName);
        /*
        signal(stHandlerRecord.nSignal, stHandlerRecord.fnHandler);
        m_mnsSignal2Name[stHandlerRecord.nSignal] = stHandlerRecord.sName;
        */
        } // void SignalChangeHandler(const SSignalHandler& stHandlerRecord)

      void HandlerSet(int nSignal, FHandler fnHandler, const std::string& sName)
        {
        if (fnHandler == 0)
          {
          signal(nSignal, SignalUnhandled);
          }
        else
          {
          signal(nSignal, fnHandler);
          }
        m_mnsSignal2Name[nSignal] = sName;
        } // void SignalSetHandler(int nSignal, const std::string& sName, 
FHandler fnHandler)

    private:

      // handler for unhandled signals
      // shows the number and the name of the signal
      // should show the time too?
      static void SignalUnhandled(int nSignal)
        {
        if (s_poManager != 0)
          {
          s_poManager->Unhandled(nSignal);
          }
        } // void SignalUnhandled(int nSignal)

      static void Signal(int nSignal)
        {
        if (s_poManager != 0)
          {
          s_poManager->Handled(nSignal);
          }
        else // if (CSignalManager::s_poManager != 0)
          {
          TRACE("Signal handler not established\n")
          } // else if (CSignalManager::s_poManager != 0)
        } // void SignalUnhandled(int nSignal)

    }; // class CSignalManager

  } // namespace family

#endif // __SIGNAL_HANDLER_H

--- NEW FILE ---
/***************************************************************************
                               Threaded.h
                           -------------------
    begin                : Sun Mar 04 2001
    copyright            : (C) 2001 by Manfred Morgner
    email                : address@hidden

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

  2001-12-18 Manfred Morgner

    * changed to "all inline" - no cxx file anymore

 ***************************************************************************/

#ifndef __THREADED_H
#define __THREADED_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#if __GNU__ >= 2
#  pragma interface
#endif // #if __GNU__ >= 2

#include "generic.h"
#include <family.h>

// beginthread, endthread
// WINAPI, LPVOID
#ifdef WIN32
//-#include "generic.h"
#else // WIN32
#  include <pthread.h>
#endif
 // else WIN32



namespace family
  {
  ////////////////////////////////////////////////////////////////////////////
  //  CThread is a abstract base class to provide derived classes with
  //  "self activity". This means, a class, derived from CThreaded is able
  //  to do anything independend of any other bussiness in the application.
 
  class CThreaded
    {
    protected:
#ifdef WIN32
    typedef DWORD (WINAPI *pfn)(LPVOID);
#  else
    typedef void* (*pfn)(void*);
#endif // else WIN32

    public:
#ifdef WIN32
      static DWORD WINAPI CThreaded::ThreadDefaultForCThreaded(LPVOID pData)
#else // WIN32
        static void* CThreaded::ThreadDefaultForCThreaded(void* pData)
#endif // else WIN32
        {
        // static type is CThreaded
        CThreaded *poThreaded = (CThreaded*)pData;
        
        for (;;)
          {
          // may be, the pointer becomes invalid
          if (poThreaded == 0)
            {
            SLEEP(100);
            continue;
            } // if (poThreaded == 0)
          
          // or the object becomes inactive
          if (poThreaded->IsStalled() == true)
            {
            SLEEP(100);
            continue;
            }
          
          // if nothings of them, we should call the object
          // to make the next step in his life time
          if (poThreaded->Step() < 0)
            {
            // poThreaded is now invalid because of a call to
            // delete this;
            poThreaded = 0;
            break;
            } // if (poThreaded->Step() < 0)
          
          } // for (ever)
        
#ifdef WIN32
        return (DWORD)0;
#else
        return (void*)0;
        //  _endthread();
#endif // WIN32
        
        } // void* ThreadDefaultForCThreaded(void* pData)


    protected:
      // if this is set to true, the worker thread does not work
      bool m_bIsStalled;
      // the worker thread calls the methode "Step()"
      // if the class isn't "Stopp()"-ed
#ifdef WIN32
      HANDLE m_hWorkerThread;
#  else
      unsigned long m_ulWorkerThread;
#endif // else WIN32
      
    public:
      // may be, we are given a prepaired thread; currently I can't imagine why
      CThreaded(pfn fnThread = 0)
        {
        TRACE("CThreaded\n");
        Initialize(fnThread);
        } // CThreaded()

      virtual ~CThreaded()
        {
        TRACE("CThreaded Destructor\n")
        Stopp();
        ThreadKill();
        TRACE("CThreaded Destructor End\n")
        } // ~CThreaded()

    public:
      // does what the object is good for
      // this methode is abstract to ensure it is overwritten in the
      // derived classes; because, otherwise any derived class is useless
      virtual long Step() = 0;
      
      // starts/restarts the thread calling "Step()"
      virtual long Start()
        {
        m_bIsStalled = false;
        return FAMILY_SUCCESS;
        } // long CThreaded::Start()

      // stops the thread calling "Step()"
      virtual long Stopp()
        {
        m_bIsStalled = true;
        return FAMILY_SUCCESS;
        } // long CThreaded::Stopp()

      // anyone (e.g. the thread) needs to know if the object is
      // working or stalled
      virtual bool IsStalled()
        {
        return m_bIsStalled;
        } // bool IsStalled()

    protected:
      // initializes the class
      void Initialize(pfn fnThread)
        {
#ifdef _DEBUG
        std::cout << "INIT : " << fnThread << "<-" << std::endl;
#endif // _DEBUG
        m_bIsStalled     = true;
#ifdef WIN32
        m_hWorkerThread = 0;
#else
        m_ulWorkerThread = 0;
#endif
        if ( fnThread == 0 )
          {
          ThreadCreate(ThreadDefaultForCThreaded, this);
          }
        else // if ( fnThread == 0 )
          {
          ThreadCreate(fnThread, this);
          } // else if ( fnThread == 0 )
        } // void Initialize()

      // adds a one working thread and assignes them with the own instance
      void ThreadCreate(pfn fnThread, void* pArgument)
        {
#ifdef _DEBUG
        std::cout << "ThreadCreate :" << fnThread << ":" << std::endl;
#endif // _DEBUG
        ThreadKill();
        
        TRACE("ThreadCreate2\n")
#ifdef WIN32
          //-  m_ulWorkerThread = _beginthread(fnThread, 0, pArgument);
          
        DWORD dwThreadId;
        m_hWorkerThread = CreateThread(0, 0, fnThread, pArgument, 0, 
&dwThreadId);
        
#else
        pthread_create(&m_ulWorkerThread, 0, fnThread, pArgument);
#endif
        TRACE("ThreadCreate3\n")
        } // void ThreadCreate( pfn fnThread, void* pArgument )
      
    private:
      // kills the working thread (only called by the destructor)
      // no derived class is allowed to do this, because after killing
      // the worker thread, the instance is useless
      void ThreadKill()
        {
#ifdef WIN32
        if (m_hWorkerThread == NULL)
#else // WIN32
          if (m_ulWorkerThread == 0)
#endif // else WIN32
            {
            return;
            }
#ifdef WIN32
          //  _endthread(m_ulWorkerThread);
          CloseHandle(m_hWorkerThread);
          m_hWorkerThread = 0;
#else
          pthread_exit(0);
          m_ulWorkerThread = 0;
#endif
        } // void CThreaded::ThreadKill()
      
    }; // class CThreaded
  
  } // namespace family

#endif // __THREADED_H

--- NEW FILE ---
/***************************************************************************
                                family.h
                           -------------------
    begin                : Sat Jun 23 2001
    copyright            : (C) 2001 by Alexander Feder
    email                : address@hidden

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

 common includes and defines for the the "family library"

 ***************************************************************************/

#ifndef __FAMILY_H
#define __FAMILY_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#if __GNU__ >= 2
#  pragma interface
#endif // __GNU__ >= 2

namespace family 
  {
  enum
    {
    FAMILY_SUCCESS        =  0,
    FAMILY_FAILURE        = -1,
    FAMILY_ERROR          = -2,
    FAMILY_E_NO_TRANSPORT = -3
    };
  }; //namespace family

#ifdef _DEBUG
  #include <iostream> // for tracing
#endif // _DEBUG

#ifndef TRACE
#  ifdef _DEBUG
#    define TRACE(s) std::cout << s;
#  else // _DEBUG
#    define TRACE(s) {};
#  endif // _DEBUG
#endif // TRACE

#ifndef _T
#  define _T(s) (const std::string&) s
#endif // _T


#endif //#ifndef __FAMILY_H

--- NEW FILE ---
/***************************************************************************
                               generic.h
                           -------------------
    begin                : Mon Mar 13 2000
    copyright            : (C) 1993..2001 by Manfred Morgner
    email                : address@hidden

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *                                                                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place Suite 330,                                            *
 *   Boston, MA  02111-1307, USA.                                          *
 *                                                                         *
 ***************************************************************************

 common includes and defines for the project

 ***************************************************************************/

#ifndef __GENERIC_H
#define __GENERIC_H

#if _MSC_VER >= 1000
#pragma once
#pragma warning (disable : 4786)
#endif // _MSC_VER >= 1000

#if __GNU__ >= 2
#  pragma interface
#endif // #if __GNU__ >= 2


// generic flags
#define E_NOT_IMPLEMENTED 0x80001000L

#ifdef WIN32

// #  include <afx.h>
  extern "C" int gettimeofday( struct timeval*, struct timezone*);
// will sleep for <n> milli seconds
#  define SLEEP(n) Sleep(n)
#  define LONG  long
#  define CHAR  char
#  define UCHAR unsigned char

#else // WIN32

#  include <sys/time.h>
#  include <unistd.h>
// will sleep for <n> milli seconds
#  define SLEEP(n) usleep(n * 1000)
#  define POSIX

  typedef long LONG;
  typedef char CHAR;
  typedef unsigned char UCHAR;

#endif // else WIN32


#ifdef _DEBUG
  #include <iostream> // for tracing
#endif // _DEBUG

#ifndef TRACE
#  ifdef _DEBUG
#    define TRACE(s) std::cout << s;
#  else // _DEBUG
#    define TRACE(s) {};
#  endif // _DEBUG
#endif // TRACE

#ifndef _T
#  define _T(s) (const std::string&) s
#endif // _T

bool operator > (const timeval& t1, const timeval& t2);

// #include <string>

#endif // __GENERIC_H





reply via email to

[Prev in Thread] Current Thread [Next in Thread]