00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _FXProcess_h_
00023 #define _FXProcess_h_
00024
00025 #include "fxdefs.h"
00026 #include "FXString.h"
00027 #include "FXGenericTools.h"
00028
00029 namespace FX {
00030
00042 typedef struct timeval
00043 {
00044 long tv_sec;
00045 long tv_usec;
00046 } timeval;
00047
00048 class QThreadPool;
00049 class FXACL;
00050
00130 struct FXProcessPrivate;
00131 class FXProcess_StaticInitBase;
00132 class FXProcess_StaticDepend;
00133 template<class type> class QValueList;
00134 class FXAPI FXProcess
00135 {
00136 FXProcessPrivate *p;
00137 FXDLLLOCAL void init(int &argc, char *argv[]);
00138 FXDLLLOCAL void destroy();
00139 public:
00141 FXProcess();
00143 FXProcess(int &argc, char *argv[]);
00144 ~FXProcess();
00146 static void exit(FXint code=0);
00148 bool runPendingStaticInits(int &argc, char *argv[], FXStream &txtout);
00150 static bool isAutomatedTest() throw();
00151
00153 static FXProcess *instance();
00155 static FXuint getMsCount();
00157 static void getTimeOfDay(struct timeval *ts);
00163 static FXulong getNsCount();
00165 static FXuint id();
00167 static FXuint noOfProcessors();
00169 static FXuint pageSize();
00171 struct MappedFileInfo
00172 {
00173 FXString path;
00174 FXuval startaddr, endaddr;
00175 FXuval offset;
00176 FXuval length;
00177 bool read, write, execute, copyonwrite;
00178
00179 bool operator<(const MappedFileInfo &o) { return startaddr<o.startaddr; }
00180 };
00182 typedef QValueList<MappedFileInfo> MappedFileInfoList;
00186 static MappedFileInfoList mappedFiles(bool forceRefresh=false);
00188 static const FXString &execpath();
00193 static FXString dllPath(void *addr, void **dllstart=0, void **dllend=0);
00195 class dllHandle
00196 {
00197 friend class FXProcess;
00198 void *h;
00199 dllHandle(void *_h) : h(_h) { }
00200 public:
00201 dllHandle() : h(0) { }
00202 #ifndef HAVE_CPP0XRVALUEREFS
00203 #ifdef HAVE_CONSTTEMPORARIES
00204 dllHandle(const dllHandle &other) : h(other.h)
00205 {
00206 dllHandle &o=const_cast<dllHandle &>(other);
00207 #else
00208 dllHandle(dllHandle &o) : h(o.h)
00209 {
00210 #endif
00211 #else
00212 private:
00213 dllHandle(const dllHandle &);
00214 public:
00215 dllHandle(dllHandle &&o) : h(o.h)
00216 {
00217 #endif
00218 o.h=0;
00219 }
00220 ~dllHandle() { if(h) dllUnload(*this); }
00221 #ifndef HAVE_CPP0XRVALUEREFS
00222 dllHandle &operator=(dllHandle &o)
00223 #else
00224 private:
00225 dllHandle &operator=(dllHandle &o);
00226 public:
00227 dllHandle &&operator=(dllHandle &&o)
00228 #endif
00229 {
00230 if(h)
00231 {
00232 dllUnload(*this);
00233 h=0;
00234 }
00235 h=o.h;
00236 o.h=0;
00237 return *this;
00238 }
00240 bool operator!() const throw() { return !h; }
00253 template<typename parslist> Generic::Functor<parslist> resolve(const char *apiname) const
00254 {
00255 return Generic::Functor<parslist>((typename Generic::Functor<parslist>::void_ *) FXProcess::dllResolveBase(*this, apiname));
00256 }
00257 };
00262 static dllHandle dllLoad(const FXString &path);
00267 static void *dllResolveBase(const dllHandle &h, const char *apiname);
00269 static void dllUnload(dllHandle &h);
00273 static FXString hostOS(FXString *myos=0, FXString *architecture=0);
00295 static FXString hostOSDescription(FXString *myapi=0, FXString *kernelname=0, FXString *kernelversion=0,
00296 FXString *apiversion=0, FXString *architecture=0);
00300 static void buildInfo(int *svnrev=0);
00306 static FXfloat hostOSProcessorLoad();
00308 static FXuint processorFull()
00309 {
00310 FXfloat v=hostOSProcessorLoad();
00311 if(v<0.95) return 0;
00312 else if(v<0.98) return 1;
00313 else if(v<1.0) return 2;
00314 else return 3;
00315 }
00320 static FXfloat hostOSMemoryLoad(FXuval *totalPhysMem=0);
00322 static FXuint memoryFull()
00323 {
00324 FXfloat v=hostOSMemoryLoad();
00325 if(v<0.95) return 0;
00326 else if(v<0.98) return 1;
00327 else if(v<1.0) return 2;
00328 else return 3;
00329 }
00331 struct MemoryUsageInfo
00332 {
00333 FXuval virtualUsage;
00334 FXuval workingSet;
00335 FXuval privateSet;
00336 MemoryUsageInfo() : virtualUsage(0), workingSet(0), privateSet(0) { }
00337 };
00339 static MemoryUsageInfo processMemoryUsage(FXuint processId=FXProcess::id());
00343 static FXfloat hostOSDiscIOLoad(const FXString &path);
00345 static FXfloat hostOSDiskIOLoad(const FXString &path) { return hostOSDiscIOLoad(path); }
00347 static FXuint discIOFull(const FXString &path)
00348 {
00349 FXfloat v=hostOSDiscIOLoad(path);
00350 if(v<0.95) return 0;
00351 else if(v<0.98) return 1;
00352 else if(v<1.0) return 2;
00353 else return 3;
00354 }
00356 static FXuint diskIOFull(const FXString &path) { return discIOFull(path); }
00359 static void overrideFreeResources(FXfloat memory=-1, FXfloat processor=-1, FXfloat discio=-1);
00369 static FXuval virtualAddrSpaceLeft(FXuval chunk=1);
00371 struct MountablePartition
00372 {
00373 FXString name;
00374 FXString location;
00375 FXString fstype;
00376 FXuint mounted : 1;
00377 FXuint mountable : 1;
00378 FXuint readWrite : 1;
00379 char driveLetter;
00380 };
00385 static QValueList<MountablePartition> mountablePartitions();
00387 static void mountPartition(const FXString &partitionName, const FXString &location=FXString::nullStr(), const FXString &fstype=FXString::nullStr(), bool readWrite=true);
00389 static void unmountPartition(const FXString &location);
00390
00392 static QThreadPool &threadPool();
00394 enum UserHandedness
00395 {
00396 UNKNOWN_HANDED=0,
00397 LEFT_HANDED=1,
00398 RIGHT_HANDED=2
00399 };
00401 static UserHandedness userHandedness();
00403 static void overrideUserHandedness(UserHandedness val);
00406 static FXuint screenScale(FXuint value);
00408 static void overrideScreenScale(FXuint percent);
00410 static void overrideScreenSize(FXint w, FXint h);
00411
00413 typedef Generic::Functor<Generic::TL::create<void, bool>::value> FatalExitUpcallSpec;
00425 static void addFatalExitUpcall(FatalExitUpcallSpec upcallv);
00427 static bool removeFatalExitUpcall(FatalExitUpcallSpec upcallv);
00429 static QMemArray<void *> lockedPages();
00430
00431
00432 static void int_addStaticInit(FXProcess_StaticInitBase *o);
00433 static void int_removeStaticInit(FXProcess_StaticInitBase *o);
00434 static void int_addStaticDepend(FXProcess_StaticDepend *d);
00435 private:
00436 friend class FXProcess_MemLock;
00437 static void *int_lockMem(void *addr, FXuval len);
00438 static void int_unlockMem(void *h);
00439 friend class FXWindow;
00440 friend class FXShell;
00441 static FXDLLLOCAL bool int_constrainScreen(FXint &x, FXint &y, FXint &w, FXint &h);
00442 static FXDLLLOCAL bool int_constrainScreen(FXint &w, FXint &h);
00443 };
00444
00445 class FXStream;
00446 class FXAPI FXProcess_StaticInitBase
00447 {
00448 friend class FXProcess;
00449 const char *myname;
00450 protected:
00451 bool done;
00452 void *dependencies;
00453 public:
00454 const char *name() const { return myname; }
00455 FXProcess_StaticInitBase(const char *name) : myname(name), done(false), dependencies(0) { }
00456 virtual ~FXProcess_StaticInitBase();
00457 virtual bool create(int &argc, char **argv, FXStream &txtout)=0;
00458 virtual void destroy()=0;
00459 };
00460
00481 template <class type> class FXProcess_StaticInit : public FXProcess_StaticInitBase
00482 {
00483 protected:
00484 type *object;
00485 public:
00486 FXProcess_StaticInit(const char *name) : object(0), FXProcess_StaticInitBase(name)
00487 {
00488 FXProcess::int_addStaticInit(this);
00489 }
00490 ~FXProcess_StaticInit()
00491 {
00492 FXDELETE(object);
00493 FXProcess::int_removeStaticInit(this);
00494 }
00495 bool create(int &argc, char **argv, FXStream &txtout)
00496 {
00497 if(!object)
00498 {
00499 FXERRHM(object=new type);
00500 }
00501 return false;
00502 }
00503 void destroy()
00504 {
00505 FXDELETE(object);
00506 }
00507 operator type *() { return object; }
00508 operator const type *() const { return object; }
00509 type *operator->() { return object; }
00510 };
00511 class FXAPI FXProcess_StaticDepend
00512 {
00513 friend class FXProcess;
00514 FXProcess_StaticInitBase *var;
00515 const char *onwhat;
00516 const char *module;
00517 int lineno;
00518 public:
00519 FXProcess_StaticDepend(FXProcess_StaticInitBase *_var, const char *_depname, const char *_module, int _lineno) : var(_var), onwhat(_depname), module(_module), lineno(_lineno)
00520 {
00521 FXProcess::int_addStaticDepend(this);
00522 }
00523 };
00524 #define FXPROCESS_UNIQUENAME3(var, no) var##no
00525 #define FXPROCESS_UNIQUENAME2(var, no) FXPROCESS_UNIQUENAME3(var, no)
00526 #define FXPROCESS_UNIQUENAME(var) FXPROCESS_UNIQUENAME2(var, __LINE__)
00528 #define FXPROCESS_STATICDEP(var, textname) static FXProcess_StaticDepend FXPROCESS_UNIQUENAME(var##_dep) (&var, textname, __FILE__, __LINE__)
00529
00554 class FXAPI FXProcess_MemLock
00555 {
00556 void *h;
00557 void *addr;
00558 FXuval len;
00559 public:
00561 FXProcess_MemLock() : h(0), addr((void *)-1), len(0) { }
00563 FXProcess_MemLock(void *startaddr, FXuval length) : h(FXProcess::int_lockMem(startaddr, length)), addr(startaddr), len(length) { }
00564 FXProcess_MemLock(const FXProcess_MemLock &o) : h(h ? FXProcess::int_lockMem(o.addr, o.len) : 0), addr(o.addr), len(o.len) { }
00565 FXProcess_MemLock &operator=(const FXProcess_MemLock &o)
00566 {
00567 if(h) { FXProcess::int_unlockMem(h); h=0; }
00568 h=o.h ? FXProcess::int_lockMem(o.addr, o.len) : 0;
00569 addr=o.addr; len=o.len;
00570 return *this;
00571 }
00572 ~FXProcess_MemLock() { if(h) { FXProcess::int_unlockMem(h); h=0; } }
00574 void *location() const throw() { return addr; }
00576 FXuval length() const throw() { return len; }
00577 };
00578
00579 }
00580
00581 #endif
00582
00583