pprocess.h
Go to the documentation of this file.
/*
* pprocess.h
*
* Operating System Process (running program executable) class.
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 27817 $
* $Author: rjongbloed $
* $Date: 2012-06-12 21:52:14 -0500 (Tue, 12 Jun 2012) $
*/
#ifndef PTLIB_PROCESS_H
#define PTLIB_PROCESS_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/mutex.h>
#include <ptlib/syncpoint.h>
#include <ptlib/thread.h>
#include <ptlib/pfactory.h>
#include <queue>
#include <set>
#ifdef P_VXWORKS
#define PCREATE_PROCESS(cls) \
cls instance; \
instance.InternalMain();
#elif defined(P_RTEMS)
#define PCREATE_PROCESS(cls) \
extern "C" {\
void* POSIX_Init( void* argument) \
{ \
static cls instance; \
exit( instance.InternalMain() ); \
} \
}
#elif defined(_WIN32_WCE)
#define PCREATE_PROCESS(cls) \
PDEFINE_WINMAIN(hInstance, , lpCmdLine, ) \
{ \
cls *pInstance = new cls(); \
pInstance->GetArguments().SetArgs(lpCmdLine); \
int terminationValue = pInstance->InternalMain(hInstance); \
delete pInstance; \
return terminationValue; \
}
#else
#define PCREATE_PROCESS(cls) \
int main(int argc, char ** argv, char ** envp) \
{ \
cls *pInstance = new cls(); \
pInstance->PreInitialise(argc, argv, envp); \
int terminationValue = pInstance->InternalMain(); \
delete pInstance; \
return terminationValue; \
}
#endif // P_VXWORKS
/*$MACRO PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build)
This macro is used to declare the components necessary for a user PWLib
process. This will declare the PProcess descendent class, eg PApplication,
and create an instance of the class. See the <code>PCREATE_PROCESS</code> macro
for more details.
*/
#define PDECLARE_PROCESS(cls,ancestor,manuf,name,major,minor,status,build) \
class cls : public ancestor { \
PCLASSINFO(cls, ancestor); \
public: \
cls() : ancestor(manuf, name, major, minor, status, build) { } \
private: \
virtual void Main(); \
};
class PTimerList : public PObject
/* This class defines a list of <code>PTimer</code> objects. It is primarily used
internally by the library and the user should never create an instance of
it. The <code>PProcess</code> instance for the application maintains an instance
of all of the timers created so that it may decrements them at regular
intervals.
*/
{
PCLASSINFO(PTimerList, PObject);
public:
// Create a new timer list
PTimerList();
/* Decrement all the created timers and dispatch to their callback
functions if they have expired. The <code>PTimer::Tick()</code> function
value is used to determine the time elapsed since the last call to
Process().
The return value is the number of milliseconds until the next timer
needs to be despatched. The function need not be called again for this
amount of time, though it can (and usually is).
@return
maximum time interval before function should be called again.
*/
PTimeInterval Process();
PTimer::IDType GetNewTimerId() const { return ++timerId; }
class RequestType {
public:
enum Action {
Stop,
Start
} m_action;
RequestType(Action act, PTimer * t)
: m_action(act)
, m_timer(t)
, m_id(t->GetTimerId())
, m_absoluteTime(t->GetAbsoluteTime())
, m_serialNumber(t->GetNextSerialNumber())
, m_sync(NULL)
{ }
PTimer * m_timer;
PTimer::IDType m_id;
PInt64 m_absoluteTime;
PAtomicInteger::IntegerType m_serialNumber;
PSyncPoint * m_sync;
};
void QueueRequest(RequestType::Action action, PTimer * timer, bool isSync = true);
void ProcessTimerQueue();
private:
// queue of timer action requests
PMutex m_queueMutex;
typedef std::queue<RequestType> RequestQueueType;
RequestQueueType m_requestQueue;
// add an active timer to the lists
void AddActiveTimer(const RequestType & request);
// counter to keep track of timer IDs
mutable PAtomicInteger timerId;
// map used to store active timer information
struct ActiveTimerInfo {
ActiveTimerInfo(PTimer * t, PAtomicInteger::IntegerType serialNumber)
: m_timer(t), m_serialNumber(serialNumber) { }
PTimer * m_timer;
PAtomicInteger::IntegerType m_serialNumber;
};
typedef std::map<PTimer::IDType, ActiveTimerInfo> ActiveTimerInfoMap;
ActiveTimerInfoMap m_activeTimers;
// set used to store timer expiry times, in order
struct TimerExpiryInfo {
TimerExpiryInfo(PTimer::IDType id, PInt64 expireTime, PAtomicInteger::IntegerType serialNumber)
: m_timerId(id), m_expireTime(expireTime), m_serialNumber(serialNumber) { }
PTimer::IDType m_timerId;
PInt64 m_expireTime;
PAtomicInteger::IntegerType m_serialNumber;
};
struct TimerExpiryInfo_compare
: public binary_function<TimerExpiryInfo, TimerExpiryInfo, bool>
{
bool operator()(const TimerExpiryInfo & _Left, const TimerExpiryInfo & _Right) const
{ return (_Left.m_expireTime < _Right.m_expireTime); }
};
typedef std::multiset<TimerExpiryInfo, TimerExpiryInfo_compare> TimerExpiryInfoList;
TimerExpiryInfoList m_expiryList;
// The last system timer tick value that was used to process timers.
PTimeInterval m_lastSample;
// thread that handles the timer stuff
PThread * m_timerThread;
};
// PProcess
class PProcess : public PThread
{
PCLASSINFO(PProcess, PThread);
public:
enum CodeStatus {
AlphaCode,
BetaCode,
ReleaseCode,
NumCodeStatuses
};
PProcess(
const char * manuf = "",
const char * name = "",
WORD majorVersion = 1,
WORD minorVersion = 0,
CodeStatus status = ReleaseCode,
WORD buildNumber = 1,
bool library = false
);
Comparison Compare(
const PObject & obj
) const;
virtual void Terminate();
virtual PString GetThreadName() const;
virtual void SetThreadName(
const PString & name
);
static PProcess & Current();
virtual void OnThreadStart(
PThread & thread
);
virtual void OnThreadEnded(
PThread & thread
);
virtual bool OnInterrupt(
bool terminating
);
static PBoolean IsInitialised();
void SetTerminationValue(
int value
);
int GetTerminationValue() const;
PArgList & GetArguments();
virtual const PString & GetManufacturer() const;
virtual const PString & GetName() const;
virtual PString GetVersion(
PBoolean full = true
) const;
const PFilePath & GetFile() const;
PProcessIdentifier GetProcessID() const { return m_processID; }
static PProcessIdentifier GetCurrentProcessID();
PTime GetStartTime() const;
PString GetUserName() const;
PBoolean SetUserName(
const PString & username,
PBoolean permanent = false
);
PString GetGroupName() const;
PBoolean SetGroupName(
const PString & groupname,
PBoolean permanent = false
);
int GetMaxHandles() const;
PBoolean SetMaxHandles(
int newLimit
);
#ifdef P_CONFIG_FILE
virtual PString GetConfigurationFile();
#endif
void SetConfigurationPath(
const PString & path
);
static PString GetOSClass();
static PString GetOSName();
static PString GetOSHardware();
static PString GetOSVersion();
static bool IsOSVersion(
unsigned major,
unsigned minor = 0,
unsigned build = 0
);
static PDirectory GetOSConfigDir();
static PString GetLibVersion();
PTimerList * GetTimerList();
void PreInitialise(
int argc, // Number of program arguments.
char ** argv, // Array of strings for program arguments.
char ** envp // Array of string for the system environment
);
static void PreShutdown();
static void PostShutdown();
virtual int InternalMain(void * arg = NULL);
class HostSystemURLHandlerInfo
{
public:
HostSystemURLHandlerInfo()
{ }
HostSystemURLHandlerInfo(const PString & t)
: type(t)
{ }
static bool RegisterTypes(const PString & types, bool force = true);
void SetIcon(const PString & icon);
PString GetIcon() const;
void SetCommand(const PString & key, const PString & command);
PString GetCommand(const PString & key) const;
bool GetFromSystem();
bool CheckIfRegistered();
bool Register();
PString type;
#if _WIN32
PString iconFileName;
PStringToString cmds;
#endif
};
protected:
void Construct();
// Member variables
bool m_library; // Indication PTLib is being used as a library for an external process.
int terminationValue; // Application return value
PString manufacturer; // Application manufacturer name.
PString productName; // Application executable base name from argv[0]
WORD majorVersion; // Major version number of the product
WORD minorVersion; // Minor version number of the product
CodeStatus status; // Development status of the product
WORD buildNumber; // Build number of the product
PFilePath executableFile; // Application executable file from argv[0] (not open)
PStringArray configurationPaths; // Explicit file or set of directories to find default PConfig
PArgList arguments; // The list of arguments
int maxHandles; // Maximum number of file handles process can open.
PTime programStartTime; // time at which process was intantiated, i.e. started
bool m_shuttingDown;
typedef std::map<PThreadIdentifier, PThread *> ThreadMap;
ThreadMap m_activeThreads;
PMutex m_activeThreadMutex;
PTimerList timers;
PProcessIdentifier m_processID;
friend class PThread;
// Include platform dependent part of class
#ifdef _WIN32
#include "msos/ptlib/pprocess.h"
#else
#include "unix/ptlib/pprocess.h"
#endif
};
class PLibraryProcess : public PProcess
{
PCLASSINFO(PLibraryProcess, PProcess);
public:
PLibraryProcess(
const char * manuf = "",
const char * name = "",
WORD majorVersionNum = 1,
WORD minorVersionNum = 0,
CodeStatus statusCode = ReleaseCode,
WORD buildNum = 1
) : PProcess(manuf, name, majorVersionNum, minorVersionNum, statusCode, buildNum, true) { }
virtual void Main() { }
};
/*
* one instance of this class (or any descendants) will be instantiated
* via PGenericFactory<PProessStartup> one "main" has been started, and then
* the OnStartup() function will be called. The OnShutdown function will
* be called after main exits, and the instances will be destroyed if they
* are not singletons
*/
class PProcessStartup : public PObject
{
PCLASSINFO(PProcessStartup, PObject)
public:
virtual void OnStartup() { }
virtual void OnShutdown() { }
};
typedef PFactory<PProcessStartup> PProcessStartupFactory;
#if PTRACING
// using an inline definition rather than a #define crashes gcc 2.95. Go figure
#define P_DEFAULT_TRACE_OPTIONS ( PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine )
template <unsigned level, unsigned options = P_DEFAULT_TRACE_OPTIONS >
class PTraceLevelSetStartup : public PProcessStartup
{
public:
void OnStartup()
{ PTrace::Initialise(level, NULL, options); }
};
#endif // PTRACING
#endif // PTLIB_PROCESS_H
// End Of File ///////////////////////////////////////////////////////////////
PProcess::GetLibVersion
static PString GetLibVersion()
Get the version of the PTLib library the process is running on, eg "2.5beta3".
PProcess::OnInterrupt
virtual bool OnInterrupt(bool terminating)
Callback for when a ^C (SIGINT) or termination request (SIGTERM) is received by process.
PProcess::m_shuttingDown
bool m_shuttingDown
Definition: pprocess.h:724
PProcess::GetName
virtual const PString & GetName() const
Get the name of the process.
PProcess::ThreadMap
std::map< PThreadIdentifier, PThread * > ThreadMap
Definition: pprocess.h:726
PLibraryProcess::Main
virtual void Main()
< Dummy Main() as libraries do not have one.
Definition: pprocess.h:768
PProcess::PreShutdown
static void PreShutdown()
Internal shutdown function called directly from the ~PProcess InternalMain().
PProcess::buildNumber
WORD buildNumber
Definition: pprocess.h:715
PProcess::programStartTime
PTime programStartTime
Definition: pprocess.h:722
PTimerList::QueueRequest
void QueueRequest(RequestType::Action action, PTimer *timer, bool isSync=true)
PProcess::GetGroupName
PString GetGroupName() const
Get the effective group name of the owner of the process, eg "root" etc.
PProcess::GetOSHardware
static PString GetOSHardware()
Get the hardware the process is running on, eg "sparc".
PTimerList::RequestType::m_absoluteTime
PInt64 m_absoluteTime
Definition: pprocess.h:154
PTimeInterval
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PProcess::NumCodeStatuses
Definition: pprocess.h:242
PProcess::Current
static PProcess & Current()
Get the current processes object instance.
pfactory.h
PProcess::productName
PString productName
Definition: pprocess.h:710
PProcess::HostSystemURLHandlerInfo::GetFromSystem
bool GetFromSystem()
PTime
This class defines an absolute time and date.
Definition: ptime.h:53
PProcess::HostSystemURLHandlerInfo::SetIcon
void SetIcon(const PString &icon)
PProcess::m_library
bool m_library
Definition: pprocess.h:706
PProcessStartupFactory
PFactory< PProcessStartup > PProcessStartupFactory
Definition: pprocess.h:787
PStringToString
This is a dictionary collection class of PString objects, keyed by another string.
Definition: pstring.h:2784
PFilePath
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
PProcess::SetTerminationValue
void SetTerminationValue(int value)
Set the termination value for the process.
PObject::Comparison
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
PProcess::GetOSVersion
static PString GetOSVersion()
Get the version of the operating system the process is running on, eg "2.0.33".
PProcess::HostSystemURLHandlerInfo::HostSystemURLHandlerInfo
HostSystemURLHandlerInfo()
Definition: pprocess.h:673
PTimerList::RequestType::m_timer
PTimer * m_timer
Definition: pprocess.h:152
PProcess::status
CodeStatus status
Definition: pprocess.h:714
PTimerList::GetNewTimerId
PTimer::IDType GetNewTimerId() const
Definition: pprocess.h:134
PProcess::Terminate
virtual void Terminate()
Terminate the process.
PProcess::AlphaCode
Code is still very much under construction.
Definition: pprocess.h:237
PTimerList::RequestType::RequestType
RequestType(Action act, PTimer *t)
Definition: pprocess.h:143
PProcess::GetOSName
static PString GetOSName()
Get the name of the operating system the process is running on, eg "Linux".
PProcess::PostShutdown
static void PostShutdown()
PTrace::Initialise
static void Initialise(unsigned level, const char *filename=NULL, unsigned options=Timestamp|Thread|Blocks)
Set the most common trace options.
PProcess::GetArguments
PArgList & GetArguments()
Get the programme arguments.
PProcess::InternalMain
virtual int InternalMain(void *arg=NULL)
Main function for process, called from real main after initialisation.
PProcess::GetOSClass
static PString GetOSClass()
Get the class of the operating system the process is running on, eg "unix".
PProcess::IsOSVersion
static bool IsOSVersion(unsigned major, unsigned minor=0, unsigned build=0)
See if operating system is later than the version specified.
PTimerList::ProcessTimerQueue
void ProcessTimerQueue()
PStringArray
This is an array collection class of PString objects.
Definition: pstring.h:2024
PProcess::minorVersion
WORD minorVersion
Definition: pprocess.h:713
PProcess::OnThreadEnded
virtual void OnThreadEnded(PThread &thread)
Callback for when a thread is ended if wqas started in the PTLib system.
PProcess::HostSystemURLHandlerInfo::SetCommand
void SetCommand(const PString &key, const PString &command)
PProcess::SetMaxHandles
PBoolean SetMaxHandles(int newLimit)
Set the maximum number of file handles for the process.
PProcess::GetConfigurationFile
virtual PString GetConfigurationFile()
Get the default file to use in PConfig instances.
PProcess::GetManufacturer
virtual const PString & GetManufacturer() const
Get the name of the manufacturer of the software.
thread.h
PProcess::Compare
Comparison Compare(const PObject &obj) const
Compare two process instances.
PProcess::timers
PTimerList timers
Definition: pprocess.h:730
PProcess::manufacturer
PString manufacturer
Definition: pprocess.h:709
PProcess::BetaCode
Code is largely complete and is under test.
Definition: pprocess.h:239
PProcess
This class represents an operating system process.
Definition: pprocess.h:227
PTimer
A class representing a system timer.
Definition: timer.h:181
PBoolean
BOOL PBoolean
Definition: object.h:102
PDirectory
Class to represent a directory in the operating system file system.
Definition: pdirect.h:182
PProcess::majorVersion
WORD majorVersion
Definition: pprocess.h:712
PTimerList
Definition: pprocess.h:106
PProcess::Construct
void Construct()
PProcess::m_processID
PProcessIdentifier m_processID
Definition: pprocess.h:732
PAtomicInteger
This class implements an integer that can be atomically incremented and decremented in a thread-safe ...
Definition: critsec.h:171
PTimerList::RequestType::Action
Action
Definition: pprocess.h:138
PProcess::GetStartTime
PTime GetStartTime() const
Return the time at which the program was started.
PProcess::GetOSConfigDir
static PDirectory GetOSConfigDir()
Get the configuration directory of the operating system the process is running on, eg "/etc" for Unix, "c:\windows" for Win95 or "c:\winnt\system32\drivers\etc" for NT.
PProcess::IsInitialised
static PBoolean IsInitialised()
Determine if the current processes object instance has been initialised.
PProcess::HostSystemURLHandlerInfo
This class can be used to register various URL types with the host operating system so that URLs will...
Definition: pprocess.h:670
PLibraryProcess::PLibraryProcess
PLibraryProcess(const char *manuf="", const char *name="", WORD majorVersionNum=1, WORD minorVersionNum=0, CodeStatus statusCode=ReleaseCode, WORD buildNum=1)
Create a new process instance.
Definition: pprocess.h:757
PProcess::PProcess
PProcess(const char *manuf="", const char *name="", WORD majorVersion=1, WORD minorVersion=0, CodeStatus status=ReleaseCode, WORD buildNumber=1, bool library=false)
Create a new process instance.
PLibraryProcess
Class for a process that is a dynamically loaded library.
Definition: pprocess.h:748
PTimerList::RequestType::m_action
enum PTimerList::RequestType::Action m_action
PProcess::SetThreadName
virtual void SetThreadName(const PString &name)
Change the name of the thread.
PProcess::GetUserName
PString GetUserName() const
Get the effective user name of the owner of the process, eg "root" etc.
PString
The character string class.
Definition: pstring.h:108
mutex.h
PProcess::GetMaxHandles
int GetMaxHandles() const
Get the maximum file handle value for the process.
PArgList
This class allows the parsing of a set of program arguments.
Definition: args.h:45
PProcess::GetTimerList
PTimerList * GetTimerList()
Get the list of timers handled by the application.
PTimerList::RequestType::m_id
PTimer::IDType m_id
Definition: pprocess.h:153
PThread
This class defines a thread of execution in the system.
Definition: thread.h:66
PTimerList::PTimerList
PTimerList()
PProcess::configurationPaths
PStringArray configurationPaths
Definition: pprocess.h:718
PProcessStartup::OnShutdown
virtual void OnShutdown()
Definition: pprocess.h:784
PProcess::HostSystemURLHandlerInfo::GetCommand
PString GetCommand(const PString &key) const
syncpoint.h
PTimerList::RequestType::m_serialNumber
PAtomicInteger::IntegerType m_serialNumber
Definition: pprocess.h:155
PFactory
Template class for generic factories of an abstract class.
Definition: pfactory.h:144
PProcess::GetThreadName
virtual PString GetThreadName() const
Get the name of the thread.
PProcess::m_activeThreadMutex
PMutex m_activeThreadMutex
Definition: pprocess.h:728
PProcess::SetUserName
PBoolean SetUserName(const PString &username, PBoolean permanent=false)
Set the effective owner of the process.
PTimerList::RequestType
Definition: pprocess.h:136
PProcessStartup
Definition: pprocess.h:779
PProcess::GetCurrentProcessID
static PProcessIdentifier GetCurrentProcessID()
Get the platform dependent process identifier for the currentprocess.
PProcess::GetFile
const PFilePath & GetFile() const
Get the processes executable image file path.
PMutex
Synonym for PTimedMutex.
PProcess::executableFile
PFilePath executableFile
Definition: pprocess.h:717
PTimerList::RequestType::Stop
Definition: pprocess.h:139
PProcess::ReleaseCode
Code has all known bugs removed and is shipping.
Definition: pprocess.h:241
PProcess::SetGroupName
PBoolean SetGroupName(const PString &groupname, PBoolean permanent=false)
Set the effective group of the process.
PProcess::HostSystemURLHandlerInfo::Register
bool Register()
PProcessStartup::OnStartup
virtual void OnStartup()
Definition: pprocess.h:783
PProcess::maxHandles
int maxHandles
Definition: pprocess.h:720
PProcess::CodeStatus
CodeStatus
Release status for the program.
Definition: pprocess.h:235
PProcess::terminationValue
int terminationValue
Definition: pprocess.h:707
PProcess::arguments
PArgList arguments
Definition: pprocess.h:719
PAtomicInteger::IntegerType
PAtomicBase::IntegerType IntegerType
Definition: critsec.h:174
PProcess::HostSystemURLHandlerInfo::RegisterTypes
static bool RegisterTypes(const PString &types, bool force=true)
PProcess::m_activeThreads
ThreadMap m_activeThreads
Definition: pprocess.h:727
PProcess::HostSystemURLHandlerInfo::HostSystemURLHandlerInfo
HostSystemURLHandlerInfo(const PString &t)
Definition: pprocess.h:676
PProcess::SetConfigurationPath
void SetConfigurationPath(const PString &path)
Set the default file or set of directories to search for use in PConfig.
PTimerList::Process
PTimeInterval Process()
PProcess::PreInitialise
void PreInitialise(int argc, char **argv, char **envp)
Internal initialisation function called directly from InternalMain().
PProcess::OnThreadStart
virtual void OnThreadStart(PThread &thread)
Callback for when a thread is started by the PTLib system.
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PTimerList::RequestType::Start
Definition: pprocess.h:140
PProcess::GetProcessID
PProcessIdentifier GetProcessID() const
Get the platform dependent process identifier for the process.
Definition: pprocess.h:425
PProcess::HostSystemURLHandlerInfo::type
PString type
Definition: pprocess.h:693
PSyncPoint
This class defines a thread synchronisation object.
Definition: syncpoint.h:67
PProcess::GetVersion
virtual PString GetVersion(PBoolean full=true) const
Get the version of the software.
PTimerList::RequestType::m_sync
PSyncPoint * m_sync
Definition: pprocess.h:156
PProcess::GetTerminationValue
int GetTerminationValue() const
Get the termination value for the process.
PTimer::IDType
unsigned IDType
Definition: timer.h:186
PProcess::HostSystemURLHandlerInfo::GetIcon
PString GetIcon() const
PProcess::HostSystemURLHandlerInfo::CheckIfRegistered
bool CheckIfRegistered()