Vault  4.1
vthread.h
Go to the documentation of this file.
00001 /*
00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved.
00003 This file is part of the Code Vault version 4.1
00004 http://www.bombaydigital.com/
00005 License: MIT. See LICENSE.md in the Vault top level directory.
00006 */
00007 
00008 #ifndef vthread_h
00009 #define vthread_h
00010 
00013 #include "vthread_platform.h"
00014 
00015 #include "vstring.h"
00016 #include "vmutex.h"
00017 #include "vlogger.h"
00018 
00019 class VDuration;
00020 class VManagementInterface;
00021 class VBentoNode;
00022 
00136 class VThread {
00137     public:
00138 
00139         /*
00140         The following three static functions are to be implemented in your client application
00141         code. The APIs are defined here and are called when needed, and you just have to
00142         implement them to do what's needed depending on what you have available.
00143         */
00152         static int userMain(int argc, char** argv);
00160         static void* userThreadMain(void* arg);
00174         static void logStackCrawl(const VString& headerMessage, VNamedLoggerPtr logger, bool verbose);
00175 
00176         // Constants to pass for VThread constructor deleteSelfAtEnd parameter:
00177         static const bool kDeleteSelfAtEnd = true;      
00178         static const bool kDontDeleteSelfAtEnd = false; 
00179 
00180         // Constants to pass for VThread constructor createDetached parameter:
00181         static const bool kCreateThreadDetached = true;     
00182         static const bool kCreateThreadJoinable = false;    
00183 
00202         VThread(const VString& name, const VString& loggerName, bool deleteSelfAtEnd, bool createDetached, VManagementInterface* manager);
00206         virtual ~VThread();
00207 
00213         virtual void start();
00219         virtual void stop();
00228         virtual void run() = 0;
00229 
00235         VThreadID_Type threadID() const;
00244         bool isRunning() const;
00249         bool join();
00256         bool getDeleteAtEnd() const;
00261         VManagementInterface* getManagementInterface() const;
00262 
00267         const VString& getName() const { return mName; }
00279         void setName(const VString& threadName) { mName = threadName; }
00284         const VString& getLoggerName() const { return mLoggerName; }
00285 
00291         static void* threadMain(void* arg);
00292 
00300         static void getThreadsInfo(VBentoNode& bento);
00301 
00308         static VString getThreadName(VThreadID_Type threadID);
00309 
00317         static void stopThread(VThreadID_Type threadID);
00318 
00319         /* PLATFORM-SPECIFIC STATIC FUNCTIONS --------------------------------
00320         The remaining functions defined here are the low-level interfaces to
00321         the platform-specific threading APIs. These are implemented in each
00322         platform-specific version of vthread_platform.cpp.
00323         */
00324 
00325         typedef void*(*threadMainFunction)(void* param);
00326 
00336         static void threadCreate(VThreadID_Type* threadID, bool createDetached, threadMainFunction threadMainProcPtr, void* threadArgument);
00337 
00345         static void threadExit();
00346 
00354         static bool threadJoin(VThreadID_Type threadID, void** value);
00355 
00362         static void threadDetach(VThreadID_Type threadID);
00363 
00369         static VThreadID_Type threadSelf();
00370 
00376         static VThread* getCurrentThread();
00377 
00384         static VString getCurrentThreadName();
00385 
00392         static bool setPriority(int nice);
00393 
00401         static void sleep(const VDuration& interval);
00402 
00409         static void yield();
00410 
00411     protected:
00412 
00413         bool                    mIsDeleted;         
00414         VString                 mName;              
00415         VString                 mLoggerName;        
00416         bool                    mDeleteAtEnd;       
00417         bool                    mCreateDetached;    
00418         VManagementInterface*   mManager;           
00419         VThreadID_Type          mThreadID;          
00420         volatile bool           mIsRunning;         
00421 
00422     private:
00423 
00424         // Prevent copy construction and assignment since there is no provision for sharing the underlying thread.
00425         VThread(const VThread& other);
00426         VThread& operator=(const VThread& other);
00427 
00428         // These two function are implemented in the platform-specific code to perform any
00429         // necessary per-thread bookkeeping. They are called from VThread::threadMain() just
00430         // before and after the corresponding VManagementInterface threadStarting() and
00431         // threadEnded() calls.
00432         static void _threadStarting(const VThread* thread);
00433         static void _threadEnded(const VThread* thread);
00434 };
00435 
00447 class VMainThread : public VThread {
00448     public:
00449         VMainThread();
00450         virtual ~VMainThread();
00451 
00452         virtual void start();
00453         virtual void run() {} // Will never be called because start() throws.
00454 
00455         int execute(int argc, char** argv);
00456 };
00457 
00464 class VForeignThread : public VThread {
00465     public:
00466         VForeignThread(const VString& name);
00467         virtual ~VForeignThread();
00468 
00469         virtual void start();
00470         virtual void run() {} // Will never be called because start() throws.
00471 };
00472 
00473 #endif /* vthread_h */
00474 

Copyright ©1997-2014 Trygve Isaacson. All rights reserved. This documentation was generated with Doxygen.