pprocess.h

Go to the documentation of this file.
00001 /*
00002  * pprocess.h
00003  *
00004  * Operating System Process (running program executable) class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 19008 $
00030  * $Author: rjongbloed $
00031  * $Date: 2007-11-29 09:17:41 +0000 (Thu, 29 Nov 2007) $
00032  */
00033 
00034 #ifndef _PPROCESS
00035 #define _PPROCESS
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <ptlib/mutex.h>
00042 #include <ptlib/syncpoint.h>
00043 #include <ptlib/thread.h>
00044 #include <ptlib/pfactory.h>
00045 
00052 #ifdef P_VXWORKS
00053 #define PCREATE_PROCESS(cls) \
00054   PProcess::PreInitialise(0, NULL, NULL); \
00055   cls instance; \
00056   instance._main();
00057 #elif defined(P_RTEMS)
00058 #define PCREATE_PROCESS(cls) \
00059 extern "C" {\
00060    void* POSIX_Init( void* argument) \
00061      { PProcess::PreInitialise(0, 0, 0); \
00062        static cls instance; \
00063        exit( instance._main() ); \
00064      } \
00065 }
00066 #elif defined(_WIN32_WCE)
00067 #define PCREATE_PROCESS(cls) \
00068   int WinMain(HINSTANCE, HINSTANCE, LPWSTR, int) \
00069     { cls *pInstance = new cls(); \
00070       int terminationValue = pInstance->_main(); \
00071       delete pInstance; \
00072       return terminationValue; \
00073     }
00074 #else
00075 #define PCREATE_PROCESS(cls) \
00076   int main(int argc, char ** argv, char ** envp) \
00077     { PProcess::PreInitialise(argc, argv, envp); \
00078       cls *pInstance = new cls(); \
00079       int terminationValue = pInstance->_main(); \
00080       delete pInstance; \
00081       return terminationValue; \
00082     }
00083 #endif // P_VXWORKS
00084 
00085 /*$MACRO PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build)
00086    This macro is used to declare the components necessary for a user PWLib
00087    process. This will declare the PProcess descendent class, eg PApplication,
00088    and create an instance of the class. See the #PCREATE_PROCESS# macro
00089    for more details.
00090  */
00091 #define PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build) \
00092   class cls : public ancestor { \
00093     PCLASSINFO(cls, ancestor); \
00094     public: \
00095       cls() : ancestor(manuf, name, major, minor, status, build) { } \
00096     private: \
00097       virtual void Main(); \
00098   };
00099 
00100 
00101 PLIST(PInternalTimerList, PTimer);
00102 
00103 class PTimerList : PInternalTimerList // Want this to be private
00104 /* This class defines a list of #PTimer# objects. It is primarily used
00105    internally by the library and the user should never create an instance of
00106    it. The #PProcess# instance for the application maintains an instance
00107    of all of the timers created so that it may decrements them at regular
00108    intervals.
00109  */
00110 {
00111   PCLASSINFO(PTimerList, PInternalTimerList);
00112 
00113   public:
00114     PTimerList();
00115     // Create a new timer list
00116 
00117     PTimeInterval Process();
00118     /* Decrement all the created timers and dispatch to their callback
00119        functions if they have expired. The #PTimer::Tick()# function
00120        value is used to determine the time elapsed since the last call to
00121        Process().
00122 
00123        The return value is the number of milliseconds until the next timer
00124        needs to be despatched. The function need not be called again for this
00125        amount of time, though it can (and usually is).
00126        
00127        @return
00128        maximum time interval before function should be called again.
00129      */
00130 
00131   private:
00132     PMutex listMutex, processingMutex, inTimeoutMutex;
00133     // Mutual exclusion for multi tasking
00134 
00135     PTimeInterval lastSample;
00136     // The last system timer tick value that was used to process timers.
00137 
00138     PTimer * currentTimer;
00139     // The timer which is currently being handled
00140 
00141   friend class PTimer;
00142 };
00143 
00144 
00146 // PProcess
00147 
00160 class PProcess : public PThread
00161 {
00162   PCLASSINFO(PProcess, PThread);
00163 
00164   public:
00167 
00168     enum CodeStatus {
00170       AlphaCode,    
00172       BetaCode,     
00174       ReleaseCode,  
00175       NumCodeStatuses
00176     };
00177 
00180     PProcess(
00181       const char * manuf = "",         
00182       const char * name = "",          
00183       WORD majorVersion = 1,           
00184       WORD minorVersion = 0,           
00185       CodeStatus status = ReleaseCode, 
00186       WORD buildNumber = 1             
00187     );
00189 
00198     Comparison Compare(
00199       const PObject & obj   
00200     ) const;
00202 
00207     virtual void Terminate();
00208 
00214     virtual PString GetThreadName() const;
00215 
00221     virtual void SetThreadName(
00222       const PString & name        
00223     );
00225 
00234     static PProcess & Current();
00235 
00239     virtual void OnThreadStart(
00240       PThread & thread
00241     );
00242 
00246     virtual void OnThreadEnded(
00247       PThread & thread
00248     );
00249 
00256     static PBoolean IsInitialised();
00257 
00264     void SetTerminationValue(
00265       int value  
00266     );
00267 
00277     int GetTerminationValue() const;
00278 
00285     PArgList & GetArguments();
00286 
00296     virtual const PString & GetManufacturer() const;
00297 
00307     virtual const PString & GetName() const;
00308 
00323     virtual PString GetVersion(
00324       PBoolean full = PTrue 
00325     ) const;
00326 
00332     const PFilePath & GetFile() const;
00333 
00341     DWORD GetProcessID() const;
00342 
00345     PTime GetStartTime() const;
00346 
00355     PString GetUserName() const;
00356 
00379     PBoolean SetUserName(
00380       const PString & username, 
00381       PBoolean permanent = PFalse    
00382     );
00383 
00392     PString GetGroupName() const;
00393 
00418     PBoolean SetGroupName(
00419       const PString & groupname, 
00420       PBoolean permanent = PFalse     
00421     );
00422 
00429     int GetMaxHandles() const;
00430 
00440     PBoolean SetMaxHandles(
00441       int newLimit  
00442     );
00443 
00444 #ifdef P_CONFIG_FILE
00445 
00447     virtual PString GetConfigurationFile();
00448 #endif
00449 
00463     void SetConfigurationPath(
00464       const PString & path   
00465     );
00467 
00476     static PString GetOSClass();
00477 
00484     static PString GetOSName();
00485 
00491     static PString GetOSHardware();
00492 
00499     static PString GetOSVersion();
00500 
00508     static PDirectory GetOSConfigDir();
00510 
00517     PTimerList * GetTimerList();
00518 
00522     static void PreInitialise(
00523       int argc,     // Number of program arguments.
00524       char ** argv, // Array of strings for program arguments.
00525       char ** envp  // Array of string for the system environment
00526     );
00527 
00531     static void PreShutdown();
00532 
00534     virtual int _main(void * arg = NULL);
00535 
00536   private:
00537     void Construct();
00538 
00539   // Member variables
00540     static int p_argc;
00541     static char ** p_argv;
00542     static char ** p_envp;
00543     // main arguments
00544 
00545     int terminationValue;
00546     // Application return value
00547 
00548     PString manufacturer;
00549     // Application manufacturer name.
00550 
00551     PString productName;
00552     // Application executable base name from argv[0]
00553 
00554     WORD majorVersion;
00555     // Major version number of the product
00556     
00557     WORD minorVersion;
00558     // Minor version number of the product
00559     
00560     CodeStatus status;
00561     // Development status of the product
00562     
00563     WORD buildNumber;
00564     // Build number of the product
00565 
00566     PFilePath executableFile;
00567     // Application executable file from argv[0] (not open)
00568 
00569     PStringList configurationPaths;
00570     // Explicit file or set of directories to find default PConfig
00571 
00572     PArgList arguments;
00573     // The list of arguments
00574 
00575     PTimerList timers;
00576     // List of active timers in system
00577 
00578     PTime programStartTime;
00579     // time at which process was intantiated, i.e. started
00580 
00581     int maxHandles;
00582     // Maximum number of file handles process can open.
00583 
00584 
00585   friend class PThread;
00586 
00587 
00588 // Include platform dependent part of class
00589 #ifdef _WIN32
00590 #include "msos/ptlib/pprocess.h"
00591 #else
00592 #include "unix/ptlib/pprocess.h"
00593 #endif
00594 };
00595 
00596 /*
00597  *  one instance of this class (or any descendants) will be instantiated
00598  *  via PGenericFactory<PProessStartup> one "main" has been started, and then
00599  *  the OnStartup() function will be called. The OnShutdown function will
00600  *  be called after main exits, and the instances will be destroyed if they
00601  *  are not singletons
00602  */
00603 class PProcessStartup : public PObject
00604 {
00605   PCLASSINFO(PProcessStartup, PObject)
00606   public:
00607     virtual void OnStartup()  { }
00608     virtual void OnShutdown() { }
00609 };
00610 
00611 typedef PFactory<PProcessStartup> PProcessStartupFactory;
00612 
00613 // using an inline definition rather than a #define crashes gcc 2.95. Go figure
00614 #define P_DEFAULT_TRACE_OPTIONS ( PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine )
00615 
00616 template <unsigned _level, unsigned _options = P_DEFAULT_TRACE_OPTIONS >
00617 class PTraceLevelSetStartup : public PProcessStartup
00618 {
00619   public:
00620     void OnStartup()
00621     { PTrace::Initialise(_level, NULL, _options); }
00622 };
00623 
00624 #endif
00625 
00626 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Dec 10 11:18:57 2007 for PTLib by  doxygen 1.5.1