| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Pierre Aubert | ||
| 3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | #ifndef __DETACHPOOL_H__ | ||
| 8 | #define __DETACHPOOL_H__ | ||
| 9 | |||
| 10 | #include <unistd.h> | ||
| 11 | #include <thread> | ||
| 12 | #include <list> | ||
| 13 | #include <mutex> | ||
| 14 | #include <string> | ||
| 15 | #include <iostream> | ||
| 16 | |||
| 17 | #include "phoenix_thread_clock.h" | ||
| 18 | |||
| 19 | #define PEXIT_STATUS_NOT_FIHISHED 0 | ||
| 20 | #define PEXIT_STATUS_OK 1 | ||
| 21 | #define PEXIT_STATUS_FAIL 2 | ||
| 22 | |||
| 23 | ///@brief Information related to the current detached thread | ||
| 24 | struct ThreadDetachInfo{ | ||
| 25 | ///Description of the current thread | ||
| 26 | std::string description; | ||
| 27 | ///Exit status of the current thread | ||
| 28 | int exitStatus; | ||
| 29 | }; | ||
| 30 | |||
| 31 | typedef std::list<ThreadDetachInfo> ListThreadDetachInfo; | ||
| 32 | |||
| 33 | ///@brief Pool of detached threads | ||
| 34 | class DetachPool{ | ||
| 35 | public: | ||
| 36 | DetachPool(); | ||
| 37 | DetachPool(const DetachPool & other); | ||
| 38 | virtual ~DetachPool(); | ||
| 39 | DetachPool & operator = (const DetachPool & other); | ||
| 40 | |||
| 41 | void setRefreshTime(PEllapsedTime refreshTime); | ||
| 42 | PEllapsedTime getRefreshTime() const; | ||
| 43 | |||
| 44 | void setDestructionWaitTime(PEllapsedTime destructorWaitTime); | ||
| 45 | PEllapsedTime getDestructorWaitTime() const; | ||
| 46 | |||
| 47 | ListThreadDetachInfo getListThreadInfo(); | ||
| 48 | |||
| 49 | template<typename _Callable, typename... _Args> | ||
| 50 | void addThread(const std::string & description, _Callable&& __f, _Args&&... __args); | ||
| 51 | void removeFinishedThread(std::ostream & out = std::cout); | ||
| 52 | |||
| 53 | void refresh(); | ||
| 54 | void waitUntilAllFinish(PEllapsedTime maxWaitTime = 10000000); //Wait 10 seconds by default | ||
| 55 | |||
| 56 | protected: | ||
| 57 | void copyDetachPool(const DetachPool & other); | ||
| 58 | |||
| 59 | private: | ||
| 60 | void initialisationDetachPool(); | ||
| 61 | |||
| 62 | ///Mutex to handle exit status of detached threads | ||
| 63 | std::mutex p_mtx; | ||
| 64 | ///Ellapsed time between two refresh (in micro second) | ||
| 65 | PEllapsedTime p_refreshTime; | ||
| 66 | ///Time of last refresh (in micro second) | ||
| 67 | PhoenixTime p_timeLastRefresh; | ||
| 68 | ///List of the information related to all ongoing threads | ||
| 69 | ListThreadDetachInfo p_listThreadInfo; | ||
| 70 | ///Wait time when object is destroyed | ||
| 71 | PEllapsedTime p_destructorWaitTime; | ||
| 72 | }; | ||
| 73 | |||
| 74 | std::string phoenix_statusToString(int exitStatus); | ||
| 75 | void phoenix_printThreadStatus(const ListThreadDetachInfo & listThreadInfo, std::ostream & out = std::cout); | ||
| 76 | |||
| 77 | #include "DetachPool_impl.h" | ||
| 78 | |||
| 79 | #endif | ||
| 80 | |||
| 81 |